@wise/components-theming 0.7.0 → 0.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.d.ts +3 -2
- package/dist/cjs/index.js +5 -3
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/index.d.ts +3 -2
- package/dist/es/index.js +5 -3
- package/dist/es/index.js.map +1 -1
- package/package.json +5 -2
- package/src/ThemeProvider.tsx +4 -3
- package/src/const.ts +1 -0
- package/src/index.ts +1 -1
package/dist/cjs/index.d.ts
CHANGED
|
@@ -21,13 +21,14 @@ type ScreenMode = (typeof screenModes)[number];
|
|
|
21
21
|
type Theming = {
|
|
22
22
|
theme?: ComponentTheme | BaseTheme | ExtraTheme;
|
|
23
23
|
screenMode?: ScreenMode;
|
|
24
|
+
isNotRootProvider?: boolean | undefined;
|
|
24
25
|
};
|
|
25
26
|
declare const isThemeModern: (theme: ComponentTheme | BaseTheme | ExtraTheme) => theme is "personal" | "forest-green" | "bright-green";
|
|
26
27
|
declare const isExtraTheme: (theme: ComponentTheme | BaseTheme | ExtraTheme) => theme is "forest-green" | "bright-green";
|
|
27
28
|
declare const isScreenModeDark: (theme: ComponentTheme | BaseTheme | ExtraTheme, screenMode: ScreenMode) => screenMode is "dark";
|
|
28
29
|
declare const getThemeClassName: (theme: ComponentTheme | BaseTheme | ExtraTheme, screenMode: ScreenMode) => string;
|
|
29
30
|
type ThemeProviderProps = PropsWithChildren<Theming>;
|
|
30
|
-
declare const ThemeProvider: ({ theme, screenMode, children }: ThemeProviderProps) => JSX.Element;
|
|
31
|
+
declare const ThemeProvider: ({ theme, screenMode, isNotRootProvider, children }: ThemeProviderProps) => JSX.Element;
|
|
31
32
|
interface ThemeHookValue {
|
|
32
33
|
theme: ComponentTheme | BaseTheme | ExtraTheme;
|
|
33
34
|
screenMode: ScreenMode;
|
|
@@ -37,4 +38,4 @@ interface ThemeHookValue {
|
|
|
37
38
|
}
|
|
38
39
|
declare const useTheme: () => ThemeHookValue;
|
|
39
40
|
export type { ComponentTheme, BaseTheme, ExtraTheme, ScreenMode, Theming };
|
|
40
|
-
export {
|
|
41
|
+
export { getThemeClassName, isExtraTheme, isScreenModeDark, isThemeModern, ThemeProvider, useTheme };
|
package/dist/cjs/index.js
CHANGED
|
@@ -91,14 +91,16 @@ var ThemeProvider = function ThemeProvider(_ref) {
|
|
|
91
91
|
theme = _ref$theme === void 0 ? DEFAULT_BASE_THEME : _ref$theme,
|
|
92
92
|
_ref$screenMode = _ref.screenMode,
|
|
93
93
|
screenMode = _ref$screenMode === void 0 ? DEFAULT_SCREEN_MODE : _ref$screenMode,
|
|
94
|
+
_ref$isNotRootProvide = _ref.isNotRootProvider,
|
|
95
|
+
isNotRootProvider = _ref$isNotRootProvide === void 0 ? false : _ref$isNotRootProvide,
|
|
94
96
|
children = _ref.children;
|
|
95
|
-
var
|
|
97
|
+
var isContextRoot = react.useContext(ThemeContext) === undefined; // RegEx to check for `np-theme-` class name
|
|
96
98
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
97
99
|
|
|
98
100
|
var themeClass = new RegExp(/\bnp-theme-[a-z-]+\b/, 'g'); // useEffect hook used to apply the theme class to the HTML element
|
|
99
101
|
|
|
100
102
|
react.useEffect(function () {
|
|
101
|
-
if (
|
|
103
|
+
if (!isNotRootProvider && isContextRoot) {
|
|
102
104
|
var _document$documentEle;
|
|
103
105
|
|
|
104
106
|
// Remove all the theme classes from the documentElement
|
|
@@ -109,7 +111,7 @@ var ThemeProvider = function ThemeProvider(_ref) {
|
|
|
109
111
|
document.documentElement.classList.add(item);
|
|
110
112
|
});
|
|
111
113
|
}
|
|
112
|
-
}, [
|
|
114
|
+
}, [isNotRootProvider, isContextRoot, theme, screenMode, themeClass]);
|
|
113
115
|
var contextValue = react.useMemo(function () {
|
|
114
116
|
return {
|
|
115
117
|
theme: theme,
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/const.ts","../../src/helpers.ts","../../src/useTheme.ts","../../src/ThemedChildren.tsx","../../src/ThemeProvider.tsx"],"sourcesContent":["// TODO: Change 'light' with 'legacy' in the future\nexport const baseThemes = ['light', 'personal'] as const;\nexport const extraThemes = ['forest-green', 'bright-green'] as const;\nexport const screenModes = ['light', 'dark'] as const;\nexport const modernThemes = [baseThemes[1], ...extraThemes] as const;\n\n// TODO: componentThemes returned back for backward compatibility, refactor this place in the future\nexport type ComponentTheme = typeof baseThemes[number];\nexport type ModernTheme = typeof modernThemes[number];\nexport type BaseTheme = typeof baseThemes[number];\nexport type ExtraTheme = typeof extraThemes[number];\nexport type ScreenMode = typeof screenModes[number];\nexport type ScreenModeDark = typeof screenModes[1];\n\nexport const DEFAULT_BASE_THEME = 'light' as const;\nexport const DEFAULT_SCREEN_MODE = 'light' as const;\n\nexport type Theming = {\n theme?: ComponentTheme | BaseTheme | ExtraTheme;\n screenMode?: ScreenMode;\n};\n","import type {\n ComponentTheme,\n ModernTheme,\n BaseTheme,\n ExtraTheme,\n ScreenMode,\n ScreenModeDark,\n} from './const';\nimport { extraThemes, screenModes, modernThemes } from './const';\n\nexport const isThemeModern = (\n theme: ComponentTheme | BaseTheme | ExtraTheme,\n): theme is ModernTheme => modernThemes.includes(theme as ModernTheme);\n\nexport const isExtraTheme = (theme: ComponentTheme | BaseTheme | ExtraTheme): theme is ExtraTheme =>\n extraThemes.includes(theme as ExtraTheme);\n\nexport const isScreenModeDark = (\n theme: ComponentTheme | BaseTheme | ExtraTheme,\n screenMode: ScreenMode,\n): screenMode is ScreenModeDark =>\n isThemeModern(theme as ModernTheme) && screenModes[1] === screenMode;\n\nexport const getThemeClassName = (\n theme: ComponentTheme | BaseTheme | ExtraTheme,\n screenMode: ScreenMode,\n) => {\n if (!isThemeModern(theme)) {\n return `np-theme-${theme}`;\n }\n\n let classes = `np-theme-personal`;\n\n if (isExtraTheme(theme)) {\n classes += ` ${classes}--${theme}`;\n } else if (screenModes[1] === screenMode) {\n classes += ` ${classes}--${screenMode}`;\n }\n\n return classes;\n};\n","import { useContext, useMemo } from 'react';\n\nimport { ThemeContext } from './ThemeProvider';\nimport type { ComponentTheme, BaseTheme, ScreenMode, ExtraTheme } from './const';\nimport { DEFAULT_BASE_THEME, DEFAULT_SCREEN_MODE } from './const';\nimport { isThemeModern, isScreenModeDark, getThemeClassName } from './helpers';\n\ninterface ThemeHookValue {\n theme: ComponentTheme | BaseTheme | ExtraTheme;\n screenMode: ScreenMode;\n isModern: boolean;\n isScreenModeDark: boolean;\n className: string;\n}\n\nconst FALLBACK_VALUES = {\n theme: DEFAULT_BASE_THEME,\n screenMode: DEFAULT_SCREEN_MODE,\n};\n\nconst isNotProduction = () => {\n try {\n return ['localhost', 'dev-wi.se'].includes(window.location.hostname);\n } catch {\n return false;\n }\n};\n\nexport const useTheme = (): ThemeHookValue => {\n const theming = useContext(ThemeContext);\n\n if (!theming && isNotProduction()) {\n // eslint-disable-next-line no-console\n console.warn('Call to useTheme outside a ThemeProvider');\n }\n\n const { theme, screenMode: contextScreenMode } = theming ?? FALLBACK_VALUES;\n\n const screenMode = theme === DEFAULT_BASE_THEME ? DEFAULT_SCREEN_MODE : contextScreenMode;\n\n return useMemo(\n () => ({\n theme,\n screenMode,\n isModern: isThemeModern(theme),\n isScreenModeDark: isScreenModeDark(theme, screenMode),\n className: getThemeClassName(theme, screenMode),\n }),\n [theme, screenMode],\n );\n};\n","import { ReactNode } from 'react';\n\nimport { useTheme } from './useTheme';\n\ntype ThemedChildrenProps = { children: ReactNode };\n\nexport const ThemedChildren = ({ children }: ThemedChildrenProps) => {\n const { className } = useTheme();\n\n return <div className={className}>{children}</div>;\n};\n","import { createContext, PropsWithChildren, useContext, useEffect, useMemo } from 'react';\n\nimport { ThemedChildren } from './ThemedChildren';\nimport type { ComponentTheme, BaseTheme, ExtraTheme, ScreenMode, Theming } from './const';\nimport { DEFAULT_BASE_THEME, DEFAULT_SCREEN_MODE } from './const';\nimport { getThemeClassName } from './helpers';\n\nexport const ThemeContext = createContext<\n | {\n theme: ComponentTheme | BaseTheme | ExtraTheme;\n screenMode: ScreenMode;\n }\n | undefined\n>(undefined);\n\ntype ThemeProviderProps = PropsWithChildren<Theming>;\n\nexport const ThemeProvider = ({\n theme = DEFAULT_BASE_THEME,\n screenMode = DEFAULT_SCREEN_MODE,\n children,\n}: ThemeProviderProps) => {\n const isRootProvider = useContext(ThemeContext) === undefined;\n\n // RegEx to check for `np-theme-` class name\n // eslint-disable-next-line react-hooks/exhaustive-deps\n const themeClass = new RegExp(/\\bnp-theme-[a-z-]+\\b/, 'g');\n\n // useEffect hook used to apply the theme class to the HTML element\n useEffect(() => {\n if (isRootProvider) {\n // Remove all the theme classes from the documentElement\n document.documentElement.className.match(themeClass)?.forEach((item) => {\n document.documentElement.classList.remove(item);\n });\n getThemeClassName(theme, screenMode)\n .split(' ')\n .forEach((item) => {\n document.documentElement.classList.add(item);\n });\n }\n }, [isRootProvider, theme, screenMode, themeClass]);\n\n const contextValue = useMemo(() => ({ theme, screenMode }), [screenMode, theme]);\n\n return (\n <ThemeContext.Provider value={contextValue}>\n <ThemedChildren>{children}</ThemedChildren>\n </ThemeContext.Provider>\n );\n};\n"],"names":["baseThemes","extraThemes","screenModes","modernThemes","DEFAULT_BASE_THEME","DEFAULT_SCREEN_MODE","isThemeModern","theme","includes","isExtraTheme","isScreenModeDark","screenMode","getThemeClassName","classes","FALLBACK_VALUES","isNotProduction","window","location","hostname","useTheme","theming","useContext","ThemeContext","console","warn","contextScreenMode","useMemo","isModern","className","ThemedChildren","children","_jsx","createContext","undefined","ThemeProvider","isRootProvider","themeClass","RegExp","useEffect","documentElement","match","forEach","item","document","classList","remove","split","add","contextValue","Provider","value"],"mappings":";;;;;AAAA;AACO,IAAMA,UAAU,GAAG,CAAC,OAAD,EAAU,UAAV,CAAnB,CAAA;AACA,IAAMC,WAAW,GAAG,CAAC,cAAD,EAAiB,cAAjB,CAApB,CAAA;AACA,IAAMC,W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AA,IAAAA,IAAAA,qBAAAA,KAAAA,KAAAA,CAAAA,GAAAA,KAAAA,CAAAA,GAAAA,qBAAAA,CAAAA,OAAtD,CAA8D,UAACC,IAAD,EAAS;AACrEC,QAAAA,QAAQ,CAACJ,eAAT,CAAyBK,SAAzB,CAAmCC,MAAnC,CAA0CH,IAA1C,CAAA,CAAA;OADF,CAAA,CAAA;AAGA9B,MAAAA,iBAAiB,CAACL,KAAD,EAAQI,UAAR,CAAjB,CACGmC,KADH,CACS,GADT,CAEGL,CAAAA,OAFH,CAEW,UAACC,IAAD,EAAS;AAChBC,QAAAA,QAAQ,CAACJ,eAAT,CAAyBK,SAAzB,CAAmCG,GAAnC,CAAuCL,IAAvC,CAAA,CAAA;OAHJ,CAAA,CAAA;AAKD,KAAA;GAXM,EAYN,CAACP,cAAD,EAAiB5B,KAAjB,EAAwBI,UAAxB,EAAoCyB,UAApC,CAZM,CAAT,CAAA;EAcA,IAAMY,YAAY,GAAGtB,aAAO,CAAC,YAAA;IAAA,OAAO;AAAEnB,MAAAA,KAAK,EAALA,KAAF;AAASI,MAAAA,UAAU,EAAVA,UAAAA;KAAhB,CAAA;AAAA,GAAD,EAAgC,CAACA,UAAD,EAAaJ,KAAb,CAAhC,CAA5B,CAAA;AAEA,EAAA,OACEwB,cAACT,CAAAA,YAAY,CAAC2B,QAAd,EAAsB;AAACC,IAAAA,KAAK,EAAEF,YAAR;IAAoBlB,QACxCC,EAAAA,cAAAA,CAACF,cAAD,EAAe;MAAAC,QAAEA,EAAAA,QAAAA;KAAjB,CAAA;AADoB,GAAtB,CADF,CAAA;AAKD;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/const.ts","../../src/helpers.ts","../../src/useTheme.ts","../../src/ThemedChildren.tsx","../../src/ThemeProvider.tsx"],"sourcesContent":["// TODO: Change 'light' with 'legacy' in the future\nexport const baseThemes = ['light', 'personal'] as const;\nexport const extraThemes = ['forest-green', 'bright-green'] as const;\nexport const screenModes = ['light', 'dark'] as const;\nexport const modernThemes = [baseThemes[1], ...extraThemes] as const;\n\n// TODO: componentThemes returned back for backward compatibility, refactor this place in the future\nexport type ComponentTheme = typeof baseThemes[number];\nexport type ModernTheme = typeof modernThemes[number];\nexport type BaseTheme = typeof baseThemes[number];\nexport type ExtraTheme = typeof extraThemes[number];\nexport type ScreenMode = typeof screenModes[number];\nexport type ScreenModeDark = typeof screenModes[1];\n\nexport const DEFAULT_BASE_THEME = 'light' as const;\nexport const DEFAULT_SCREEN_MODE = 'light' as const;\n\nexport type Theming = {\n theme?: ComponentTheme | BaseTheme | ExtraTheme;\n screenMode?: ScreenMode;\n isNotRootProvider?: boolean | undefined;\n};\n","import type {\n ComponentTheme,\n ModernTheme,\n BaseTheme,\n ExtraTheme,\n ScreenMode,\n ScreenModeDark,\n} from './const';\nimport { extraThemes, screenModes, modernThemes } from './const';\n\nexport const isThemeModern = (\n theme: ComponentTheme | BaseTheme | ExtraTheme,\n): theme is ModernTheme => modernThemes.includes(theme as ModernTheme);\n\nexport const isExtraTheme = (theme: ComponentTheme | BaseTheme | ExtraTheme): theme is ExtraTheme =>\n extraThemes.includes(theme as ExtraTheme);\n\nexport const isScreenModeDark = (\n theme: ComponentTheme | BaseTheme | ExtraTheme,\n screenMode: ScreenMode,\n): screenMode is ScreenModeDark =>\n isThemeModern(theme as ModernTheme) && screenModes[1] === screenMode;\n\nexport const getThemeClassName = (\n theme: ComponentTheme | BaseTheme | ExtraTheme,\n screenMode: ScreenMode,\n) => {\n if (!isThemeModern(theme)) {\n return `np-theme-${theme}`;\n }\n\n let classes = `np-theme-personal`;\n\n if (isExtraTheme(theme)) {\n classes += ` ${classes}--${theme}`;\n } else if (screenModes[1] === screenMode) {\n classes += ` ${classes}--${screenMode}`;\n }\n\n return classes;\n};\n","import { useContext, useMemo } from 'react';\n\nimport { ThemeContext } from './ThemeProvider';\nimport type { ComponentTheme, BaseTheme, ScreenMode, ExtraTheme } from './const';\nimport { DEFAULT_BASE_THEME, DEFAULT_SCREEN_MODE } from './const';\nimport { isThemeModern, isScreenModeDark, getThemeClassName } from './helpers';\n\ninterface ThemeHookValue {\n theme: ComponentTheme | BaseTheme | ExtraTheme;\n screenMode: ScreenMode;\n isModern: boolean;\n isScreenModeDark: boolean;\n className: string;\n}\n\nconst FALLBACK_VALUES = {\n theme: DEFAULT_BASE_THEME,\n screenMode: DEFAULT_SCREEN_MODE,\n};\n\nconst isNotProduction = () => {\n try {\n return ['localhost', 'dev-wi.se'].includes(window.location.hostname);\n } catch {\n return false;\n }\n};\n\nexport const useTheme = (): ThemeHookValue => {\n const theming = useContext(ThemeContext);\n\n if (!theming && isNotProduction()) {\n // eslint-disable-next-line no-console\n console.warn('Call to useTheme outside a ThemeProvider');\n }\n\n const { theme, screenMode: contextScreenMode } = theming ?? FALLBACK_VALUES;\n\n const screenMode = theme === DEFAULT_BASE_THEME ? DEFAULT_SCREEN_MODE : contextScreenMode;\n\n return useMemo(\n () => ({\n theme,\n screenMode,\n isModern: isThemeModern(theme),\n isScreenModeDark: isScreenModeDark(theme, screenMode),\n className: getThemeClassName(theme, screenMode),\n }),\n [theme, screenMode],\n );\n};\n","import { ReactNode } from 'react';\n\nimport { useTheme } from './useTheme';\n\ntype ThemedChildrenProps = { children: ReactNode };\n\nexport const ThemedChildren = ({ children }: ThemedChildrenProps) => {\n const { className } = useTheme();\n\n return <div className={className}>{children}</div>;\n};\n","import { createContext, PropsWithChildren, useContext, useEffect, useMemo } from 'react';\n\nimport { ThemedChildren } from './ThemedChildren';\nimport type { ComponentTheme, BaseTheme, ExtraTheme, ScreenMode, Theming } from './const';\nimport { DEFAULT_BASE_THEME, DEFAULT_SCREEN_MODE } from './const';\nimport { getThemeClassName } from './helpers';\n\nexport const ThemeContext = createContext<\n | {\n theme: ComponentTheme | BaseTheme | ExtraTheme;\n screenMode: ScreenMode;\n }\n | undefined\n>(undefined);\n\ntype ThemeProviderProps = PropsWithChildren<Theming>;\n\nexport const ThemeProvider = ({\n theme = DEFAULT_BASE_THEME,\n screenMode = DEFAULT_SCREEN_MODE,\n isNotRootProvider = false,\n children,\n}: ThemeProviderProps) => {\n const isContextRoot = useContext(ThemeContext) === undefined;\n\n // RegEx to check for `np-theme-` class name\n // eslint-disable-next-line react-hooks/exhaustive-deps\n const themeClass = new RegExp(/\\bnp-theme-[a-z-]+\\b/, 'g');\n\n // useEffect hook used to apply the theme class to the HTML element\n useEffect(() => {\n if (!isNotRootProvider && isContextRoot) {\n // Remove all the theme classes from the documentElement\n document.documentElement.className.match(themeClass)?.forEach((item) => {\n document.documentElement.classList.remove(item);\n });\n getThemeClassName(theme, screenMode)\n .split(' ')\n .forEach((item) => {\n document.documentElement.classList.add(item);\n });\n }\n }, [isNotRootProvider, isContextRoot, theme, screenMode, themeClass]);\n\n const contextValue = useMemo(() => ({ theme, screenMode }), [screenMode, theme]);\n\n return (\n <ThemeContext.Provider value={contextValue}>\n <ThemedChildren>{children}</ThemedChildren>\n </ThemeContext.Provider>\n );\n};\n"],"names":["baseThemes","extraThemes","screenModes","modernThemes","DEFAULT_BASE_THEME","DEFAULT_SCREEN_MODE","isThemeModern","theme","includes","isExtraTheme","isScreenModeDark","screenMode","getThemeClassName","classes","FALLBACK_VALUES","isNotProduction","window","location","hostname","useTheme","theming","useContext","ThemeContext","console","warn","contextScreenMode","useMemo","isModern","className","ThemedChildren","children","_jsx","createContext","undefined","ThemeProvider","isNotRootProvider","isContextRoot","themeClass","RegExp","useEffect","documentElement","match","forEach","item","document","classList","remove","split","add","contextValue","Provider","value"],"mappings":";;;;;AAAA;AACO,IAAMA,UAAU,GAAG,CAAC,OAAD,EAAU,UAAV,CAAnB,CAAA;AACA,IAAMC,WAAW,GAAG,CAAC,cAAD,EAAiB,cAAjB,CAApB,CAAA;AACA,IAAMC,WAAW,GAAG,CAAC,OAAD,EAAU,MAAV,CAApB,CAAA;AACA,IAAMC,YAAY,GAAIH,CAAAA,UAAU,CAAC,CAAD,CAAd,CAAsBC,CAAAA,MAAAA,CAAAA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A,qBAAAA,KAAAA,KAAAA,CAAAA,GAAAA,KAAAA,CAAAA,GAAAA,qBAAAA,CAAAA,OAAtD,CAA8D,UAACC,IAAD,EAAS;AACrEC,QAAAA,QAAQ,CAACJ,eAAT,CAAyBK,SAAzB,CAAmCC,MAAnC,CAA0CH,IAA1C,CAAA,CAAA;OADF,CAAA,CAAA;AAGA/B,MAAAA,iBAAiB,CAACL,KAAD,EAAQI,UAAR,CAAjB,CACGoC,KADH,CACS,GADT,CAEGL,CAAAA,OAFH,CAEW,UAACC,IAAD,EAAS;AAChBC,QAAAA,QAAQ,CAACJ,eAAT,CAAyBK,SAAzB,CAAmCG,GAAnC,CAAuCL,IAAvC,CAAA,CAAA;OAHJ,CAAA,CAAA;AAKD,KAAA;AACF,GAZQ,EAYN,CAACR,iBAAD,EAAoBC,aAApB,EAAmC7B,KAAnC,EAA0CI,UAA1C,EAAsD0B,UAAtD,CAZM,CAAT,CAAA;EAcA,IAAMY,YAAY,GAAGvB,aAAO,CAAC,YAAA;IAAA,OAAO;AAAEnB,MAAAA,KAAK,EAALA,KAAF;AAASI,MAAAA,UAAU,EAAVA,UAAAA;KAAhB,CAAA;AAAA,GAAD,EAAgC,CAACA,UAAD,EAAaJ,KAAb,CAAhC,CAA5B,CAAA;AAEA,EAAA,OACEwB,cAACT,CAAAA,YAAY,CAAC4B,QAAd,EAAsB;AAACC,IAAAA,KAAK,EAAEF,YAAR;IAAoBnB,QACxCC,EAAAA,cAAAA,CAACF,cAAD,EAAe;MAAAC,QAAEA,EAAAA,QAAAA;KAAjB,CAAA;AADoB,GAAtB,CADF,CAAA;AAKD;;;;;;;;;"}
|
package/dist/es/index.d.ts
CHANGED
|
@@ -21,13 +21,14 @@ type ScreenMode = (typeof screenModes)[number];
|
|
|
21
21
|
type Theming = {
|
|
22
22
|
theme?: ComponentTheme | BaseTheme | ExtraTheme;
|
|
23
23
|
screenMode?: ScreenMode;
|
|
24
|
+
isNotRootProvider?: boolean | undefined;
|
|
24
25
|
};
|
|
25
26
|
declare const isThemeModern: (theme: ComponentTheme | BaseTheme | ExtraTheme) => theme is "personal" | "forest-green" | "bright-green";
|
|
26
27
|
declare const isExtraTheme: (theme: ComponentTheme | BaseTheme | ExtraTheme) => theme is "forest-green" | "bright-green";
|
|
27
28
|
declare const isScreenModeDark: (theme: ComponentTheme | BaseTheme | ExtraTheme, screenMode: ScreenMode) => screenMode is "dark";
|
|
28
29
|
declare const getThemeClassName: (theme: ComponentTheme | BaseTheme | ExtraTheme, screenMode: ScreenMode) => string;
|
|
29
30
|
type ThemeProviderProps = PropsWithChildren<Theming>;
|
|
30
|
-
declare const ThemeProvider: ({ theme, screenMode, children }: ThemeProviderProps) => JSX.Element;
|
|
31
|
+
declare const ThemeProvider: ({ theme, screenMode, isNotRootProvider, children }: ThemeProviderProps) => JSX.Element;
|
|
31
32
|
interface ThemeHookValue {
|
|
32
33
|
theme: ComponentTheme | BaseTheme | ExtraTheme;
|
|
33
34
|
screenMode: ScreenMode;
|
|
@@ -37,4 +38,4 @@ interface ThemeHookValue {
|
|
|
37
38
|
}
|
|
38
39
|
declare const useTheme: () => ThemeHookValue;
|
|
39
40
|
export type { ComponentTheme, BaseTheme, ExtraTheme, ScreenMode, Theming };
|
|
40
|
-
export {
|
|
41
|
+
export { getThemeClassName, isExtraTheme, isScreenModeDark, isThemeModern, ThemeProvider, useTheme };
|
package/dist/es/index.js
CHANGED
|
@@ -89,14 +89,16 @@ var ThemeProvider = function ThemeProvider(_ref) {
|
|
|
89
89
|
theme = _ref$theme === void 0 ? DEFAULT_BASE_THEME : _ref$theme,
|
|
90
90
|
_ref$screenMode = _ref.screenMode,
|
|
91
91
|
screenMode = _ref$screenMode === void 0 ? DEFAULT_SCREEN_MODE : _ref$screenMode,
|
|
92
|
+
_ref$isNotRootProvide = _ref.isNotRootProvider,
|
|
93
|
+
isNotRootProvider = _ref$isNotRootProvide === void 0 ? false : _ref$isNotRootProvide,
|
|
92
94
|
children = _ref.children;
|
|
93
|
-
var
|
|
95
|
+
var isContextRoot = useContext(ThemeContext) === undefined; // RegEx to check for `np-theme-` class name
|
|
94
96
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
95
97
|
|
|
96
98
|
var themeClass = new RegExp(/\bnp-theme-[a-z-]+\b/, 'g'); // useEffect hook used to apply the theme class to the HTML element
|
|
97
99
|
|
|
98
100
|
useEffect(function () {
|
|
99
|
-
if (
|
|
101
|
+
if (!isNotRootProvider && isContextRoot) {
|
|
100
102
|
var _document$documentEle;
|
|
101
103
|
|
|
102
104
|
// Remove all the theme classes from the documentElement
|
|
@@ -107,7 +109,7 @@ var ThemeProvider = function ThemeProvider(_ref) {
|
|
|
107
109
|
document.documentElement.classList.add(item);
|
|
108
110
|
});
|
|
109
111
|
}
|
|
110
|
-
}, [
|
|
112
|
+
}, [isNotRootProvider, isContextRoot, theme, screenMode, themeClass]);
|
|
111
113
|
var contextValue = useMemo(function () {
|
|
112
114
|
return {
|
|
113
115
|
theme: theme,
|
package/dist/es/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/const.ts","../../src/helpers.ts","../../src/useTheme.ts","../../src/ThemedChildren.tsx","../../src/ThemeProvider.tsx"],"sourcesContent":["// TODO: Change 'light' with 'legacy' in the future\nexport const baseThemes = ['light', 'personal'] as const;\nexport const extraThemes = ['forest-green', 'bright-green'] as const;\nexport const screenModes = ['light', 'dark'] as const;\nexport const modernThemes = [baseThemes[1], ...extraThemes] as const;\n\n// TODO: componentThemes returned back for backward compatibility, refactor this place in the future\nexport type ComponentTheme = typeof baseThemes[number];\nexport type ModernTheme = typeof modernThemes[number];\nexport type BaseTheme = typeof baseThemes[number];\nexport type ExtraTheme = typeof extraThemes[number];\nexport type ScreenMode = typeof screenModes[number];\nexport type ScreenModeDark = typeof screenModes[1];\n\nexport const DEFAULT_BASE_THEME = 'light' as const;\nexport const DEFAULT_SCREEN_MODE = 'light' as const;\n\nexport type Theming = {\n theme?: ComponentTheme | BaseTheme | ExtraTheme;\n screenMode?: ScreenMode;\n};\n","import type {\n ComponentTheme,\n ModernTheme,\n BaseTheme,\n ExtraTheme,\n ScreenMode,\n ScreenModeDark,\n} from './const';\nimport { extraThemes, screenModes, modernThemes } from './const';\n\nexport const isThemeModern = (\n theme: ComponentTheme | BaseTheme | ExtraTheme,\n): theme is ModernTheme => modernThemes.includes(theme as ModernTheme);\n\nexport const isExtraTheme = (theme: ComponentTheme | BaseTheme | ExtraTheme): theme is ExtraTheme =>\n extraThemes.includes(theme as ExtraTheme);\n\nexport const isScreenModeDark = (\n theme: ComponentTheme | BaseTheme | ExtraTheme,\n screenMode: ScreenMode,\n): screenMode is ScreenModeDark =>\n isThemeModern(theme as ModernTheme) && screenModes[1] === screenMode;\n\nexport const getThemeClassName = (\n theme: ComponentTheme | BaseTheme | ExtraTheme,\n screenMode: ScreenMode,\n) => {\n if (!isThemeModern(theme)) {\n return `np-theme-${theme}`;\n }\n\n let classes = `np-theme-personal`;\n\n if (isExtraTheme(theme)) {\n classes += ` ${classes}--${theme}`;\n } else if (screenModes[1] === screenMode) {\n classes += ` ${classes}--${screenMode}`;\n }\n\n return classes;\n};\n","import { useContext, useMemo } from 'react';\n\nimport { ThemeContext } from './ThemeProvider';\nimport type { ComponentTheme, BaseTheme, ScreenMode, ExtraTheme } from './const';\nimport { DEFAULT_BASE_THEME, DEFAULT_SCREEN_MODE } from './const';\nimport { isThemeModern, isScreenModeDark, getThemeClassName } from './helpers';\n\ninterface ThemeHookValue {\n theme: ComponentTheme | BaseTheme | ExtraTheme;\n screenMode: ScreenMode;\n isModern: boolean;\n isScreenModeDark: boolean;\n className: string;\n}\n\nconst FALLBACK_VALUES = {\n theme: DEFAULT_BASE_THEME,\n screenMode: DEFAULT_SCREEN_MODE,\n};\n\nconst isNotProduction = () => {\n try {\n return ['localhost', 'dev-wi.se'].includes(window.location.hostname);\n } catch {\n return false;\n }\n};\n\nexport const useTheme = (): ThemeHookValue => {\n const theming = useContext(ThemeContext);\n\n if (!theming && isNotProduction()) {\n // eslint-disable-next-line no-console\n console.warn('Call to useTheme outside a ThemeProvider');\n }\n\n const { theme, screenMode: contextScreenMode } = theming ?? FALLBACK_VALUES;\n\n const screenMode = theme === DEFAULT_BASE_THEME ? DEFAULT_SCREEN_MODE : contextScreenMode;\n\n return useMemo(\n () => ({\n theme,\n screenMode,\n isModern: isThemeModern(theme),\n isScreenModeDark: isScreenModeDark(theme, screenMode),\n className: getThemeClassName(theme, screenMode),\n }),\n [theme, screenMode],\n );\n};\n","import { ReactNode } from 'react';\n\nimport { useTheme } from './useTheme';\n\ntype ThemedChildrenProps = { children: ReactNode };\n\nexport const ThemedChildren = ({ children }: ThemedChildrenProps) => {\n const { className } = useTheme();\n\n return <div className={className}>{children}</div>;\n};\n","import { createContext, PropsWithChildren, useContext, useEffect, useMemo } from 'react';\n\nimport { ThemedChildren } from './ThemedChildren';\nimport type { ComponentTheme, BaseTheme, ExtraTheme, ScreenMode, Theming } from './const';\nimport { DEFAULT_BASE_THEME, DEFAULT_SCREEN_MODE } from './const';\nimport { getThemeClassName } from './helpers';\n\nexport const ThemeContext = createContext<\n | {\n theme: ComponentTheme | BaseTheme | ExtraTheme;\n screenMode: ScreenMode;\n }\n | undefined\n>(undefined);\n\ntype ThemeProviderProps = PropsWithChildren<Theming>;\n\nexport const ThemeProvider = ({\n theme = DEFAULT_BASE_THEME,\n screenMode = DEFAULT_SCREEN_MODE,\n children,\n}: ThemeProviderProps) => {\n const isRootProvider = useContext(ThemeContext) === undefined;\n\n // RegEx to check for `np-theme-` class name\n // eslint-disable-next-line react-hooks/exhaustive-deps\n const themeClass = new RegExp(/\\bnp-theme-[a-z-]+\\b/, 'g');\n\n // useEffect hook used to apply the theme class to the HTML element\n useEffect(() => {\n if (isRootProvider) {\n // Remove all the theme classes from the documentElement\n document.documentElement.className.match(themeClass)?.forEach((item) => {\n document.documentElement.classList.remove(item);\n });\n getThemeClassName(theme, screenMode)\n .split(' ')\n .forEach((item) => {\n document.documentElement.classList.add(item);\n });\n }\n }, [isRootProvider, theme, screenMode, themeClass]);\n\n const contextValue = useMemo(() => ({ theme, screenMode }), [screenMode, theme]);\n\n return (\n <ThemeContext.Provider value={contextValue}>\n <ThemedChildren>{children}</ThemedChildren>\n </ThemeContext.Provider>\n );\n};\n"],"names":["baseThemes","extraThemes","screenModes","modernThemes","DEFAULT_BASE_THEME","DEFAULT_SCREEN_MODE","isThemeModern","theme","includes","isExtraTheme","isScreenModeDark","screenMode","getThemeClassName","classes","FALLBACK_VALUES","isNotProduction","window","location","hostname","useTheme","theming","useContext","ThemeContext","console","warn","contextScreenMode","useMemo","isModern","className","ThemedChildren","children","_jsx","createContext","undefined","ThemeProvider","isRootProvider","themeClass","RegExp","useEffect","documentElement","match","forEach","item","document","classList","remove","split","add","contextValue","Provider","value"],"mappings":";;;AAAA;AACO,IAAMA,UAAU,GAAG,CAAC,OAAD,EAAU,UAAV,CAAnB,CAAA;AACA,IAAMC,WAAW,GAAG,CAAC,cAAD,EAAiB,cAAjB,CAApB,CAAA;AACA,IAAMC,WAAW,GAAG,CAAC,OAAD,EAAU,MAAV,CAApB,CAAA;AACA,IAAMC,YAAY,GAAIH,CAAAA,UAAU,CAAC,CAAD,CAAd,CAAsBC,CAAAA,MAAAA,CAAAA,WAAtB,CAAlB,CAAA;AAUA,IAAMG,kBAAkB,GAAG,OAA3B,CAAA;AACA,IAAMC,mBAAmB,GAAG,OAA5B;;ICLMC,aAAa,GAAG,SAAhBA,aAAgB,CAC3BC,KAD2B,EAAA;AAAA,EAAA,OAEFJ,YAAY,CAACK,QAAb,CAAsBD,KAAtB,CAFE,CAAA;AAAA,EAAtB;IAIME,YAAY,GAAG,SAAfA,YAAe,CAACF,KAAD,EAAA;AAAA,EAAA,OAC1BN,WAAW,CAACO,QAAZ,CAAqBD,KAArB,CAD0B,CAAA;AAAA,EAArB;AAGA,IAAMG,gBAAgB,GAAG,SAAnBA,gBAAmB,CAC9BH,KAD8B,EAE9BI,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,CAAA;EAcA,IAAMY,YAAY,GAAGtB,OAAO,CAAC,YAAA;IAAA,OAAO;AAAEnB,MAAAA,KAAK,EAALA,KAAF;AAASI,MAAAA,UAAU,EAAVA,UAAAA;KAAhB,CAAA;AAAA,GAAD,EAAgC,CAACA,UAAD,EAAaJ,KAAb,CAAhC,CAA5B,CAAA;AAEA,EAAA,OACEwB,GAACT,CAAAA,YAAY,CAAC2B,QAAd,EAAsB;AAACC,IAAAA,KAAK,EAAEF,YAAR;IAAoBlB,QACxCC,EAAAA,GAAAA,CAACF,cAAD,EAAe;MAAAC,QAAEA,EAAAA,QAAAA;KAAjB,CAAA;AADoB,GAAtB,CADF,CAAA;AAKD;;;;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/const.ts","../../src/helpers.ts","../../src/useTheme.ts","../../src/ThemedChildren.tsx","../../src/ThemeProvider.tsx"],"sourcesContent":["// TODO: Change 'light' with 'legacy' in the future\nexport const baseThemes = ['light', 'personal'] as const;\nexport const extraThemes = ['forest-green', 'bright-green'] as const;\nexport const screenModes = ['light', 'dark'] as const;\nexport const modernThemes = [baseThemes[1], ...extraThemes] as const;\n\n// TODO: componentThemes returned back for backward compatibility, refactor this place in the future\nexport type ComponentTheme = typeof baseThemes[number];\nexport type ModernTheme = typeof modernThemes[number];\nexport type BaseTheme = typeof baseThemes[number];\nexport type ExtraTheme = typeof extraThemes[number];\nexport type ScreenMode = typeof screenModes[number];\nexport type ScreenModeDark = typeof screenModes[1];\n\nexport const DEFAULT_BASE_THEME = 'light' as const;\nexport const DEFAULT_SCREEN_MODE = 'light' as const;\n\nexport type Theming = {\n theme?: ComponentTheme | BaseTheme | ExtraTheme;\n screenMode?: ScreenMode;\n isNotRootProvider?: boolean | undefined;\n};\n","import type {\n ComponentTheme,\n ModernTheme,\n BaseTheme,\n ExtraTheme,\n ScreenMode,\n ScreenModeDark,\n} from './const';\nimport { extraThemes, screenModes, modernThemes } from './const';\n\nexport const isThemeModern = (\n theme: ComponentTheme | BaseTheme | ExtraTheme,\n): theme is ModernTheme => modernThemes.includes(theme as ModernTheme);\n\nexport const isExtraTheme = (theme: ComponentTheme | BaseTheme | ExtraTheme): theme is ExtraTheme =>\n extraThemes.includes(theme as ExtraTheme);\n\nexport const isScreenModeDark = (\n theme: ComponentTheme | BaseTheme | ExtraTheme,\n screenMode: ScreenMode,\n): screenMode is ScreenModeDark =>\n isThemeModern(theme as ModernTheme) && screenModes[1] === screenMode;\n\nexport const getThemeClassName = (\n theme: ComponentTheme | BaseTheme | ExtraTheme,\n screenMode: ScreenMode,\n) => {\n if (!isThemeModern(theme)) {\n return `np-theme-${theme}`;\n }\n\n let classes = `np-theme-personal`;\n\n if (isExtraTheme(theme)) {\n classes += ` ${classes}--${theme}`;\n } else if (screenModes[1] === screenMode) {\n classes += ` ${classes}--${screenMode}`;\n }\n\n return classes;\n};\n","import { useContext, useMemo } from 'react';\n\nimport { ThemeContext } from './ThemeProvider';\nimport type { ComponentTheme, BaseTheme, ScreenMode, ExtraTheme } from './const';\nimport { DEFAULT_BASE_THEME, DEFAULT_SCREEN_MODE } from './const';\nimport { isThemeModern, isScreenModeDark, getThemeClassName } from './helpers';\n\ninterface ThemeHookValue {\n theme: ComponentTheme | BaseTheme | ExtraTheme;\n screenMode: ScreenMode;\n isModern: boolean;\n isScreenModeDark: boolean;\n className: string;\n}\n\nconst FALLBACK_VALUES = {\n theme: DEFAULT_BASE_THEME,\n screenMode: DEFAULT_SCREEN_MODE,\n};\n\nconst isNotProduction = () => {\n try {\n return ['localhost', 'dev-wi.se'].includes(window.location.hostname);\n } catch {\n return false;\n }\n};\n\nexport const useTheme = (): ThemeHookValue => {\n const theming = useContext(ThemeContext);\n\n if (!theming && isNotProduction()) {\n // eslint-disable-next-line no-console\n console.warn('Call to useTheme outside a ThemeProvider');\n }\n\n const { theme, screenMode: contextScreenMode } = theming ?? FALLBACK_VALUES;\n\n const screenMode = theme === DEFAULT_BASE_THEME ? DEFAULT_SCREEN_MODE : contextScreenMode;\n\n return useMemo(\n () => ({\n theme,\n screenMode,\n isModern: isThemeModern(theme),\n isScreenModeDark: isScreenModeDark(theme, screenMode),\n className: getThemeClassName(theme, screenMode),\n }),\n [theme, screenMode],\n );\n};\n","import { ReactNode } from 'react';\n\nimport { useTheme } from './useTheme';\n\ntype ThemedChildrenProps = { children: ReactNode };\n\nexport const ThemedChildren = ({ children }: ThemedChildrenProps) => {\n const { className } = useTheme();\n\n return <div className={className}>{children}</div>;\n};\n","import { createContext, PropsWithChildren, useContext, useEffect, useMemo } from 'react';\n\nimport { ThemedChildren } from './ThemedChildren';\nimport type { ComponentTheme, BaseTheme, ExtraTheme, ScreenMode, Theming } from './const';\nimport { DEFAULT_BASE_THEME, DEFAULT_SCREEN_MODE } from './const';\nimport { getThemeClassName } from './helpers';\n\nexport const ThemeContext = createContext<\n | {\n theme: ComponentTheme | BaseTheme | ExtraTheme;\n screenMode: ScreenMode;\n }\n | undefined\n>(undefined);\n\ntype ThemeProviderProps = PropsWithChildren<Theming>;\n\nexport const ThemeProvider = ({\n theme = DEFAULT_BASE_THEME,\n screenMode = DEFAULT_SCREEN_MODE,\n isNotRootProvider = false,\n children,\n}: ThemeProviderProps) => {\n const isContextRoot = useContext(ThemeContext) === undefined;\n\n // RegEx to check for `np-theme-` class name\n // eslint-disable-next-line react-hooks/exhaustive-deps\n const themeClass = new RegExp(/\\bnp-theme-[a-z-]+\\b/, 'g');\n\n // useEffect hook used to apply the theme class to the HTML element\n useEffect(() => {\n if (!isNotRootProvider && isContextRoot) {\n // Remove all the theme classes from the documentElement\n document.documentElement.className.match(themeClass)?.forEach((item) => {\n document.documentElement.classList.remove(item);\n });\n getThemeClassName(theme, screenMode)\n .split(' ')\n .forEach((item) => {\n document.documentElement.classList.add(item);\n });\n }\n }, [isNotRootProvider, isContextRoot, theme, screenMode, themeClass]);\n\n const contextValue = useMemo(() => ({ theme, screenMode }), [screenMode, theme]);\n\n return (\n <ThemeContext.Provider value={contextValue}>\n <ThemedChildren>{children}</ThemedChildren>\n </ThemeContext.Provider>\n );\n};\n"],"names":["baseThemes","extraThemes","screenModes","modernThemes","DEFAULT_BASE_THEME","DEFAULT_SCREEN_MODE","isThemeModern","theme","includes","isExtraTheme","isScreenModeDark","screenMode","getThemeClassName","classes","FALLBACK_VALUES","isNotProduction","window","location","hostname","useTheme","theming","useContext","ThemeContext","console","warn","contextScreenMode","useMemo","isModern","className","ThemedChildren","children","_jsx","createContext","undefined","ThemeProvider","isNotRootProvider","isContextRoot","themeClass","RegExp","useEffect","documentElement","match","forEach","item","document","classList","remove","split","add","contextValue","Provider","value"],"mappings":";;;AAAA;AACO,IAAMA,UAAU,GAAG,CAAC,OAAD,EAAU,UAAV,CAAnB,CAAA;AACA,IAAMC,WAAW,GAAG,CAAC,cAAD,EAAiB,cAAjB,CAApB,CAAA;AACA,IAAMC,WAAW,GAAG,CAAC,OAAD,EAAU,MAAV,CAApB,CAAA;AACA,IAAMC,YAAY,GAAIH,CAAAA,UAAU,CAAC,CAAD,CAAd,CAAsBC,CAAAA,MAAAA,CAAAA,WAAtB,CAAlB,CAAA;AAUA,IAAMG,kBAAkB,GAAG,OAA3B,CAAA;AACA,IAAMC,mBAAmB,GAAG,OAA5B;;ICLMC,aAAa,GAAG,SAAhBA,aAAgB,CAC3BC,KAD2B,EAAA;AAAA,EAAA,OAEFJ,YAAY,CAACK,QAAb,CAAsBD,KAAtB,CAFE,CAAA;AAAA,EAAtB;IAIME,YAAY,GAAG,SAAfA,YAAe,CAACF,KAAD,EAAA;AAAA,EAAA,OAC1BN,WAAW,CAACO,QAAZ,CAAqBD,KAArB,CAD0B,CAAA;AAAA,EAArB;AAGA,IAAMG,gBAAgB,GAAG,SAAnBA,gBAAmB,CAC9BH,KAD8B,EAE9BI,UAF8B,EAAA;EAAA,OAI9BL,aAAa,CAACC,KAAD,CAAb,IAAuCL,WAAW,CAAC,CAAD,CAAX,KAAmBS,UAJ5B,CAAA;AAAA,EAAzB;AAMA,IAAMC,iBAAiB,GA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A;EAcA,IAAMY,YAAY,GAAGvB,OAAO,CAAC,YAAA;IAAA,OAAO;AAAEnB,MAAAA,KAAK,EAALA,KAAF;AAASI,MAAAA,UAAU,EAAVA,UAAAA;KAAhB,CAAA;AAAA,GAAD,EAAgC,CAACA,UAAD,EAAaJ,KAAb,CAAhC,CAA5B,CAAA;AAEA,EAAA,OACEwB,GAACT,CAAAA,YAAY,CAAC4B,QAAd,EAAsB;AAACC,IAAAA,KAAK,EAAEF,YAAR;IAAoBnB,QACxCC,EAAAA,GAAAA,CAACF,cAAD,EAAe;MAAAC,QAAEA,EAAAA,QAAAA;KAAjB,CAAA;AADoB,GAAtB,CADF,CAAA;AAKD;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wise/components-theming",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.3",
|
|
4
4
|
"description": "Provides theming support for the Wise Design system components",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"keywords": [
|
|
@@ -46,5 +46,8 @@
|
|
|
46
46
|
"dist/",
|
|
47
47
|
"src/"
|
|
48
48
|
],
|
|
49
|
-
"
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public"
|
|
51
|
+
},
|
|
52
|
+
"gitHead": "cfd9081163807d17f4e2f894071b4638964599d5"
|
|
50
53
|
}
|
package/src/ThemeProvider.tsx
CHANGED
|
@@ -18,9 +18,10 @@ type ThemeProviderProps = PropsWithChildren<Theming>;
|
|
|
18
18
|
export const ThemeProvider = ({
|
|
19
19
|
theme = DEFAULT_BASE_THEME,
|
|
20
20
|
screenMode = DEFAULT_SCREEN_MODE,
|
|
21
|
+
isNotRootProvider = false,
|
|
21
22
|
children,
|
|
22
23
|
}: ThemeProviderProps) => {
|
|
23
|
-
const
|
|
24
|
+
const isContextRoot = useContext(ThemeContext) === undefined;
|
|
24
25
|
|
|
25
26
|
// RegEx to check for `np-theme-` class name
|
|
26
27
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
@@ -28,7 +29,7 @@ export const ThemeProvider = ({
|
|
|
28
29
|
|
|
29
30
|
// useEffect hook used to apply the theme class to the HTML element
|
|
30
31
|
useEffect(() => {
|
|
31
|
-
if (
|
|
32
|
+
if (!isNotRootProvider && isContextRoot) {
|
|
32
33
|
// Remove all the theme classes from the documentElement
|
|
33
34
|
document.documentElement.className.match(themeClass)?.forEach((item) => {
|
|
34
35
|
document.documentElement.classList.remove(item);
|
|
@@ -39,7 +40,7 @@ export const ThemeProvider = ({
|
|
|
39
40
|
document.documentElement.classList.add(item);
|
|
40
41
|
});
|
|
41
42
|
}
|
|
42
|
-
}, [
|
|
43
|
+
}, [isNotRootProvider, isContextRoot, theme, screenMode, themeClass]);
|
|
43
44
|
|
|
44
45
|
const contextValue = useMemo(() => ({ theme, screenMode }), [screenMode, theme]);
|
|
45
46
|
|
package/src/const.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type { ComponentTheme, BaseTheme, ExtraTheme, ScreenMode, Theming } from './const';
|
|
2
|
-
export {
|
|
2
|
+
export { getThemeClassName, isExtraTheme, isScreenModeDark, isThemeModern } from './helpers';
|
|
3
3
|
|
|
4
4
|
export { ThemeProvider } from './ThemeProvider';
|
|
5
5
|
export { useTheme } from './useTheme';
|