Skip to content

Commit 9bb14ad

Browse files
authored
Fix theme toggle button opacity (#302)
* fix theme toggle button opacity * optimise * fix/simplify * fix/simplify * fix/simplify
1 parent 23d99fa commit 9bb14ad

File tree

4 files changed

+21
-71
lines changed

4 files changed

+21
-71
lines changed
Lines changed: 1 addition & 1 deletion
Loading
Lines changed: 1 addition & 1 deletion
Loading
Lines changed: 18 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,45 @@
11
<script lang="ts">
2+
import { on } from 'svelte/events';
23
import { theme } from '../stores';
3-
import { onDestroy } from 'svelte';
4-
5-
let { label = 'Dark mode' }: { label?: string } = $props();
64
75
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';
208
21-
$theme.current = upcoming_theme;
9+
$theme.preference = next === system ? 'system' : next;
10+
$theme.current = next;
2211
}
2312
24-
const cb = (e: MediaQueryListEvent) =>
25-
theme.set({ preference: $theme.preference, current: e.matches ? 'dark' : 'light' });
26-
27-
let query: MediaQueryList | undefined;
28-
2913
$effect(() => {
30-
query?.removeEventListener('change', cb);
31-
3214
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+
});
3520
}
3621
});
37-
38-
onDestroy(() => query?.removeEventListener('change', cb));
3922
</script>
4023

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>
5131

5232
<style>
5333
button {
5434
width: 3.2rem;
5535
aspect-ratio: 1;
5636
background: red;
5737
background: url($lib/icons/theme-light.svg) no-repeat 50% 50% / 2.3rem 2.3rem;
58-
opacity: 0.6;
38+
margin-left: 1rem;
5939
6040
:global(.dark) & {
6141
background-image: url($lib/icons/theme-dark.svg);
6242
opacity: 0.8;
6343
}
6444
}
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-
}
9245
</style>

packages/site-kit/src/lib/nav/Nav.svelte

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,7 @@ Top navigation bar for the application. It provides a slot for the left side, th
100100

101101
{@render external_links?.()}
102102

103-
<div class="appearance">
104-
<span class="caption">Theme</span>
105-
<ThemeToggle />
106-
</div>
103+
<ThemeToggle />
107104
</div>
108105
</div>
109106

0 commit comments

Comments
 (0)