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:
9
theme/theme.client.ts
Normal file
9
theme/theme.client.ts
Normal 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
21
theme/theme.server.ts
Normal 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
|
||||
}
|
Reference in New Issue
Block a user