mirror of
https://github.com/twbs/bootstrap.git
synced 2025-03-15 15:29:22 +01:00
Add a contextual color mode switcher in the documentation
This commit is contained in:
parent
323f9d6f68
commit
7d52cf793f
@ -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();
|
||||||
|
@ -31,6 +31,39 @@
|
|||||||
<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="position-relative">
|
||||||
|
<button class="btn btn-link nav-link py-2 px-0 px-lg-2 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>
|
||||||
|
@ -29,19 +29,17 @@
|
|||||||
|
|
||||||
setTheme(getPreferredTheme())
|
setTheme(getPreferredTheme())
|
||||||
|
|
||||||
const showActiveTheme = (theme, focus = false) => {
|
const showActiveTheme = (theme, focus = false, themeSwitcher = document.querySelector('#bd-theme')) => {
|
||||||
const themeSwitcher = document.querySelector('#bd-theme')
|
|
||||||
|
|
||||||
if (!themeSwitcher) {
|
if (!themeSwitcher) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
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.parentElement.querySelector('.theme-icon-active use')
|
||||||
const btnToActive = document.querySelector(`[data-bs-theme-value="${theme}"]`)
|
const btnToActive = themeSwitcher.parentElement.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.parentElement.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,7 +47,7 @@
|
|||||||
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)
|
themeSwitcher.setAttribute('aria-label', themeSwitcherLabel)
|
||||||
|
|
||||||
if (focus) {
|
if (focus) {
|
||||||
@ -71,9 +69,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('.highlight-toolbar')) {
|
||||||
showActiveTheme(theme, true)
|
toggle.closest('.highlight-toolbar').previousElementSibling.setAttribute('data-bs-theme', theme)
|
||||||
|
showActiveTheme(theme, true, toggle.closest('.dropdown-menu').previousElementSibling)
|
||||||
|
} else {
|
||||||
|
setStoredTheme(theme)
|
||||||
|
setTheme(theme)
|
||||||
|
showActiveTheme(theme, true)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user