aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/providers/theme-provider/theme-provider.tsx
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2023-10-28 17:12:58 +0200
committerArmand Philippot <git@armandphilippot.com>2023-11-11 18:15:27 +0100
commit60c49f18389ff625177a57277ef8f292a31097bf (patch)
tree76b0f1f1792b57659e54d282f93df70088446e3c /src/utils/providers/theme-provider/theme-provider.tsx
parent05f1dfc6896d3affa7c494a1b955f230d836a4b7 (diff)
refactor(providers,hooks): rewrite PrismThemeProvider & usePrismTheme
* reuse Theme provider logic * move DOM mutation from provider to hook * add a script to init theme before page load
Diffstat (limited to 'src/utils/providers/theme-provider/theme-provider.tsx')
-rw-r--r--src/utils/providers/theme-provider/theme-provider.tsx17
1 files changed, 7 insertions, 10 deletions
diff --git a/src/utils/providers/theme-provider/theme-provider.tsx b/src/utils/providers/theme-provider/theme-provider.tsx
index b743a7f..f09f74d 100644
--- a/src/utils/providers/theme-provider/theme-provider.tsx
+++ b/src/utils/providers/theme-provider/theme-provider.tsx
@@ -7,13 +7,14 @@ import {
useMemo,
useEffect,
} from 'react';
-import { getDataAttributeFrom, getThemeFromSystem } from '../../helpers';
+import type { Theme } from '../../../types';
+import {
+ getDataAttributeFrom,
+ getThemeFromSystem,
+ themeValidator,
+} from '../../helpers';
import { useLocalStorage, useSystemColorScheme } from '../../hooks';
-const validThemes = ['dark', 'light', 'system'] as const;
-
-type Theme = (typeof validThemes)[number];
-
type ThemeContextProps = {
resolvedTheme: Exclude<Theme, 'system'>;
setTheme: Dispatch<SetStateAction<Theme>>;
@@ -26,10 +27,6 @@ export const ThemeContext = createContext<ThemeContextProps>({
theme: 'system',
});
-const validator = (value: unknown): value is Theme =>
- typeof value === 'string' &&
- (validThemes as readonly string[]).includes(value);
-
export type ThemeProviderProps = {
/**
* The attribute name to append to document root.
@@ -60,7 +57,7 @@ export const ThemeProvider: FC<ThemeProviderProps> = ({
const [theme, setTheme] = useLocalStorage(
storageKey,
defaultTheme,
- validator
+ themeValidator
);
const userColorScheme = useSystemColorScheme();
const resolvedTheme: Exclude<Theme, 'system'> =