Skip to content

Commit 1067a1f

Browse files
author
Jed Mao
committed
fix: more flexible theme type
1 parent e4792b0 commit 1067a1f

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/ThemeContext.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ import React, {
99
useState,
1010
} from 'react'
1111

12-
type Theme = Record<string, string | number | boolean>
13-
1412
/**
1513
* Creates `React.Context` for a theme, exposes its `Provider` as well as some
1614
* utility functions and hooks for getting and setting the theme.
1715
*/
18-
export default class ThemeContext<T extends Theme> {
16+
export default class ThemeContext<T extends object> {
1917
private Context: React.Context<[T, Dispatch<SetStateAction<T>>]>
2018

2119
/**
@@ -66,13 +64,15 @@ export default class ThemeContext<T extends Theme> {
6664
): [T, Dispatch<SetStateAction<T>>] {
6765
const [theme, setTheme] = this.use()
6866
useLayoutEffect(() => {
69-
Object.keys(theme).forEach(key => {
70-
element.style.setProperty(`--${kebabCase(key)}`, theme[
71-
key
72-
] as string)
73-
})
67+
Object.keys(theme).forEach(setProp)
7468
}, [theme])
7569
return [theme, setTheme]
70+
function setProp(key: string) {
71+
element.style.setProperty(
72+
`--${kebabCase(key)}`,
73+
(theme as Record<string, string>)[key],
74+
)
75+
}
7676
}
7777

7878
/**

0 commit comments

Comments
 (0)