$npx -y skills add dylantarre/design-system-skills --skill svelteBuilds token-driven Svelte 5 components with runes ($state, $props) and TypeScript. Use when creating Svelte component libraries, integrating design tokens, or building SvelteKit design system components.
| 1 | # Svelte Component Patterns |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Build accessible, token-driven Svelte 5 components using runes and modern patterns. Covers component structure, TypeScript integration, stores, and consuming design tokens. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - Creating a Svelte component library |
| 10 | - Building components that use design tokens |
| 11 | - Setting up a design system in SvelteKit |
| 12 | - Converting designs to Svelte components |
| 13 | |
| 14 | ## The Process |
| 15 | |
| 16 | 1. **Identify component type**: Primitive, composite, or layout? |
| 17 | 2. **Choose styling approach**: Scoped CSS, Tailwind, or global tokens? |
| 18 | 3. **Define props with TypeScript**: Use `$props()` rune |
| 19 | 4. **Implement with tokens**: CSS custom properties |
| 20 | 5. **Add accessibility**: ARIA, keyboard handling, focus management |
| 21 | 6. **Extract state logic**: Use `$state`, `$derived`, and `$effect` |
| 22 | |
| 23 | ## Svelte 5 Runes Quick Reference |
| 24 | |
| 25 | | Rune | Purpose | Example | |
| 26 | |------|---------|---------| |
| 27 | | `$state` | Reactive state | `let count = $state(0)` | |
| 28 | | `$derived` | Computed values | `let double = $derived(count * 2)` | |
| 29 | | `$effect` | Side effects | `$effect(() => console.log(count))` | |
| 30 | | `$props` | Component props | `let { variant = 'primary' } = $props()` | |
| 31 | | `$bindable` | Two-way binding | `let { value = $bindable() } = $props()` | |
| 32 | |
| 33 | ## Project Structure |
| 34 | |
| 35 | ``` |
| 36 | src/ |
| 37 | ├── lib/ |
| 38 | │ ├── components/ |
| 39 | │ │ ├── primitives/ |
| 40 | │ │ │ ├── Button/ |
| 41 | │ │ │ │ ├── Button.svelte |
| 42 | │ │ │ │ ├── Button.test.ts |
| 43 | │ │ │ │ └── index.ts |
| 44 | │ │ │ ├── Input/ |
| 45 | │ │ │ └── Text/ |
| 46 | │ │ ├── composite/ |
| 47 | │ │ │ ├── Card/ |
| 48 | │ │ │ ├── Modal/ |
| 49 | │ │ │ └── Dropdown/ |
| 50 | │ │ └── layout/ |
| 51 | │ │ ├── Stack/ |
| 52 | │ │ ├── Grid/ |
| 53 | │ │ └── Container/ |
| 54 | │ ├── stores/ |
| 55 | │ │ └── theme.svelte.ts |
| 56 | │ ├── tokens/ |
| 57 | │ │ └── index.css |
| 58 | │ └── index.ts |
| 59 | └── routes/ |
| 60 | ``` |
| 61 | |
| 62 | ## Component Patterns |
| 63 | |
| 64 | ### Button Component |
| 65 | |
| 66 | **Button.svelte:** |
| 67 | ```svelte |
| 68 | <script lang="ts"> |
| 69 | import type { Snippet } from 'svelte'; |
| 70 | import type { HTMLButtonAttributes } from 'svelte/elements'; |
| 71 | |
| 72 | interface Props extends HTMLButtonAttributes { |
| 73 | /** Visual style variant */ |
| 74 | variant?: 'primary' | 'secondary' | 'ghost' | 'danger'; |
| 75 | /** Size of the button */ |
| 76 | size?: 'sm' | 'md' | 'lg'; |
| 77 | /** Full width button */ |
| 78 | fullWidth?: boolean; |
| 79 | /** Loading state */ |
| 80 | loading?: boolean; |
| 81 | /** Icon before text */ |
| 82 | leftIcon?: Snippet; |
| 83 | /** Icon after text */ |
| 84 | rightIcon?: Snippet; |
| 85 | /** Button content */ |
| 86 | children: Snippet; |
| 87 | } |
| 88 | |
| 89 | let { |
| 90 | variant = 'primary', |
| 91 | size = 'md', |
| 92 | fullWidth = false, |
| 93 | loading = false, |
| 94 | disabled = false, |
| 95 | leftIcon, |
| 96 | rightIcon, |
| 97 | children, |
| 98 | class: className = '', |
| 99 | ...restProps |
| 100 | }: Props = $props(); |
| 101 | |
| 102 | let isDisabled = $derived(disabled || loading); |
| 103 | </script> |
| 104 | |
| 105 | <button |
| 106 | class="btn btn--{variant} btn--{size} {className}" |
| 107 | class:btn--full-width={fullWidth} |
| 108 | class:btn--loading={loading} |
| 109 | disabled={isDisabled} |
| 110 | aria-busy={loading} |
| 111 | {...restProps} |
| 112 | > |
| 113 | {#if loading} |
| 114 | <span class="btn__spinner" aria-hidden="true"></span> |
| 115 | {/if} |
| 116 | |
| 117 | {#if leftIcon} |
| 118 | <span class="btn__icon"> |
| 119 | {@render leftIcon()} |
| 120 | </span> |
| 121 | {/if} |
| 122 | |
| 123 | <span class="btn__label"> |
| 124 | {@render children()} |
| 125 | </span> |
| 126 | |
| 127 | {#if rightIcon} |
| 128 | <span class="btn__icon"> |
| 129 | {@render rightIcon()} |
| 130 | </span> |
| 131 | {/if} |
| 132 | </button> |
| 133 | |
| 134 | <style> |
| 135 | .btn { |
| 136 | display: inline-flex; |
| 137 | align-items: center; |
| 138 | justify-content: center; |
| 139 | gap: var(--spacing-xs); |
| 140 | font-family: inherit; |
| 141 | font-weight: 500; |
| 142 | line-height: 1; |
| 143 | white-space: nowrap; |
| 144 | cursor: pointer; |
| 145 | user-select: none; |
| 146 | border: 1px solid transparent; |
| 147 | border-radius: var(--radius-md); |
| 148 | transition: |
| 149 | background-color 150ms ease, |
| 150 | border-color 150ms ease, |
| 151 | transform 100ms ease; |
| 152 | } |
| 153 | |
| 154 | .btn:focus-visible { |
| 155 | outline: 2px solid var(--color-primary-500); |
| 156 | outline-offset: 2px; |
| 157 | } |
| 158 | |
| 159 | .btn:active:not(:disabled) { |
| 160 | transform: scale(0.98); |
| 161 | } |
| 162 | |
| 163 | .btn:disabled { |
| 164 | cursor: not-allowed; |
| 165 | opacity: 0.5; |
| 166 | } |
| 167 | |
| 168 | /* Variants */ |
| 169 | .btn--primary { |
| 170 | background-color: var(--color-primary-500); |
| 171 | color: white; |
| 172 | } |
| 173 | |
| 174 | .btn--primary:hover:not(:disabled) { |
| 175 | background-color: var(--color-primary-600); |
| 176 | } |
| 177 | |
| 178 | .btn--secondary { |
| 179 | background-color: transparent; |
| 180 | border-color: var(--color-gray-300); |
| 181 | color: var(--color-gray-700); |
| 182 | } |
| 183 | |
| 184 | .btn--secondary:hover:not(:disabled) { |
| 185 | background-color: var(--color-gray-50); |
| 186 | border-color: var(--color-gray-400); |
| 187 | } |
| 188 | |
| 189 | .btn--ghost { |
| 190 | background-color: transparent; |
| 191 | color: var(--color-gray-700); |
| 192 | } |
| 193 | |
| 194 | .btn--ghost:hover:not(:disabled) { |
| 195 | background-color: var(--color-gray-100); |
| 196 | } |
| 197 | |
| 198 | .btn--danger { |
| 199 | background-color: var(--color-error-500); |
| 200 | color: white; |
| 201 | } |
| 202 | |
| 203 | .btn--danger:hover:not(:disabled) { |
| 204 | background-color: var(--color-er |