📝 Tailwind CSSでスケーラブルなデザインシステムを構築する
カスタムテーマ設定、コンポーネント化、チーム開発での一貫性を保つTailwind CSSのベストプラクティス
Tailwind CSSでスケーラブルなデザインシステムを構築する
Tailwind CSSは柔軟性の高いユーティリティファーストのCSSフレームワークですが、適切な設計なしに使用すると保守性に問題が生じることがあります。今回は、スケーラブルなデザインシステムを構築する方法を解説します。
カスタムテーマ設定で一貫性を確保
まずはプロジェクト全体で使用するデザイントークンをtailwind.config.js
で定義します。
カラーパレットの設計
// tailwind.config.js
const colors = require('tailwindcss/colors')
module.exports = {
content: [
'./src/pages/**/*.{js,ts,jsx,tsx,mdx}',
'./src/components/**/*.{js,ts,jsx,tsx,mdx}',
'./src/app/**/*.{js,ts,jsx,tsx,mdx}',
],
theme: {
extend: {
colors: {
// ブランドカラー
primary: {
50: '#eff6ff',
100: '#dbeafe',
200: '#bfdbfe',
300: '#93c5fd',
400: '#60a5fa',
500: '#3b82f6', // メインカラー
600: '#2563eb',
700: '#1d4ed8',
800: '#1e40af',
900: '#1e3a8a',
950: '#172554',
},
// セカンダリカラー
secondary: {
50: '#f0fdf4',
100: '#dcfce7',
200: '#bbf7d0',
300: '#86efac',
400: '#4ade80',
500: '#22c55e',
600: '#16a34a',
700: '#15803d',
800: '#166534',
900: '#14532d',
950: '#052e16',
},
// セマンティックカラー
success: colors.green,
warning: colors.yellow,
error: colors.red,
info: colors.blue,
// グレースケール
gray: colors.slate,
},
// タイポグラフィ
fontFamily: {
sans: ['Inter', 'Hiragino Sans', 'sans-serif'],
mono: ['JetBrains Mono', 'monospace'],
},
fontSize: {
'xs': ['0.75rem', { lineHeight: '1rem' }],
'sm': ['0.875rem', { lineHeight: '1.25rem' }],
'base': ['1rem', { lineHeight: '1.5rem' }],
'lg': ['1.125rem', { lineHeight: '1.75rem' }],
'xl': ['1.25rem', { lineHeight: '1.75rem' }],
'2xl': ['1.5rem', { lineHeight: '2rem' }],
'3xl': ['1.875rem', { lineHeight: '2.25rem' }],
'4xl': ['2.25rem', { lineHeight: '2.5rem' }],
'5xl': ['3rem', { lineHeight: '1' }],
'6xl': ['3.75rem', { lineHeight: '1' }],
},
// スペーシング
spacing: {
'18': '4.5rem',
'88': '22rem',
'128': '32rem',
},
// アニメーション
animation: {
'fade-in': 'fadeIn 0.5s ease-in-out',
'slide-up': 'slideUp 0.3s ease-out',
'bounce-subtle': 'bounceSubtle 2s infinite',
},
keyframes: {
fadeIn: {
'0%': { opacity: '0' },
'100%': { opacity: '1' },
},
slideUp: {
'0%': { transform: 'translateY(10px)', opacity: '0' },
'100%': { transform: 'translateY(0)', opacity: '1' },
},
bounceSubtle: {
'0%, 20%, 50%, 80%, 100%': { transform: 'translateY(0)' },
'40%': { transform: 'translateY(-5px)' },
'60%': { transform: 'translateY(-3px)' },
},
},
},
},
plugins: [
require('@tailwindcss/forms'),
require('@tailwindcss/typography'),
require('@tailwindcss/aspect-ratio'),
],
}
コンポーネントライブラリの構築
共通コンポーネントを作成することで、一貫性を保ちながら再利用性を高めます。
ボタンコンポーネント
// components/ui/Button.tsx
import { cn } from '@/lib/utils';
import { VariantProps, cva } from 'class-variance-authority';
import { forwardRef } from 'react';
const buttonVariants = cva(
// ベースクラス
'inline-flex items-center justify-center rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:opacity-50 disabled:pointer-events-none ring-offset-background',
{
variants: {
variant: {
default: 'bg-primary-500 text-white hover:bg-primary-600',
destructive: 'bg-error-500 text-white hover:bg-error-600',
outline: 'border border-gray-300 bg-transparent hover:bg-gray-50',
secondary: 'bg-secondary-100 text-secondary-900 hover:bg-secondary-200',
ghost: 'hover:bg-gray-100 hover:text-gray-900',
link: 'underline-offset-4 hover:underline text-primary-500',
},
size: {
default: 'h-10 py-2 px-4',
sm: 'h-9 px-3 rounded-md',
lg: 'h-11 px-8 rounded-md',
icon: 'h-10 w-10',
},
},
defaultVariants: {
variant: 'default',
size: 'default',
},
}
);
export interface ButtonProps
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
VariantProps<typeof buttonVariants> {
asChild?: boolean;
}
const Button = forwardRef<HTMLButtonElement, ButtonProps>(
({ className, variant, size, asChild = false, ...props }, ref) => {
return (
<button
className={cn(buttonVariants({ variant, size, className }))}
ref={ref}
{...props}
/>
);
}
);
Button.displayName = 'Button';
export { Button, buttonVariants };
カードコンポーネント
// components/ui/Card.tsx
import { cn } from '@/lib/utils';
import { forwardRef } from 'react';
const Card = forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn(
'rounded-lg border border-gray-200 bg-white shadow-sm',
className
)}
{...props}
/>
));
Card.displayName = 'Card';
const CardHeader = forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn('flex flex-col space-y-1.5 p-6', className)}
{...props}
/>
));
CardHeader.displayName = 'CardHeader';
const CardTitle = forwardRef<
HTMLParagraphElement,
React.HTMLAttributes<HTMLHeadingElement>
>(({ className, ...props }, ref) => (
<h3
ref={ref}
className={cn(
'text-lg font-semibold leading-none tracking-tight',
className
)}
{...props}
/>
));
CardTitle.displayName = 'CardTitle';
const CardDescription = forwardRef<
HTMLParagraphElement,
React.HTMLAttributes<HTMLParagraphElement>
>(({ className, ...props }, ref) => (
<p
ref={ref}
className={cn('text-sm text-gray-600', className)}
{...props}
/>
));
CardDescription.displayName = 'CardDescription';
const CardContent = forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div ref={ref} className={cn('p-6 pt-0', className)} {...props} />
));
CardContent.displayName = 'CardContent';
const CardFooter = forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn(' flex items-center p-6 pt-0', className)}
{...props}
/>
));
CardFooter.displayName = 'CardFooter';
export { Card, CardHeader, CardFooter, CardTitle, CardDescription, CardContent };
レイアウトコンポーネント
レスポンシブコンテナ
// components/layout/Container.tsx
import { cn } from '@/lib/utils';
interface ContainerProps {
children: React.ReactNode;
size?: 'sm' | 'md' | 'lg' | 'xl' | 'full';
className?: string;
}
const containerSizes = {
sm: 'max-w-2xl',
md: 'max-w-4xl',
lg: 'max-w-6xl',
xl: 'max-w-7xl',
full: 'max-w-full',
};
export function Container({
children,
size = 'lg',
className
}: ContainerProps) {
return (
<div className={cn(
'mx-auto px-4 sm:px-6 lg:px-8',
containerSizes[size],
className
)}>
{children}
</div>
);
}
グリッドレイアウト
// components/layout/Grid.tsx
import { cn } from '@/lib/utils';
interface GridProps {
children: React.ReactNode;
cols?: 1 | 2 | 3 | 4 | 5 | 6 | 12;
gap?: 'sm' | 'md' | 'lg' | 'xl';
className?: string;
}
const gridCols = {
1: 'grid-cols-1',
2: 'grid-cols-1 md:grid-cols-2',
3: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3',
4: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-4',
5: 'grid-cols-1 md:grid-cols-3 lg:grid-cols-5',
6: 'grid-cols-1 md:grid-cols-3 lg:grid-cols-6',
12: 'grid-cols-12',
};
const gridGaps = {
sm: 'gap-4',
md: 'gap-6',
lg: 'gap-8',
xl: 'gap-12',
};
export function Grid({
children,
cols = 3,
gap = 'md',
className
}: GridProps) {
return (
<div className={cn(
'grid',
gridCols[cols],
gridGaps[gap],
className
)}>
{children}
</div>
);
}
フォームコンポーネント
インプットコンポーネント
// components/ui/Input.tsx
import { cn } from '@/lib/utils';
import { forwardRef } from 'react';
export interface InputProps
extends React.InputHTMLAttributes<HTMLInputElement> {
error?: string;
label?: string;
helperText?: string;
}
const Input = forwardRef<HTMLInputElement, InputProps>(
({ className, type, error, label, helperText, ...props }, ref) => {
return (
<div className="space-y-2">
{label && (
<label className="text-sm font-medium text-gray-700">
{label}
</label>
)}
<input
type={type}
className={cn(
'flex h-10 w-full rounded-md border border-gray-300 bg-white px-3 py-2 text-sm ring-offset-white file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-gray-500 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary-500 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50',
error && 'border-error-500 focus-visible:ring-error-500',
className
)}
ref={ref}
{...props}
/>
{error && (
<p className="text-sm text-error-600">{error}</p>
)}
{helperText && !error && (
<p className="text-sm text-gray-500">{helperText}</p>
)}
</div>
);
}
);
Input.displayName = 'Input';
export { Input };
状態管理とバリアント
ローディング状態の統一
// components/ui/LoadingSpinner.tsx
import { cn } from '@/lib/utils';
interface LoadingSpinnerProps {
size?: 'sm' | 'md' | 'lg';
className?: string;
}
const spinnerSizes = {
sm: 'h-4 w-4',
md: 'h-6 w-6',
lg: 'h-8 w-8',
};
export function LoadingSpinner({ size = 'md', className }: LoadingSpinnerProps) {
return (
<div
className={cn(
'animate-spin rounded-full border-2 border-gray-300 border-t-primary-500',
spinnerSizes[size],
className
)}
/>
);
}
// ローディング付きボタン
export function LoadingButton({
loading,
children,
...props
}: ButtonProps & { loading?: boolean }) {
return (
<Button disabled={loading} {...props}>
{loading && <LoadingSpinner size="sm" className="mr-2" />}
{children}
</Button>
);
}
デザインシステムの使用例
// pages/contact.tsx
import { Container } from '@/components/layout/Container';
import { Grid } from '@/components/layout/Grid';
import { Card, CardHeader, CardTitle, CardContent } from '@/components/ui/Card';
import { Button } from '@/components/ui/Button';
import { Input } from '@/components/ui/Input';
export default function ContactPage() {
return (
<Container size="md" className="py-12">
<div className="text-center mb-12">
<h1 className="text-4xl font-bold text-gray-900 mb-4">
お問い合わせ
</h1>
<p className="text-lg text-gray-600">
ご質問やご相談がございましたら、お気軽にお問い合わせください。
</p>
</div>
<Grid cols={2} gap="lg">
<Card>
<CardHeader>
<CardTitle>フォームから送信</CardTitle>
</CardHeader>
<CardContent className="space-y-4">
<Input
label="お名前"
placeholder="山田太郎"
required
/>
<Input
type="email"
label="メールアドレス"
placeholder="example@email.com"
required
/>
<div className="space-y-2">
<label className="text-sm font-medium text-gray-700">
お問い合わせ内容
</label>
<textarea
className="flex min-h-[80px] w-full rounded-md border border-gray-300 bg-white px-3 py-2 text-sm placeholder:text-gray-500 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary-500 focus-visible:ring-offset-2"
placeholder="お問い合わせ内容をご記入ください"
/>
</div>
<Button className="w-full">
送信する
</Button>
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle>その他のお問い合わせ方法</CardTitle>
</CardHeader>
<CardContent className="space-y-4">
<div className="flex items-center space-x-3">
<div className="flex-shrink-0">
<div className="h-10 w-10 bg-primary-100 rounded-lg flex items-center justify-center">
📧
</div>
</div>
<div>
<p className="text-sm font-medium">メール</p>
<p className="text-sm text-gray-600">contact@example.com</p>
</div>
</div>
<div className="flex items-center space-x-3">
<div className="flex-shrink-0">
<div className="h-10 w-10 bg-secondary-100 rounded-lg flex items-center justify-center">
📞
</div>
</div>
<div>
<p className="text-sm font-medium">電話</p>
<p className="text-sm text-gray-600">03-1234-5678</p>
</div>
</div>
</CardContent>
</Card>
</Grid>
</Container>
);
}
まとめ
スケーラブルなデザインシステムを構築するポイント:
- 統一されたテーマ設定: カラー、タイポグラフィ、スペーシングの一貫性
- 再利用可能なコンポーネント: バリアントシステムで柔軟性を保つ
- レイアウトコンポーネント: グリッドやコンテナで構造を統一
- 状態管理の統一: ローディング、エラー状態の一貫した表現
- TypeScript統合: 型安全性によるコンポーネントAPI
これらの原則に従うことで、チーム開発でも一貫性を保ちながら、保守性の高いUIコンポーネントライブラリを構築できます。