|
50 | 50 | <meta name="msapplication-config" content="https://assets.phalcon.io/phalcon/favicons/browserconfig.xml" />
|
51 | 51 | <meta name="msapplication-TileColor" content="#273646">
|
52 | 52 | <meta name="theme-color" content="#273646">
|
53 |
| - <script>document.documentElement.classList.add(localStorage.getItem('mode') === 'auto' ? (matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') : localStorage.getItem('mode'))</script> |
| 53 | + |
| 54 | + <script> |
| 55 | + (() => { |
| 56 | + 'use strict' |
| 57 | + |
| 58 | + const getStoredTheme = () => localStorage.getItem('theme') |
| 59 | + const setStoredTheme = theme => localStorage.setItem('theme', theme) |
| 60 | + |
| 61 | + const getPreferredTheme = () => { |
| 62 | + const storedTheme = getStoredTheme() |
| 63 | + if (storedTheme) { |
| 64 | + return storedTheme |
| 65 | + } |
| 66 | + |
| 67 | + return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light' |
| 68 | + } |
| 69 | + |
| 70 | + const setTheme = theme => { |
| 71 | + if (theme === 'auto') { |
| 72 | + document.documentElement.setAttribute('data-theme', (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light')) |
| 73 | + } else { |
| 74 | + document.documentElement.setAttribute('data-theme', theme) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + setTheme(getPreferredTheme()) |
| 79 | + |
| 80 | + const showActiveTheme = (theme, focus = false) => { |
| 81 | + const themeSwitcher = document.querySelector('#bd-theme') |
| 82 | + |
| 83 | + if (!themeSwitcher) { |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + const themeSwitcherText = document.querySelector('#bd-theme-text') |
| 88 | + const activeThemeIcon = document.querySelector('.theme-icon-active use') |
| 89 | + const btnToActive = document.querySelector(`[data-theme-value="${theme}"]`) |
| 90 | + const svgOfActiveBtn = btnToActive.querySelector('svg use').getAttribute('href') |
| 91 | + |
| 92 | + document.querySelectorAll('[data-theme-value]').forEach(element => { |
| 93 | + element.classList.remove('active') |
| 94 | + element.setAttribute('aria-pressed', 'false') |
| 95 | + }) |
| 96 | + |
| 97 | + btnToActive.classList.add('active') |
| 98 | + btnToActive.setAttribute('aria-pressed', 'true') |
| 99 | + activeThemeIcon.setAttribute('href', svgOfActiveBtn) |
| 100 | + const themeSwitcherLabel = `${themeSwitcherText.textContent} (${btnToActive.dataset.bsThemeValue})` |
| 101 | + themeSwitcher.setAttribute('aria-label', themeSwitcherLabel) |
| 102 | + |
| 103 | + if (focus) { |
| 104 | + themeSwitcher.focus() |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => { |
| 109 | + const storedTheme = getStoredTheme() |
| 110 | + if (storedTheme !== 'light' && storedTheme !== 'dark') { |
| 111 | + setTheme(getPreferredTheme()) |
| 112 | + } |
| 113 | + }) |
| 114 | + |
| 115 | + window.addEventListener('DOMContentLoaded', () => { |
| 116 | + showActiveTheme(getPreferredTheme()) |
| 117 | + |
| 118 | + document.querySelectorAll('[data-theme-value]') |
| 119 | + .forEach(toggle => { |
| 120 | + toggle.addEventListener('click', () => { |
| 121 | + const theme = toggle.getAttribute('data-theme-value') |
| 122 | + setStoredTheme(theme) |
| 123 | + setTheme(theme) |
| 124 | + showActiveTheme(theme, true) |
| 125 | + }) |
| 126 | + }) |
| 127 | + }) |
| 128 | + })() |
| 129 | + </script> |
54 | 130 |
|
55 | 131 | <link rel="stylesheet"
|
56 | 132 | href="https://assets.phalcon.io/phalcon/test/dark.css">
|
|
0 commit comments