コード
写して使うコードを等幅の文字で見せる部品です。右上のボタンで写せます。
見本で種類と状態を確かめる
surface-2 の地、角丸 radius-sm、内側 16、等幅の 14px・行の高さ 1.6 です。長い行は折り返し、横のスクロールにしません。台の上の右上のボタンを押すと、実際に写せます。
短い
<link rel="stylesheet" href="/vendor/mizolabo-design/css/index.css">長い行
<html lang="ja" data-theme="dark">
<head>
<link rel="stylesheet" href="/vendor/mizolabo-design/css/index.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Zen+Kaku+Gothic+New:wght@400;500;700&display=swap">
</head>コードを写す
HTML
<pre class="mz-code"><code><html lang="ja" data-theme="dark">
<head>
<link rel="stylesheet" href="https://design.mizolabo.com/css/index.css">
</head></code><button class="mz-icon-button mz-code__copy" data-size="sm" type="button" aria-label="コードをコピーする"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="8" y="8" width="14" height="14" rx="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg></button></pre>React の呼び出し方
const toast = useToast();
return (
<Code
code={head}
onCopied={() => toast.show({ text: 'コピーしました' })}
onCopyError={() => toast.show({ text: 'コードを写せませんでした。範囲を選んで手で写してください' })}
/>
);実装を見る(react/Code.tsx)
React の実装(react/Code.tsx)
/**
* Code — コード(docs/spec/components.md「code」)
* 見た目は css/components/code.css だけが持つ。
* 右上のコピーのボタン(icon-button sm)は文字をクリップボードに写し、onCopied を呼ぶ。
* 「コピーしました」のトーストは、呼んだ側が onCopied で出す(toast は群 3 の部品)。
*/
import { forwardRef, type HTMLAttributes } from 'react';
export interface CodeProps extends HTMLAttributes<HTMLPreElement> {
code: string;
/** コピーできたとき(トーストを出す) */
onCopied?: () => void;
/** コピーできなかったとき(権限がない・クリップボードが使えない)。何が起きてどうすればよいかを出す */
onCopyError?: (error: unknown) => void;
/** コピーのボタンの名前(既定「コードをコピーする」) */
copyLabel?: string;
}
export const Code = forwardRef<HTMLPreElement, CodeProps>(function Code(
{ code, onCopied, onCopyError, copyLabel = 'コードをコピーする', className, ...rest },
ref,
) {
const copy = async () => {
try {
if (!navigator.clipboard) throw new Error('クリップボードが使えません');
await navigator.clipboard.writeText(code);
onCopied?.();
} catch (error) {
onCopyError?.(error);
}
};
return (
<pre ref={ref} className={className ? `mz-code ${className}` : 'mz-code'} {...rest}>
<code>{code}</code>
<button type="button" className="mz-icon-button mz-code__copy" data-size="sm" aria-label={copyLabel} onClick={copy}>
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
<rect x="8" y="8" width="14" height="14" rx="2" />
<path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2" />
</svg>
</button>
</pre>
);
});HTML の中の < と > は、< と > に書き換えて入れます。写す処理は JS が持ちます(React の Code は持っています)。
いつ使うか
- 利用者が写して使うコード・コマンド・設定の値に使います。
- 文の中の短いコード(クラス名など)は、
<code>だけで本文に入れます。 - コピーのボタンを押したら、「コピーしました」をトーストで出します。写せなかったときは、範囲を選んで手で写すよう案内します。
読み上げとキーボードにどう応えるか
- コピーのボタンはアイコンのボタンの sm で、「コードをコピーする」の名前を付けます。
- 折り返すので、キーボードで横にスクロールする必要がありません。
よい例と悪い例
pnpm build:tokensまずトークンを作ります
pnpm build:tokens更新日