Skip to content

Commit ea666d3

Browse files
committed
chore: update docs
1 parent 2e6226c commit ea666d3

File tree

2 files changed

+59
-55
lines changed

2 files changed

+59
-55
lines changed

README.md

+58-54
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Palettez
22

3-
A flexible and powerful theme management library for JavaScript applications.
3+
A flexible and powerful theme management library for JavaScript applications
44

55
## Features
66

@@ -33,7 +33,7 @@ pnpm add palettez
3333

3434
## Basic Usage
3535

36-
For client-side persistence (eg. localStorage), it's recommended to initialize Palettez in a synchronous script to avoid theme flicker on page load.If your project's bundler supports importing static asset as string, you can inline the minified version of Palettez to reduce the number of HTTP requests. Check out the demo for example usage with Astro and Vite.
36+
For client-side persistence (eg. localStorage), it's recommended to initialize Palettez in a synchronous script to avoid theme flicker on page load. If your project's bundler supports importing static asset as string, you can inline the minified version of Palettez to reduce the number of HTTP requests. Check out the demo for example usage with Astro and Vite.
3737

3838
```html
3939
<script src="https://unpkg.com/palettez"></script>
@@ -69,7 +69,7 @@ For client-side persistence (eg. localStorage), it's recommended to initialize P
6969
})
7070
})
7171
72-
themeManager.restore()
72+
await themeManager.restore()
7373
themeManager.sync()
7474
})()
7575
</script>
@@ -133,39 +133,43 @@ const themeManager = create({
133133
},
134134
},
135135

136-
// optional, specify your own storage solution
136+
// optional, specify your own storage solution. localStorage is used by default.
137137
getStorage: () => {
138138
return {
139-
getItem: (key: string) => {
140-
return JSON.parse(window.localStorage.getItem(key) || 'null')
141-
},
139+
getItem: (key: string) => {
140+
try {
141+
return JSON.parse(window.localStorage.getItem(key) || 'null')
142+
} catch {
143+
return null
144+
}
145+
},
142146

143-
setItem: (key: string, value: object) => {
144-
window.localStorage.setItem(key, JSON.stringify(value))
145-
},
147+
setItem: (key: string, value: object) => {
148+
window.localStorage.setItem(key, JSON.stringify(value))
149+
},
146150

147-
removeItem: (key: string) => {
148-
window.localStorage.removeItem(key)
149-
},
151+
removeItem: (key: string) => {
152+
window.localStorage.removeItem(key)
153+
},
150154

151155
// optional, useful for syncing theme selection across tabs and windows
152-
watch: (cb) => {
153-
const controller = new AbortController()
154-
155-
window.addEventListener(
156-
'storage',
157-
(e) => {
158-
const persistedThemes = JSON.parse(e.newValue || 'null')
159-
cb(e.key, persistedThemes)
160-
},
161-
{ signal: controller.signal },
162-
)
163-
164-
return () => {
165-
controller.abort()
166-
}
167-
},
168-
}
156+
watch: (cb) => {
157+
const controller = new AbortController()
158+
159+
window.addEventListener(
160+
'storage',
161+
(e) => {
162+
const persistedThemes = JSON.parse(e.newValue || 'null')
163+
cb(e.key, persistedThemes)
164+
},
165+
{ signal: controller.signal },
166+
)
167+
168+
return () => {
169+
controller.abort()
170+
}
171+
},
172+
}
169173
}
170174
})
171175
```
@@ -192,7 +196,7 @@ themeManager.subscribe((themes, resolvedThemes) => { /* ... */ })
192196

193197
## React Integration
194198

195-
Ensure that you have called `create` before `usePalettez`.
199+
Ensure that you have called `create` before `usePalettez`. Since Palettez is initialized in a synchronous script, `usePalettez` should only be used in a client-only component.
196200

197201
```tsx
198202
import { usePalettez } from 'palettez/react'
@@ -201,33 +205,33 @@ export function ThemeSelect() {
201205
const {
202206
themesAndOptions,
203207
themes,
204-
setThemes,
208+
setThemes,
205209

206210
getResolvedThemes,
207-
restore,
208-
sync,
209-
clear,
210-
subscribe,
211+
restore,
212+
sync,
213+
clear,
214+
subscribe,
211215
} = usePalettez('palettez')
212216

213217
return themesAndOptions.map((theme) => (
214-
<div key={theme.key}>
215-
<label htmlFor={theme.key}>{theme.label}</label>
216-
<select
217-
id={theme.key}
218-
name={theme.key}
219-
onChange={(e) => {
220-
setThemes({ [theme.key]: e.target.value })
221-
}}
222-
value={themes[theme.key]}
223-
>
224-
{theme.options.map((option) => (
225-
<option key={option.key} value={option.key}>
226-
{option.value}
227-
</option>
228-
))}
229-
</select>
230-
</div>
231-
))
218+
<div key={theme.key}>
219+
<label htmlFor={theme.key}>{theme.label}</label>
220+
<select
221+
id={theme.key}
222+
name={theme.key}
223+
onChange={(e) => {
224+
setThemes({ [theme.key]: e.target.value })
225+
}}
226+
value={themes[theme.key]}
227+
>
228+
{theme.options.map((option) => (
229+
<option key={option.key} value={option.key}>
230+
{option.value}
231+
</option>
232+
))}
233+
</select>
234+
</div>
235+
))
232236
}
233237
```

demo/.stackblitzrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"installDependencies": true,
3-
"startCommand": "npm i && npm run dev",
3+
"startCommand": "pnpm dev",
44
"env": {
55
"ENABLE_CJS_IMPORTS": true
66
}

0 commit comments

Comments
 (0)