Skip to content

Commit 67fb85a

Browse files
committed
feat(initial-mode): add initial theme mode plugin for dark/light mode handling
- Introduced a new plugin to manage the initial theme mode based on user preferences and local storage. - Updated main.ts to include the initialMode plugin in the theme creation process.
1 parent 95da8a3 commit 67fb85a

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

themes/shadcn/src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import './styles/globals.css'
33
import { codeCopyButton } from './plugins/code-copy-button'
44
import { smoothScroll } from './plugins/smooth-scroll'
55
import { spa } from './plugins/spa'
6+
import { initialMode } from './plugins/initial-mode'
67

7-
createTheme().use(spa).use(smoothScroll).use(codeCopyButton)
8+
createTheme().use(initialMode).use(spa).use(smoothScroll).use(codeCopyButton)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export const initialMode = () => {
2+
try {
3+
const savedTheme = localStorage.getItem('theme')
4+
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches
5+
const resolvedTheme =
6+
savedTheme === 'dark' || savedTheme === 'light'
7+
? savedTheme
8+
: prefersDark
9+
? 'dark'
10+
: 'light'
11+
const root = document.documentElement
12+
root.classList.toggle('dark', resolvedTheme === 'dark')
13+
root.classList.toggle('light', resolvedTheme === 'light')
14+
root.style.setProperty('color-scheme', resolvedTheme)
15+
} catch {
16+
// Ignore localStorage or matchMedia access errors.
17+
}
18+
}

0 commit comments

Comments
 (0)