|
| 1 | +<script lang="ts"> |
| 2 | + import * as Card from '@/lib/components/ui/card/index'; |
| 3 | + import { ScrollArea } from '@/lib/components/ui/scroll-area/index'; |
| 4 | + import { themes, type ThemeOptions } from '@/lib/utils/themes'; |
| 5 | + import { Sun, Moon } from '@lucide/svelte'; |
| 6 | + import * as RadioGroup from '@/lib/components/ui/radio-group/index'; |
| 7 | + import Label from '@/lib/components/ui/label/label.svelte'; |
| 8 | + import { onMount } from 'svelte'; |
| 9 | +
|
| 10 | + let currentTheme = $state(themes[0]); |
| 11 | +
|
| 12 | + onMount(() => { |
| 13 | + const storedTheme = localStorage.getItem('theme'); |
| 14 | + if (storedTheme) return handleThemeChange(storedTheme); |
| 15 | + checkSystemColorPreference(); |
| 16 | + }); |
| 17 | +
|
| 18 | + const handleThemeChange = (theme: ThemeOptions) => { |
| 19 | + currentTheme = theme; |
| 20 | + document.documentElement.className = ''; |
| 21 | + document.documentElement.classList.add(theme); |
| 22 | + localStorage.setItem('theme', theme); |
| 23 | + }; |
| 24 | +
|
| 25 | + const checkSystemColorPreference = () => { |
| 26 | + const prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)').matches; |
| 27 | + currentTheme = prefersDarkScheme ? 'dark' : 'light'; |
| 28 | + }; |
| 29 | +</script> |
| 30 | + |
| 31 | +<Card.Root> |
| 32 | + <Card.Header> |
| 33 | + <Card.Title>Theme Settings</Card.Title> |
| 34 | + <Card.Description>Manage your theme here</Card.Description> |
| 35 | + </Card.Header> |
| 36 | + <Card.Content> |
| 37 | + <ScrollArea class="h-[300px] w-full rounded-md border p-4"> |
| 38 | + <RadioGroup.Root value={currentTheme} onValueChange={handleThemeChange}> |
| 39 | + {#each themes as theme} |
| 40 | + <div class="flex items-center space-x-2"> |
| 41 | + <RadioGroup.Item value={theme} id={theme} /> |
| 42 | + <Label for={theme} class="flex items-center gap-2"> |
| 43 | + {#if theme.includes('light')} |
| 44 | + <Sun /> |
| 45 | + {:else} |
| 46 | + <Moon /> |
| 47 | + {/if} |
| 48 | + {theme} |
| 49 | + </Label> |
| 50 | + </div> |
| 51 | + {/each} |
| 52 | + </RadioGroup.Root> |
| 53 | + </ScrollArea> |
| 54 | + </Card.Content> |
| 55 | +</Card.Root> |
0 commit comments