Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change setTheme type to React.Dispatch<React.SetStateAction<string>>. #178

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,22 @@ const Theme: React.FC<ThemeProviderProps> = ({
}, [])

const setTheme = useCallback(
theme => {
setThemeState(theme)

// Save to storage
try {
localStorage.setItem(storageKey, theme)
} catch (e) {
// Unsupported
}
(newTheme: React.SetStateAction<string>) => {

setThemeState(
prevTheme => {
const _theme = (newTheme instanceof Function) ? newTheme(prevTheme ?? defaultTheme) : prevTheme ?? defaultTheme;
yxshv marked this conversation as resolved.
Show resolved Hide resolved

// Save to storage
try {
localStorage.setItem(storageKey, _theme)
} catch (e) {
// Unsupported
}

return _theme
}
)
},
[forcedTheme]
)
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface UseThemeProps {
/** Forced theme name for the current page */
forcedTheme?: string
/** Update the theme */
setTheme: (theme: string) => void
setTheme: React.Dispatch<React.SetStateAction<string>>
/** Active theme name */
theme?: string
/** If `enableSystem` is true and the active theme is "system", this returns whether the system preference resolved to "dark" or "light". Otherwise, identical to `theme` */
Expand Down