# ADR-012: UI Component Library Strategy **Status:** ✅ Accepted **Date:** 2026-02-24 **Decision Makers:** Frontend Team, UX Designer **Related Documents:** [Frontend Guidelines](../05-Engineering-Guidelines/05-03-frontend-guidelines.md), [ADR-005: Technology Stack](./ADR-005-technology-stack.md) **Version Applicability:** v1.8.0+ **Next Review:** 2026-08-01 (6-month cycle) --- ## Gap Analysis & Requirement Linking ### ปิด Gap จาก Requirements: | Gap/Requirement | แหล่งที่มา | วิธีการแก้ไขใน ADR นี้ | |----------------|-------------|-------------------| | **Design Consistency** | [Product Vision](../00-overview/00-03-product-vision.md) - UI/UX Requirements | Shadcn/UI for consistent design system | | **Accessibility Support** | [Acceptance Criteria](../01-Requirements/01-05-acceptance-criteria.md) - AC-UI-003 | Radix UI primitives for WCAG 2.1 AA | | **Performance Optimization** | [Frontend Guidelines](../05-Engineering-Guidelines/05-03-frontend-guidelines.md) - Performance | Tree-shakeable components, minimal bundle | | **Customization Flexibility** | [Engineering Guidelines](../05-Engineering-Guidelines/05-01-fullstack-js-guidelines.md) - DX | Full ownership of component code | | **Bundle Size Constraints** | [Architecture](../02-architecture/02-02-software-architecture.md) - Performance | Copy-only-what-you-use approach | ### แก้ไขความขัดแย้ง: - **Conflict:** Library vs. Custom (MUI/Ant Design vs. Shadcn/UI) - **Resolution:** Chose Shadcn/UI for full control and minimal bundle - **Trade-off:** Manual updates vs. Complete customization freedom --- ## Impact Analysis ### Affected Components (ส่วนประกอบที่ได้รับผลกระทบ): | Component | ผลกระทบ | ความสำคัญ | |-----------|----------|-----------| | **UI Components** | All UI components use Shadcn/UI | 🔴 Critical | | **Tailwind Config** | CSS variables and theming | 🔴 Critical | | **Component Library** | Custom component compositions | 🔴 Critical | | **Form Components** | Integration with React Hook Form | 🟡 Important | | **Layout Components** | Page layouts and navigation | 🟡 Important | | **Theme System** | Dark/light mode support | 🟡 Important | | **Component Testing** | Unit tests for custom components | 🟡 Important | | **Documentation** | Component usage guidelines | 🟢 Guidelines | | **Update Process** | Manual component update workflow | 🟢 Guidelines | ### Required Changes (การเปลี่ยนแปลงที่ต้องดำเนินการ): #### Frontend (Next.js) - [x] Initialize Shadcn/UI project - [x] Add core components (Button, Input, Card, etc.) - [x] Setup Tailwind CSS with CSS variables - [x] Create custom component compositions - [x] Implement theme switching - [x] Update all pages to use new components - [x] Add component testing #### Design System - [x] Define design tokens and colors - [x] Create component usage guidelines - [x] Document customization patterns - [x] Setup component update workflow --- ## 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}