jingrow-ui/histoire.setup.ts
jingrow c7bac1a7a0
Some checks failed
Publish on NPM / publish (push) Has been cancelled
Build and Deploy Storybook / build (push) Has been cancelled
Tests / test (push) Has been cancelled
initial commit
2025-10-24 00:40:30 +08:00

49 lines
1.2 KiB
TypeScript

import './histoire.css'
import './src/style.css'
// development
if (document.readyState == 'complete') {
updateThemeAttrOnThemeChange()
}
// production
window.addEventListener('DOMContentLoaded', () => {
updateThemeAttrOnThemeChange()
})
function updateThemeAttrOnThemeChange() {
const theme = document.documentElement.classList.contains('htw-dark')
? 'htw-dark'
: 'light'
updateTheme(theme)
let observer = new MutationObserver((mutations) => {
for (const m of mutations) {
const newValue = m.target.getAttribute(m.attributeName)
updateTheme(newValue)
}
})
// observe changes to the class attribute on root element
observer.observe(document.documentElement, {
attributes: true,
attributeOldValue: true,
attributeFilter: ['class'],
})
}
function updateTheme(value: string) {
if (value === 'htw-dark') {
document.documentElement.setAttribute('data-theme', 'dark')
} else {
document.documentElement.setAttribute('data-theme', 'light')
}
}
// handle route param in url
const urlParams = new URLSearchParams(window.location.search)
const route = urlParams.get('route')
if (route) {
history.pushState({}, '', route)
}