|
1 | 1 | <script lang="ts">
|
| 2 | + import { on } from 'svelte/events'; |
2 | 3 | import { theme } from '../stores';
|
3 |
| - import { onDestroy } from 'svelte'; |
4 |
| -
|
5 |
| - let { label = 'Dark mode' }: { label?: string } = $props(); |
6 | 4 |
|
7 | 5 | function toggle() {
|
8 |
| - const upcoming_theme = $theme.current === 'light' ? 'dark' : 'light'; |
9 |
| -
|
10 |
| - if ( |
11 |
| - upcoming_theme === |
12 |
| - (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') |
13 |
| - ) { |
14 |
| - // Switch the preference to `system` |
15 |
| - $theme.preference = 'system'; |
16 |
| - } else { |
17 |
| - // Switch the preference to `light` or `dark` |
18 |
| - $theme.preference = upcoming_theme; |
19 |
| - } |
| 6 | + const next = $theme.current === 'light' ? 'dark' : 'light'; |
| 7 | + const system = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; |
20 | 8 |
|
21 |
| - $theme.current = upcoming_theme; |
| 9 | + $theme.preference = next === system ? 'system' : next; |
| 10 | + $theme.current = next; |
22 | 11 | }
|
23 | 12 |
|
24 |
| - const cb = (e: MediaQueryListEvent) => |
25 |
| - theme.set({ preference: $theme.preference, current: e.matches ? 'dark' : 'light' }); |
26 |
| -
|
27 |
| - let query: MediaQueryList | undefined; |
28 |
| -
|
29 | 13 | $effect(() => {
|
30 |
| - query?.removeEventListener('change', cb); |
31 |
| -
|
32 | 14 | if ($theme.preference === 'system') {
|
33 |
| - query = window.matchMedia('(prefers-color-scheme: dark)'); |
34 |
| - query.addEventListener('change', cb); |
| 15 | + const query = window.matchMedia('(prefers-color-scheme: dark)'); |
| 16 | +
|
| 17 | + return on(query, 'change', (e) => { |
| 18 | + $theme.current = e.matches ? 'dark' : 'light'; |
| 19 | + }); |
35 | 20 | }
|
36 | 21 | });
|
37 |
| -
|
38 |
| - onDestroy(() => query?.removeEventListener('change', cb)); |
39 | 22 | </script>
|
40 | 23 |
|
41 |
| -<div class="appearance"> |
42 |
| - <span class="caption">Theme</span> |
43 |
| - <button |
44 |
| - onclick={toggle} |
45 |
| - class="raised" |
46 |
| - type="button" |
47 |
| - aria-pressed={$theme.current === 'dark' ? 'true' : 'false'} |
48 |
| - aria-label={label} |
49 |
| - ></button> |
50 |
| -</div> |
| 24 | +<button |
| 25 | + onclick={toggle} |
| 26 | + class="raised" |
| 27 | + type="button" |
| 28 | + aria-pressed={$theme.current === 'dark'} |
| 29 | + aria-label="Toggle dark mode" |
| 30 | +></button> |
51 | 31 |
|
52 | 32 | <style>
|
53 | 33 | button {
|
54 | 34 | width: 3.2rem;
|
55 | 35 | aspect-ratio: 1;
|
56 | 36 | background: red;
|
57 | 37 | background: url($lib/icons/theme-light.svg) no-repeat 50% 50% / 2.3rem 2.3rem;
|
58 |
| - opacity: 0.6; |
| 38 | + margin-left: 1rem; |
59 | 39 |
|
60 | 40 | :global(.dark) & {
|
61 | 41 | background-image: url($lib/icons/theme-dark.svg);
|
62 | 42 | opacity: 0.8;
|
63 | 43 | }
|
64 | 44 | }
|
65 |
| -
|
66 |
| - .appearance { |
67 |
| - display: flex; |
68 |
| - align-items: center; |
69 |
| -
|
70 |
| - .caption { |
71 |
| - display: none; |
72 |
| - font-size: var(--sk-text-xs); |
73 |
| - line-height: 1; |
74 |
| - margin-right: 0rem; |
75 |
| - } |
76 |
| - } |
77 |
| -
|
78 |
| - @media (max-width: 799px) { |
79 |
| - .appearance { |
80 |
| - position: relative; |
81 |
| - display: flex; |
82 |
| - padding: 1.5rem 0; |
83 |
| - justify-content: space-between; |
84 |
| - } |
85 |
| -
|
86 |
| - .appearance .caption { |
87 |
| - display: block; |
88 |
| -
|
89 |
| - font-size: var(--sk-text-s); |
90 |
| - } |
91 |
| - } |
92 | 45 | </style>
|
0 commit comments