-
Notifications
You must be signed in to change notification settings - Fork 104
/
Theme.tsx
56 lines (48 loc) · 1.76 KB
/
Theme.tsx
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
import React, { MutableRefObject, useEffect, useRef, useState } from 'react'
import { defaultTheme } from '../constants'
import { DataTheme, IComponentBaseProps } from '../types'
import { ThemeContext } from './ThemeContext'
import { getThemeFromClosestAncestor } from './utils'
export type ThemeProps = Omit<
React.HTMLAttributes<HTMLDivElement>,
'onChange'
> &
IComponentBaseProps & {
onChange?: (theme: DataTheme) => void
}
const Theme = React.forwardRef<HTMLDivElement, ThemeProps>(
(
{ children, dataTheme, onChange, className, ...props },
ref
): JSX.Element => {
// Either use provided ref or create a new ref
const themeRef = useRef<HTMLDivElement>(
(ref as MutableRefObject<HTMLDivElement>)?.current
)
const closestAncestorTheme = getThemeFromClosestAncestor(themeRef)
// If no theme is provided, use the closest ancestor theme, if no ancestor theme, fallback to default theme (defined in constants)
const [theme, setTheme] = useState<DataTheme>(
dataTheme || closestAncestorTheme || defaultTheme
)
const handleThemeChange = (theme: DataTheme) => {
// Fire custom onChange, if provided. ie, user provided function to store theme in session/local storage
onChange && onChange(theme)
// Update state/context
setTheme(theme)
}
// Properly handle changes to theme prop on Theme component
useEffect(() => {
if (dataTheme !== theme) {
dataTheme && handleThemeChange(dataTheme)
}
}, [dataTheme])
return (
<ThemeContext.Provider value={{ theme, setTheme: handleThemeChange }}>
<div {...props} data-theme={theme} className={className} ref={themeRef}>
{children}
</div>
</ThemeContext.Provider>
)
}
)
export default Theme