-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathuserTheme.ts
More file actions
34 lines (31 loc) · 1.06 KB
/
userTheme.ts
File metadata and controls
34 lines (31 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { Signal } from "solid-js";
import { MaxAgeOptions, createServerCookie } from "./serverCookie";
export type Theme = "light" | "dark";
export type UserThemeOptions = MaxAgeOptions & {
/**
* The default theme to be used if the cookie is not set
*/
defaultValue?: Theme;
};
/**
* Composes {@link createServerCookie} to provide a type safe way to store a theme and access it on the server or client.
*
* @param name The name of the cookie to be set
* @param options Options for the cookie {@link UserThemeOptions}
*/
export function createUserTheme(
name: string | undefined,
options: UserThemeOptions & { defaultValue: Theme },
): Signal<Theme>;
export function createUserTheme(
name?: string,
options?: UserThemeOptions,
): Signal<Theme | undefined>;
export function createUserTheme(name = "theme", options?: UserThemeOptions): Signal<any> {
const defaultValue = options?.defaultValue;
return createServerCookie(name, {
...options,
deserialize: str => (str === "light" || str === "dark" ? str : defaultValue),
serialize: String,
});
}