Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Add a contextual color mode switcher for each example #39274

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions site/assets/js/code-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// Insert copy to clipboard button before .highlight
const btnTitle = 'Copy to clipboard'
const btnEdit = 'Edit on StackBlitz'
// const btnTheme = 'Toggle local theme'

const btnHtml = [
'<div class="bd-code-snippet">',
Expand Down Expand Up @@ -51,6 +52,7 @@

snippetButtonTooltip('.btn-clipboard', btnTitle)
snippetButtonTooltip('.btn-edit', btnEdit)
// snippetButtonTooltip('.btn-theme', btnTheme)
louismaximepiton marked this conversation as resolved.
Show resolved Hide resolved

const clipboard = new ClipboardJS('.btn-clipboard', {
target: trigger => trigger.closest('.bd-code-snippet').querySelector('.highlight'),
Expand Down
3 changes: 2 additions & 1 deletion site/assets/scss/_clipboard-js.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
}

.btn-clipboard,
.btn-edit {
.btn-edit,
.btn-theme > .dropdown-toggle {
display: block;
padding: .5em;
line-height: 1;
Expand Down
2 changes: 2 additions & 0 deletions site/assets/scss/_component-examples.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
position: relative;
padding: var(--bd-example-padding);
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-width: 1px 0;
@include clearfix();
Expand Down
28 changes: 28 additions & 0 deletions site/layouts/shortcodes/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<small class="font-monospace text-body-secondary text-uppercase">{{- $lang -}}</small>
<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">
<svg class="bi" aria-hidden="true"><use xlink:href="#lightning-charge-fill"/></svg>
</button>
Expand Down
36 changes: 21 additions & 15 deletions site/static/docs/5.3/assets/js/color-modes.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,41 @@
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
}

const setTheme = theme => {
const setTheme = (theme, element = document.documentElement) => {
if (theme === 'auto' && window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.documentElement.setAttribute('data-bs-theme', 'dark')
element.setAttribute('data-bs-theme', 'dark')
} else {
document.documentElement.setAttribute('data-bs-theme', theme)
element.setAttribute('data-bs-theme', theme)
}
}

setTheme(getPreferredTheme())

const showActiveTheme = (theme, focus = false) => {
const themeSwitcher = document.querySelector('#bd-theme')

const showActiveTheme = (theme, focus = false, selectedToggler = document.querySelector('#bd-theme')) => {
const themeSwitcher = selectedToggler.closest('.dropdown')
if (!themeSwitcher) {
return
}

const themeSwitcherButton = themeSwitcher.querySelector('.dropdown-toggle')
const themeSwitcherText = document.querySelector('#bd-theme-text')
const activeThemeIcon = document.querySelector('.theme-icon-active use')
const btnToActive = document.querySelector(`[data-bs-theme-value="${theme}"]`)
const activeThemeIcon = themeSwitcher.querySelector('.theme-icon-active use')
const btnToActive = themeSwitcher.querySelector(`[data-bs-theme-value="${theme}"]`)
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.setAttribute('aria-pressed', 'false')
})

btnToActive.classList.add('active')
btnToActive.setAttribute('aria-pressed', 'true')
activeThemeIcon.setAttribute('href', svgOfActiveBtn)
const themeSwitcherLabel = `${themeSwitcherText.textContent} (${btnToActive.dataset.bsThemeValue})`
themeSwitcher.setAttribute('aria-label', themeSwitcherLabel)
const themeSwitcherLabel = `${themeSwitcherText.textContent} (${btnToActive.dataset.bsThemeValue})${btnToActive.closest('.highlight-toolbar') ? ' (local)' : ''}`
themeSwitcherButton.setAttribute('aria-label', themeSwitcherLabel)

if (focus) {
themeSwitcher.focus()
themeSwitcherButton.focus()
}
}

Expand All @@ -71,9 +71,15 @@
.forEach(toggle => {
toggle.addEventListener('click', () => {
const theme = toggle.getAttribute('data-bs-theme-value')
setStoredTheme(theme)
setTheme(theme)
showActiveTheme(theme, true)

if (toggle.closest('.bd-code-snippet')) {
setTheme(theme, toggle.closest('.bd-code-snippet').firstChild)
showActiveTheme(theme, true, toggle.closest('.dropdown-menu').previousElementSibling)
} else {
setStoredTheme(theme)
setTheme(theme)
showActiveTheme(theme, true)
}
})
})
})
Expand Down