Skip to content

Commit d44fe21

Browse files
committed
Revamp how we're doing themes in docs to be more instant
1 parent 86697e4 commit d44fe21

5 files changed

Lines changed: 48 additions & 15 deletions

File tree

site/src/assets/application.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
import sidebarScroll from './partials/sidebar.js'
1111
import snippets from './partials/snippets.js'
1212
import stickyNav from './partials/sticky.js'
13+
import theme from './partials/theme.js'
1314
import tocDrawer from './partials/toc.js'
1415

1516
export default () => {
1617
sidebarScroll()
1718
snippets()
1819
stickyNav()
20+
theme()
1921
tocDrawer()
2022
}

site/src/assets/partials/theme.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// NOTICE: Internal docs helpers — not shipped in Bootstrap; not for reuse.
2+
3+
/*
4+
* JavaScript for Bootstrap's docs (https://getbootstrap.com/)
5+
* Copyright 2011-2026 The Bootstrap Authors
6+
* Licensed under the Creative Commons Attribution 3.0 Unported License.
7+
* For details, see https://creativecommons.org/licenses/by/3.0/.
8+
*/
9+
10+
import { Menu } from '@bootstrap'
11+
12+
export default () => {
13+
const themeSwitcher = document.getElementById('bd-theme')
14+
15+
if (!themeSwitcher) {
16+
return
17+
}
18+
19+
document.addEventListener('click', event => {
20+
const toggle = event.target.closest('[data-bs-theme-value]')
21+
22+
if (!toggle) {
23+
return
24+
}
25+
26+
Menu.getOrCreateInstance(themeSwitcher).hide()
27+
}, true)
28+
}

site/src/libs/highlight.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export async function highlightCode(
5858
light: { ...(bootstrapLight as any), name: '' },
5959
dark: { ...(bootstrapDark as any), name: '' }
6060
},
61+
defaultColor: 'light-dark()',
6162
transformers
6263
})
6364

site/src/scss/_syntax.scss

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,8 @@
3939
background-color: color-mix(in oklch, var(--primary-bg) 10%, transparent);
4040
}
4141

42-
// Dark mode theming for Shiki
43-
// When using dual themes, Shiki generates inline styles with CSS variables
44-
// We need to switch from the light theme vars to dark theme vars
4542
[data-bs-theme="dark"] {
4643
--shell-prompt-color: #868e96;
47-
48-
.astro-code span,
49-
.astro-code-themes span {
50-
// Override Shiki's inline color with the dark theme color
51-
color: var(--shiki-dark) !important; // stylelint-disable-line declaration-no-important
52-
}
5344
}
5445

5546
// Shell prompts - these work with Shiki's transformer that adds .line class

site/static/docs/[version]/assets/js/color-modes.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,19 @@
1919
return 'auto'
2020
}
2121

22-
const setTheme = theme => {
22+
const resolveTheme = theme => {
2323
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)
24+
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
25+
}
26+
27+
return theme
28+
}
29+
30+
const setTheme = theme => {
31+
const resolved = resolveTheme(theme)
32+
33+
if (document.documentElement.getAttribute('data-bs-theme') !== resolved) {
34+
document.documentElement.setAttribute('data-bs-theme', resolved)
2735
}
2836
}
2937

@@ -70,8 +78,11 @@
7078
toggle.addEventListener('click', () => {
7179
const theme = toggle.getAttribute('data-bs-theme-value')
7280
setStoredTheme(theme)
73-
setTheme(theme)
74-
showActiveTheme(theme, true)
81+
82+
requestAnimationFrame(() => {
83+
setTheme(theme)
84+
showActiveTheme(theme)
85+
})
7586
})
7687
})
7788
})

0 commit comments

Comments
 (0)