mirror of
https://github.com/ourongxing/newsnow.git
synced 2025-01-19 03:09:14 +08:00
fix: flash in loading
This commit is contained in:
parent
4607758dd0
commit
dc0deb4ead
@ -4,7 +4,9 @@ import { useLocalStorage, useMedia } from "react-use"
|
|||||||
export declare type ColorScheme = "dark" | "light" | "auto"
|
export declare type ColorScheme = "dark" | "light" | "auto"
|
||||||
|
|
||||||
export function useDark(key = "color-scheme", defaultColorScheme: ColorScheme = "auto") {
|
export function useDark(key = "color-scheme", defaultColorScheme: ColorScheme = "auto") {
|
||||||
const [colorScheme, setColorScheme] = useLocalStorage(key, defaultColorScheme)
|
const [colorScheme, setColorScheme] = useLocalStorage(key, defaultColorScheme, {
|
||||||
|
raw: true,
|
||||||
|
})
|
||||||
const prefersDarkMode = useMedia("(prefers-color-scheme: dark)")
|
const prefersDarkMode = useMedia("(prefers-color-scheme: dark)")
|
||||||
const isDark = useMemo(() => colorScheme === "auto" ? prefersDarkMode : colorScheme === "dark", [colorScheme, prefersDarkMode])
|
const isDark = useMemo(() => colorScheme === "auto" ? prefersDarkMode : colorScheme === "dark", [colorScheme, prefersDarkMode])
|
||||||
|
|
||||||
|
@ -16,6 +16,12 @@ export const Route = createRootRouteWithContext<{
|
|||||||
component: RootComponent,
|
component: RootComponent,
|
||||||
notFoundComponent: NotFoundComponent,
|
notFoundComponent: NotFoundComponent,
|
||||||
beforeLoad: () => {
|
beforeLoad: () => {
|
||||||
|
const theme = localStorage.getItem("color-scheme") || "auto"
|
||||||
|
const isDark = window.matchMedia("(prefers-color-scheme: dark)").matches
|
||||||
|
if (!theme ? isDark : theme === "dark") {
|
||||||
|
document.documentElement.classList.add("dark")
|
||||||
|
}
|
||||||
|
|
||||||
const query = new URLSearchParams(window.location.search)
|
const query = new URLSearchParams(window.location.search)
|
||||||
if (query.has("login")) {
|
if (query.has("login")) {
|
||||||
[...query.entries()].forEach(key => localStorage.setItem(key[0], key[1]))
|
[...query.entries()].forEach(key => localStorage.setItem(key[0], key[1]))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user