I'm zeroing in on the core challenge: crafting a design system built around SvelteKit, Tailwind CSS, and CSS Variables. Right now, I'm defining the framework and identifying key technologies. I've begun to break down the task into smaller, manageable pieces, focusing on how these elements will integrate.
Decomposing the Strategy
Now I'm diving deeper into the technicalities of the theming system. I'm focusing on defining how the CSS variables will work. Next, I'm mapping the variables into the Tailwind configuration. The plan is to create a Svelte store to handle the theme swapping, and use it in the layout.
Detailing the Implementation
Now I'm delving into the specific implementation steps. First, I'm setting up the SvelteKit project and configuring Tailwind. Then, I'll establish the fundamental CSS variables and integrate them with Tailwind. Next, I'll construct a button component to test the system and create themed examples, along with the actual switching logic. I have a draft of the CSS and Tailwind config and I'm aware of the requirements for alpha values.
Developing the Code
Now, I'm at the point of writing the actual code, iterating on the key components. I've mapped the directory structure, and I'm drafting the base CSS in app.css using @layer directives. The Tailwind config is coming along, along with the all-important RGB channel implementation. I'm focusing on the theme store and its reactivity, especially localStorage persistence.
Revising and Refining
I'm now iterating on the core mechanics, especially the theme store and its integration within the SvelteKit layout. My current focus is how the theme store interacts with localStorage for persistence, and how to prevent FOUC. The directory structure is in place, and I've established the base styles in app.css. Also, I'm verifying the opacity modifiers in the Tailwind config and looking at ways to streamline adding new themes.
This guide outlines how to build a robust design system in SvelteKit where Tailwind CSS is purely the utility engine, but the actual values (colors, spacing, radii) are driven by CSS variables. This allows for instant, runtime theme switching without external libraries.
Core Concept
Tailwind usually compiles colors at build time. To make it dynamic:
CSS: We define variables (Custom Properties) for our design tokens.
Tailwind: We tell Tailwind to look at those variables.
SvelteKit: We manipulate the data-theme attribute on the <html> tag to swap variable values.
Step 1: Project Setup
Assuming you have a SvelteKit project with Tailwind installed:
We need a Svelte store to manage the state across the app and persist it in localStorage.
Create src/lib/stores/theme.ts:
typescript
import { writable } from 'svelte/store';
import { browser } from '$app/environment';
// Define your theme types
export type Theme = 'light' | 'dark' | 'coffee';
// Initial value logic
const defaultValue = 'light';
const initialValue = browser
? (window.localStorage.getItem('theme') as Theme) ?? defaultValue
: defaultValue;
export const theme = writable<Theme>(initialValue);
// Subscribe to the store to keep localStorage and DOM in sync
theme.subscribe((value) => {
if (browser) {
window.localStorage.setItem('theme', value);
document.documentElement.setAttribute('data-theme', value);
}
});
// Helper to toggle functionality
export const toggleTheme = () => {
theme.update(current => current === 'dark' ? 'light' : 'dark');
}
Step 5: Prevent "Flash of Unstyled Content" (FOUC)
If a user has "Dark" selected, but the server renders "Light" default HTML, the screen will flash white before turning dark. Fix this in src/app.html by adding a script in <head>:
html
<head>
<!-- ... codes ... -->
<script>
// Immediately set the theme before the page renders content
const savedTheme = localStorage.getItem('theme') || 'light';
document.documentElement.setAttribute('data-theme', savedTheme);
</script>
</head>
Step 6: Create Reusable Components
Now we build components using Tailwind classes like bg-primary. They will automatically adapt to the theme.
<script lang="ts">
import { theme } from '$lib/stores/theme';
import Button from '$lib/components/Button.svelte';
import Card from '$lib/components/Card.svelte';
</script>
<div class="min-h-screen flex flex-col items-center justify-center space-y-8 p-4">
<div class="text-center space-y-2">
<h1 class="text-4xl font-extrabold text-primary">Themable System</h1>
<p class="text-text/60">SvelteKit + Tailwind + CSS Variables</p>
</div>
<!-- Theme Switcher Controls -->
<div class="flex gap-4 bg-surface p-2 rounded-box">
{#each ['light', 'dark', 'coffee'] as t}
<button
class="px-3 py-1 rounded capitalize {$theme === t ? 'bg-primary text-white shadow' : 'text-text/60 hover:text-text'}"
on:click={() => theme.set(t)}
>
{t}
</button>
{/each}
</div>
<!-- Component Showcase -->
<div class="grid grid-cols-1 md:grid-cols-2 gap-6 max-w-2xl w-full">
<Card>
<span slot="title">Primary Colors</span>
This card uses the <code>surface</code> color. The title is in <code>primary</code>.
Notice how the border uses opacity: <code>border-text/10</code>.
</Card>
<Card>
<span slot="title">Interactive Elements</span>
<div class="flex gap-2 mt-4">
<Button>Primary</Button>
<Button variant="secondary">Secondary</Button>
</div>
</Card>
</div>
</div>
Summary of Benefits
Zero Config Build: You don't need separate CSS files for themes. It's one build.
Opacity Support: By using the rgb variable strategy, utilities like bg-primary/20 work perfectly.
Layout Theming: Notice the "Coffee" theme in Step 2 also changes --radius-box. Switching to Coffee changes colors and makes buttons square instantly.
Portability: The Card and Button components don't know what theme is active. They just ask for bg-surface and the browser renders the correct hex code.