@wise/components-theming 0.6.3-beta-1ecce65b8a.45 → 0.7.1
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.js +16 -2
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/index.js +16 -2
- package/dist/es/index.js.map +1 -1
- package/package.json +2 -2
- package/src/useTheme.spec.tsx +96 -0
- package/src/useTheme.ts +16 -1
package/dist/cjs/index.js
CHANGED
|
@@ -40,10 +40,24 @@ var FALLBACK_VALUES = {
|
|
|
40
40
|
theme: DEFAULT_BASE_THEME,
|
|
41
41
|
screenMode: DEFAULT_SCREEN_MODE
|
|
42
42
|
};
|
|
43
|
+
|
|
44
|
+
var isNotProduction = function isNotProduction() {
|
|
45
|
+
try {
|
|
46
|
+
return ['localhost', 'dev-wi.se'].includes(window.location.hostname);
|
|
47
|
+
} catch (_unused) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
43
52
|
var useTheme = function useTheme() {
|
|
44
|
-
var
|
|
53
|
+
var theming = react.useContext(ThemeContext);
|
|
54
|
+
|
|
55
|
+
if (!theming && isNotProduction()) {
|
|
56
|
+
// eslint-disable-next-line no-console
|
|
57
|
+
console.warn('Call to useTheme outside a ThemeProvider');
|
|
58
|
+
}
|
|
45
59
|
|
|
46
|
-
var _ref =
|
|
60
|
+
var _ref = theming !== null && theming !== void 0 ? theming : FALLBACK_VALUES,
|
|
47
61
|
theme = _ref.theme,
|
|
48
62
|
contextScreenMode = _ref.screenMode;
|
|
49
63
|
|
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 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\nexport const useTheme = (): ThemeHookValue => {\n const { theme, screenMode: contextScreenMode } = useContext(ThemeContext) ?? 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","useTheme","useContext","ThemeContext","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,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OAAO;AAAEZ,MAAAA,KAAK,EAALA,KAAF;AAASI,MAAAA,UAAU,EAAVA,UAAAA;KAAhB,CAAA;AAAA,GAAD,EAAgC,CAACA,UAAD,EAAaJ,KAAb,CAAhC,CAA5B,CAAA;AAEA,EAAA,OACEiB,cAACP,CAAAA,YAAY,CAAC0B,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;;;;;;;;;"}
|
|
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,GAAG,SAApBA,iBAAoB,CAC/BL,KAD+B,EAE/BI,UAF+B,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;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.js
CHANGED
|
@@ -38,10 +38,24 @@ var FALLBACK_VALUES = {
|
|
|
38
38
|
theme: DEFAULT_BASE_THEME,
|
|
39
39
|
screenMode: DEFAULT_SCREEN_MODE
|
|
40
40
|
};
|
|
41
|
+
|
|
42
|
+
var isNotProduction = function isNotProduction() {
|
|
43
|
+
try {
|
|
44
|
+
return ['localhost', 'dev-wi.se'].includes(window.location.hostname);
|
|
45
|
+
} catch (_unused) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
41
50
|
var useTheme = function useTheme() {
|
|
42
|
-
var
|
|
51
|
+
var theming = useContext(ThemeContext);
|
|
52
|
+
|
|
53
|
+
if (!theming && isNotProduction()) {
|
|
54
|
+
// eslint-disable-next-line no-console
|
|
55
|
+
console.warn('Call to useTheme outside a ThemeProvider');
|
|
56
|
+
}
|
|
43
57
|
|
|
44
|
-
var _ref =
|
|
58
|
+
var _ref = theming !== null && theming !== void 0 ? theming : FALLBACK_VALUES,
|
|
45
59
|
theme = _ref.theme,
|
|
46
60
|
contextScreenMode = _ref.screenMode;
|
|
47
61
|
|
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 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\nexport const useTheme = (): ThemeHookValue => {\n const { theme, screenMode: contextScreenMode } = useContext(ThemeContext) ?? 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","useTheme","useContext","ThemeContext","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,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,UAA1C,EAAsDmB,UAAtD,CAZM,CAAT,CAAA;EAcA,IAAMY,YAAY,GAAGvB,OAAO,CAAC,YAAA;IAAA,OAAO;AAAEZ,MAAAA,KAAK,EAALA,KAAF;AAASI,MAAAA,UAAU,EAAVA,UAAAA;KAAhB,CAAA;AAAA,GAAD,EAAgC,CAACA,UAAD,EAAaJ,KAAb,CAAhC,CAA5B,CAAA;AAEA,EAAA,OACEiB,GAACP,CAAAA,YAAY,CAAC0B,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;;;;"}
|
|
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;A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1C,EAAsD0B,UAAtD,CAZM,CAAT,CAAA;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.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "Provides theming support for the Wise Design system components",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"keywords": [
|
|
@@ -46,5 +46,5 @@
|
|
|
46
46
|
"dist/",
|
|
47
47
|
"src/"
|
|
48
48
|
],
|
|
49
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "45678ae290c1f64b73358f6b4ecc27ca8d8720a3"
|
|
50
50
|
}
|
package/src/useTheme.spec.tsx
CHANGED
|
@@ -6,6 +6,8 @@ import { useTheme } from './useTheme';
|
|
|
6
6
|
|
|
7
7
|
describe('useTheme', () => {
|
|
8
8
|
it('returns default light theme', () => {
|
|
9
|
+
jest.spyOn(console, 'warn').mockImplementation();
|
|
10
|
+
|
|
9
11
|
const {
|
|
10
12
|
result: { current },
|
|
11
13
|
} = renderHook(() => useTheme());
|
|
@@ -17,6 +19,8 @@ describe('useTheme', () => {
|
|
|
17
19
|
isScreenModeDark: false,
|
|
18
20
|
className: 'np-theme-light',
|
|
19
21
|
});
|
|
22
|
+
|
|
23
|
+
jest.clearAllMocks();
|
|
20
24
|
});
|
|
21
25
|
|
|
22
26
|
it('returns personal theme', () => {
|
|
@@ -115,4 +119,96 @@ describe('useTheme', () => {
|
|
|
115
119
|
className: 'np-theme-personal np-theme-personal--dark',
|
|
116
120
|
});
|
|
117
121
|
});
|
|
122
|
+
|
|
123
|
+
it('warns when used outside a theme provider on staging or localhost', () => {
|
|
124
|
+
jest.spyOn(console, 'warn').mockImplementation();
|
|
125
|
+
|
|
126
|
+
Object.defineProperty(window, 'location', {
|
|
127
|
+
value: {
|
|
128
|
+
hostname: 'wise.com',
|
|
129
|
+
},
|
|
130
|
+
writable: true,
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
const {
|
|
134
|
+
result: { current: productionValue },
|
|
135
|
+
} = renderHook(() => useTheme());
|
|
136
|
+
|
|
137
|
+
expect(productionValue).toStrictEqual({
|
|
138
|
+
theme: DEFAULT_BASE_THEME,
|
|
139
|
+
screenMode: DEFAULT_SCREEN_MODE,
|
|
140
|
+
isModern: false,
|
|
141
|
+
isScreenModeDark: false,
|
|
142
|
+
className: 'np-theme-light',
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
// eslint-disable-next-line no-console
|
|
146
|
+
expect(console.warn).not.toHaveBeenCalled();
|
|
147
|
+
|
|
148
|
+
Object.defineProperty(window, 'location', {
|
|
149
|
+
value: {
|
|
150
|
+
hostname: 'dev-wi.se',
|
|
151
|
+
},
|
|
152
|
+
writable: true,
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
const {
|
|
156
|
+
result: { current: stagingValue },
|
|
157
|
+
} = renderHook(() => useTheme());
|
|
158
|
+
|
|
159
|
+
expect(stagingValue).toStrictEqual({
|
|
160
|
+
theme: DEFAULT_BASE_THEME,
|
|
161
|
+
screenMode: DEFAULT_SCREEN_MODE,
|
|
162
|
+
isModern: false,
|
|
163
|
+
isScreenModeDark: false,
|
|
164
|
+
className: 'np-theme-light',
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
// eslint-disable-next-line no-console
|
|
168
|
+
expect(console.warn).toHaveBeenCalledTimes(1);
|
|
169
|
+
|
|
170
|
+
Object.defineProperty(window, 'location', {
|
|
171
|
+
value: {
|
|
172
|
+
hostname: 'localhost',
|
|
173
|
+
},
|
|
174
|
+
writable: true,
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
const {
|
|
178
|
+
result: { current: localhostValue },
|
|
179
|
+
} = renderHook(() => useTheme());
|
|
180
|
+
|
|
181
|
+
expect(localhostValue).toStrictEqual({
|
|
182
|
+
theme: DEFAULT_BASE_THEME,
|
|
183
|
+
screenMode: DEFAULT_SCREEN_MODE,
|
|
184
|
+
isModern: false,
|
|
185
|
+
isScreenModeDark: false,
|
|
186
|
+
className: 'np-theme-light',
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
// eslint-disable-next-line no-console
|
|
190
|
+
expect(console.warn).toHaveBeenCalledTimes(2);
|
|
191
|
+
|
|
192
|
+
Object.defineProperty(window, 'location', {
|
|
193
|
+
value: undefined,
|
|
194
|
+
writable: true,
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
const {
|
|
198
|
+
result: { current: noHostnameValue },
|
|
199
|
+
} = renderHook(() => useTheme());
|
|
200
|
+
|
|
201
|
+
expect(noHostnameValue).toStrictEqual({
|
|
202
|
+
theme: DEFAULT_BASE_THEME,
|
|
203
|
+
screenMode: DEFAULT_SCREEN_MODE,
|
|
204
|
+
isModern: false,
|
|
205
|
+
isScreenModeDark: false,
|
|
206
|
+
className: 'np-theme-light',
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
// eslint-disable-next-line no-console
|
|
210
|
+
expect(console.warn).toHaveBeenCalledTimes(2);
|
|
211
|
+
|
|
212
|
+
jest.clearAllMocks();
|
|
213
|
+
});
|
|
118
214
|
});
|
package/src/useTheme.ts
CHANGED
|
@@ -18,8 +18,23 @@ const FALLBACK_VALUES = {
|
|
|
18
18
|
screenMode: DEFAULT_SCREEN_MODE,
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
+
const isNotProduction = () => {
|
|
22
|
+
try {
|
|
23
|
+
return ['localhost', 'dev-wi.se'].includes(window.location.hostname);
|
|
24
|
+
} catch {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
21
29
|
export const useTheme = (): ThemeHookValue => {
|
|
22
|
-
const
|
|
30
|
+
const theming = useContext(ThemeContext);
|
|
31
|
+
|
|
32
|
+
if (!theming && isNotProduction()) {
|
|
33
|
+
// eslint-disable-next-line no-console
|
|
34
|
+
console.warn('Call to useTheme outside a ThemeProvider');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const { theme, screenMode: contextScreenMode } = theming ?? FALLBACK_VALUES;
|
|
23
38
|
|
|
24
39
|
const screenMode = theme === DEFAULT_BASE_THEME ? DEFAULT_SCREEN_MODE : contextScreenMode;
|
|
25
40
|
|