-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into update-gh-actions
- Loading branch information
Showing
10 changed files
with
672 additions
and
490 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,72 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import React, { useCallback, useEffect, useState } from "react"; | ||
|
||
import { Element } from "../Element/Element"; | ||
import type { UseThemeProps } from "./types"; | ||
import { CommonAndHTMLProps } from "../Element/constants"; | ||
|
||
export type ThemeProviderElementType = HTMLDivElement; | ||
export type RenderProps = () => JSX.Element; | ||
|
||
const storageKey = "fictoan-theme"; | ||
const themes = ["light", "dark"]; | ||
|
||
const ThemeContext = React.createContext<UseThemeProps | undefined>(undefined); | ||
const defaultContext: UseThemeProps = { setTheme: (_) => {} }; | ||
|
||
export const useTheme = () => React.useContext(ThemeContext) ?? defaultContext; | ||
|
||
export interface ThemeProviderProps extends CommonAndHTMLProps<ThemeProviderElementType> { | ||
theme?: { | ||
[key: string]: string; | ||
}; | ||
customColors?: { | ||
[key: string]: string; | ||
}; | ||
currentTheme?: string; | ||
} | ||
|
||
const getTheme = (key: string, fallback?: string) => { | ||
let theme; | ||
try { | ||
theme = localStorage.getItem(key) || undefined; | ||
} catch (e) { | ||
// Unsupported | ||
} | ||
return theme || fallback; | ||
}; | ||
|
||
export const ThemeProvider = React.forwardRef( | ||
( | ||
{ theme, customColors, currentTheme, children, ...props }: ThemeProviderProps, | ||
ref: React.Ref<ThemeProviderElementType> | ||
) => { | ||
({ currentTheme, children, ...props }: ThemeProviderProps, ref: React.Ref<ThemeProviderElementType>) => { | ||
const [shouldRender, setShouldRender] = useState<boolean>(false); | ||
const [themeState, setThemeState] = React.useState(() => getTheme(storageKey, "theme-light")); | ||
|
||
useEffect(() => { | ||
if (theme) { | ||
const styleTag = getStyleTag("fictoan-theme"); | ||
addCssVariables(styleTag, theme); | ||
if (!shouldRender) { | ||
setShouldRender(true); | ||
const setTheme = useCallback( | ||
(newTheme: any) => { | ||
if (!themes.includes(newTheme)) { | ||
newTheme = "light"; | ||
} | ||
} | ||
|
||
return () => { | ||
const styleTag = getStyleTag("fictoan-theme"); | ||
styleTag.innerHTML = ""; | ||
}; | ||
}, [theme]); | ||
|
||
useEffect(() => { | ||
if (customColors) { | ||
const styleTag = getStyleTag("fictoan-custom-colors"); | ||
addCssVariables(styleTag, customColors); | ||
const styles = Object.keys(customColors).map( | ||
(colorName) => ` | ||
.bg-${colorName} {background-color: var(--${colorName});} | ||
.text-${colorName} {color: var(--${colorName}); } | ||
.fill-${colorName} {fill: var(--${colorName});} | ||
.stroke-${colorName} {stroke: var(--${colorName});} | ||
.border-${colorName} {border-color: var(--${colorName});}` | ||
); | ||
styleTag.appendChild(document.createTextNode(styles.join("\n"))); | ||
document.documentElement.className = ""; | ||
setThemeState(`theme-${newTheme}`); | ||
document.documentElement.classList.add(`theme-${newTheme}`); | ||
if (!shouldRender) { | ||
setShouldRender(true); | ||
} | ||
} | ||
|
||
return () => { | ||
const styleTag = getStyleTag("fictoan-custom-colors"); | ||
styleTag.innerHTML = ""; | ||
}; | ||
}, [customColors]); | ||
// Save to storage | ||
try { | ||
localStorage.setItem(storageKey, `theme-${newTheme}`); | ||
} catch (e) { | ||
// Unsupported | ||
} | ||
}, | ||
[themeState] | ||
); | ||
|
||
useEffect(() => { | ||
if (currentTheme) { | ||
document.documentElement.classList.add(currentTheme); | ||
if (!shouldRender) { | ||
setShouldRender(true); | ||
} | ||
setTheme(currentTheme); | ||
} | ||
|
||
return () => { | ||
document.documentElement.className = ""; | ||
}; | ||
}, [currentTheme]); | ||
|
||
const getStyleTag = (id: string): HTMLElement => { | ||
let styleTag = document.getElementById(id); | ||
if (!styleTag) { | ||
styleTag = document.createElement("style"); | ||
styleTag.id = id; | ||
document.head.appendChild(styleTag); | ||
} | ||
return styleTag; | ||
}; | ||
|
||
const addCssVariables = (element: HTMLElement, variables: { [key: string]: string }) => { | ||
let cssVariables: string | string[] = Object.entries(variables).map( | ||
([variableName, variableValue]) => `--${variableName}: ${variableValue};` | ||
); | ||
cssVariables = ":root {\n" + cssVariables.join("\n") + "\n}"; | ||
element.appendChild(document.createTextNode(cssVariables)); | ||
}; | ||
|
||
return ( | ||
<Element<ThemeProviderElementType> as="div" data-theme-provider ref={ref} {...props}> | ||
{shouldRender && children} | ||
</Element> | ||
<ThemeContext.Provider value={{ theme: themeState, setTheme }}> | ||
<Element<ThemeProviderElementType> as="div" data-theme-provider ref={ref} {...props}> | ||
{shouldRender && children} | ||
</Element> | ||
</ThemeContext.Provider> | ||
); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { ThemeProvider, type ThemeProviderProps } from "./ThemeProvider"; | ||
export { ThemeProvider, type ThemeProviderProps, useTheme } from "./ThemeProvider"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import * as React from "react"; | ||
|
||
export interface UseThemeProps { | ||
/** Update the theme */ | ||
setTheme: React.Dispatch<React.SetStateAction<string>>; | ||
/** Active theme name */ | ||
theme?: string | undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.