mirror of
https://github.com/wassname/chatGPTBox.git
synced 2026-08-12 11:50:13 +08:00
chore: improve compatibility for upgraded packages
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
export function useWindowTheme() {
|
||||
const [theme, setTheme] = useState(
|
||||
window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
|
||||
? 'dark'
|
||||
: 'light',
|
||||
)
|
||||
useEffect(() => {
|
||||
if (!window.matchMedia) return
|
||||
const listener = (e) => {
|
||||
setTheme(e.matches ? 'dark' : 'light')
|
||||
}
|
||||
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', listener)
|
||||
return () =>
|
||||
window.matchMedia('(prefers-color-scheme: dark)').removeEventListener('change', listener)
|
||||
}, [])
|
||||
return theme
|
||||
}
|
||||
Reference in New Issue
Block a user