Skip to content

Commit

Permalink
feat: add color-scheme on root to enable native color scheme features
Browse files Browse the repository at this point in the history
  • Loading branch information
ambar committed Mar 28, 2024
1 parent f79f2e7 commit 2996e9f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-experts-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rspress/core": minor
---

feat: add color-scheme on root to enable native color scheme features
2 changes: 2 additions & 0 deletions packages/core/src/node/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ export const importStatementRegex =
// In the first render, the theme will be set according to the user's system theme
// - Should be injected into both development and production modes
// - Class hook (.dark) is set for internal use (Tailwind)
// - Style hook (colorScheme) is set for external use (CSS media queries or `light-dark()` function)
export const inlineThemeScript = `{
const saved = localStorage.getItem('${APPEARANCE_KEY}')
const preferDark = window.matchMedia('(prefers-color-scheme: dark)').matches
const isDark = !saved || saved === 'auto' ? preferDark : saved === 'dark'
document.documentElement.classList.toggle('dark', isDark)
document.documentElement.style.colorScheme = isDark ? 'dark' : 'light'
}`
.replace(/\n/g, ';')
.replace(/\s{2,}/g, '');
Expand Down

0 comments on commit 2996e9f

Please sign in to comment.