|
1 | 1 | /*! |
2 | | -* Color mode toggler for Bootstrap's docs (https://getbootstrap.com/) |
3 | | -* Copyright 2011-2023 The Bootstrap Authors |
4 | | -* Licensed under the Creative Commons Attribution 3.0 Unported License. |
5 | | -* https://getbootstrap.com/docs/5.3/customize/color-modes/#javascript |
6 | | -*/ |
| 2 | + * Color mode toggler for Bootstrap's docs (https://getbootstrap.com/) |
| 3 | + * Copyright 2011-2025 The Bootstrap Authors |
| 4 | + * Licensed under the Creative Commons Attribution 3.0 Unported License. |
| 5 | + */ |
7 | 6 |
|
8 | 7 | (() => { |
9 | | -'use strict' |
| 8 | + 'use strict' |
10 | 9 |
|
11 | 10 | const getStoredTheme = () => localStorage.getItem('theme') |
12 | 11 | const setStoredTheme = theme => localStorage.setItem('theme', theme) |
13 | 12 |
|
14 | | -const getPreferredTheme = () => { |
15 | | - const storedTheme = getStoredTheme() |
16 | | - if (storedTheme) { |
17 | | - return storedTheme |
18 | | - } |
| 13 | + const getPreferredTheme = () => { |
| 14 | + const storedTheme = getStoredTheme() |
| 15 | + if (storedTheme) { |
| 16 | + return storedTheme |
| 17 | + } |
19 | 18 |
|
20 | | - return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light' |
21 | | -} |
| 19 | + return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light' |
| 20 | + } |
22 | 21 |
|
23 | | -const setTheme = theme => { |
24 | | - if (theme === 'auto' && window.matchMedia('(prefers-color-scheme: dark)').matches) { |
25 | | - document.documentElement.setAttribute('data-bs-theme', 'dark') |
26 | | - } else { |
27 | | - document.documentElement.setAttribute('data-bs-theme', theme) |
| 22 | + const setTheme = theme => { |
| 23 | + if (theme === 'auto') { |
| 24 | + document.documentElement.setAttribute('data-bs-theme', (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light')) |
| 25 | + } else { |
| 26 | + document.documentElement.setAttribute('data-bs-theme', theme) |
| 27 | + } |
28 | 28 | } |
29 | | -} |
30 | 29 |
|
31 | | -setTheme(getPreferredTheme()) |
| 30 | + setTheme(getPreferredTheme()) |
32 | 31 |
|
33 | | -const showActiveTheme = (theme, focus = false) => { |
34 | | - const themeSwitcher = document.querySelector('#bd-theme') |
| 32 | + const showActiveTheme = (theme, focus = false) => { |
| 33 | + const themeSwitcher = document.querySelector('#bd-theme') |
35 | 34 |
|
36 | | - if (!themeSwitcher) { |
37 | | - return |
38 | | - } |
| 35 | + if (!themeSwitcher) { |
| 36 | + return |
| 37 | + } |
39 | 38 |
|
40 | | - const themeSwitcherText = document.querySelector('#bd-theme-text') |
41 | | - const activeThemeIcon = document.querySelector('.theme-icon-active use') |
42 | | - const btnToActive = document.querySelector(`[data-bs-theme-value="${theme}"]`) |
43 | | - const svgOfActiveBtn = btnToActive.querySelector('svg use').getAttribute('href') |
| 39 | + const themeSwitcherText = document.querySelector('#bd-theme-text') |
| 40 | + const activeThemeIcon = document.querySelector('.theme-icon-active use') |
| 41 | + const btnToActive = document.querySelector(`[data-bs-theme-value="${theme}"]`) |
| 42 | + const svgOfActiveBtn = btnToActive.querySelector('svg use').getAttribute('href') |
44 | 43 |
|
45 | | - document.querySelectorAll('[data-bs-theme-value]').forEach(element => { |
46 | | - element.classList.remove('active') |
47 | | - element.setAttribute('aria-pressed', 'false') |
48 | | - }) |
| 44 | + document.querySelectorAll('[data-bs-theme-value]').forEach(element => { |
| 45 | + element.classList.remove('active') |
| 46 | + element.setAttribute('aria-pressed', 'false') |
| 47 | + }) |
49 | 48 |
|
50 | | - btnToActive.classList.add('active') |
51 | | - btnToActive.setAttribute('aria-pressed', 'true') |
52 | | - activeThemeIcon.setAttribute('href', svgOfActiveBtn) |
53 | | - const themeSwitcherLabel = `${themeSwitcherText.textContent} (${btnToActive.dataset.bsThemeValue})` |
54 | | - themeSwitcher.setAttribute('aria-label', themeSwitcherLabel) |
| 49 | + btnToActive.classList.add('active') |
| 50 | + btnToActive.setAttribute('aria-pressed', 'true') |
| 51 | + activeThemeIcon.setAttribute('href', svgOfActiveBtn) |
| 52 | + const themeSwitcherLabel = `${themeSwitcherText.textContent} (${btnToActive.dataset.bsThemeValue})` |
| 53 | + themeSwitcher.setAttribute('aria-label', themeSwitcherLabel) |
55 | 54 |
|
56 | | - if (focus) { |
57 | | - themeSwitcher.focus() |
| 55 | + if (focus) { |
| 56 | + themeSwitcher.focus() |
| 57 | + } |
58 | 58 | } |
59 | | -} |
60 | 59 |
|
61 | | -window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => { |
62 | | - const storedTheme = getStoredTheme() |
63 | | - if (storedTheme !== 'light' && storedTheme !== 'dark') { |
64 | | - setTheme(getPreferredTheme()) |
65 | | - } |
66 | | -}) |
| 60 | + window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => { |
| 61 | + const storedTheme = getStoredTheme() |
| 62 | + if (storedTheme !== 'light' && storedTheme !== 'dark') { |
| 63 | + setTheme(getPreferredTheme()) |
| 64 | + } |
| 65 | + }) |
67 | 66 |
|
68 | | -window.addEventListener('DOMContentLoaded', () => { |
69 | | - showActiveTheme(getPreferredTheme()) |
| 67 | + window.addEventListener('DOMContentLoaded', () => { |
| 68 | + showActiveTheme(getPreferredTheme()) |
70 | 69 |
|
71 | | - document.querySelectorAll('[data-bs-theme-value]') |
72 | | - .forEach(toggle => { |
73 | | - toggle.addEventListener('click', () => { |
74 | | - const theme = toggle.getAttribute('data-bs-theme-value') |
75 | | - setStoredTheme(theme) |
76 | | - setTheme(theme) |
77 | | - showActiveTheme(theme, true) |
| 70 | + document.querySelectorAll('[data-bs-theme-value]') |
| 71 | + .forEach(toggle => { |
| 72 | + toggle.addEventListener('click', () => { |
| 73 | + const theme = toggle.getAttribute('data-bs-theme-value') |
| 74 | + setStoredTheme(theme) |
| 75 | + setTheme(theme) |
| 76 | + showActiveTheme(theme, true) |
| 77 | + }) |
78 | 78 | }) |
79 | | - }) |
80 | | -}) |
| 79 | + }) |
81 | 80 | })() |
0 commit comments