-
Notifications
You must be signed in to change notification settings - Fork 1
/
dark-mode-switch.ts
268 lines (225 loc) · 7.44 KB
/
dark-mode-switch.ts
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import { CSSResultGroup, LitElement, css, html } from "lit";
import { property } from "lit/decorators.js";
import { makeValueChangeEmitter } from "../make_value_change_emitter";
import {
CustomElementInputGetValue,
makeInputBinding,
} from "../shiny/make-input-binding";
// Adapted from:
// https://web.dev/building-a-theme-switch-component/
// https://web.dev/patterns/theming/theme-switch/
// https://github.com/argyleink/gui-challenges/tree/main/theme-switch
export class ForgeDarkModeSwitch
extends LitElement
implements CustomElementInputGetValue<string>
{
@property({ type: String }) themeValue: "light" | "dark" = "light";
static styles: CSSResultGroup = [
css`
.sun-and-moon > :is(.moon, .sun, .sun-beams) {
transform-origin: center center;
}
.sun-and-moon > .sun {
fill: none;
stroke: var(--text-1);
stroke-width: var(--stroke-w);
}
button:is(:hover, :focus-visible)
> :is(.sun-and-moon > :is(.moon, .sun)) {
fill: var(--text-2);
}
.sun-and-moon > .sun-beams {
stroke: var(--text-1);
stroke-width: var(--stroke-w);
}
button:is(:hover, :focus-visible) :is(.sun-and-moon > .sun-beams) {
background-color: var(--text-2);
}
[data-theme="dark"] .sun-and-moon > .sun {
fill: var(--text-1);
stroke: none;
stroke-width: 0;
transform: scale(1.6);
}
[data-theme="dark"] .sun-and-moon > .sun-beams {
opacity: 0;
}
[data-theme="dark"] .sun-and-moon > .moon > circle {
transform: translateX(-10px);
}
@supports (cx: 1) {
[data-theme="dark"] .sun-and-moon > .moon > circle {
transform: translateX(0);
cx: 15;
}
}
`,
// Transitions
css`
.sun-and-moon > .sun {
transition: transform var(--speed-fast) var(--ease-in-out-2)
var(--speed-fast),
fill var(--speed-fast) var(--ease-in-out-2) var(--speed-fast),
stroke-width var(--speed-normal) var(--ease-in-out-2);
}
.sun-and-moon > .sun-beams {
transition: transform var(--speed-fast) var(--ease-out-3),
opacity var(--speed-fast) var(--ease-out-4);
transition-delay: var(--speed-normal);
}
.sun-and-moon .moon > circle {
transition: transform var(--speed-fast) var(--ease-in-out-2),
fill var(--speed-fast) var(--ease-in-out-2);
transition-delay: 0s;
}
@supports (cx: 1) {
.sun-and-moon .moon > circle {
transition: cx var(--speed-normal) var(--ease-in-out-2);
}
[data-theme="dark"] .sun-and-moon .moon > circle {
transition: cx var(--speed-fast) var(--ease-in-out-2);
transition-delay: var(--speed-fast);
}
}
[data-theme="dark"] .sun-and-moon > .sun {
transition-delay: 0s;
transition-duration: var(--speed-normal);
transition-timing-function: var(--ease-in-out-2);
}
[data-theme="dark"] .sun-and-moon > .sun-beams {
transform: scale(0.3);
transition: transform var(--speed-normal) var(--ease-in-out-2),
opacity var(--speed-fast) var(--ease-out-1);
transition-delay: 0s;
}
`,
css`
:host {
display: inline-block;
/* We control the stroke size manually here. We don't want it getting so
small its not visible but also not so big it looks cartoonish */
--stroke-w: clamp(1px, 0.1em, 6px);
}
button {
/* This is needed to let the svg use the em sizes */
font-size: inherit;
/* Make sure the button is fully centered */
display: grid;
place-content: center;
/* A little bit of padding to make it easier to press */
padding: var(--size-xxs);
background: none;
border: none;
aspect-ratio: 1;
border-radius: 50%;
cursor: pointer;
touch-action: manipulation;
-webkit-tap-highlight-color: transparent;
outline-offset: var(--size-xxs);
/* Move down to adjust for being large than 1em */
/* Size of the icon, uses em units so it scales to font-size */
--size: 1.3em;
/* Because we are (most likely) bigger than one em we will need to move
the button up or down to keep it looking right inline */
--vertical_correction: calc((var(--size) - 1em) / 2);
transform: translateY(var(--vertical_correction));
margin-block-end: var(--vertical_correction);
}
/*
button:is(:hover, :focus-visible) {
background: var(--surface-4);
}
*/
button > svg {
height: var(--size);
width: var(--size);
stroke-linecap: round;
overflow: visible;
}
svg line,
svg circle {
vector-effect: non-scaling-stroke;
}
`,
];
onChangeCallback: (x: boolean) => void = (x: boolean) => {};
onValueChange = makeValueChangeEmitter(this, this.id);
connectedCallback() {
super.connectedCallback();
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
this.themeValue = "dark";
}
this.setPreference();
// Sync with system changes
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", ({ matches: isDark }) => {
this.themeValue = isDark ? "dark" : "light";
this.setPreference();
});
}
getValue(): string {
return this.themeValue;
}
render() {
return html`
<button
title="Toggles light & dark"
aria-label="auto"
aria-live="polite"
data-theme="${this.themeValue}"
@click="${this.onClick}"
>
<svg class="sun-and-moon" aria-hidden="true" viewBox="0 0 24 24">
<mask class="moon" id="moon-mask">
<rect x="0" y="0" width="100%" height="100%" fill="white" />
<circle cx="25" cy="10" r="6" fill="black" />
</mask>
<circle
class="sun"
cx="12"
cy="12"
r="6"
mask="url(#moon-mask)"
fill="currentColor"
/>
<g class="sun-beams" stroke="currentColor">
<line x1="12" y1="1" x2="12" y2="3" />
<line x1="12" y1="21" x2="12" y2="23" />
<line x1="4.22" y1="4.22" x2="5.64" y2="5.64" />
<line x1="18.36" y1="18.36" x2="19.78" y2="19.78" />
<line x1="1" y1="12" x2="3" y2="12" />
<line x1="21" y1="12" x2="23" y2="12" />
<line x1="4.22" y1="19.78" x2="5.64" y2="18.36" />
<line x1="18.36" y1="5.64" x2="19.78" y2="4.22" />
</g>
</svg>
</button>
`;
}
onClick(e: MouseEvent): void {
e.stopPropagation();
this.themeValue = this.themeValue === "light" ? "dark" : "light";
this.setPreference();
this.onChangeCallback(true);
}
setPreference() {
document.documentElement.dataset["shinytheme"] = this.themeValue;
this.reflectPreference();
}
reflectPreference() {
this.shadowRoot
?.querySelector("button")
?.setAttribute("data-theme", this.themeValue);
this.shadowRoot
?.querySelector("button")
?.setAttribute("aria-label", this.themeValue);
}
}
customElements.define("forge-dark-mode-switch", ForgeDarkModeSwitch);
makeInputBinding("forge-dark-mode-switch");
declare global {
interface HTMLElementTagNameMap {
"forge-dark-mode-switch": ForgeDarkModeSwitch;
}
}