fix: solved FCP flash issue. (#551)

This commit is contained in:
Caspian Zhao
2023-05-18 11:39:02 +01:00
committed by GitHub
parent c3f891e1ab
commit 55944cd5e1
5 changed files with 35 additions and 6 deletions
+3 -1
View File
@@ -5,6 +5,7 @@ import { Toaster } from "react-hot-toast"
import "~/css/main.css"
import { useAcceptLang } from "~/hooks/useAcceptLang"
import { APP_DESCRIPTION, APP_NAME, APP_SLOGAN, SITE_URL } from "~/lib/env"
import { getColorScheme } from "~/lib/get-color-scheme"
import Providers from "./providers"
@@ -75,9 +76,10 @@ export default function RootLayout({
children: React.ReactNode
}) {
const lang = useAcceptLang()
const colorScheme = getColorScheme()
return (
<html lang={lang} dir={dir(lang)}>
<html lang={lang} dir={dir(lang)} className={colorScheme}>
<body>
<Providers lang={lang}>{children}</Providers>
<Toaster />
+17 -5
View File
@@ -1,6 +1,12 @@
import { MouseEvent, useEffect, useRef, useState } from "react"
import { create } from "zustand"
import {
COLOR_SCHEME_DARK,
COLOR_SCHEME_LIGHT,
DEFAULT_COLOR_SCHEME,
IS_DEV,
} from "~/lib/constants"
import { noop } from "~/lib/noop"
import { delStorage, getStorage, setStorage } from "~/lib/storage"
import { isServerSide } from "~/lib/utils"
@@ -14,7 +20,7 @@ interface IMediaStore {
const useMediaStore = create<IMediaStore>(() => {
return {
isDark: false,
isDark: DEFAULT_COLOR_SCHEME === COLOR_SCHEME_DARK,
toggle: () => void 0,
}
})
@@ -33,8 +39,8 @@ const useDarkModeInternal = (
options: DarkModeConfig,
) => {
const {
classNameDark = "dark",
classNameLight = "light",
classNameDark = COLOR_SCHEME_DARK,
classNameLight = COLOR_SCHEME_LIGHT,
storageKey = darkModeKey,
element,
transition,
@@ -178,8 +184,8 @@ const mockElement = {
export const useDarkMode = () => {
const { toggle, value } = useDarkModeInternal(getStorage(darkModeKey), {
classNameDark: "dark",
classNameLight: "light",
classNameDark: COLOR_SCHEME_DARK,
classNameLight: COLOR_SCHEME_LIGHT,
storageKey: darkModeKey,
element: (globalThis.document && document.documentElement) || mockElement,
transition:
@@ -194,6 +200,12 @@ export const useDarkMode = () => {
useMediaStore.setState({
isDark: value,
})
const colorScheme = value ? COLOR_SCHEME_DARK : COLOR_SCHEME_LIGHT
const date = new Date()
date.setMonth(date.getMonth() + 1)
document.cookie = IS_DEV
? `color_scheme=${colorScheme};`
: `color_scheme=${colorScheme}; Domain=.xlog.app; Path=/; Secure; HttpOnly; expires=${date.toUTCString()}`
}, [value])
const onceRef = useRef(false)
+6
View File
@@ -1,3 +1,5 @@
import { ColorScheme } from "./types"
export const IS_BROWSER = typeof document !== "undefined"
export const IS_PROD = IS_BROWSER
@@ -10,3 +12,7 @@ export const IS_VERCEL_PREVIEW =
process.env.VERCEL_ENV === "preview"
export const MAXIMUM_FILE_SIZE = 100 // MB
export const COLOR_SCHEME_DARK = "dark"
export const COLOR_SCHEME_LIGHT = "light"
export const DEFAULT_COLOR_SCHEME: ColorScheme = "light"
+7
View File
@@ -0,0 +1,7 @@
import { cookies } from "next/headers"
import { DEFAULT_COLOR_SCHEME } from "./constants"
import { ColorScheme } from "./types"
export const getColorScheme = () =>
(cookies().get("color_scheme")?.value || DEFAULT_COLOR_SCHEME) as ColorScheme
+2
View File
@@ -109,3 +109,5 @@ export type ExpandedCharacter = CharacterEntity & {
}
}
}
export type ColorScheme = "dark" | "light"