デザインシステム¶
はじめに¶
本サイトにつきまして、【IT技術の知見】はじめに - 俺の技術ノート をご認識のほど宜しくお願いいたします。
01. デザインシステムとは¶
プロダクトの UI に一貫性を持たせるための、共通の基盤のこと。
色や文字などのデザインの決まりと、それを実装した再利用可能な部品を、利用ルールとともにまとめる。
02. デザインシステムの構成要素¶
カラー¶
たとえば、役割ごとに使う色を次のとおりに指定し、デザインの方法を定義づける。
| 役割 | クラス |
|---|---|
| 本文 | text-gray-700 |
| 補足、表見出し | text-gray-500 |
| リンク | text-blue-600 |
| 主要な操作の背景 | bg-blue-500(ホバー:hover:bg-blue-600、フォーカス:focus:bg-blue-400) |
| 主要な操作の文字 | text-white |
| 危険な操作の背景 | bg-red-500(ホバー:hover:bg-red-700) |
| エラーメッセージ | text-red-700 |
| 入力欄の枠線 | border-gray-500 |
| カードの背景 | bg-white |
| 成功の通知 | bg-green-500 |
| エラーの通知 | bg-red-500 |
タイポグラフィ¶
たとえば、フォントファミリーは指定せず、Tailwind CSSの標準を使うように指定し、デザインの方法を定義づける。
| 用途 | クラス |
|---|---|
| 本文、入力欄、ボタン | text-sm |
| 補足、表見出し | text-xs |
| 大見出し | text-xl font-bold |
| 小見出し | text-lg font-semibold text-gray-900 |
| フォームのラベル | block text-sm font-medium text-gray-700 |
コンテンツ¶
たとえば、文言は日本語にするように指定し、デザインの方法を定義づける。
スペーシング¶
たとえば、要素ごとの余白を次のとおりに指定し、デザインの方法を定義づける。
| 要素 | クラス |
|---|---|
| 表の見出しセル | px-6 py-3 |
| 表のデータセル | px-6 py-4 |
| ボタン | px-7 py-2 |
| 入力欄 | px-2 py-2 |
| カード | p-5 |
| 要素どうしの間隔 | gap-4 |
コンポーネント¶
部品ごとのクラス¶
| 部品 | クラス |
|---|---|
| 主要な操作のボタン | rounded bg-blue-500 px-7 py-2 text-sm text-white hover:bg-blue-600 focus:bg-blue-400 |
| 危険な操作のボタン(詳細画面) | rounded bg-red-500 px-7 py-2 text-sm text-white hover:bg-red-700 |
| 危険な操作のボタン(一覧の行の中) | rounded bg-red-500 px-2 py-1 text-white hover:bg-red-700 |
| 前後のページへ移るボタン | rounded-lg border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-500 hover:bg-gray-100 hover:text-gray-700 |
| テキスト入力、テキストエリア | w-full rounded border border-gray-500 px-2 py-2 text-sm |
| カード | bg-white p-5 shadow-md |
共通部品¶
たとえば、共通部品がある場合は、同じ見た目を新しく作らず、その部品を使うように指定し、デザインの方法を定義づける。
| 部品 | 用途 |
|---|---|
RequiredBadge |
必須項目のラベルに付ける |
Tag |
短い文字を丸いラベルで表示する |
LoadingSpinner |
読み込み中の画面全体に重ねて表示する |
アイコン¶
たとえば、アイコンは@heroicons/react/20/solidを使い、サイズはh-4 w-4を基本とし、大きく見せたい場合にh-6 w-6を使うように指定し、デザインの方法を定義づける。
03. 実装例¶
React の場合の実装例
// 例:src/components/FooList.tsx
// コンポーネント
export function FooList({ foos }: { foos: Foo[] }) {
// 中略
return (
// UIレンダリングとCSSスタイリング
<div className="flex flex-col items-start gap-4 bg-white p-5 shadow-md">
<h1 className="text-xl font-bold text-gray-700">Foo一覧</h1>
<table className="w-full text-sm text-gray-700">
<thead className="text-xs text-gray-500">
<tr>
<th className="px-6 py-3">Foo名</th>
</tr>
</thead>
<tbody>
{foos.map((foo) => (
<tr key={foo.name}>
<td className="px-6 py-4">{foo.name}</td>
</tr>
))}
</tbody>
</table>
<a
href="/foos/new"
className="rounded bg-blue-500 px-7 py-2 text-sm text-white hover:bg-blue-600 focus:bg-blue-400"
>
新規作成
</a>
</div>
);
}
Remix の場合の実装例
// 例:app/routes/foos.tsx
// コンポーネント
export default function FoosRoute() {
const { foos } = useTypedLoaderData<typeof loader>();
// 中略
return (
// UIレンダリングとCSSスタイリング
<div className="flex flex-col items-start gap-4 bg-white p-5 shadow-md">
<h1 className="text-xl font-bold text-gray-700">Foo一覧</h1>
<table className="w-full text-sm text-gray-700">
<thead className="text-xs text-gray-500">
<tr>
<th className="px-6 py-3">Foo名</th>
</tr>
</thead>
<tbody>
{foos.map((foo) => (
<tr key={foo.name}>
<td className="px-6 py-4">{foo.name}</td>
</tr>
))}
</tbody>
</table>
<Link
to="/foos/new"
className="rounded bg-blue-500 px-7 py-2 text-sm text-white hover:bg-blue-600 focus:bg-blue-400"
>
新規作成
</Link>
</div>
);
}