npm create svelte@latest my-design-system
cd my-design-system
npm install
npm install -D tailwindcss postcss autoprefixer @tailwindcss/typography clsx tailwind-merge
npx tailwindcss init -p
Step 1: Tailwind Config (tailwind.config.js)
Extend Tailwind to reference CSS variables for colors, spacing, etc. This makes everything themeable.
/** @type {import('tailwindcss').Config} */
export default {
content: ['./src/**/*.{html,js,svelte,ts}', './node_modules/@trailblazer/svelte-design-system/**/*.{html,js,svelte,ts}'],
theme: {
extend: {
colors: {
// Semantic colors using CSS vars (HSL format for Tailwind)
background: 'hsl(var(--color-background))',
foreground: 'hsl(var(--color-foreground))',
primary: {
DEFAULT: 'hsl(var(--color-primary))',
foreground: 'hsl(var(--color-primary-foreground))',
},
secondary: {
DEFAULT: 'hsl(var(--color-secondary))',
foreground: 'hsl(var(--color-secondary-foreground))',
},
destructive: {
DEFAULT: 'hsl(var(--color-destructive))',
foreground: 'hsl(var(--color-destructive-foreground))',
},
muted: {
DEFAULT: 'hsl(var(--color-muted))',
foreground: 'hsl(var(--color-muted-foreground))',
},
accent: {
DEFAULT: 'hsl(var(--color-accent))',
foreground: 'hsl(var(--color-accent-foreground))',
},
popover: {
DEFAULT: 'hsl(var(--color-popover))',
foreground: 'hsl(var(--color-popover-foreground))',
},
card: {
DEFAULT: 'hsl(var(--color-card))',
foreground: 'hsl(var(--color-card-foreground))',
},
},
borderRadius: {
lg: 'var(--radius)',
md: 'calc(var(--radius) - 2px)',
sm: 'calc(var(--radius) - 4px)',
},
fontFamily: {
sans: ['var(--font-family)', 'ui-sans-serif', 'system-ui'],
},
keyframes: {
"accordion-down": {
from: { height: "0" },
to: { height: "var(--radix-accordion-content-height)" },
},
"accordion-up": {
from: { height: "var(--radix-accordion-content-height)" },
to: { height: "0" },
},
},
animation: {
"accordion-down": "accordion-down 0.2s ease-out",
"accordion-up": "accordion-up 0.2s ease-out",
},
},
},
plugins: [require('@tailwindcss/typography')],
};
Step 2: Global Styles with CSS Variables (src/app.css)
Define design tokens as CSS vars on :root (light theme) and [data-theme="dark"]. Easy to extend for more themes (e.g., data-theme="blue").
@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';
@layer base {
:root {
/* Color Tokens (HSL for Tailwind) */
--color-background: 0 0% 100%;
--color-foreground: 240 10% 3.9%;
--color-card: 0 0% 100%;
--color-card-foreground: 240 10% 3.9%;
--color-popover: 0 0% 100%;
--color-popover-foreground: 240 10% 3.9%;
--color-primary: 240 5.3% 26.1%;
--color-primary-foreground: 0 0% 98%;
--color-secondary: 240 4.8% 95.9%;
--color-secondary-foreground: 240 5.9% 10%;
--color-muted: 240 4.8% 95.9%;
--color-muted-foreground: 240 3.8% 46.1%;
--color-accent: 240 4.8% 95.9%;
--color-accent-foreground: 240 5.9% 10%;
--color-destructive: 0 84.2% 60.2%;
--color-destructive-foreground: 0 0% 98%;
--color-border: 240 5.9% 90%;
--color-input: 240 5.9% 90%;
--color-ring: 240 5.9% 10%;
/* Other Tokens */
--radius: 0.5rem;
--font-family: ui-sans-serif, system-ui, sans-serif;
}
[data-theme="dark"] {
--color-background: 240 10% 3.9%;
--color-foreground: 0 0% 98%;
--color-card: 240 10% 3.9%;
--color-card-foreground: 0 0% 98%;
--color-popover: 240 10% 3.9%;
--color-popover-foreground: 0 0% 98%;
--color-primary: 0 0% 98%;
--color-primary-foreground: 240 5.9% 10%;
--color-secondary: 240 3.7% 15.9%;
--color-secondary-foreground: 0 0% 98%;
--color-muted: 240 3.7% 15.9%;
--color-muted-foreground: 240 5% 64.9%;
--color-accent: 240 3.7% 15.9%;
--color-accent-foreground: 0 0% 98%;
--color-destructive: 0 62.8% 30.6%;
--color-destructive-foreground: 0 0% 98%;
--color-border: 240 3.7% 15.9%;
--color-input: 240 3.7% 15.9%;
--color-ring: 240 4.9% 83.9%;
}
/* Custom scrollbar (optional) */
*::-webkit-scrollbar {
width: 0.5rem;
}
*::-webkit-scrollbar-track {
@apply bg-transparent;
}
*::-webkit-scrollbar-thumb {
@apply rounded-md bg-muted/50 border border-transparent bg-clip-content;
}
}
Step 3: Theme Store (src/lib/stores/theme.js)
Svelte store for theme state. Persists to localStorage.
import { writable } from 'svelte/store';
import { browser } from '$app/environment';
function createThemeStore() {
const { subscribe, set } = writable('light');
if (browser) {
const saved = localStorage.getItem('theme');
if (saved) set(saved);
}
return {
subscribe,
set: (value) => {
if (browser) {
localStorage.setItem('theme', value);
document.documentElement.setAttribute('data-theme', value);
}
set(value);
}
};
}
export const theme = createThemeStore();
Step 4: Root Layout (src/routes/+layout.svelte)
Apply theme to <html> dynamically.
<script>
import '../app.css';
import { theme } from '$lib/stores/theme.js';
import { onMount } from 'svelte';
onMount(() => {
// Ensure theme is applied on mount
if (typeof document !== 'undefined') {
document.documentElement.setAttribute('data-theme', $theme);
}
});
</script>
<svelte:head>
<html lang="en" class="scroll-smooth" data-theme={$theme}>
<body class="min-h-screen bg-background font-sans antialiased">
<slot />
</body>
</html>
</svelte:head>
Step 5: Utility Functions (src/lib/utils.js)
For class composition (clsx + tailwind-merge).
import { clsx, type ClassValue } from 'clsx';
import { twMerge } from 'tailwind-merge';
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
Step 6: Design System Components
Create a src/lib/components/ui/ folder. Here's a starter set (Button, Card, Input, Badge). Each uses semantic Tailwind classes backed by CSS vars.
Button (src/lib/components/ui/button.svelte)
<script lang="ts">
import { cn } from '$lib/utils.js';
type Variant = 'default' | 'destructive' | 'outline' | 'secondary' | 'ghost' | 'link';
type Size = 'default' | 'sm' | 'lg' | 'icon';
export let variant: Variant = 'default';
export let size: Size = 'default';
export let className: string = '';
export let isLoading: boolean = false;
</script>
<button
class={cn(
'inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50',
{
'border bg-primary text-primary-foreground hover:bg-primary/90': variant === 'default',
'border border-destructive bg-destructive text-destructive-foreground hover:bg-destructive/90': variant === 'destructive',
'border border-input bg-background hover:bg-accent hover:text-accent-foreground': variant === 'outline',
'hover:bg-accent hover:text-accent-foreground': variant === 'ghost',
'underline-offset-4 hover:underline': variant === 'link',
},
{
'h-10 px-4 py-2': size === 'default',
'h-9 rounded-md px-3': size === 'sm',
'h-11 rounded-md px-8': size === 'lg',
},
className
)}
disabled={isLoading}
>
{#if isLoading}
<svg class="mr-2 h-4 w-4 animate-spin" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4" />
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z" />
</svg>
{/if}
<slot />
</button>
Card (src/lib/components/ui/card.svelte)
<script>
import { cn } from '$lib/utils.js';
export let className = '';
</script>
<div class={cn('rounded-lg border bg-card text-card-foreground shadow-sm', className)}>
<div class="p-6">
<slot />
</div>
</div>
Full Card with Header/Content/Footer:
<!-- card-header.svelte, card-content.svelte, card-footer.svelte similar, using cn with appropriate classes -->
<!-- Usage example in parent:
<Card>
<CardHeader>
<CardTitle>Title</CardTitle>
</CardHeader>
<CardContent>Content</CardContent>
</Card>
-->
Input (src/lib/components/ui/input.svelte)
<script>
import { cn } from '$lib/utils.js';
export let className = '';
</script>
<input
class={cn(
'flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50',
className
)}
{...$$restProps}
/>
Badge (src/lib/components/ui/badge.svelte)
<script>
import { cn } from '$lib/utils.js';
export let variant: 'default' | 'secondary' | 'destructive' | 'outline' = 'default';
export let className = '';
</script>
<span
class={cn(
'inline-flex items-center rounded-full border px-2.5 py-0.5 text-xs font-semibold transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2',
{
'border-transparent bg-primary text-primary-foreground hover:bg-primary/80': variant === 'default',
'border-transparent bg-secondary text-secondary-foreground hover:bg-secondary/80': variant === 'secondary',
'border border-destructive bg-destructive text-destructive-foreground hover:bg-destructive/80': variant === 'destructive',
'text-foreground': variant === 'outline',
},
className
)}
>
<slot />
</span>
Step 7: Theme Switcher Example (src/routes/+page.svelte)
<script>
import { theme } from '$lib/stores/theme.js';
import Button from '$lib/components/ui/button.svelte';
</script>
<div class="container mx-auto p-8">
<h1 class="text-4xl font-bold bg-gradient-to-r from-primary/50 to-secondary/50 bg-clip-text text-transparent">
Themeable Design System
</h1>
<div class="mt-8 space-x-2">
<Button on:click={() => theme.set('light')}>Light</Button>
<Button variant="secondary" on:click={() => theme.set('dark')}>Dark</Button>
</div>
<div class="mt-12 grid gap-6 md:grid-cols-2 lg:grid-cols-3">
<div class="space-y-4">
<Button>Default Button</Button>
<Button variant="destructive">Destructive</Button>
<Button variant="outline" size="lg">Outline Large</Button>
</div>
<div class="space-y-4">
<label class="block text-sm font-medium">Input</label>
<input class="w-full h-12 px-4 rounded-lg border bg-card" placeholder="Themeable input..." />
</div>
<div>
<div class="rounded-lg border bg-card p-6 shadow-sm">
<h3 class="font-semibold mb-2">Card</h3>
<p>Fully themeable with CSS vars.</p>
<span class="inline-flex mt-2 items-center rounded-full bg-muted px-2 py-1 text-xs">Badge</span>
</div>
</div>
</div>
</div>
Adding New Themes
- Add new
[data-theme="new-theme"] block in app.css with custom CSS vars.
- Switch via
theme.set('new-theme').
Extending the System
- New Components: Copy pattern (use
cn, semantic classes like bg-card).
- Tokens: Add vars to
:root/[data-theme], reference in Tailwind config.
- Variants: Use objects in
cn for props.
- Publish as Package: Move to
node_modules/@trailblazer/svelte-design-system and update tailwind.config.js content.
Run npm run dev to test. Everything switches instantly, persists, and scales! 🚀