アイコンのボタン
文字を置く場所がないときに、アイコンだけで操作を示すボタンです。名前は aria-label で必ず付けます。
見本で種類と状態を確かめる
種類は地のない quiet(既定)と、枠の付いた secondary の 2 つです。大きさは見た目 44 の既定と 36 の sm で、sm も押せる範囲は 44 あります。
種類と大きさ
押した状態
注目の予定に入っています
押していない状態
注目の予定に入っていません
コードを写す
HTML
<button class="mz-icon-button" type="button" aria-label="明細を編集する">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21.174 6.812a1 1 0 0 0-3.986-3.987L3.842 16.174a2 2 0 0 0-.5.83l-1.321 4.352a.5.5 0 0 0 .623.622l4.353-1.32a2 2 0 0 0 .83-.497z"/><path d="m15 5 4 4"/></svg>
</button>
<button class="mz-icon-button" data-variant="secondary" type="button" aria-label="ほかの操作を開く">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="1"/><circle cx="19" cy="12" r="1"/><circle cx="5" cy="12" r="1"/></svg>
</button>
<button class="mz-icon-button" data-size="sm" type="button" aria-label="お知らせを閉じる">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg>
</button>
<button class="mz-icon-button" data-variant="secondary" data-size="sm" type="button" aria-label="明細を削除する">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 6h18"/><path d="M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6"/><path d="M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2"/></svg>
</button>
<button class="mz-icon-button" type="button" aria-label="注目の予定に入れる" aria-pressed="true" data-toggle>
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M11.525 2.295a.53.53 0 0 1 .95 0l2.31 4.679a2.123 2.123 0 0 0 1.595 1.16l5.166.756a.53.53 0 0 1 .294.904l-3.736 3.638a2.123 2.123 0 0 0-.611 1.878l.882 5.14a.53.53 0 0 1-.771.56l-4.618-2.428a2.122 2.122 0 0 0-1.973 0L6.396 21.01a.53.53 0 0 1-.77-.56l.881-5.139a2.122 2.122 0 0 0-.611-1.879L2.16 9.795a.53.53 0 0 1 .294-.906l5.165-.755a2.122 2.122 0 0 0 1.597-1.16z"/></svg>
</button>React の呼び出し方
<>
<IconButton label="明細を編集する" icon={<Pencil strokeWidth={1.5} aria-hidden="true" />} />
<IconButton label="ほかの操作を開く" variant="secondary" icon={<Ellipsis strokeWidth={1.5} aria-hidden="true" />} />
</>実装を見る(react/IconButton.tsx)
React の実装(react/IconButton.tsx)
/**
* IconButton — アイコンだけの操作(docs/spec/components.md「icon-button」・css/components/icon-button.css)
* label は必須(aria-label になる)。ツールチップに頼らない。
*/
import { forwardRef, type ButtonHTMLAttributes, type ReactNode } from 'react';
export type IconButtonVariant = 'quiet' | 'secondary';
export type IconButtonSize = 'sm' | 'md';
export interface IconButtonProps
extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'disabled' | 'aria-label' | 'aria-pressed' | 'children'> {
/** 操作の名前(例:「明細を編集する」)。aria-label になる */
label: string;
/** アイコン 20(lucide-react など。aria-hidden を付けて渡す必要はない) */
icon: ReactNode;
variant?: IconButtonVariant;
size?: IconButtonSize;
/** 入れる・外すを切り替える操作のときに渡す(aria-pressed) */
pressed?: boolean;
}
export const IconButton = forwardRef<HTMLButtonElement, IconButtonProps>(function IconButton(
{ label, icon, variant = 'quiet', size = 'md', pressed, className, type = 'button', ...rest },
ref,
) {
return (
<button
ref={ref}
type={type}
className={className ? `mz-icon-button ${className}` : 'mz-icon-button'}
data-variant={variant === 'quiet' ? undefined : variant}
data-size={size === 'md' ? undefined : size}
aria-label={label}
aria-pressed={pressed}
{...rest}
>
<span className="mz-icon-button__icon" aria-hidden="true">
{icon}
</span>
</button>
);
});アイコンは Lucide の線のアイコン(20)を、stroke-width="1.5" で入れます。上の … には、使うアイコンの SVG の中身を入れます。
いつ使うか
- 行の操作・閉じる・編集など、意味がアイコンで広く通じる操作に使います。
- 文字で書ける場所があるなら、ボタンにアイコンと文字を並べます。
- 入れる・外すを切り替える操作は
aria-pressedで押した状態を持たせ、地を accent-soft にします。
読み上げとキーボードにどう応えるか
aria-labelに操作の名前を動詞で書きます(「明細を編集する」)。ツールチップに頼りません。- アイコンの SVG には
aria-hidden="true"を付け、名前は button だけが持ちます。 - 押した状態は
aria-pressedで伝わるので、名前は切り替えても変えません。
よい例と悪い例
更新日