@wise/components-theming 1.3.0 → 1.5.0
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/ThemeProvider.d.ts +1 -1
- package/dist/ThemeProvider.d.ts.map +1 -1
- package/dist/ThemeProvider.js +47 -0
- package/dist/ThemeProvider.js.map +1 -0
- package/dist/ThemeProvider.mjs +44 -0
- package/dist/ThemeProvider.mjs.map +1 -0
- package/dist/ThemedChildren.js +21 -0
- package/dist/ThemedChildren.js.map +1 -0
- package/dist/ThemedChildren.mjs +19 -0
- package/dist/ThemedChildren.mjs.map +1 -0
- package/dist/const.js +17 -0
- package/dist/const.js.map +1 -0
- package/dist/const.mjs +10 -0
- package/dist/const.mjs.map +1 -0
- package/dist/helpers.d.ts +6 -6
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js +27 -0
- package/dist/helpers.js.map +1 -0
- package/dist/helpers.mjs +21 -0
- package/dist/helpers.mjs.map +1 -0
- package/dist/index.js +10 -118
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3 -111
- package/dist/index.mjs.map +1 -1
- package/dist/useTheme.js +41 -0
- package/dist/useTheme.js.map +1 -0
- package/dist/useTheme.mjs +39 -0
- package/dist/useTheme.mjs.map +1 -0
- package/package.json +14 -15
- package/src/ThemedChildren.tsx +2 -2
package/dist/ThemeProvider.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { PropsWithChildren } from 'react';
|
|
2
2
|
import type { ScreenMode, Theming } from './const';
|
|
3
3
|
export declare const ThemeContext: import("react").Context<{
|
|
4
|
-
theme: NonNullable<Theming[
|
|
4
|
+
theme: NonNullable<Theming["theme"]>;
|
|
5
5
|
screenMode: ScreenMode;
|
|
6
6
|
} | undefined>;
|
|
7
7
|
type ThemeProviderProps = PropsWithChildren<Theming> & {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ThemeProvider.d.ts","sourceRoot":"","sources":["../src/ThemeProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAiB,iBAAiB,EAAkC,MAAM,OAAO,CAAC;AAGzF,OAAO,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAInD,eAAO,MAAM,YAAY;WAEZ,
|
|
1
|
+
{"version":3,"file":"ThemeProvider.d.ts","sourceRoot":"","sources":["../src/ThemeProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAiB,iBAAiB,EAAkC,MAAM,OAAO,CAAC;AAGzF,OAAO,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAInD,eAAO,MAAM,YAAY;WAEZ,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;gBACxB,UAAU;cAGhB,CAAC;AAEb,KAAK,kBAAkB,GAAG,iBAAiB,CAAC,OAAO,CAAC,GAAG;IAAE,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAK9E,eAAO,MAAM,aAAa,mEAMvB,kBAAkB,gCAyBpB,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
var ThemedChildren = require('./ThemedChildren.js');
|
|
5
|
+
var _const = require('./const.js');
|
|
6
|
+
var helpers = require('./helpers.js');
|
|
7
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
8
|
+
|
|
9
|
+
const ThemeContext = /*#__PURE__*/react.createContext(undefined);
|
|
10
|
+
// RegEx to check for `np-theme-` class name
|
|
11
|
+
const themeClass = /\bnp-theme-[a-z-]+\b/g;
|
|
12
|
+
const ThemeProvider = ({
|
|
13
|
+
theme = _const.DEFAULT_BASE_THEME,
|
|
14
|
+
screenMode = _const.DEFAULT_SCREEN_MODE,
|
|
15
|
+
isNotRootProvider = false,
|
|
16
|
+
children,
|
|
17
|
+
className = undefined
|
|
18
|
+
}) => {
|
|
19
|
+
const isContextRoot = react.useContext(ThemeContext) === undefined;
|
|
20
|
+
// useEffect hook used to apply the theme class to the HTML element
|
|
21
|
+
react.useEffect(() => {
|
|
22
|
+
if (!isNotRootProvider && isContextRoot) {
|
|
23
|
+
// Remove all the theme classes from the documentElement
|
|
24
|
+
document.documentElement.className.match(themeClass)?.forEach(item => {
|
|
25
|
+
document.documentElement.classList.remove(item);
|
|
26
|
+
});
|
|
27
|
+
helpers.getThemeClassName(theme, screenMode).split(' ').forEach(item => {
|
|
28
|
+
document.documentElement.classList.add(item);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}, [isNotRootProvider, isContextRoot, theme, screenMode]);
|
|
32
|
+
const contextValue = react.useMemo(() => ({
|
|
33
|
+
theme,
|
|
34
|
+
screenMode
|
|
35
|
+
}), [screenMode, theme]);
|
|
36
|
+
return /*#__PURE__*/jsxRuntime.jsx(ThemeContext.Provider, {
|
|
37
|
+
value: contextValue,
|
|
38
|
+
children: /*#__PURE__*/jsxRuntime.jsx(ThemedChildren.ThemedChildren, {
|
|
39
|
+
className: className,
|
|
40
|
+
children: children
|
|
41
|
+
})
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
exports.ThemeContext = ThemeContext;
|
|
46
|
+
exports.ThemeProvider = ThemeProvider;
|
|
47
|
+
//# sourceMappingURL=ThemeProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ThemeProvider.js","sources":["../src/ThemeProvider.tsx"],"sourcesContent":["import { createContext, PropsWithChildren, useContext, useEffect, useMemo } from 'react';\n\nimport { ThemedChildren } from './ThemedChildren';\nimport type { 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: NonNullable<Theming['theme']>;\n screenMode: ScreenMode;\n }\n | undefined\n>(undefined);\n\ntype ThemeProviderProps = PropsWithChildren<Theming> & { className?: string };\n\n// RegEx to check for `np-theme-` class name\nconst themeClass = /\\bnp-theme-[a-z-]+\\b/g;\n\nexport const ThemeProvider = ({\n theme = DEFAULT_BASE_THEME,\n screenMode = DEFAULT_SCREEN_MODE,\n isNotRootProvider = false,\n children,\n className = undefined,\n}: ThemeProviderProps) => {\n const isContextRoot = useContext(ThemeContext) === undefined;\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]);\n\n const contextValue = useMemo(() => ({ theme, screenMode }), [screenMode, theme]);\n\n return (\n <ThemeContext.Provider value={contextValue}>\n <ThemedChildren className={className}>{children}</ThemedChildren>\n </ThemeContext.Provider>\n );\n};\n"],"names":["ThemeContext","createContext","undefined","themeClass","ThemeProvider","theme","DEFAULT_BASE_THEME","screenMode","DEFAULT_SCREEN_MODE","isNotRootProvider","children","className","isContextRoot","useContext","useEffect","document","documentElement","match","forEach","item","classList","remove","getThemeClassName","split","add","contextValue","useMemo","_jsx","Provider","value","ThemedChildren"],"mappings":";;;;;;;;MAOaA,YAAY,gBAAGC,mBAAa,CAMvCC,SAAS,EAAC;AAIZ;AACA,MAAMC,UAAU,GAAG,uBAAuB,CAAA;AAEnC,MAAMC,aAAa,GAAGA,CAAC;AAC5BC,EAAAA,KAAK,GAAGC,yBAAkB;AAC1BC,EAAAA,UAAU,GAAGC,0BAAmB;AAChCC,EAAAA,iBAAiB,GAAG,KAAK;EACzBC,QAAQ;AACRC,EAAAA,SAAS,GAAGT,SAAAA;AACO,CAAA,KAAI;AACvB,EAAA,MAAMU,aAAa,GAAGC,gBAAU,CAACb,YAAY,CAAC,KAAKE,SAAS,CAAA;AAE5D;AACAY,EAAAA,eAAS,CAAC,MAAK;AACb,IAAA,IAAI,CAACL,iBAAiB,IAAIG,aAAa,EAAE;AACvC;AACAG,MAAAA,QAAQ,CAACC,eAAe,CAACL,SAAS,CAACM,KAAK,CAACd,UAAU,CAAC,EAAEe,OAAO,CAAEC,IAAI,IAAI;QACrEJ,QAAQ,CAACC,eAAe,CAACI,SAAS,CAACC,MAAM,CAACF,IAAI,CAAC,CAAA;AACjD,OAAC,CAAC,CAAA;AACFG,MAAAA,yBAAiB,CAACjB,KAAK,EAAEE,UAAU,CAAC,CACjCgB,KAAK,CAAC,GAAG,CAAC,CACVL,OAAO,CAAEC,IAAI,IAAI;QAChBJ,QAAQ,CAACC,eAAe,CAACI,SAAS,CAACI,GAAG,CAACL,IAAI,CAAC,CAAA;AAC9C,OAAC,CAAC,CAAA;AACN,KAAA;GACD,EAAE,CAACV,iBAAiB,EAAEG,aAAa,EAAEP,KAAK,EAAEE,UAAU,CAAC,CAAC,CAAA;AAEzD,EAAA,MAAMkB,YAAY,GAAGC,aAAO,CAAC,OAAO;IAAErB,KAAK;AAAEE,IAAAA,UAAAA;AAAU,GAAE,CAAC,EAAE,CAACA,UAAU,EAAEF,KAAK,CAAC,CAAC,CAAA;AAEhF,EAAA,oBACEsB,cAAA,CAAC3B,YAAY,CAAC4B,QAAQ,EAAA;AAACC,IAAAA,KAAK,EAAEJ,YAAa;IAAAf,QAAA,eACzCiB,cAAA,CAACG,6BAAc,EAAA;AAACnB,MAAAA,SAAS,EAAEA,SAAU;AAAAD,MAAAA,QAAA,EAAEA,QAAAA;KAAyB,CAAA;AAClE,GAAuB,CAAC,CAAA;AAE5B;;;;;"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { createContext, useContext, useEffect, useMemo } from 'react';
|
|
2
|
+
import { ThemedChildren } from './ThemedChildren.mjs';
|
|
3
|
+
import { DEFAULT_BASE_THEME, DEFAULT_SCREEN_MODE } from './const.mjs';
|
|
4
|
+
import { getThemeClassName } from './helpers.mjs';
|
|
5
|
+
import { jsx } from 'react/jsx-runtime';
|
|
6
|
+
|
|
7
|
+
const ThemeContext = /*#__PURE__*/createContext(undefined);
|
|
8
|
+
// RegEx to check for `np-theme-` class name
|
|
9
|
+
const themeClass = /\bnp-theme-[a-z-]+\b/g;
|
|
10
|
+
const ThemeProvider = ({
|
|
11
|
+
theme = DEFAULT_BASE_THEME,
|
|
12
|
+
screenMode = DEFAULT_SCREEN_MODE,
|
|
13
|
+
isNotRootProvider = false,
|
|
14
|
+
children,
|
|
15
|
+
className = undefined
|
|
16
|
+
}) => {
|
|
17
|
+
const isContextRoot = useContext(ThemeContext) === undefined;
|
|
18
|
+
// useEffect hook used to apply the theme class to the HTML element
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
if (!isNotRootProvider && isContextRoot) {
|
|
21
|
+
// Remove all the theme classes from the documentElement
|
|
22
|
+
document.documentElement.className.match(themeClass)?.forEach(item => {
|
|
23
|
+
document.documentElement.classList.remove(item);
|
|
24
|
+
});
|
|
25
|
+
getThemeClassName(theme, screenMode).split(' ').forEach(item => {
|
|
26
|
+
document.documentElement.classList.add(item);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}, [isNotRootProvider, isContextRoot, theme, screenMode]);
|
|
30
|
+
const contextValue = useMemo(() => ({
|
|
31
|
+
theme,
|
|
32
|
+
screenMode
|
|
33
|
+
}), [screenMode, theme]);
|
|
34
|
+
return /*#__PURE__*/jsx(ThemeContext.Provider, {
|
|
35
|
+
value: contextValue,
|
|
36
|
+
children: /*#__PURE__*/jsx(ThemedChildren, {
|
|
37
|
+
className: className,
|
|
38
|
+
children: children
|
|
39
|
+
})
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export { ThemeContext, ThemeProvider };
|
|
44
|
+
//# sourceMappingURL=ThemeProvider.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ThemeProvider.mjs","sources":["../src/ThemeProvider.tsx"],"sourcesContent":["import { createContext, PropsWithChildren, useContext, useEffect, useMemo } from 'react';\n\nimport { ThemedChildren } from './ThemedChildren';\nimport type { 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: NonNullable<Theming['theme']>;\n screenMode: ScreenMode;\n }\n | undefined\n>(undefined);\n\ntype ThemeProviderProps = PropsWithChildren<Theming> & { className?: string };\n\n// RegEx to check for `np-theme-` class name\nconst themeClass = /\\bnp-theme-[a-z-]+\\b/g;\n\nexport const ThemeProvider = ({\n theme = DEFAULT_BASE_THEME,\n screenMode = DEFAULT_SCREEN_MODE,\n isNotRootProvider = false,\n children,\n className = undefined,\n}: ThemeProviderProps) => {\n const isContextRoot = useContext(ThemeContext) === undefined;\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]);\n\n const contextValue = useMemo(() => ({ theme, screenMode }), [screenMode, theme]);\n\n return (\n <ThemeContext.Provider value={contextValue}>\n <ThemedChildren className={className}>{children}</ThemedChildren>\n </ThemeContext.Provider>\n );\n};\n"],"names":["ThemeContext","createContext","undefined","themeClass","ThemeProvider","theme","DEFAULT_BASE_THEME","screenMode","DEFAULT_SCREEN_MODE","isNotRootProvider","children","className","isContextRoot","useContext","useEffect","document","documentElement","match","forEach","item","classList","remove","getThemeClassName","split","add","contextValue","useMemo","_jsx","Provider","value","ThemedChildren"],"mappings":";;;;;;MAOaA,YAAY,gBAAGC,aAAa,CAMvCC,SAAS,EAAC;AAIZ;AACA,MAAMC,UAAU,GAAG,uBAAuB,CAAA;AAEnC,MAAMC,aAAa,GAAGA,CAA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A;KAAyB,CAAA;AAClE,GAAuB,CAAC,CAAA;AAE5B;;;;"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var clsx = require('clsx');
|
|
4
|
+
var useTheme = require('./useTheme.js');
|
|
5
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
6
|
+
|
|
7
|
+
const ThemedChildren = ({
|
|
8
|
+
className = undefined,
|
|
9
|
+
children
|
|
10
|
+
}) => {
|
|
11
|
+
const {
|
|
12
|
+
className: themeClass
|
|
13
|
+
} = useTheme.useTheme();
|
|
14
|
+
return /*#__PURE__*/jsxRuntime.jsx("div", {
|
|
15
|
+
className: clsx.clsx(themeClass, className),
|
|
16
|
+
children: children
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
exports.ThemedChildren = ThemedChildren;
|
|
21
|
+
//# sourceMappingURL=ThemedChildren.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ThemedChildren.js","sources":["../src/ThemedChildren.tsx"],"sourcesContent":["import { clsx } from 'clsx';\nimport { ReactNode } from 'react';\n\nimport { useTheme } from './useTheme';\n\ntype ThemedChildrenProps = { className?: string; children: ReactNode };\n\nexport const ThemedChildren = ({ className = undefined, children }: ThemedChildrenProps) => {\n const { className: themeClass } = useTheme();\n\n return <div className={clsx(themeClass, className)}>{children}</div>;\n};\n"],"names":["ThemedChildren","className","undefined","children","themeClass","useTheme","_jsx","clsx"],"mappings":";;;;;;AAOO,MAAMA,cAAc,GAAGA,CAAC;AAAEC,EAAAA,SAAS,GAAGC,SAAS;AAAEC,EAAAA,QAAAA;AAAQ,CAAuB,KAAI;EACzF,MAAM;AAAEF,IAAAA,SAAS,EAAEG,UAAAA;GAAY,GAAGC,iBAAQ,EAAE,CAAA;AAE5C,EAAA,oBAAOC,cAAA,CAAA,KAAA,EAAA;AAAKL,IAAAA,SAAS,EAAEM,SAAI,CAACH,UAAU,EAAEH,SAAS,CAAE;AAAAE,IAAAA,QAAA,EAAEA,QAAAA;AAAQ,GAAM,CAAC,CAAA;AACtE;;;;"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { clsx } from 'clsx';
|
|
2
|
+
import { useTheme } from './useTheme.mjs';
|
|
3
|
+
import { jsx } from 'react/jsx-runtime';
|
|
4
|
+
|
|
5
|
+
const ThemedChildren = ({
|
|
6
|
+
className = undefined,
|
|
7
|
+
children
|
|
8
|
+
}) => {
|
|
9
|
+
const {
|
|
10
|
+
className: themeClass
|
|
11
|
+
} = useTheme();
|
|
12
|
+
return /*#__PURE__*/jsx("div", {
|
|
13
|
+
className: clsx(themeClass, className),
|
|
14
|
+
children: children
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export { ThemedChildren };
|
|
19
|
+
//# sourceMappingURL=ThemedChildren.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ThemedChildren.mjs","sources":["../src/ThemedChildren.tsx"],"sourcesContent":["import { clsx } from 'clsx';\nimport { ReactNode } from 'react';\n\nimport { useTheme } from './useTheme';\n\ntype ThemedChildrenProps = { className?: string; children: ReactNode };\n\nexport const ThemedChildren = ({ className = undefined, children }: ThemedChildrenProps) => {\n const { className: themeClass } = useTheme();\n\n return <div className={clsx(themeClass, className)}>{children}</div>;\n};\n"],"names":["ThemedChildren","className","undefined","children","themeClass","useTheme","_jsx","clsx"],"mappings":";;;;AAOO,MAAMA,cAAc,GAAGA,CAAC;AAAEC,EAAAA,SAAS,GAAGC,SAAS;AAAEC,EAAAA,QAAAA;AAAQ,CAAuB,KAAI;EACzF,MAAM;AAAEF,IAAAA,SAAS,EAAEG,UAAAA;GAAY,GAAGC,QAAQ,EAAE,CAAA;AAE5C,EAAA,oBAAOC,GAAA,CAAA,KAAA,EAAA;AAAKL,IAAAA,SAAS,EAAEM,IAAI,CAACH,UAAU,EAAEH,SAAS,CAAE;AAAAE,IAAAA,QAAA,EAAEA,QAAAA;AAAQ,GAAM,CAAC,CAAA;AACtE;;;;"}
|
package/dist/const.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// TODO: Change 'light' with 'legacy' in the future
|
|
4
|
+
const baseThemes = ['light', 'personal'];
|
|
5
|
+
const extraThemes = ['forest-green', 'bright-green'];
|
|
6
|
+
const screenModes = ['light', 'dark'];
|
|
7
|
+
const modernThemes = [baseThemes[1], ...extraThemes];
|
|
8
|
+
const DEFAULT_BASE_THEME = 'light';
|
|
9
|
+
const DEFAULT_SCREEN_MODE = 'light';
|
|
10
|
+
|
|
11
|
+
exports.DEFAULT_BASE_THEME = DEFAULT_BASE_THEME;
|
|
12
|
+
exports.DEFAULT_SCREEN_MODE = DEFAULT_SCREEN_MODE;
|
|
13
|
+
exports.baseThemes = baseThemes;
|
|
14
|
+
exports.extraThemes = extraThemes;
|
|
15
|
+
exports.modernThemes = modernThemes;
|
|
16
|
+
exports.screenModes = screenModes;
|
|
17
|
+
//# sourceMappingURL=const.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"const.js","sources":["../src/const.ts"],"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;\nexport const platformThemes = ['platform', 'platform--forest-green'] 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 ForestGreenTheme = (typeof extraThemes)[0];\nexport type ScreenMode = (typeof screenModes)[number];\nexport type ScreenModeDark = (typeof screenModes)[1];\nexport type PlatformTheme = (typeof platformThemes)[0];\nexport type PlatformForestGreenTheme = (typeof platformThemes)[1];\n\nexport const DEFAULT_BASE_THEME = 'light';\nexport const DEFAULT_SCREEN_MODE = 'light';\n\nexport type Theming = {\n theme?: ComponentTheme | BaseTheme | ExtraTheme | PlatformTheme | PlatformForestGreenTheme;\n screenMode?: ScreenMode;\n isNotRootProvider?: boolean | undefined;\n};\n"],"names":["baseThemes","extraThemes","screenModes","modernThemes","DEFAULT_BASE_THEME","DEFAULT_SCREEN_MODE"],"mappings":";;AAAA;MACaA,UAAU,GAAG,CAAC,OAAO,EAAE,UAAU,EAAU;MAC3CC,WAAW,GAAG,CAAC,cAAc,EAAE,cAAc,EAAU;MACvDC,WAAW,GAAG,CAAC,OAAO,EAAE,MAAM,EAAU;AAC9C,MAAMC,YAAY,GAAG,CAACH,UAAU,CAAC,CAAC,CAAC,EAAE,GAAGC,WAAW,EAAU;AAc7D,MAAMG,kBAAkB,GAAG,QAAO;AAClC,MAAMC,mBAAmB,GAAG;;;;;;;;;"}
|
package/dist/const.mjs
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// TODO: Change 'light' with 'legacy' in the future
|
|
2
|
+
const baseThemes = ['light', 'personal'];
|
|
3
|
+
const extraThemes = ['forest-green', 'bright-green'];
|
|
4
|
+
const screenModes = ['light', 'dark'];
|
|
5
|
+
const modernThemes = [baseThemes[1], ...extraThemes];
|
|
6
|
+
const DEFAULT_BASE_THEME = 'light';
|
|
7
|
+
const DEFAULT_SCREEN_MODE = 'light';
|
|
8
|
+
|
|
9
|
+
export { DEFAULT_BASE_THEME, DEFAULT_SCREEN_MODE, baseThemes, extraThemes, modernThemes, screenModes };
|
|
10
|
+
//# sourceMappingURL=const.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"const.mjs","sources":["../src/const.ts"],"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;\nexport const platformThemes = ['platform', 'platform--forest-green'] 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 ForestGreenTheme = (typeof extraThemes)[0];\nexport type ScreenMode = (typeof screenModes)[number];\nexport type ScreenModeDark = (typeof screenModes)[1];\nexport type PlatformTheme = (typeof platformThemes)[0];\nexport type PlatformForestGreenTheme = (typeof platformThemes)[1];\n\nexport const DEFAULT_BASE_THEME = 'light';\nexport const DEFAULT_SCREEN_MODE = 'light';\n\nexport type Theming = {\n theme?: ComponentTheme | BaseTheme | ExtraTheme | PlatformTheme | PlatformForestGreenTheme;\n screenMode?: ScreenMode;\n isNotRootProvider?: boolean | undefined;\n};\n"],"names":["baseThemes","extraThemes","screenModes","modernThemes","DEFAULT_BASE_THEME","DEFAULT_SCREEN_MODE"],"mappings":"AAAA;MACaA,UAAU,GAAG,CAAC,OAAO,EAAE,UAAU,EAAU;MAC3CC,WAAW,GAAG,CAAC,cAAc,EAAE,cAAc,EAAU;MACvDC,WAAW,GAAG,CAAC,OAAO,EAAE,MAAM,EAAU;AAC9C,MAAMC,YAAY,GAAG,CAACH,UAAU,CAAC,CAAC,CAAC,EAAE,GAAGC,WAAW,EAAU;AAc7D,MAAMG,kBAAkB,GAAG,QAAO;AAClC,MAAMC,mBAAmB,GAAG;;;;"}
|
package/dist/helpers.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { ScreenMode, Theming } from './const';
|
|
2
|
-
export declare const isThemeModern: (theme: NonNullable<Theming[
|
|
3
|
-
export declare const isExtraTheme: (theme: NonNullable<Theming[
|
|
4
|
-
export declare const isForestGreenTheme: (theme: NonNullable<Theming[
|
|
5
|
-
export declare const isScreenModeDark: (theme: NonNullable<Theming[
|
|
6
|
-
export declare const getThemeClassName: (theme: NonNullable<Theming[
|
|
1
|
+
import type { ModernTheme, ExtraTheme, ForestGreenTheme, ScreenMode, ScreenModeDark, Theming } from './const';
|
|
2
|
+
export declare const isThemeModern: (theme: NonNullable<Theming["theme"]>) => theme is ModernTheme;
|
|
3
|
+
export declare const isExtraTheme: (theme: NonNullable<Theming["theme"]>) => theme is ExtraTheme;
|
|
4
|
+
export declare const isForestGreenTheme: (theme: NonNullable<Theming["theme"]>) => theme is ForestGreenTheme;
|
|
5
|
+
export declare const isScreenModeDark: (theme: NonNullable<Theming["theme"]>, screenMode: ScreenMode) => screenMode is ScreenModeDark;
|
|
6
|
+
export declare const getThemeClassName: (theme: NonNullable<Theming["theme"]>, screenMode: ScreenMode) => string;
|
|
7
7
|
//# sourceMappingURL=helpers.d.ts.map
|
package/dist/helpers.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,UAAU,EACV,gBAAgB,EAChB,UAAU,EACV,cAAc,EACd,OAAO,EACR,MAAM,SAAS,CAAC;AAGjB,eAAO,MAAM,aAAa,UAAW,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,KAAG,KAAK,IAAI,WACjC,CAAC;AAE9C,eAAO,MAAM,YAAY,UAAW,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,KAAG,KAAK,IAAI,UAClC,CAAC;AAE5C,eAAO,MAAM,kBAAkB,UACtB,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,KACnC,KAAK,IAAI,gBAA4C,CAAC;AAEzD,eAAO,MAAM,gBAAgB,UACpB,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,cACxB,UAAU,KACrB,UAAU,IAAI,cACqD,CAAC;AAEvE,eAAO,MAAM,iBAAiB,UAAW,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,cAAc,UAAU,WAc7F,CAAC"}
|
package/dist/helpers.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _const = require('./const.js');
|
|
4
|
+
|
|
5
|
+
const isThemeModern = theme => _const.modernThemes.includes(theme);
|
|
6
|
+
const isExtraTheme = theme => _const.extraThemes.includes(theme);
|
|
7
|
+
const isForestGreenTheme = theme => theme === _const.extraThemes[0];
|
|
8
|
+
const isScreenModeDark = (theme, screenMode) => isThemeModern(theme) && _const.screenModes[1] === screenMode;
|
|
9
|
+
const getThemeClassName = (theme, screenMode) => {
|
|
10
|
+
if (!isThemeModern(theme)) {
|
|
11
|
+
return `np-theme-${theme || 'personal'}`;
|
|
12
|
+
}
|
|
13
|
+
let classes = `np-theme-personal`;
|
|
14
|
+
if (isExtraTheme(theme)) {
|
|
15
|
+
classes += ` ${classes}--${theme}`;
|
|
16
|
+
} else if (_const.screenModes[1] === screenMode) {
|
|
17
|
+
classes += ` ${classes}--${screenMode}`;
|
|
18
|
+
}
|
|
19
|
+
return classes;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
exports.getThemeClassName = getThemeClassName;
|
|
23
|
+
exports.isExtraTheme = isExtraTheme;
|
|
24
|
+
exports.isForestGreenTheme = isForestGreenTheme;
|
|
25
|
+
exports.isScreenModeDark = isScreenModeDark;
|
|
26
|
+
exports.isThemeModern = isThemeModern;
|
|
27
|
+
//# sourceMappingURL=helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.js","sources":["../src/helpers.ts"],"sourcesContent":["import type {\n ModernTheme,\n ExtraTheme,\n ForestGreenTheme,\n ScreenMode,\n ScreenModeDark,\n Theming,\n} from './const';\nimport { extraThemes, screenModes, modernThemes } from './const';\n\nexport const isThemeModern = (theme: NonNullable<Theming['theme']>): theme is ModernTheme =>\n modernThemes.includes(theme as ModernTheme);\n\nexport const isExtraTheme = (theme: NonNullable<Theming['theme']>): theme is ExtraTheme =>\n extraThemes.includes(theme as ExtraTheme);\n\nexport const isForestGreenTheme = (\n theme: NonNullable<Theming['theme']>,\n): theme is ForestGreenTheme => theme === extraThemes[0];\n\nexport const isScreenModeDark = (\n theme: NonNullable<Theming['theme']>,\n screenMode: ScreenMode,\n): screenMode is ScreenModeDark =>\n isThemeModern(theme as ModernTheme) && screenModes[1] === screenMode;\n\nexport const getThemeClassName = (theme: NonNullable<Theming['theme']>, screenMode: ScreenMode) => {\n if (!isThemeModern(theme)) {\n return `np-theme-${theme || 'personal'}`;\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"],"names":["isThemeModern","theme","modernThemes","includes","isExtraTheme","extraThemes","isForestGreenTheme","isScreenModeDark","screenMode","screenModes","getThemeClassName","classes"],"mappings":";;;;AAUO,MAAMA,aAAa,GAAIC,KAAoC,IAChEC,mBAAY,CAACC,QAAQ,CAACF,KAAoB,EAAC;AAEtC,MAAMG,YAAY,GAAIH,KAAoC,IAC/DI,kBAAW,CAACF,QAAQ,CAACF,KAAmB,EAAC;AAEpC,MAAMK,kBAAkB,GAC7BL,KAAoC,IACNA,KAAK,KAAKI,kBAAW,CAAC,CAAC,EAAC;MAE3CE,gBAAgB,GAAGA,CAC9BN,KAAoC,EACpCO,UAAsB,KAEtBR,aAAa,CAACC,KAAoB,CAAC,IAAIQ,kBAAW,CAAC,CAAC,CAAC,KAAKD,WAAU;MAEzDE,iBAAiB,GAAGA,CAACT,KAAoC,EAAEO,UAAsB,KAAI;AAChG,EAAA,IAAI,CAACR,aAAa,CAACC,KAAK,CAAC,EAAE;AACzB,IAAA,OAAO,CAAYA,SAAAA,EAAAA,KAAK,IAAI,UAAU,CAAE,CAAA,CAAA;AAC1C,GAAA;EAEA,IAAIU,OAAO,GAAG,CAAmB,iBAAA,CAAA,CAAA;AAEjC,EAAA,IAAIP,YAAY,CAACH,KAAK,CAAC,EAAE;AACvBU,IAAAA,OAAO,IAAI,CAAA,CAAA,EAAIA,OAAO,CAAA,EAAA,EAAKV,KAAK,CAAE,CAAA,CAAA;GACnC,MAAM,IAAIQ,kBAAW,CAAC,CAAC,CAAC,KAAKD,UAAU,EAAE;AACxCG,IAAAA,OAAO,IAAI,CAAA,CAAA,EAAIA,OAAO,CAAA,EAAA,EAAKH,UAAU,CAAE,CAAA,CAAA;AACzC,GAAA;AAEA,EAAA,OAAOG,OAAO,CAAA;AAChB;;;;;;;;"}
|
package/dist/helpers.mjs
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { modernThemes, extraThemes, screenModes } from './const.mjs';
|
|
2
|
+
|
|
3
|
+
const isThemeModern = theme => modernThemes.includes(theme);
|
|
4
|
+
const isExtraTheme = theme => extraThemes.includes(theme);
|
|
5
|
+
const isForestGreenTheme = theme => theme === extraThemes[0];
|
|
6
|
+
const isScreenModeDark = (theme, screenMode) => isThemeModern(theme) && screenModes[1] === screenMode;
|
|
7
|
+
const getThemeClassName = (theme, screenMode) => {
|
|
8
|
+
if (!isThemeModern(theme)) {
|
|
9
|
+
return `np-theme-${theme || 'personal'}`;
|
|
10
|
+
}
|
|
11
|
+
let classes = `np-theme-personal`;
|
|
12
|
+
if (isExtraTheme(theme)) {
|
|
13
|
+
classes += ` ${classes}--${theme}`;
|
|
14
|
+
} else if (screenModes[1] === screenMode) {
|
|
15
|
+
classes += ` ${classes}--${screenMode}`;
|
|
16
|
+
}
|
|
17
|
+
return classes;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export { getThemeClassName, isExtraTheme, isForestGreenTheme, isScreenModeDark, isThemeModern };
|
|
21
|
+
//# sourceMappingURL=helpers.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.mjs","sources":["../src/helpers.ts"],"sourcesContent":["import type {\n ModernTheme,\n ExtraTheme,\n ForestGreenTheme,\n ScreenMode,\n ScreenModeDark,\n Theming,\n} from './const';\nimport { extraThemes, screenModes, modernThemes } from './const';\n\nexport const isThemeModern = (theme: NonNullable<Theming['theme']>): theme is ModernTheme =>\n modernThemes.includes(theme as ModernTheme);\n\nexport const isExtraTheme = (theme: NonNullable<Theming['theme']>): theme is ExtraTheme =>\n extraThemes.includes(theme as ExtraTheme);\n\nexport const isForestGreenTheme = (\n theme: NonNullable<Theming['theme']>,\n): theme is ForestGreenTheme => theme === extraThemes[0];\n\nexport const isScreenModeDark = (\n theme: NonNullable<Theming['theme']>,\n screenMode: ScreenMode,\n): screenMode is ScreenModeDark =>\n isThemeModern(theme as ModernTheme) && screenModes[1] === screenMode;\n\nexport const getThemeClassName = (theme: NonNullable<Theming['theme']>, screenMode: ScreenMode) => {\n if (!isThemeModern(theme)) {\n return `np-theme-${theme || 'personal'}`;\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"],"names":["isThemeModern","theme","modernThemes","includes","isExtraTheme","extraThemes","isForestGreenTheme","isScreenModeDark","screenMode","screenModes","getThemeClassName","classes"],"mappings":";;AAUO,MAAMA,aAAa,GAAIC,KAAoC,IAChEC,YAAY,CAACC,QAAQ,CAACF,KAAoB,EAAC;AAEtC,MAAMG,YAAY,GAAIH,KAAoC,IAC/DI,WAAW,CAACF,QAAQ,CAACF,KAAmB,EAAC;AAEpC,MAAMK,kBAAkB,GAC7BL,KAAoC,IACNA,KAAK,KAAKI,WAAW,CAAC,CAAC,EAAC;MAE3CE,gBAAgB,GAAGA,CAC9BN,KAAoC,EACpCO,UAAsB,KAEtBR,aAAa,CAACC,KAAoB,CAAC,IAAIQ,WAAW,CAAC,CAAC,CAAC,KAAKD,WAAU;MAEzDE,iBAAiB,GAAGA,CAACT,KAAoC,EAAEO,UAAsB,KAAI;AAChG,EAAA,IAAI,CAACR,aAAa,CAACC,KAAK,CAAC,EAAE;AACzB,IAAA,OAAO,CAAYA,SAAAA,EAAAA,KAAK,IAAI,UAAU,CAAE,CAAA,CAAA;AAC1C,GAAA;EAEA,IAAIU,OAAO,GAAG,CAAmB,iBAAA,CAAA,CAAA;AAEjC,EAAA,IAAIP,YAAY,CAACH,KAAK,CAAC,EAAE;AACvBU,IAAAA,OAAO,IAAI,CAAA,CAAA,EAAIA,OAAO,CAAA,EAAA,EAAKV,KAAK,CAAE,CAAA,CAAA;GACnC,MAAM,IAAIQ,WAAW,CAAC,CAAC,CAAC,KAAKD,UAAU,EAAE;AACxCG,IAAAA,OAAO,IAAI,CAAA,CAAA,EAAIA,OAAO,CAAA,EAAA,EAAKH,UAAU,CAAE,CAAA,CAAA;AACzC,GAAA;AAEA,EAAA,OAAOG,OAAO,CAAA;AAChB;;;;"}
|
package/dist/index.js
CHANGED
|
@@ -1,124 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
3
|
+
var helpers = require('./helpers.js');
|
|
4
|
+
var ThemeProvider = require('./ThemeProvider.js');
|
|
5
|
+
var useTheme = require('./useTheme.js');
|
|
6
6
|
|
|
7
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
8
7
|
|
|
9
|
-
var classNames__default = /*#__PURE__*/_interopDefault(classNames);
|
|
10
8
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const isThemeModern = theme => modernThemes.includes(theme);
|
|
20
|
-
const isExtraTheme = theme => extraThemes.includes(theme);
|
|
21
|
-
const isForestGreenTheme = theme => theme === extraThemes[0];
|
|
22
|
-
const isScreenModeDark = (theme, screenMode) => isThemeModern(theme) && screenModes[1] === screenMode;
|
|
23
|
-
const getThemeClassName = (theme, screenMode) => {
|
|
24
|
-
if (!isThemeModern(theme)) {
|
|
25
|
-
return `np-theme-${theme || 'personal'}`;
|
|
26
|
-
}
|
|
27
|
-
let classes = `np-theme-personal`;
|
|
28
|
-
if (isExtraTheme(theme)) {
|
|
29
|
-
classes += ` ${classes}--${theme}`;
|
|
30
|
-
} else if (screenModes[1] === screenMode) {
|
|
31
|
-
classes += ` ${classes}--${screenMode}`;
|
|
32
|
-
}
|
|
33
|
-
return classes;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
const FALLBACK_VALUES = {
|
|
37
|
-
theme: DEFAULT_BASE_THEME,
|
|
38
|
-
screenMode: DEFAULT_SCREEN_MODE
|
|
39
|
-
};
|
|
40
|
-
const isNotProduction = () => {
|
|
41
|
-
try {
|
|
42
|
-
return ['localhost', 'dev-wi.se'].includes(window.location.hostname);
|
|
43
|
-
} catch {
|
|
44
|
-
return false;
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
const useTheme = () => {
|
|
48
|
-
const theming = react.useContext(ThemeContext);
|
|
49
|
-
if (!theming && isNotProduction()) {
|
|
50
|
-
// eslint-disable-next-line no-console
|
|
51
|
-
console.warn('Call to useTheme outside a ThemeProvider');
|
|
52
|
-
}
|
|
53
|
-
const {
|
|
54
|
-
theme,
|
|
55
|
-
screenMode: contextScreenMode
|
|
56
|
-
} = theming ?? FALLBACK_VALUES;
|
|
57
|
-
const screenMode = theme === DEFAULT_BASE_THEME ? DEFAULT_SCREEN_MODE : contextScreenMode;
|
|
58
|
-
return react.useMemo(() => ({
|
|
59
|
-
theme,
|
|
60
|
-
screenMode,
|
|
61
|
-
isModern: isThemeModern(theme),
|
|
62
|
-
isForestGreenTheme: isForestGreenTheme(theme),
|
|
63
|
-
isScreenModeDark: isScreenModeDark(theme, screenMode),
|
|
64
|
-
className: getThemeClassName(theme, screenMode)
|
|
65
|
-
}), [theme, screenMode]);
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
const ThemedChildren = ({
|
|
69
|
-
className = undefined,
|
|
70
|
-
children
|
|
71
|
-
}) => {
|
|
72
|
-
const {
|
|
73
|
-
className: themeClass
|
|
74
|
-
} = useTheme();
|
|
75
|
-
return /*#__PURE__*/jsxRuntime.jsx("div", {
|
|
76
|
-
className: classNames__default.default(themeClass, className),
|
|
77
|
-
children: children
|
|
78
|
-
});
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
const ThemeContext = /*#__PURE__*/react.createContext(undefined);
|
|
82
|
-
// RegEx to check for `np-theme-` class name
|
|
83
|
-
const themeClass = /\bnp-theme-[a-z-]+\b/g;
|
|
84
|
-
const ThemeProvider = ({
|
|
85
|
-
theme = DEFAULT_BASE_THEME,
|
|
86
|
-
screenMode = DEFAULT_SCREEN_MODE,
|
|
87
|
-
isNotRootProvider = false,
|
|
88
|
-
children,
|
|
89
|
-
className = undefined
|
|
90
|
-
}) => {
|
|
91
|
-
const isContextRoot = react.useContext(ThemeContext) === undefined;
|
|
92
|
-
// useEffect hook used to apply the theme class to the HTML element
|
|
93
|
-
react.useEffect(() => {
|
|
94
|
-
if (!isNotRootProvider && isContextRoot) {
|
|
95
|
-
// Remove all the theme classes from the documentElement
|
|
96
|
-
document.documentElement.className.match(themeClass)?.forEach(item => {
|
|
97
|
-
document.documentElement.classList.remove(item);
|
|
98
|
-
});
|
|
99
|
-
getThemeClassName(theme, screenMode).split(' ').forEach(item => {
|
|
100
|
-
document.documentElement.classList.add(item);
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
}, [isNotRootProvider, isContextRoot, theme, screenMode]);
|
|
104
|
-
const contextValue = react.useMemo(() => ({
|
|
105
|
-
theme,
|
|
106
|
-
screenMode
|
|
107
|
-
}), [screenMode, theme]);
|
|
108
|
-
return /*#__PURE__*/jsxRuntime.jsx(ThemeContext.Provider, {
|
|
109
|
-
value: contextValue,
|
|
110
|
-
children: /*#__PURE__*/jsxRuntime.jsx(ThemedChildren, {
|
|
111
|
-
className: className,
|
|
112
|
-
children: children
|
|
113
|
-
})
|
|
114
|
-
});
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
exports.ThemeProvider = ThemeProvider;
|
|
118
|
-
exports.getThemeClassName = getThemeClassName;
|
|
119
|
-
exports.isExtraTheme = isExtraTheme;
|
|
120
|
-
exports.isForestGreenTheme = isForestGreenTheme;
|
|
121
|
-
exports.isScreenModeDark = isScreenModeDark;
|
|
122
|
-
exports.isThemeModern = isThemeModern;
|
|
123
|
-
exports.useTheme = useTheme;
|
|
9
|
+
exports.getThemeClassName = helpers.getThemeClassName;
|
|
10
|
+
exports.isExtraTheme = helpers.isExtraTheme;
|
|
11
|
+
exports.isForestGreenTheme = helpers.isForestGreenTheme;
|
|
12
|
+
exports.isScreenModeDark = helpers.isScreenModeDark;
|
|
13
|
+
exports.isThemeModern = helpers.isThemeModern;
|
|
14
|
+
exports.ThemeProvider = ThemeProvider.ThemeProvider;
|
|
15
|
+
exports.useTheme = useTheme.useTheme;
|
|
124
16
|
//# sourceMappingURL=index.js.map
|
package/dist/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;\nexport const platformThemes = ['platform', 'platform--forest-green'] 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 ForestGreenTheme = (typeof extraThemes)[0];\nexport type ScreenMode = (typeof screenModes)[number];\nexport type ScreenModeDark = (typeof screenModes)[1];\nexport type PlatformTheme = (typeof platformThemes)[0];\nexport type PlatformForestGreenTheme = (typeof platformThemes)[1];\n\nexport const DEFAULT_BASE_THEME = 'light';\nexport const DEFAULT_SCREEN_MODE = 'light';\n\nexport type Theming = {\n theme?: ComponentTheme | BaseTheme | ExtraTheme | PlatformTheme | PlatformForestGreenTheme;\n screenMode?: ScreenMode;\n isNotRootProvider?: boolean | undefined;\n};\n","import type {\n ModernTheme,\n ExtraTheme,\n ForestGreenTheme,\n ScreenMode,\n ScreenModeDark,\n Theming,\n} from './const';\nimport { extraThemes, screenModes, modernThemes } from './const';\n\nexport const isThemeModern = (theme: NonNullable<Theming['theme']>): theme is ModernTheme =>\n modernThemes.includes(theme as ModernTheme);\n\nexport const isExtraTheme = (theme: NonNullable<Theming['theme']>): theme is ExtraTheme =>\n extraThemes.includes(theme as ExtraTheme);\n\nexport const isForestGreenTheme = (\n theme: NonNullable<Theming['theme']>,\n): theme is ForestGreenTheme => theme === extraThemes[0];\n\nexport const isScreenModeDark = (\n theme: NonNullable<Theming['theme']>,\n screenMode: ScreenMode,\n): screenMode is ScreenModeDark =>\n isThemeModern(theme as ModernTheme) && screenModes[1] === screenMode;\n\nexport const getThemeClassName = (theme: NonNullable<Theming['theme']>, screenMode: ScreenMode) => {\n if (!isThemeModern(theme)) {\n return `np-theme-${theme || 'personal'}`;\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 { ScreenMode, Theming } from './const';\nimport { DEFAULT_BASE_THEME, DEFAULT_SCREEN_MODE } from './const';\nimport { isThemeModern, isForestGreenTheme, isScreenModeDark, getThemeClassName } from './helpers';\n\ninterface ThemeHookValue {\n theme: NonNullable<Theming['theme']>;\n screenMode: ScreenMode;\n isModern: boolean;\n isForestGreenTheme: boolean;\n isScreenModeDark: boolean;\n className: string;\n}\n\nconst FALLBACK_VALUES = {\n theme: DEFAULT_BASE_THEME,\n screenMode: DEFAULT_SCREEN_MODE,\n} as const;\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 isForestGreenTheme: isForestGreenTheme(theme),\n isScreenModeDark: isScreenModeDark(theme, screenMode),\n className: getThemeClassName(theme, screenMode),\n }),\n [theme, screenMode],\n );\n};\n","import classNames from 'classnames';\nimport { ReactNode } from 'react';\n\nimport { useTheme } from './useTheme';\n\ntype ThemedChildrenProps = { className?: string; children: ReactNode };\n\nexport const ThemedChildren = ({ className = undefined, children }: ThemedChildrenProps) => {\n const { className: themeClass } = useTheme();\n\n return <div className={classNames(themeClass, className)}>{children}</div>;\n};\n","import { createContext, PropsWithChildren, useContext, useEffect, useMemo } from 'react';\n\nimport { ThemedChildren } from './ThemedChildren';\nimport type { 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: NonNullable<Theming['theme']>;\n screenMode: ScreenMode;\n }\n | undefined\n>(undefined);\n\ntype ThemeProviderProps = PropsWithChildren<Theming> & { className?: string };\n\n// RegEx to check for `np-theme-` class name\nconst themeClass = /\\bnp-theme-[a-z-]+\\b/g;\n\nexport const ThemeProvider = ({\n theme = DEFAULT_BASE_THEME,\n screenMode = DEFAULT_SCREEN_MODE,\n isNotRootProvider = false,\n children,\n className = undefined,\n}: ThemeProviderProps) => {\n const isContextRoot = useContext(ThemeContext) === undefined;\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]);\n\n const contextValue = useMemo(() => ({ theme, screenMode }), [screenMode, theme]);\n\n return (\n <ThemeContext.Provider value={contextValue}>\n <ThemedChildren className={className}>{children}</ThemedChildren>\n </ThemeContext.Provider>\n );\n};\n"],"names":["baseThemes","extraThemes","screenModes","modernThemes","DEFAULT_BASE_THEME","DEFAULT_SCREEN_MODE","isThemeModern","theme","includes","isExtraTheme","isForestGreenTheme","isScreenModeDark","screenMode","getThemeClassName","classes","FALLBACK_VALUES","isNotProduction","window","location","hostname","useTheme","theming","useContext","ThemeContext","console","warn","contextScreenMode","useMemo","isModern","className","ThemedChildren","undefined","children","themeClass","_jsx","classNames","createContext","ThemeProvider","isNotRootProvider","isContextRoot","useEffect","document","documentElement","match","forEach","item","classList","remove","split","add","contextValue","Provider","value"],"mappings":";;;;;;;;;;AAAA;AACO,MAAMA,UAAU,GAAG,CAAC,OAAO,EAAE,UAAU,CAAU,CAAA;AACjD,MAAMC,WAAW,GAAG,CAAC,cAAc,EAAE,cAAc,CAAU,CAAA;AAC7D,MAAMC,WAAW,GAAG,CAAC,OAAO,EAAE,MAAM,CAAU,CAAA;AAC9C,MAAMC,YAAY,GAAG,CAACH,UAAU,CAAC,CAAC,CAAC,EAAE,GAAGC,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A;AAAU,GAAE,CAAC,EAAE,CAACA,UAAU,EAAEL,KAAK,CAAC,CAAC,CAAA;AAEhF,EAAA,oBACE2B,cAAA,CAACX,YAAY,CAAC4B,QAAQ,EAAA;AAACC,IAAAA,KAAK,EAAEF,YAAa;IAAAlB,QAAA,eACzCE,cAAA,CAACJ,cAAc,EAAA;AAACD,MAAAA,SAAS,EAAEA,SAAU;AAAAG,MAAAA,QAAA,EAAEA,QAAAA;KAAyB,CAAA;AAClE,GAAuB,CAAC,CAAA;AAE5B;;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,112 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
// TODO: Change 'light' with 'legacy' in the future
|
|
6
|
-
const baseThemes = ['light', 'personal'];
|
|
7
|
-
const extraThemes = ['forest-green', 'bright-green'];
|
|
8
|
-
const screenModes = ['light', 'dark'];
|
|
9
|
-
const modernThemes = [baseThemes[1], ...extraThemes];
|
|
10
|
-
const DEFAULT_BASE_THEME = 'light';
|
|
11
|
-
const DEFAULT_SCREEN_MODE = 'light';
|
|
12
|
-
|
|
13
|
-
const isThemeModern = theme => modernThemes.includes(theme);
|
|
14
|
-
const isExtraTheme = theme => extraThemes.includes(theme);
|
|
15
|
-
const isForestGreenTheme = theme => theme === extraThemes[0];
|
|
16
|
-
const isScreenModeDark = (theme, screenMode) => isThemeModern(theme) && screenModes[1] === screenMode;
|
|
17
|
-
const getThemeClassName = (theme, screenMode) => {
|
|
18
|
-
if (!isThemeModern(theme)) {
|
|
19
|
-
return `np-theme-${theme || 'personal'}`;
|
|
20
|
-
}
|
|
21
|
-
let classes = `np-theme-personal`;
|
|
22
|
-
if (isExtraTheme(theme)) {
|
|
23
|
-
classes += ` ${classes}--${theme}`;
|
|
24
|
-
} else if (screenModes[1] === screenMode) {
|
|
25
|
-
classes += ` ${classes}--${screenMode}`;
|
|
26
|
-
}
|
|
27
|
-
return classes;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const FALLBACK_VALUES = {
|
|
31
|
-
theme: DEFAULT_BASE_THEME,
|
|
32
|
-
screenMode: DEFAULT_SCREEN_MODE
|
|
33
|
-
};
|
|
34
|
-
const isNotProduction = () => {
|
|
35
|
-
try {
|
|
36
|
-
return ['localhost', 'dev-wi.se'].includes(window.location.hostname);
|
|
37
|
-
} catch {
|
|
38
|
-
return false;
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
const useTheme = () => {
|
|
42
|
-
const theming = useContext(ThemeContext);
|
|
43
|
-
if (!theming && isNotProduction()) {
|
|
44
|
-
// eslint-disable-next-line no-console
|
|
45
|
-
console.warn('Call to useTheme outside a ThemeProvider');
|
|
46
|
-
}
|
|
47
|
-
const {
|
|
48
|
-
theme,
|
|
49
|
-
screenMode: contextScreenMode
|
|
50
|
-
} = theming ?? FALLBACK_VALUES;
|
|
51
|
-
const screenMode = theme === DEFAULT_BASE_THEME ? DEFAULT_SCREEN_MODE : contextScreenMode;
|
|
52
|
-
return useMemo(() => ({
|
|
53
|
-
theme,
|
|
54
|
-
screenMode,
|
|
55
|
-
isModern: isThemeModern(theme),
|
|
56
|
-
isForestGreenTheme: isForestGreenTheme(theme),
|
|
57
|
-
isScreenModeDark: isScreenModeDark(theme, screenMode),
|
|
58
|
-
className: getThemeClassName(theme, screenMode)
|
|
59
|
-
}), [theme, screenMode]);
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
const ThemedChildren = ({
|
|
63
|
-
className = undefined,
|
|
64
|
-
children
|
|
65
|
-
}) => {
|
|
66
|
-
const {
|
|
67
|
-
className: themeClass
|
|
68
|
-
} = useTheme();
|
|
69
|
-
return /*#__PURE__*/jsx("div", {
|
|
70
|
-
className: classNames(themeClass, className),
|
|
71
|
-
children: children
|
|
72
|
-
});
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
const ThemeContext = /*#__PURE__*/createContext(undefined);
|
|
76
|
-
// RegEx to check for `np-theme-` class name
|
|
77
|
-
const themeClass = /\bnp-theme-[a-z-]+\b/g;
|
|
78
|
-
const ThemeProvider = ({
|
|
79
|
-
theme = DEFAULT_BASE_THEME,
|
|
80
|
-
screenMode = DEFAULT_SCREEN_MODE,
|
|
81
|
-
isNotRootProvider = false,
|
|
82
|
-
children,
|
|
83
|
-
className = undefined
|
|
84
|
-
}) => {
|
|
85
|
-
const isContextRoot = useContext(ThemeContext) === undefined;
|
|
86
|
-
// useEffect hook used to apply the theme class to the HTML element
|
|
87
|
-
useEffect(() => {
|
|
88
|
-
if (!isNotRootProvider && isContextRoot) {
|
|
89
|
-
// Remove all the theme classes from the documentElement
|
|
90
|
-
document.documentElement.className.match(themeClass)?.forEach(item => {
|
|
91
|
-
document.documentElement.classList.remove(item);
|
|
92
|
-
});
|
|
93
|
-
getThemeClassName(theme, screenMode).split(' ').forEach(item => {
|
|
94
|
-
document.documentElement.classList.add(item);
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
}, [isNotRootProvider, isContextRoot, theme, screenMode]);
|
|
98
|
-
const contextValue = useMemo(() => ({
|
|
99
|
-
theme,
|
|
100
|
-
screenMode
|
|
101
|
-
}), [screenMode, theme]);
|
|
102
|
-
return /*#__PURE__*/jsx(ThemeContext.Provider, {
|
|
103
|
-
value: contextValue,
|
|
104
|
-
children: /*#__PURE__*/jsx(ThemedChildren, {
|
|
105
|
-
className: className,
|
|
106
|
-
children: children
|
|
107
|
-
})
|
|
108
|
-
});
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
export { ThemeProvider, getThemeClassName, isExtraTheme, isForestGreenTheme, isScreenModeDark, isThemeModern, useTheme };
|
|
1
|
+
export { getThemeClassName, isExtraTheme, isForestGreenTheme, isScreenModeDark, isThemeModern } from './helpers.mjs';
|
|
2
|
+
export { ThemeProvider } from './ThemeProvider.mjs';
|
|
3
|
+
export { useTheme } from './useTheme.mjs';
|
|
112
4
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","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;\nexport const platformThemes = ['platform', 'platform--forest-green'] 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 ForestGreenTheme = (typeof extraThemes)[0];\nexport type ScreenMode = (typeof screenModes)[number];\nexport type ScreenModeDark = (typeof screenModes)[1];\nexport type PlatformTheme = (typeof platformThemes)[0];\nexport type PlatformForestGreenTheme = (typeof platformThemes)[1];\n\nexport const DEFAULT_BASE_THEME = 'light';\nexport const DEFAULT_SCREEN_MODE = 'light';\n\nexport type Theming = {\n theme?: ComponentTheme | BaseTheme | ExtraTheme | PlatformTheme | PlatformForestGreenTheme;\n screenMode?: ScreenMode;\n isNotRootProvider?: boolean | undefined;\n};\n","import type {\n ModernTheme,\n ExtraTheme,\n ForestGreenTheme,\n ScreenMode,\n ScreenModeDark,\n Theming,\n} from './const';\nimport { extraThemes, screenModes, modernThemes } from './const';\n\nexport const isThemeModern = (theme: NonNullable<Theming['theme']>): theme is ModernTheme =>\n modernThemes.includes(theme as ModernTheme);\n\nexport const isExtraTheme = (theme: NonNullable<Theming['theme']>): theme is ExtraTheme =>\n extraThemes.includes(theme as ExtraTheme);\n\nexport const isForestGreenTheme = (\n theme: NonNullable<Theming['theme']>,\n): theme is ForestGreenTheme => theme === extraThemes[0];\n\nexport const isScreenModeDark = (\n theme: NonNullable<Theming['theme']>,\n screenMode: ScreenMode,\n): screenMode is ScreenModeDark =>\n isThemeModern(theme as ModernTheme) && screenModes[1] === screenMode;\n\nexport const getThemeClassName = (theme: NonNullable<Theming['theme']>, screenMode: ScreenMode) => {\n if (!isThemeModern(theme)) {\n return `np-theme-${theme || 'personal'}`;\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 { ScreenMode, Theming } from './const';\nimport { DEFAULT_BASE_THEME, DEFAULT_SCREEN_MODE } from './const';\nimport { isThemeModern, isForestGreenTheme, isScreenModeDark, getThemeClassName } from './helpers';\n\ninterface ThemeHookValue {\n theme: NonNullable<Theming['theme']>;\n screenMode: ScreenMode;\n isModern: boolean;\n isForestGreenTheme: boolean;\n isScreenModeDark: boolean;\n className: string;\n}\n\nconst FALLBACK_VALUES = {\n theme: DEFAULT_BASE_THEME,\n screenMode: DEFAULT_SCREEN_MODE,\n} as const;\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 isForestGreenTheme: isForestGreenTheme(theme),\n isScreenModeDark: isScreenModeDark(theme, screenMode),\n className: getThemeClassName(theme, screenMode),\n }),\n [theme, screenMode],\n );\n};\n","import classNames from 'classnames';\nimport { ReactNode } from 'react';\n\nimport { useTheme } from './useTheme';\n\ntype ThemedChildrenProps = { className?: string; children: ReactNode };\n\nexport const ThemedChildren = ({ className = undefined, children }: ThemedChildrenProps) => {\n const { className: themeClass } = useTheme();\n\n return <div className={classNames(themeClass, className)}>{children}</div>;\n};\n","import { createContext, PropsWithChildren, useContext, useEffect, useMemo } from 'react';\n\nimport { ThemedChildren } from './ThemedChildren';\nimport type { 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: NonNullable<Theming['theme']>;\n screenMode: ScreenMode;\n }\n | undefined\n>(undefined);\n\ntype ThemeProviderProps = PropsWithChildren<Theming> & { className?: string };\n\n// RegEx to check for `np-theme-` class name\nconst themeClass = /\\bnp-theme-[a-z-]+\\b/g;\n\nexport const ThemeProvider = ({\n theme = DEFAULT_BASE_THEME,\n screenMode = DEFAULT_SCREEN_MODE,\n isNotRootProvider = false,\n children,\n className = undefined,\n}: ThemeProviderProps) => {\n const isContextRoot = useContext(ThemeContext) === undefined;\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]);\n\n const contextValue = useMemo(() => ({ theme, screenMode }), [screenMode, theme]);\n\n return (\n <ThemeContext.Provider value={contextValue}>\n <ThemedChildren className={className}>{children}</ThemedChildren>\n </ThemeContext.Provider>\n );\n};\n"],"names":["baseThemes","extraThemes","screenModes","modernThemes","DEFAULT_BASE_THEME","DEFAULT_SCREEN_MODE","isThemeModern","theme","includes","isExtraTheme","isForestGreenTheme","isScreenModeDark","screenMode","getThemeClassName","classes","FALLBACK_VALUES","isNotProduction","window","location","hostname","useTheme","theming","useContext","ThemeContext","console","warn","contextScreenMode","useMemo","isModern","className","ThemedChildren","undefined","children","themeClass","_jsx","classNames","createContext","ThemeProvider","isNotRootProvider","isContextRoot","useEffect","document","documentElement","match","forEach","item","classList","remove","split","add","contextValue","Provider","value"],"mappings":";;;;AAAA;AACO,MAAMA,UAAU,GAAG,CAAC,OAAO,EAAE,UAAU,CAAU,CAAA;AACjD,MAAMC,WAAW,GAAG,CAAC,cAAc,EAAE,cAAc,CAAU,CAAA;AAC7D,MAAMC,WAAW,GAAG,CAAC,OAAO,EAAE,MAAM,CAAU,CAAA;AAC9C,MAAMC,YAAY,GAAG,CAACH,UAAU,CAAC,CAAC,CAAC,EAAE,GAAGC,WAAW,CAAU,CAAA;AAc7D,MAAMG,kBAAkB,GAAG,OAAO,CAAA;AAClC,MAAMC,mBAAmB,GAAG,OAAO;;ACTnC,MAAMC,aAAa,GAAIC,KAAoC,IAChEJ,YAAY,CAACK,QAAQ,CAACD,KAAoB,EAAC;AAEtC,MAAME,YAAY,GAAIF,KAAoC,IAC/DN,WAAW,CAACO,QAAQ,CAACD,KAAmB,EAAC;AAEpC,MAAMG,kBAAkB,GAC7BH,KAAoC,IACNA,KAAK,KAAKN,WAAW,CAAC,CAAC,EAAC;MAE3CU,gBAAgB,GAAGA,CAC9BJ,KAAoC,EACpCK,UAAsB,KAEtBN,aAAa,CAACC,KAAoB,CAAC,IAAIL,WAAW,CAAC,CAAC,CAAC,KAAKU,WAAU;MAEzDC,iBAAiB,GAAGA,CAACN,KAAoC,EAAEK,UAAsB,KAAI;AAChG,EAAA,IAAI,CAACN,aAAa,CAACC,KAAK,CAAC,EAAE;AACzB,IAAA,OAAmB,CAAAA,SAAAA,EAAAA,KAAK,IAAI,WAAY,CAAA,CAAA;AACzC,GAAA;EAED,IAAIO,OAAO,GAAG,CAAm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W,OAAO,CAAEC,IAAI,IAAI;QACrEJ,QAAQ,CAACC,eAAe,CAACI,SAAS,CAACC,MAAM,CAACF,IAAI,CAAC,CAAA;AACjD,OAAC,CAAC,CAAA;AACFhC,MAAAA,iBAAiB,CAACN,KAAK,EAAEK,UAAU,CAAC,CACjCoC,KAAK,CAAC,GAAG,CAAC,CACVJ,OAAO,CAAEC,IAAI,IAAI;QAChBJ,QAAQ,CAACC,eAAe,CAACI,SAAS,CAACG,GAAG,CAACJ,IAAI,CAAC,CAAA;AAC9C,OAAC,CAAC,CAAA;AACL,KAAA;GACF,EAAE,CAACP,iBAAiB,EAAEC,aAAa,EAAEhC,KAAK,EAAEK,UAAU,CAAC,CAAC,CAAA;AAEzD,EAAA,MAAMsC,YAAY,GAAGvB,OAAO,CAAC,OAAO;IAAEpB,KAAK;AAAEK,IAAAA,UAAAA;AAAU,GAAE,CAAC,EAAE,CAACA,UAAU,EAAEL,KAAK,CAAC,CAAC,CAAA;AAEhF,EAAA,oBACE2B,GAAA,CAACX,YAAY,CAAC4B,QAAQ,EAAA;AAACC,IAAAA,KAAK,EAAEF,YAAa;IAAAlB,QAAA,eACzCE,GAAA,CAACJ,cAAc,EAAA;AAACD,MAAAA,SAAS,EAAEA,SAAU;AAAAG,MAAAA,QAAA,EAAEA,QAAAA;KAAyB,CAAA;AAClE,GAAuB,CAAC,CAAA;AAE5B;;;;"}
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}
|
package/dist/useTheme.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
var ThemeProvider = require('./ThemeProvider.js');
|
|
5
|
+
var _const = require('./const.js');
|
|
6
|
+
var helpers = require('./helpers.js');
|
|
7
|
+
|
|
8
|
+
const FALLBACK_VALUES = {
|
|
9
|
+
theme: _const.DEFAULT_BASE_THEME,
|
|
10
|
+
screenMode: _const.DEFAULT_SCREEN_MODE
|
|
11
|
+
};
|
|
12
|
+
const isNotProduction = () => {
|
|
13
|
+
try {
|
|
14
|
+
return ['localhost', 'dev-wi.se'].includes(window.location.hostname);
|
|
15
|
+
} catch {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
const useTheme = () => {
|
|
20
|
+
const theming = react.useContext(ThemeProvider.ThemeContext);
|
|
21
|
+
if (!theming && isNotProduction()) {
|
|
22
|
+
// eslint-disable-next-line no-console
|
|
23
|
+
console.warn('Call to useTheme outside a ThemeProvider');
|
|
24
|
+
}
|
|
25
|
+
const {
|
|
26
|
+
theme,
|
|
27
|
+
screenMode: contextScreenMode
|
|
28
|
+
} = theming ?? FALLBACK_VALUES;
|
|
29
|
+
const screenMode = theme === _const.DEFAULT_BASE_THEME ? _const.DEFAULT_SCREEN_MODE : contextScreenMode;
|
|
30
|
+
return react.useMemo(() => ({
|
|
31
|
+
theme,
|
|
32
|
+
screenMode,
|
|
33
|
+
isModern: helpers.isThemeModern(theme),
|
|
34
|
+
isForestGreenTheme: helpers.isForestGreenTheme(theme),
|
|
35
|
+
isScreenModeDark: helpers.isScreenModeDark(theme, screenMode),
|
|
36
|
+
className: helpers.getThemeClassName(theme, screenMode)
|
|
37
|
+
}), [theme, screenMode]);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
exports.useTheme = useTheme;
|
|
41
|
+
//# sourceMappingURL=useTheme.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTheme.js","sources":["../src/useTheme.ts"],"sourcesContent":["import { useContext, useMemo } from 'react';\n\nimport { ThemeContext } from './ThemeProvider';\nimport type { ScreenMode, Theming } from './const';\nimport { DEFAULT_BASE_THEME, DEFAULT_SCREEN_MODE } from './const';\nimport { isThemeModern, isForestGreenTheme, isScreenModeDark, getThemeClassName } from './helpers';\n\ninterface ThemeHookValue {\n theme: NonNullable<Theming['theme']>;\n screenMode: ScreenMode;\n isModern: boolean;\n isForestGreenTheme: boolean;\n isScreenModeDark: boolean;\n className: string;\n}\n\nconst FALLBACK_VALUES = {\n theme: DEFAULT_BASE_THEME,\n screenMode: DEFAULT_SCREEN_MODE,\n} as const;\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 isForestGreenTheme: isForestGreenTheme(theme),\n isScreenModeDark: isScreenModeDark(theme, screenMode),\n className: getThemeClassName(theme, screenMode),\n }),\n [theme, screenMode],\n );\n};\n"],"names":["FALLBACK_VALUES","theme","DEFAULT_BASE_THEME","screenMode","DEFAULT_SCREEN_MODE","isNotProduction","includes","window","location","hostname","useTheme","theming","useContext","ThemeContext","console","warn","contextScreenMode","useMemo","isModern","isThemeModern","isForestGreenTheme","isScreenModeDark","className","getThemeClassName"],"mappings":";;;;;;;AAgBA,MAAMA,eAAe,GAAG;AACtBC,EAAAA,KAAK,EAAEC,yBAAkB;AACzBC,EAAAA,UAAU,EAAEC,0BAAAA;CACJ,CAAA;AAEV,MAAMC,eAAe,GAAGA,MAAK;EAC3B,IAAI;AACF,IAAA,OAAO,CAAC,WAAW,EAAE,WAAW,CAAC,CAACC,QAAQ,CAACC,MAAM,CAACC,QAAQ,CAACC,QAAQ,CAAC,CAAA;AACtE,GAAC,CAAC,MAAM;AACN,IAAA,OAAO,KAAK,CAAA;AACd,GAAA;AACF,CAAC,CAAA;AAEYC,MAAAA,QAAQ,GAAGA,MAAqB;AAC3C,EAAA,MAAMC,OAAO,GAAGC,gBAAU,CAACC,0BAAY,CAAC,CAAA;AAExC,EAAA,IAAI,CAACF,OAAO,IAAIN,eAAe,EAAE,EAAE;AACjC;AACAS,IAAAA,OAAO,CAACC,IAAI,CAAC,0CAA0C,CAAC,CAAA;AAC1D,GAAA;EAEA,MAAM;IAAEd,KAAK;AAAEE,IAAAA,UAAU,EAAEa,iBAAAA;GAAmB,GAAGL,OAAO,IAAIX,eAAe,CAAA;EAE3E,MAAMG,UAAU,GAAGF,KAAK,KAAKC,yBAAkB,GAAGE,0BAAmB,GAAGY,iBAAiB,CAAA;EAEzF,OAAOC,aAAO,CACZ,OAAO;IACLhB,KAAK;IACLE,UAAU;AACVe,IAAAA,QAAQ,EAAEC,qBAAa,CAAClB,KAAK,CAAC;AAC9BmB,IAAAA,kBAAkB,EAAEA,0BAAkB,CAACnB,KAAK,CAAC;AAC7CoB,IAAAA,gBAAgB,EAAEA,wBAAgB,CAACpB,KAAK,EAAEE,UAAU,CAAC;AACrDmB,IAAAA,SAAS,EAAEC,yBAAiB,CAACtB,KAAK,EAAEE,UAAU,CAAA;AAC/C,GAAA,CAAC,EACF,CAACF,KAAK,EAAEE,UAAU,CAAC,CACpB,CAAA;AACH;;;;"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { useContext, useMemo } from 'react';
|
|
2
|
+
import { ThemeContext } from './ThemeProvider.mjs';
|
|
3
|
+
import { DEFAULT_BASE_THEME, DEFAULT_SCREEN_MODE } from './const.mjs';
|
|
4
|
+
import { isThemeModern, isForestGreenTheme, isScreenModeDark, getThemeClassName } from './helpers.mjs';
|
|
5
|
+
|
|
6
|
+
const FALLBACK_VALUES = {
|
|
7
|
+
theme: DEFAULT_BASE_THEME,
|
|
8
|
+
screenMode: DEFAULT_SCREEN_MODE
|
|
9
|
+
};
|
|
10
|
+
const isNotProduction = () => {
|
|
11
|
+
try {
|
|
12
|
+
return ['localhost', 'dev-wi.se'].includes(window.location.hostname);
|
|
13
|
+
} catch {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const useTheme = () => {
|
|
18
|
+
const theming = useContext(ThemeContext);
|
|
19
|
+
if (!theming && isNotProduction()) {
|
|
20
|
+
// eslint-disable-next-line no-console
|
|
21
|
+
console.warn('Call to useTheme outside a ThemeProvider');
|
|
22
|
+
}
|
|
23
|
+
const {
|
|
24
|
+
theme,
|
|
25
|
+
screenMode: contextScreenMode
|
|
26
|
+
} = theming ?? FALLBACK_VALUES;
|
|
27
|
+
const screenMode = theme === DEFAULT_BASE_THEME ? DEFAULT_SCREEN_MODE : contextScreenMode;
|
|
28
|
+
return useMemo(() => ({
|
|
29
|
+
theme,
|
|
30
|
+
screenMode,
|
|
31
|
+
isModern: isThemeModern(theme),
|
|
32
|
+
isForestGreenTheme: isForestGreenTheme(theme),
|
|
33
|
+
isScreenModeDark: isScreenModeDark(theme, screenMode),
|
|
34
|
+
className: getThemeClassName(theme, screenMode)
|
|
35
|
+
}), [theme, screenMode]);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export { useTheme };
|
|
39
|
+
//# sourceMappingURL=useTheme.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTheme.mjs","sources":["../src/useTheme.ts"],"sourcesContent":["import { useContext, useMemo } from 'react';\n\nimport { ThemeContext } from './ThemeProvider';\nimport type { ScreenMode, Theming } from './const';\nimport { DEFAULT_BASE_THEME, DEFAULT_SCREEN_MODE } from './const';\nimport { isThemeModern, isForestGreenTheme, isScreenModeDark, getThemeClassName } from './helpers';\n\ninterface ThemeHookValue {\n theme: NonNullable<Theming['theme']>;\n screenMode: ScreenMode;\n isModern: boolean;\n isForestGreenTheme: boolean;\n isScreenModeDark: boolean;\n className: string;\n}\n\nconst FALLBACK_VALUES = {\n theme: DEFAULT_BASE_THEME,\n screenMode: DEFAULT_SCREEN_MODE,\n} as const;\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 isForestGreenTheme: isForestGreenTheme(theme),\n isScreenModeDark: isScreenModeDark(theme, screenMode),\n className: getThemeClassName(theme, screenMode),\n }),\n [theme, screenMode],\n );\n};\n"],"names":["FALLBACK_VALUES","theme","DEFAULT_BASE_THEME","screenMode","DEFAULT_SCREEN_MODE","isNotProduction","includes","window","location","hostname","useTheme","theming","useContext","ThemeContext","console","warn","contextScreenMode","useMemo","isModern","isThemeModern","isForestGreenTheme","isScreenModeDark","className","getThemeClassName"],"mappings":";;;;;AAgBA,MAAMA,eAAe,GAAG;AACtBC,EAAAA,KAAK,EAAEC,kBAAkB;AACzBC,EAAAA,UAAU,EAAEC,mBAAAA;CACJ,CAAA;AAEV,MAAMC,eAAe,GAAGA,MAAK;EAC3B,IAAI;AACF,IAAA,OAAO,CAAC,WAAW,EAAE,WAAW,CAAC,CAACC,QAAQ,CAACC,MAAM,CAACC,QAAQ,CAACC,QAAQ,CAAC,CAAA;AACtE,GAAC,CAAC,MAAM;AACN,IAAA,OAAO,KAAK,CAAA;AACd,GAAA;AACF,CAAC,CAAA;AAEYC,MAAAA,QAAQ,GAAGA,MAAqB;AAC3C,EAAA,MAAMC,OAAO,GAAGC,UAAU,CAACC,YAAY,CAAC,CAAA;AAExC,EAAA,IAAI,CAACF,OAAO,IAAIN,eAAe,EAAE,EAAE;AACjC;AACAS,IAAAA,OAAO,CAACC,IAAI,CAAC,0CAA0C,CAAC,CAAA;AAC1D,GAAA;EAEA,MAAM;IAAEd,KAAK;AAAEE,IAAAA,UAAU,EAAEa,iBAAAA;GAAmB,GAAGL,OAAO,IAAIX,eAAe,CAAA;EAE3E,MAAMG,UAAU,GAAGF,KAAK,KAAKC,kBAAkB,GAAGE,mBAAmB,GAAGY,iBAAiB,CAAA;EAEzF,OAAOC,OAAO,CACZ,OAAO;IACLhB,KAAK;IACLE,UAAU;AACVe,IAAAA,QAAQ,EAAEC,aAAa,CAAClB,KAAK,CAAC;AAC9BmB,IAAAA,kBAAkB,EAAEA,kBAAkB,CAACnB,KAAK,CAAC;AAC7CoB,IAAAA,gBAAgB,EAAEA,gBAAgB,CAACpB,KAAK,EAAEE,UAAU,CAAC;AACrDmB,IAAAA,SAAS,EAAEC,iBAAiB,CAACtB,KAAK,EAAEE,UAAU,CAAA;AAC/C,GAAA,CAAC,EACF,CAACF,KAAK,EAAEE,UAAU,CAAC,CACpB,CAAA;AACH;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wise/components-theming",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Provides theming support for the Wise Design system components",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"keywords": [
|
|
@@ -12,30 +12,29 @@
|
|
|
12
12
|
"url": "git+https://github.com/transferwise/neptune-web.git"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@babel/runtime": "^7.24.
|
|
16
|
-
"
|
|
15
|
+
"@babel/runtime": "^7.24.8",
|
|
16
|
+
"clsx": "^2.1.1"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"@babel/core": "^7.24.
|
|
20
|
-
"@babel/plugin-transform-runtime": "^7.24.
|
|
21
|
-
"@babel/preset-env": "^7.24.
|
|
22
|
-
"@babel/preset-react": "^7.24.
|
|
23
|
-
"@babel/preset-typescript": "^7.24.
|
|
19
|
+
"@babel/core": "^7.24.8",
|
|
20
|
+
"@babel/plugin-transform-runtime": "^7.24.7",
|
|
21
|
+
"@babel/preset-env": "^7.24.8",
|
|
22
|
+
"@babel/preset-react": "^7.24.7",
|
|
23
|
+
"@babel/preset-typescript": "^7.24.7",
|
|
24
24
|
"@rollup/plugin-babel": "^6.0.4",
|
|
25
25
|
"@rollup/plugin-typescript": "^11.1.6",
|
|
26
|
-
"@testing-library/
|
|
27
|
-
"@testing-library/
|
|
28
|
-
"@
|
|
26
|
+
"@testing-library/dom": "^10.3.1",
|
|
27
|
+
"@testing-library/jest-dom": "^6.4.6",
|
|
28
|
+
"@testing-library/react": "^16.0.0",
|
|
29
|
+
"@tsconfig/recommended": "^1.0.7",
|
|
29
30
|
"@types/babel__core": "^7.20.5",
|
|
30
31
|
"@types/jest": "^29.5.12",
|
|
31
32
|
"@types/react": "^18.3.3",
|
|
32
33
|
"@types/react-dom": "^18.3.0",
|
|
33
|
-
"@types/testing-library__jest-dom": "^5.14.9",
|
|
34
|
-
"classnames": "^2.5.1",
|
|
35
34
|
"jest": "^29.7.0",
|
|
36
35
|
"jest-environment-jsdom": "^29.7.0",
|
|
37
|
-
"rollup": "^4.
|
|
38
|
-
"
|
|
36
|
+
"rollup": "^4.18.1",
|
|
37
|
+
"rollup-preserve-directives": "^1.1.1"
|
|
39
38
|
},
|
|
40
39
|
"peerDependencies": {
|
|
41
40
|
"react": ">=18",
|
package/src/ThemedChildren.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { clsx } from 'clsx';
|
|
2
2
|
import { ReactNode } from 'react';
|
|
3
3
|
|
|
4
4
|
import { useTheme } from './useTheme';
|
|
@@ -8,5 +8,5 @@ type ThemedChildrenProps = { className?: string; children: ReactNode };
|
|
|
8
8
|
export const ThemedChildren = ({ className = undefined, children }: ThemedChildrenProps) => {
|
|
9
9
|
const { className: themeClass } = useTheme();
|
|
10
10
|
|
|
11
|
-
return <div className={
|
|
11
|
+
return <div className={clsx(themeClass, className)}>{children}</div>;
|
|
12
12
|
};
|