# Component Guide **Project**: Next.js + FastAPI Template **Version**: 1.0 **Last Updated**: 2025-10-31 --- ## Table of Contents 1. [shadcn/ui Components](#1-shadcn-ui-components) 2. [Custom Components](#2-custom-components) 3. [Component Composition](#3-component-composition) 4. [Customization](#4-customization) 5. [Accessibility](#5-accessibility) --- ## 1. shadcn/ui Components ### 1.1 Overview This project uses [shadcn/ui](https://ui.shadcn.com), a collection of accessible, customizable components built on Radix UI primitives. Components are copied into the project (not installed as npm dependencies), giving you full control. **Installation Method:** ```bash npx shadcn@latest add button card input table dialog ``` ### 1.2 Core Components #### Button ```typescript import { Button } from '@/components/ui/button'; // Variants // Sizes // States // As Link ``` #### Card ```typescript import { Card, CardHeader, CardTitle, CardDescription, CardContent, CardFooter } from '@/components/ui/card'; Users Manage system users

Card content goes here

``` #### Dialog / Modal ```typescript import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter, DialogTrigger } from '@/components/ui/dialog'; Delete User Are you sure you want to delete this user? This action cannot be undone. ``` #### Form ```typescript import { Form, FormField, FormItem, FormLabel, FormControl, FormDescription, FormMessage } from '@/components/ui/form'; import { Input } from '@/components/ui/input'; import { useForm } from 'react-hook-form'; const form = useForm();
( Email Your email address )} /> ``` #### Table ```typescript import { Table, TableHeader, TableBody, TableFooter, TableHead, TableRow, TableCell } from '@/components/ui/table'; Name Email Role {users.map((user) => ( {user.name} {user.email} {user.role} ))}
``` #### Toast / Notifications ```typescript import { toast } from 'sonner'; // Success toast.success('User created successfully'); // Error toast.error('Failed to delete user'); // Info toast.info('Processing your request...'); // Loading toast.loading('Saving changes...'); // Custom toast('Event has been created', { description: 'Monday, January 3rd at 6:00pm', action: { label: 'Undo', onClick: () => console.log('Undo'), }, }); ``` #### Tabs ```typescript import { Tabs, TabsList, TabsTrigger, TabsContent } from '@/components/ui/tabs'; Profile Password Sessions ``` --- ## 2. Custom Components ### 2.1 Layout Components #### Header ```typescript import { Header } from '@/components/layout/Header'; // Usage (in layout.tsx)
// Features: // - Logo/brand // - Navigation links // - User menu (avatar, name, dropdown) // - Theme toggle // - Mobile menu button ``` #### PageContainer ```typescript import { PageContainer } from '@/components/layout/PageContainer';

Page Title

Page content...

// Provides: // - Consistent padding // - Max-width container // - Responsive layout ``` #### PageHeader ```typescript import { PageHeader } from '@/components/common/PageHeader'; Create User} /> ``` ### 2.2 Data Display Components #### DataTable Generic, reusable data table with sorting, filtering, and pagination. ```typescript import { DataTable } from '@/components/common/DataTable'; import { ColumnDef } from '@tanstack/react-table'; // Define columns const columns: ColumnDef[] = [ { accessorKey: 'name', header: 'Name', }, { accessorKey: 'email', header: 'Email', }, { id: 'actions', cell: ({ row }) => ( ), }, ]; // Use DataTable ``` #### LoadingSpinner ```typescript import { LoadingSpinner } from '@/components/common/LoadingSpinner'; // Sizes // With text

Loading users...

``` #### EmptyState ```typescript import { EmptyState } from '@/components/common/EmptyState'; } title="No users found" description="Get started by creating a new user" action={ } /> ``` ### 2.3 Admin Components #### UserTable ```typescript import { UserTable } from '@/components/admin/UserTable'; console.log(user)} /> // Features: // - Search // - Filters (role, status) // - Sorting // - Pagination // - Bulk selection // - Bulk actions (activate, deactivate, delete) ``` #### UserForm ```typescript import { UserForm } from '@/components/admin/UserForm'; // Create mode router.push('/admin/users')} /> // Edit mode toast.success('User updated')} /> // Features: // - Validation with Zod // - Field errors // - Loading states // - Cancel/Submit actions ``` #### OrganizationTable ```typescript import { OrganizationTable } from '@/components/admin/OrganizationTable'; // Features: // - Search // - Member count display // - Actions (edit, delete, view members) ``` #### BulkActionBar ```typescript import { BulkActionBar } from '@/components/admin/BulkActionBar'; handleBulkAction(action, selectedUserIds)} onClearSelection={() => setSelectedUserIds([])} actions={[ { value: 'activate', label: 'Activate' }, { value: 'deactivate', label: 'Deactivate' }, { value: 'delete', label: 'Delete', variant: 'destructive' }, ]} /> // Displays: // - Selection count // - Action dropdown // - Confirmation dialogs // - Progress indicators ``` ### 2.4 Settings Components #### ProfileSettings ```typescript import { ProfileSettings } from '@/components/settings/ProfileSettings'; console.log('Updated:', updatedUser)} /> // Fields: // - First name, last name // - Email (readonly) // - Phone number // - Avatar upload (optional) // - Preferences ``` #### PasswordSettings ```typescript import { PasswordSettings } from '@/components/settings/PasswordSettings'; // Fields: // - Current password // - New password // - Confirm password // - Option to logout all other devices ``` #### SessionManagement ```typescript import { SessionManagement } from '@/components/settings/SessionManagement'; // Features: // - List all active sessions // - Current session badge // - Device icons // - Location display // - Last used timestamp // - Revoke session button // - Logout all other devices button ``` #### SessionCard ```typescript import { SessionCard } from '@/components/settings/SessionCard'; revokeSession(session.id)} /> // Displays: // - Device icon (desktop/mobile/tablet) // - Device name // - Location (city, country) // - IP address // - Last used (relative time) // - "This device" badge if current // - Revoke button (disabled for current) ``` ### 2.5 Chart Components #### BarChartCard ```typescript import { BarChartCard } from '@/components/charts/BarChartCard'; ``` #### LineChartCard ```typescript import { LineChartCard } from '@/components/charts/LineChartCard'; ``` #### PieChartCard ```typescript import { PieChartCard } from '@/components/charts/PieChartCard'; ``` --- ## 3. Component Composition ### 3.1 Form + Dialog Pattern ```typescript Create User Add a new user to the system { setIsOpen(false); queryClient.invalidateQueries(['users']); }} onCancel={() => setIsOpen(false)} /> ``` ### 3.2 Card + Table Pattern ```typescript
Users Manage system users
``` ### 3.3 Tabs + Settings Pattern ```typescript Account Settings Profile Password Sessions ``` ### 3.4 Bulk Actions Pattern ```typescript function UserList() { const [selectedIds, setSelectedIds] = useState([]); const { data: users } = useUsers(); return (
{selectedIds.length > 0 && ( setSelectedIds([])} /> )}
); } ``` --- ## 4. Customization ### 4.1 Theming Colors are defined in `tailwind.config.ts` using CSS variables: ```typescript // tailwind.config.ts export default { theme: { extend: { colors: { border: 'hsl(var(--border))', background: 'hsl(var(--background))', foreground: 'hsl(var(--foreground))', primary: { DEFAULT: 'hsl(var(--primary))', foreground: 'hsl(var(--primary-foreground))', }, // ... }, }, }, }; ``` **Customize colors in `globals.css`:** ```css @layer base { :root { --primary: 222.2 47.4% 11.2%; --primary-foreground: 210 40% 98%; /* ... */ } .dark { --primary: 210 40% 98%; --primary-foreground: 222.2 47.4% 11.2%; /* ... */ } } ``` ### 4.2 Component Variants Add new variants to existing components: ```typescript // components/ui/button.tsx const buttonVariants = cva( 'base-classes', { variants: { variant: { default: '...', destructive: '...', outline: '...', // Add custom variant success: 'bg-green-600 text-white hover:bg-green-700', }, }, } ); // Usage ``` ### 4.3 Extending Components Create wrapper components: ```typescript // components/common/ConfirmDialog.tsx interface ConfirmDialogProps { title: string; description: string; confirmLabel?: string; onConfirm: () => void; onCancel: () => void; } export function ConfirmDialog({ title, description, confirmLabel = 'Confirm', onConfirm, onCancel, }: ConfirmDialogProps) { return ( {title} {description} ); } ``` --- ## 5. Accessibility ### 5.1 Keyboard Navigation All shadcn/ui components support keyboard navigation: - `Tab`: Move focus - `Enter`/`Space`: Activate - `Escape`: Close dialogs/dropdowns - Arrow keys: Navigate lists/menus ### 5.2 Screen Reader Support Components include proper ARIA labels: ```typescript
Loading users...
``` ### 5.3 Focus Management Dialog components automatically manage focus: - Focus trap inside dialog - Return focus on close - Focus first focusable element ### 5.4 Color Contrast All theme colors meet WCAG 2.1 Level AA standards: - Normal text: 4.5:1 contrast ratio - Large text: 3:1 contrast ratio --- ## Conclusion This guide covers the essential components in the project. For more details: - **shadcn/ui docs**: https://ui.shadcn.com - **Radix UI docs**: https://www.radix-ui.com - **TanStack Table docs**: https://tanstack.com/table - **Recharts docs**: https://recharts.org For implementation examples, see `FEATURE_EXAMPLES.md`.