-
-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathstate.ts
More file actions
61 lines (50 loc) · 1.45 KB
/
state.ts
File metadata and controls
61 lines (50 loc) · 1.45 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { IS_SERVER } from '@literal-ui/hooks'
import type { CSSProperties } from 'react'
import { atom, AtomEffect, useRecoilState } from 'recoil'
import { RenditionSpread } from '@flow/epubjs/types/rendition'
function localStorageEffect<T>(key: string, defaultValue: T): AtomEffect<T> {
return ({ setSelf, onSet }) => {
if (IS_SERVER) return
const savedValue = localStorage.getItem(key)
if (savedValue === null) {
localStorage.setItem(key, JSON.stringify(defaultValue))
} else {
setSelf(JSON.parse(savedValue))
}
onSet((newValue, _, isReset) => {
isReset
? localStorage.removeItem(key)
: localStorage.setItem(key, JSON.stringify(newValue))
})
}
}
export const navbarState = atom<boolean>({
key: 'navbar',
default: false,
})
export interface Settings extends TypographyConfiguration {
theme?: ThemeConfiguration
enableTextSelectionMenu?: boolean
}
export interface TypographyConfiguration {
fontSize?: string
fontWeight?: number
fontFamily?: string
lineHeight?: number
textAlign?: CSSProperties['textAlign']
spread?: RenditionSpread
zoom?: number
}
interface ThemeConfiguration {
source?: string
background?: number
}
export const defaultSettings: Settings = {}
const settingsState = atom<Settings>({
key: 'settings',
default: defaultSettings,
effects: [localStorageEffect('settings', defaultSettings)],
})
export function useSettings() {
return useRecoilState(settingsState)
}