-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathThemeToggler.svelte
81 lines (69 loc) · 1.67 KB
/
ThemeToggler.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<script>
// preserve the focus ring for keyboard users because a11y,
// but hide for mouse users because fugly
let nice = false;
let theme = 'light';
try {
theme = localStorage.theme;
} catch (e) {
// ignore — could be SSR, or e.g. Firefox with restrictive permissions
}
const toggle = () => {
const { classList } = document.querySelector('html');
classList.remove(theme);
theme = theme === 'light' ? 'dark' : 'light';
classList.add(theme);
try {
localStorage.theme = theme;
} catch (e) {
// ignore
}
};
</script>
<button
aria-label="Toggle theme"
title="Toggle theme"
class:nice
on:mousedown="{() => nice = true}"
on:blur="{() => nice = false}"
on:click={toggle}
>
toggle theme
<svg viewBox="0 0 24 24">
<path class="light" d="M12,18A6,6 0 0,1 6,12A6,6 0 0,1 12,6A6,6 0 0,1 18,12A6,6 0 0,1 12,18M20,15.31L23.31,12L20,8.69V4H15.31L12,0.69L8.69,4H4V8.69L0.69,12L4,15.31V20H8.69L12,23.31L15.31,20H20V15.31Z" />
<path class="dark" d="M12,18C11.11,18 10.26,17.8 9.5,17.45C11.56,16.5 13,14.42 13,12C13,9.58 11.56,7.5 9.5,6.55C10.26,6.2 11.11,6 12,6A6,6 0 0,1 18,12A6,6 0 0,1 12,18M20,8.69V4H15.31L12,0.69L8.69,4H4V8.69L0.69,12L4,15.31V20H8.69L12,23.31L15.31,20H20V15.31L23.31,12L20,8.69Z" />
</svg>
</button>
<style>
button {
position: fixed;
right: 1em;
bottom: 1em;
width: 2em;
height: 2em;
text-indent: -9999px;
background-color: transparent;
border: none;
opacity: 0.4;
}
.nice {
outline: none;
}
svg {
position: absolute;
width: 100%;
height: 100%;
right: 0;
bottom: 0;
}
path {
fill: var(--fg);
transition: opacity 0.6s;
}
.dark {
opacity: 0;
}
:global(html).dark .dark {
opacity: 1;
}
</style>