|
1 | | -<div id="feast-theme-switcher" class="feast-theme-switcher" role="group" aria-label="Theme mode switcher"> |
| 1 | +<div id="feast-theme-switcher" class="feast-theme-switcher"> |
2 | 2 | <button |
| 3 | + id="feast-theme-toggle" |
3 | 4 | type="button" |
4 | | - class="feast-theme-switcher__button js-feast-theme-option" |
5 | | - data-theme-option="system" |
6 | | - aria-pressed="false" |
7 | | - aria-label="Use system theme setting" |
8 | | - title="System" |
| 5 | + class="feast-theme-switcher__toggle" |
| 6 | + role="switch" |
| 7 | + aria-checked="false" |
| 8 | + aria-label="Switch to dark theme" |
| 9 | + title="Switch to dark theme" |
9 | 10 | > |
10 | | - Auto |
11 | | - </button> |
12 | | - <button |
13 | | - type="button" |
14 | | - class="feast-theme-switcher__button js-feast-theme-option" |
15 | | - data-theme-option="light" |
16 | | - aria-pressed="false" |
17 | | - aria-label="Use light theme" |
18 | | - title="Light" |
19 | | - > |
20 | | - Light |
21 | | - </button> |
22 | | - <button |
23 | | - type="button" |
24 | | - class="feast-theme-switcher__button js-feast-theme-option" |
25 | | - data-theme-option="dark" |
26 | | - aria-pressed="false" |
27 | | - aria-label="Use dark theme" |
28 | | - title="Dark" |
29 | | - > |
30 | | - Dark |
| 11 | + <span class="feast-theme-switcher__glyph feast-theme-switcher__glyph--sun" aria-hidden="true"> |
| 12 | + <svg viewBox="0 0 24 24" fill="none"> |
| 13 | + <circle cx="12" cy="12" r="4"></circle> |
| 14 | + <path d="M12 2V4"></path> |
| 15 | + <path d="M12 20V22"></path> |
| 16 | + <path d="M4.93 4.93L6.34 6.34"></path> |
| 17 | + <path d="M17.66 17.66L19.07 19.07"></path> |
| 18 | + <path d="M2 12H4"></path> |
| 19 | + <path d="M20 12H22"></path> |
| 20 | + <path d="M4.93 19.07L6.34 17.66"></path> |
| 21 | + <path d="M17.66 6.34L19.07 4.93"></path> |
| 22 | + </svg> |
| 23 | + </span> |
| 24 | + <span class="feast-theme-switcher__glyph feast-theme-switcher__glyph--moon" aria-hidden="true"> |
| 25 | + <svg viewBox="0 0 24 24" fill="none"> |
| 26 | + <path d="M21 12.79A9 9 0 1 1 11.21 3a7 7 0 0 0 9.79 9.79z"></path> |
| 27 | + </svg> |
| 28 | + </span> |
| 29 | + <span class="feast-theme-switcher__thumb" aria-hidden="true"> |
| 30 | + <svg class="feast-theme-switcher__thumb-icon feast-theme-switcher__thumb-icon--sun" viewBox="0 0 24 24" fill="none"> |
| 31 | + <circle cx="12" cy="12" r="4"></circle> |
| 32 | + <path d="M12 2V4"></path> |
| 33 | + <path d="M12 20V22"></path> |
| 34 | + <path d="M4.93 4.93L6.34 6.34"></path> |
| 35 | + <path d="M17.66 17.66L19.07 19.07"></path> |
| 36 | + <path d="M2 12H4"></path> |
| 37 | + <path d="M20 12H22"></path> |
| 38 | + <path d="M4.93 19.07L6.34 17.66"></path> |
| 39 | + <path d="M17.66 6.34L19.07 4.93"></path> |
| 40 | + </svg> |
| 41 | + <svg class="feast-theme-switcher__thumb-icon feast-theme-switcher__thumb-icon--moon" viewBox="0 0 24 24" fill="none"> |
| 42 | + <path d="M21 12.79A9 9 0 1 1 11.21 3a7 7 0 0 0 9.79 9.79z"></path> |
| 43 | + </svg> |
| 44 | + </span> |
31 | 45 | </button> |
32 | 46 | </div> |
33 | 47 |
|
34 | 48 | <script> |
35 | 49 | (function () { |
36 | | - var switcher = document.getElementById("feast-theme-switcher"); |
37 | | - if (!switcher || !window.feastTheme || typeof window.feastTheme.getPreference !== "function") { |
| 50 | + var toggle = document.getElementById("feast-theme-toggle"); |
| 51 | + if ( |
| 52 | + !toggle || |
| 53 | + !window.feastTheme || |
| 54 | + typeof window.feastTheme.getPreference !== "function" || |
| 55 | + typeof window.feastTheme.togglePreference !== "function" |
| 56 | + ) { |
38 | 57 | return; |
39 | 58 | } |
40 | 59 |
|
41 | | - var buttons = switcher.querySelectorAll(".js-feast-theme-option"); |
42 | | - |
43 | | - function setButtonState(button, isActive) { |
44 | | - button.setAttribute("aria-pressed", isActive ? "true" : "false"); |
45 | | - if (button.classList && typeof button.classList.toggle === "function") { |
46 | | - button.classList.toggle("is-active", isActive); |
| 60 | + function syncToggleState() { |
| 61 | + var isDark = window.feastTheme.getPreference() === "dark"; |
| 62 | + toggle.setAttribute("aria-checked", isDark ? "true" : "false"); |
| 63 | + if (isDark) { |
| 64 | + toggle.setAttribute("aria-label", "Switch to light theme"); |
| 65 | + toggle.setAttribute("title", "Switch to light theme"); |
| 66 | + } else { |
| 67 | + toggle.setAttribute("aria-label", "Switch to dark theme"); |
| 68 | + toggle.setAttribute("title", "Switch to dark theme"); |
47 | 69 | } |
48 | | - } |
49 | | - |
50 | | - function syncSwitcherState() { |
51 | | - var activePreference = window.feastTheme.getPreference(); |
52 | | - for (var index = 0; index < buttons.length; index += 1) { |
53 | | - var button = buttons[index]; |
54 | | - var themeOption = button.getAttribute("data-theme-option"); |
55 | | - setButtonState(button, themeOption === activePreference); |
| 70 | + if (toggle.classList && typeof toggle.classList.toggle === "function") { |
| 71 | + toggle.classList.toggle("is-dark", isDark); |
56 | 72 | } |
57 | 73 | } |
58 | 74 |
|
59 | | - function bindClick(button) { |
| 75 | + function bindToggleClick() { |
60 | 76 | var handler = function () { |
61 | | - var preference = button.getAttribute("data-theme-option"); |
62 | | - window.feastTheme.setPreference(preference); |
63 | | - syncSwitcherState(); |
| 77 | + window.feastTheme.togglePreference(); |
| 78 | + syncToggleState(); |
64 | 79 | }; |
65 | | - |
66 | 80 | if (window.jtd && typeof window.jtd.addEvent === "function") { |
67 | | - window.jtd.addEvent(button, "click", handler); |
68 | | - } else if (button.addEventListener) { |
69 | | - button.addEventListener("click", handler); |
| 81 | + window.jtd.addEvent(toggle, "click", handler); |
| 82 | + } else if (toggle.addEventListener) { |
| 83 | + toggle.addEventListener("click", handler); |
70 | 84 | } |
71 | 85 | } |
72 | 86 |
|
73 | | - for (var index = 0; index < buttons.length; index += 1) { |
74 | | - bindClick(buttons[index]); |
75 | | - } |
76 | | - |
77 | | - window.feastTheme.onPreferenceApplied = syncSwitcherState; |
78 | | - syncSwitcherState(); |
| 87 | + bindToggleClick(); |
| 88 | + window.feastTheme.onPreferenceApplied = syncToggleState; |
| 89 | + syncToggleState(); |
79 | 90 | })(); |
80 | 91 | </script> |
0 commit comments