This commit is contained in:
Louis-Maxime Piton 2025-04-15 06:18:47 +00:00 committed by GitHub
commit 7bbcf1b323
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 53 additions and 16 deletions

View File

@ -18,7 +18,8 @@
} }
.btn-clipboard, .btn-clipboard,
.btn-edit { .btn-edit,
.btn-theme > .dropdown-toggle {
display: block; display: block;
padding: .5em; padding: .5em;
line-height: 1; line-height: 1;

View File

@ -21,6 +21,8 @@
position: relative; position: relative;
padding: var(--bd-example-padding); padding: var(--bd-example-padding);
margin: 0 ($bd-gutter-x * -.5) 1rem; margin: 0 ($bd-gutter-x * -.5) 1rem;
color: var(--bs-body-color);
background-color: var(--bs-body-bg);
border: solid var(--bs-border-color); border: solid var(--bs-border-color);
border-width: 1px 0; border-width: 1px 0;
@include clearfix(); @include clearfix();

View File

@ -31,6 +31,34 @@
<div class="d-flex align-items-center highlight-toolbar ps-3 pe-2 py-1 border-0 border-top border-bottom"> <div class="d-flex align-items-center highlight-toolbar ps-3 pe-2 py-1 border-0 border-top border-bottom">
<small class="font-monospace text-body-secondary text-uppercase">{{ $lang }}</small> <small class="font-monospace text-body-secondary text-uppercase">{{ $lang }}</small>
<div class="d-flex ms-auto"> <div class="d-flex ms-auto">
<div class="btn-theme dropdown" title="Toggle local theme">
<button class="dropdown-toggle d-flex align-items-center" type="button" aria-expanded="false" data-bs-toggle="dropdown" data-bs-display="static" aria-label="Toggle theme (auto)">
<svg class="bi my-1 theme-icon-active"><use href="#circle-half"></use></svg>
</button>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="bd-theme-text">
<li>
<button type="button" class="dropdown-item d-flex align-items-center" data-bs-theme-value="light" aria-pressed="false">
<svg class="bi me-2 opacity-50 theme-icon"><use href="#sun-fill"></use></svg>
Light
<svg class="bi ms-auto d-none"><use href="#check2"></use></svg>
</button>
</li>
<li>
<button type="button" class="dropdown-item d-flex align-items-center" data-bs-theme-value="dark" aria-pressed="false">
<svg class="bi me-2 opacity-50 theme-icon"><use href="#moon-stars-fill"></use></svg>
Dark
<svg class="bi ms-auto d-none"><use href="#check2"></use></svg>
</button>
</li>
<li>
<button type="button" class="dropdown-item d-flex align-items-center active" data-bs-theme-value="body" aria-pressed="true">
<svg class="bi me-2 opacity-50 theme-icon"><use href="#circle-half"></use></svg>
Body
<svg class="bi ms-auto d-none"><use href="#check2"></use></svg>
</button>
</li>
</ul>
</div>
<button type="button" class="btn-edit text-nowrap"{{ with $stackblitz_add_js }} data-sb-js-snippet="{{ $stackblitz_add_js }}"{{ end }} title="Try it on StackBlitz"> <button type="button" class="btn-edit text-nowrap"{{ with $stackblitz_add_js }} data-sb-js-snippet="{{ $stackblitz_add_js }}"{{ end }} title="Try it on StackBlitz">
<svg class="bi" aria-hidden="true"><use xlink:href="#lightning-charge-fill"/></svg> <svg class="bi" aria-hidden="true"><use xlink:href="#lightning-charge-fill"/></svg>
</button> </button>

View File

@ -19,29 +19,29 @@
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light' return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
} }
const setTheme = theme => { const setTheme = (theme, element = document.documentElement) => {
if (theme === 'auto') { if (theme === 'auto') {
document.documentElement.setAttribute('data-bs-theme', (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light')) element.setAttribute('data-bs-theme', (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'))
} else { } else {
document.documentElement.setAttribute('data-bs-theme', theme) element.setAttribute('data-bs-theme', theme)
} }
} }
setTheme(getPreferredTheme()) setTheme(getPreferredTheme())
const showActiveTheme = (theme, focus = false) => { const showActiveTheme = (theme, focus = false, selectedToggler = document.querySelector('#bd-theme')) => {
const themeSwitcher = document.querySelector('#bd-theme') const themeSwitcher = selectedToggler.closest('.dropdown')
if (!themeSwitcher) { if (!themeSwitcher) {
return return
} }
const themeSwitcherButton = themeSwitcher.querySelector('.dropdown-toggle')
const themeSwitcherText = document.querySelector('#bd-theme-text') const themeSwitcherText = document.querySelector('#bd-theme-text')
const activeThemeIcon = document.querySelector('.theme-icon-active use') const activeThemeIcon = themeSwitcher.querySelector('.theme-icon-active use')
const btnToActive = document.querySelector(`[data-bs-theme-value="${theme}"]`) const btnToActive = themeSwitcher.querySelector(`[data-bs-theme-value="${theme}"]`)
const svgOfActiveBtn = btnToActive.querySelector('svg use').getAttribute('href') const svgOfActiveBtn = btnToActive.querySelector('svg use').getAttribute('href')
document.querySelectorAll('[data-bs-theme-value]').forEach(element => { themeSwitcher.querySelectorAll('[data-bs-theme-value]').forEach(element => {
element.classList.remove('active') element.classList.remove('active')
element.setAttribute('aria-pressed', 'false') element.setAttribute('aria-pressed', 'false')
}) })
@ -49,11 +49,11 @@
btnToActive.classList.add('active') btnToActive.classList.add('active')
btnToActive.setAttribute('aria-pressed', 'true') btnToActive.setAttribute('aria-pressed', 'true')
activeThemeIcon.setAttribute('href', svgOfActiveBtn) activeThemeIcon.setAttribute('href', svgOfActiveBtn)
const themeSwitcherLabel = `${themeSwitcherText.textContent} (${btnToActive.dataset.bsThemeValue})` const themeSwitcherLabel = `${themeSwitcherText.textContent} (${btnToActive.dataset.bsThemeValue})${btnToActive.closest('.highlight-toolbar') ? ' (local)' : ''}`
themeSwitcher.setAttribute('aria-label', themeSwitcherLabel) themeSwitcherButton.setAttribute('aria-label', themeSwitcherLabel)
if (focus) { if (focus) {
themeSwitcher.focus() themeSwitcherButton.focus()
} }
} }
@ -71,9 +71,15 @@
.forEach(toggle => { .forEach(toggle => {
toggle.addEventListener('click', () => { toggle.addEventListener('click', () => {
const theme = toggle.getAttribute('data-bs-theme-value') const theme = toggle.getAttribute('data-bs-theme-value')
setStoredTheme(theme)
setTheme(theme) if (toggle.closest('.bd-code-snippet')) {
showActiveTheme(theme, true) setTheme(theme, toggle.closest('.bd-code-snippet').firstElementChild)
showActiveTheme(theme, true, toggle.closest('.dropdown-menu').previousElementSibling)
} else {
setStoredTheme(theme)
setTheme(theme)
showActiveTheme(theme, true)
}
}) })
}) })
}) })