# ADR-012: UI Component Library Strategy **Status:** ✅ Accepted **Date:** 2025-12-01 **Decision Makers:** Frontend Team, UX Designer **Related Documents:** [Frontend Guidelines](../03-implementation/frontend-guidelines.md), [ADR-005: Technology Stack](./ADR-005-technology-stack.md) --- ## Context and Problem Statement ต้องการ UI Component Library สำหรับสร้าง User Interface ที่สวยงาม สม่ำเสมอ และ Accessible ### ปัญหาที่ต้องแก้: 1. **Component Library:** ใช้ Library สำเร็จรูป หรือสร้างเอง 2. **Customization:** ปรับแต่งได้ง่ายเพียงใด 3. **Accessibility:** รองรับ ARIA และ Keyboard navigation 4. **Bundle Size:** ขนาดไฟล์ไม่ใหญ่เกินไป 5. **Developer Experience:** ใช้งานง่าย Documentation ครบ --- ## Decision Drivers - 🎨 **Design Consistency:** UI สม่ำเสมอทั้งระบบ - ♿ **Accessibility:** รองรับ WCAG 2.1 AA - 🎯 **Customization:** ปรับแต่งได้ตามต้องการ - 📦 **Bundle Size:** เล็กและ Tree-shakeable - ⚡ **Performance:** Render เร็ว - 🛠️ **DX:** Developer Experience ดี --- ## Considered Options ### Option 1: Material-UI (MUI) **Pros:** - ✅ Component ครบชุด - ✅ Documentation ดี - ✅ Community ใหญ่ - ✅ Built-in theming **Cons:** - ❌ Bundle size ใหญ่ - ❌ Design opinionated (Material Design) - ❌ Customization ยาก - ❌ Performance overhead ### Option 2: Ant Design **Pros:** - ✅ Component ครบ (เน้น Enterprise) - ✅ i18n support ดี - ✅ Form components ครบ **Cons:** - ❌ Bundle size ใหญ่มาก - ❌ Chinese-centric design - ❌ Customization จำกัด - ❌ TypeScript support ไม่ดีเท่าไร ### Option 3: Chakra UI **Pros:** - ✅ Accessibility ดี - ✅ Customization ง่าย - ✅ TypeScript first - ✅ Dark mode built-in **Cons:** - ❌ Bundle size ค่อนข้างใหญ่ - ❌ CSS-in-JS overhead - ❌ Performance issues with many components ### Option 4: Headless UI + Tailwind CSS **Pros:** - ✅ Full control over styling - ✅ Lightweight - ✅ Accessibility ดี - ✅ No styling overhead **Cons:** - ❌ ต้องเขียน styles เอง - ❌ Component library น้อย - ❌ ใช้เวลาพัฒนานาน ### Option 5: Shadcn/UI + Tailwind CSS **วิธีการ:** Copy components ที่ต้องการไปยัง Project **Pros:** - ✅ **Full ownership:** Components เป็นของเรา ไม่ใช่ dependency - ✅ **Highly customizable:** แก้ไขได้เต็มที่ - ✅ **Accessibility:** ใช้ Radix UI Primitives - ✅ **Bundle size:** เฉพาะที่ใช้เท่านั้น - ✅ **Tailwind CSS:** Utility-first ง่ายต่อการ maintain - ✅ **TypeScript:** Type-safe - ✅ **Beautiful defaults:** Design ดูทันสมัย **Cons:** - ❌ ต้อง Copy components เอง - ❌ Update ต้องทำด้วยตัวเอง - ❌ ไม่มี `npm install` แบบ Library --- ## Decision Outcome **Chosen Option:** **Option 5 - Shadcn/UI + Tailwind CSS** ### Rationale 1. **Ownership:** เป็นเจ้าของ Code 100% ปรับแต่งได้อย่างเต็มที่ 2. **Bundle Size:** เล็กที่สุด (เฉพาะที่ใช้) 3. **Accessibility:** ใช้ Radix UI primitives ที่ทดสอบแล้ว 4. **Customization:** แก้ไขได้ตามต้องการ ไม่ติด Framework 5. **Tailwind CSS:** ทีมคุ้นเคยและใช้อยู่แล้ว 6. **Modern Design:** ดูสวยงามและทันสมัย --- ## Implementation Details ### 1. Setup Shadcn/UI ```bash # Initialize shadcn/ui npx shadcn-ui@latest init # Select options: # - TypeScript: Yes # - Style: Default # - Base color: Slate # - CSS variables: Yes ``` ```typescript // File: components.json { "$schema": "https://ui.shadcn.com/schema.json", "style": "default", "rsc": true, "tsx": true, "tailwind": { "config": "tailwind.config.js", "css": "app/globals.css", "baseColor": "slate", "cssVariables": true }, "aliases": { "components": "@/components", "utils": "@/lib/utils" } } ``` ### 2. Add Components ```bash # Add specific components npx shadcn-ui@latest add button npx shadcn-ui@latest add input npx shadcn-ui@latest add card npx shadcn-ui@latest add dialog npx shadcn-ui@latest add dropdown-menu npx shadcn-ui@latest add table # Components will be copied to components/ui/ ``` ### 3. Component Usage ```typescript // File: app/correspondences/page.tsx import { Button } from '@/components/ui/button'; import { Card } from '@/components/ui/card'; import { Input } from '@/components/ui/input'; export default function CorrespondencesPage() { return (
{correspondence.description}