|
33 | 33 | } |
34 | 34 | }) |
35 | 35 |
|
36 | | - const ColorScheme = new (class { |
37 | | - constructor() { |
38 | | - window |
39 | | - .matchMedia('(prefers-color-scheme: dark)') |
40 | | - .addEventListener('change', () => { |
41 | | - this.updateCurrent(localStorage.getItem('color-scheme') ?? 'auto') |
42 | | - }) |
43 | | - } |
44 | | - get() { |
45 | | - const stored = localStorage.getItem('color-scheme') ?? 'auto' |
46 | | - this.updateCurrent(stored) |
47 | | - return stored |
48 | | - } |
49 | | - set(value) { |
50 | | - bodyEl.setAttribute('data-color-scheme', value) |
51 | | - try { |
52 | | - localStorage.setItem('color-scheme', value) |
53 | | - } catch (err) { |
54 | | - console.error(err) |
55 | | - } |
56 | | - this.updateCurrent(value) |
57 | | - return value |
58 | | - } |
59 | | - updateCurrent(value) { |
60 | | - var current = 'light' |
61 | | - if (value == 'auto') { |
62 | | - if (window.matchMedia('(prefers-color-scheme: dark)').matches) { |
63 | | - current = 'dark' |
64 | | - } |
65 | | - } else { |
66 | | - current = value |
67 | | - } |
68 | | - document.body.setAttribute('data-current-color-scheme', current) |
69 | | - } |
70 | | - })() |
71 | | - |
72 | 36 | if (document.getElementById('theme-color-scheme-toggle')) { |
73 | | - var bodyEl = document.body |
74 | 37 | var themeColorSchemeToggleEl = document.getElementById( |
75 | 38 | 'theme-color-scheme-toggle', |
76 | 39 | ) |
77 | 40 | var options = themeColorSchemeToggleEl.getElementsByTagName('input') |
78 | 41 |
|
79 | | - if (ColorScheme.get()) { |
80 | | - bodyEl.setAttribute('data-color-scheme', ColorScheme.get()) |
81 | | - } |
82 | | - |
83 | 42 | for (const option of options) { |
84 | | - if (option.value == bodyEl.getAttribute('data-color-scheme')) { |
| 43 | + if (option.value == document.body.dataset.colorScheme) { |
85 | 44 | option.checked = true |
86 | 45 | } |
87 | 46 | option.addEventListener('change', ev => { |
88 | 47 | var value = ev.target.value |
89 | | - ColorScheme.set(value) |
| 48 | + ThemeCupertino.ColorScheme.set(value) |
90 | 49 | for (const o of options) { |
91 | 50 | if (o.value != value) { |
92 | 51 | o.checked = false |
|
0 commit comments