Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Integrate and customize shadcn/ui components—discovery, installation, theming, and Radix/Base UI best practices
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
resources/customization-guide.md
1# shadcn/ui Customization Guide23Learn how to customize shadcn/ui components to match your brand and design requirements.45## Theming Approach67shadcn/ui uses a CSS variable-based theming system, making it easy to customize colors, spacing, and other design tokens globally.89## Color Customization1011### Understanding the Color System1213shadcn/ui uses HSL color values stored as CSS variables. Each color has a base value and a foreground variant for text/content that appears on top of it.1415**Base Color Variables** (in `globals.css`):16```css17:root {18--background: 0 0% 100%; /* Page background */19--foreground: 222.2 84% 4.9%; /* Primary text color */20--primary: 221.2 83.2% 53.3%; /* Primary brand color */21--primary-foreground: 210 40% 98%; /* Text on primary */22--secondary: 210 40% 96.1%; /* Secondary actions */23--accent: 210 40% 96.1%; /* Accent highlights */24--muted: 210 40% 96.1%; /* Muted backgrounds */25--destructive: 0 84.2% 60.2%; /* Error/danger */26--border: 214.3 31.8% 91.4%; /* Border colors */27--input: 214.3 31.8% 91.4%; /* Input borders */28--ring: 221.2 83.2% 53.3%; /* Focus rings */29}30```3132### Changing Brand Colors3334To match your brand, update the primary color:3536```css37:root {38/* Original blue */39--primary: 221.2 83.2% 53.3%;4041/* Change to brand purple */42--primary: 270 91% 65%;4344/* Adjust foreground for contrast */45--primary-foreground: 0 0% 100%;46}47```4849**HSL Format**: `hue saturation lightness`50- Hue: 0-360 (color wheel position)51- Saturation: 0-100% (color intensity)52- Lightness: 0-100% (brightness)5354### Tools for Color Selection55561. **HSL Color Picker**: https://hslpicker.com/572. **Shadcn Theme Generator**: https://ui.shadcn.com/themes583. **Coolors**: https://coolors.co/ (generates palettes)5960### Creating a Color Scheme6162Start with your primary brand color, then derive other colors:6364```css65:root {66/* 1. Primary brand color */67--primary: 230 90% 60%;68--primary-foreground: 0 0% 100%;6970/* 2. Lighter variant for secondary */71--secondary: 230 30% 95%;72--secondary-foreground: 230 90% 30%;7374/* 3. Subtle accent (shift hue slightly) */75--accent: 200 90% 60%;76--accent-foreground: 0 0% 100%;7778/* 4. Muted backgrounds (low saturation) */79--muted: 230 20% 96%;80--muted-foreground: 230 20% 40%;8182/* 5. Keep destructive red-based */83--destructive: 0 84% 60%;84--destructive-foreground: 0 0% 100%;85}86```8788## Dark Mode8990### Setting Up Dark Mode9192shadcn/ui includes dark mode support out of the box. Add dark mode colors:9394```css95.dark {96--background: 222.2 84% 4.9%;97--foreground: 210 40% 98%;98--primary: 221.2 83.2% 53.3%;99--primary-foreground: 210 40% 98%;100/* ... all color variables */101}102```103104### Toggle Dark Mode105106**Next.js with next-themes**:107```bash108npm install next-themes109```110111```tsx112// app/providers.tsx113"use client"114115import { ThemeProvider } from "next-themes"116117export function Providers({ children }: { children: React.ReactNode }) {118return (119<ThemeProvider attribute="class" defaultTheme="system" enableSystem>120{children}121</ThemeProvider>122)123}124125// app/layout.tsx126import { Providers } from "./providers"127128export default function RootLayout({ children }) {129return (130<html suppressHydrationWarning>131<body>132<Providers>{children}</Providers>133</body>134</html>135)136}137138// components/theme-toggle.tsx139"use client"140141import { Moon, Sun } from "lucide-react"142import { useTheme } from "next-themes"143import { Button } from "@/components/ui/button"144145export function ThemeToggle() {146const { setTheme, theme } = useTheme()147148return (149<Button150variant="ghost"151size="icon"152onClick={() => setTheme(theme === "light" ? "dark" : "light")}153>154<Sun className="h-5 w-5 rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0" />155<Moon className="absolute h-5 w-5 rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100" />156<span className="sr-only">Toggle theme</span>157</Button>158)159}160```161162## Component Customization163164### Using Variants165166shadcn/ui components use `class-variance-authority` (cva) for variants. Example from Button:167168```typescript169// components/ui/button.tsx170import { cva, type VariantProps } from "class-variance-authority"171172const buttonVariants = cva(173"inline-flex items-center justify-center rounded-md text-sm font-medium",174{175variants: {176variant: {177default: "bg-primary text-primary-foreground hover:bg-primary/90",178destructive: "bg-destructive text-destructive-foreground hover:bg-destructive/90",179outline: "border border-input bg-background hover:bg-accent",180ghost: "hover:bg-accent hover:text-accent-foreground",181link: "text-primary underline-offset-4 hover:underline",182},183size: {184default: "h-10 px-4 py-2",185sm: "h-9 rounded-md px-3",186lg: "h-11 rounded-md px-8",187icon: "h-10 w-10",188},189},190defaultVariants: {191variant: "default",192size: "default",193},194}195)196```197198### Adding Custom Variants199200To add a new variant, edit the component file in `components/ui/`:201202```typescript203// Add new "success" variant to button204const buttonVariants = cva(205"...",206{207variants: {208variant: {209default: "...",210destructive: "...",211// Add new variant212success: "bg-green-600 text-white hover:bg-green-700",213},214// Add new size215size: {216default: "...",217xl: "h-12 rounded-md px-10 text-base",218},219},220}221)222223// Update TypeScript interface224export interface ButtonProps225extends React.ButtonHTMLAttributes<HTMLButtonElement>,226VariantProps<typeof buttonVariants> {227asChild?: boolean228}229```230231Usage:232```tsx233<Button variant="success" size="xl">Save Changes</Button>234```235236### Creating Composite Components237238Don't modify `components/ui/` directly. Instead, create wrapper components:239240```tsx241// components/loading-button.tsx242import { Button, ButtonProps } from "@/components/ui/button"243import { Loader2 } from "lucide-react"244245interface LoadingButtonProps extends ButtonProps {246loading?: boolean247}248249export function LoadingButton({250loading,251children,252disabled,253...props254}: LoadingButtonProps) {255return (256<Button disabled={loading || disabled} {...props}>257{loading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}258{children}259</Button>260)261}262```263264## Typography Customization265266### Font Family267268Update `tailwind.config.js`:269270```javascript271module.exports = {272theme: {273extend: {274fontFamily: {275sans: ['Inter', 'system-ui', 'sans-serif'],276heading: ['Poppins', 'system-ui', 'sans-serif'],277mono: ['Fira Code', 'monospace'],278},279},280},281}282```283284Import fonts in your layout:285286```tsx287// app/layout.tsx288import { Inter, Poppins } from 'next/font/google'289290const inter = Inter({ subsets: ['latin'], variable: '--font-sans' })291const poppins = Poppins({292weight: ['600', '700'],293subsets: ['latin'],294variable: '--font-heading',295})296297export default function RootLayout({ children }) {298return (299<html className={`${inter.variable} ${poppins.variable}`}>300<body className="font-sans">{children}</body>301</html>302)303}304```305306Use in components:307```tsx308<h1 className="font-heading text-3xl">Heading</h1>309<p className="font-sans">Body text</p>310```311312### Font Sizes313314Extend Tailwind's font scale:315316```javascript317// tailwind.config.js318module.exports = {319theme: {320extend: {321fontSize: {322'xs': '0.75rem', // 12px323'sm': '0.875rem', // 14px324'base': '1rem', // 16px325'lg': '1.125rem', // 18px326'xl': '1.25rem', // 20px327'2xl': '1.5rem', // 24px328'3xl': '1.875rem', // 30px329'4xl': '2.25rem', // 36px330'5xl': '3rem', // 48px331'6xl': '3.75rem', // 60px332'7xl': '4.5rem', // 72px333},334},335},336}337```338339## Spacing & Layout340341### Border Radius342343Customize roundedness globally:344345```css346/* globals.css */347:root {348--radius: 0.5rem; /* Default (8px) */349350/* More rounded */351--radius: 1rem; /* 16px */352353/* Sharp edges */354--radius: 0; /* No rounding */355356/* Very rounded */357--radius: 1.5rem; /* 24px */358}359```360361This affects all components using `rounded-lg`, `rounded-md`, `rounded-sm`.362363### Custom Spacing364365Extend Tailwind's spacing scale:366367```javascript368// tailwind.config.js369module.exports = {370theme: {371extend: {372spacing: {373'72': '18rem',374'84': '21rem',375'96': '24rem',376},377},378},379}380```381382## Animation Customization383384### Adjusting Existing Animations385386```javascript387// tailwind.config.js388module.exports = {389theme: {390extend: {391keyframes: {392// Make accordion faster393"accordion-down": {394from: { height: 0 },395to: { height: "var(--radix-accordion-content-height)" },396},397"accordion-up": {398from: { height: "var(--radix-accordion-content-height)" },399to: { height: 0 },400},401},402animation: {403"accordion-down": "accordion-down 0.15s ease-out", // Faster404"accordion-up": "accordion-up 0.15s ease-out",405},406},407},408}409```410411### Adding New Animations412413```javascript414// tailwind.config.js415module.exports = {416theme: {417extend: {418keyframes: {419"fade-in": {420"0%": { opacity: 0 },421"100%": { opacity: 1 },422},423"slide-in-bottom": {424"0%": { transform: "translateY(100%)" },425"100%": { transform: "translateY(0)" },426},427},428animation: {429"fade-in": "fade-in 0.3s ease-out",430"slide-in-bottom": "slide-in-bottom 0.3s ease-out",431},432},433},434}435```436437Use in components:438```tsx439<div className="animate-fade-in">Content</div>440```441442## Advanced Customization443444### Creating a Design System445446Structure your customizations:447448```449src/450├── styles/451│ ├── globals.css # CSS variables & base styles452│ ├── themes/453│ │ ├── default.css # Default theme454│ │ └── brand.css # Brand-specific overrides455│ └── components/456│ └── custom.css # Component-specific styles457├── lib/458│ └── design-tokens.ts # Shared constants459└── components/460├── ui/ # shadcn components (don't modify)461└── custom/ # Your wrapper components462```463464### Design Tokens File465466```typescript467// lib/design-tokens.ts468export const designTokens = {469colors: {470brand: {471primary: 'hsl(230, 90%, 60%)',472secondary: 'hsl(230, 30%, 95%)',473},474},475spacing: {476section: '5rem',477card: '1.5rem',478},479radius: {480card: '1rem',481button: '0.5rem',482},483typography: {484h1: 'text-5xl font-heading font-bold',485h2: 'text-4xl font-heading font-semibold',486h3: 'text-3xl font-heading font-semibold',487body: 'text-base font-sans',488small: 'text-sm text-muted-foreground',489},490} as const491```492493Use in components:494```tsx495import { designTokens } from "@/lib/design-tokens"496497<h1 className={designTokens.typography.h1}>Title</h1>498```499500## Best Practices5015021. **Don't modify `components/ui/` files directly**: Create wrapper components instead5032. **Use CSS variables for theming**: Easier to maintain and switch themes5043. **Leverage Tailwind's `extend`**: Don't replace defaults, extend them5054. **Keep variants in component files**: Co-locate variant logic with components5065. **Test dark mode**: Ensure all customizations work in both themes5076. **Document custom variants**: Add comments for custom additions5087. **Use consistent spacing**: Stick to Tailwind's spacing scale5098. **Maintain accessibility**: Don't sacrifice contrast for aesthetics510511## Resources512513- [Tailwind CSS Customization](https://tailwindcss.com/docs/theme)514- [CVA Documentation](https://cva.style/docs)515- [Radix UI Theming](https://www.radix-ui.com/themes/docs/theme/overview)516- [HSL Color Theory](https://www.w3.org/TR/css-color-3/#hsl-color)517