diff --git a/src/app/layout.tsx b/src/app/layout.tsx
index ed607531..3c55e187 100644
--- a/src/app/layout.tsx
+++ b/src/app/layout.tsx
@@ -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 (
-
+
{children}
diff --git a/src/hooks/useDarkMode.ts b/src/hooks/useDarkMode.ts
index 0b36c93f..d04a3e1f 100644
--- a/src/hooks/useDarkMode.ts
+++ b/src/hooks/useDarkMode.ts
@@ -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(() => {
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)
diff --git a/src/lib/constants.ts b/src/lib/constants.ts
index e9c02e11..0db7c5f9 100644
--- a/src/lib/constants.ts
+++ b/src/lib/constants.ts
@@ -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"
diff --git a/src/lib/get-color-scheme.ts b/src/lib/get-color-scheme.ts
new file mode 100644
index 00000000..8056322a
--- /dev/null
+++ b/src/lib/get-color-scheme.ts
@@ -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
diff --git a/src/lib/types.ts b/src/lib/types.ts
index 9bc3ff48..dd9c27cb 100644
--- a/src/lib/types.ts
+++ b/src/lib/types.ts
@@ -109,3 +109,5 @@ export type ExpandedCharacter = CharacterEntity & {
}
}
}
+
+export type ColorScheme = "dark" | "light"