1
1
mirror of https://github.com/theoludwig/theoludwig.git synced 2025-05-29 22:37:44 +02:00

refactor: implement light/dark themes using cookies

This commit is contained in:
2023-08-01 14:11:46 +02:00
parent e82db952db
commit caa6a90418
21 changed files with 117 additions and 117 deletions

9
theme/theme.client.ts Normal file
View File

@ -0,0 +1,9 @@
import UniversalCookie from 'universal-cookie'
import { DEFAULT_THEME } from '@/utils/constants'
import type { CookiesStore, Theme } from '@/utils/constants'
export const useTheme = (cookiesStore: CookiesStore): Theme => {
const universalCookie = new UniversalCookie(cookiesStore)
return universalCookie.get('theme') ?? DEFAULT_THEME
}

21
theme/theme.server.ts Normal file
View File

@ -0,0 +1,21 @@
'use server'
import { cookies } from 'next/headers'
import type { Theme } from '@/utils/constants'
import { COOKIE_MAX_AGE, DEFAULT_THEME, THEMES } from '@/utils/constants'
export const setTheme = (theme: Theme): void => {
cookies().set('theme', theme, {
path: '/',
maxAge: COOKIE_MAX_AGE
})
}
export const getTheme = (): Theme => {
const theme = cookies().get('theme')?.value ?? DEFAULT_THEME
if (THEMES.includes(theme as Theme)) {
return theme as Theme
}
return DEFAULT_THEME
}