@wise/components-theming 0.1.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/CHANGELOG.md +17 -0
- package/LICENSE.md +13 -0
- package/README.md +1 -0
- package/dist/cjs/index.d.ts +14 -0
- package/dist/cjs/index.js +46 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/es/index.d.ts +14 -0
- package/dist/es/index.js +43 -0
- package/dist/es/index.js.map +1 -0
- package/package.json +47 -0
- package/src/ThemeProvider.spec.tsx +85 -0
- package/src/ThemeProvider.tsx +36 -0
- package/src/const.ts +5 -0
- package/src/index.ts +4 -0
- package/src/useTheme.spec.tsx +27 -0
- package/src/useTheme.ts +8 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
# 0.1.0 (2022-11-02)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* **components-theming:** Add theme provider package ([#1718](https://github.com/transferwise/neptune-web/issues/1718)) ([ee9d394](https://github.com/transferwise/neptune-web/commit/ee9d394b590f60462986b774c1d9192edf2e1ade))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# Change Log
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright 2022 Wise Ltd.
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Design System Componentents - Theming
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { PropsWithChildren } from "react";
|
|
3
|
+
declare const componentThemes: readonly [
|
|
4
|
+
"light",
|
|
5
|
+
"personal"
|
|
6
|
+
];
|
|
7
|
+
type ComponentTheme = (typeof componentThemes)[number];
|
|
8
|
+
type ThemeProviderProps = PropsWithChildren<{
|
|
9
|
+
theme: ComponentTheme | undefined;
|
|
10
|
+
}>;
|
|
11
|
+
declare const ThemeProvider: ({ theme, children }: ThemeProviderProps) => JSX.Element;
|
|
12
|
+
declare const useTheme: () => ComponentTheme;
|
|
13
|
+
export type { ComponentTheme };
|
|
14
|
+
export { ThemeProvider, useTheme };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
4
|
+
var react = require('react');
|
|
5
|
+
|
|
6
|
+
var DEFAULT_COMPONENT_THEME = 'light';
|
|
7
|
+
|
|
8
|
+
var ThemeContext = /*#__PURE__*/react.createContext(DEFAULT_COMPONENT_THEME);
|
|
9
|
+
var ThemeProvider = function ThemeProvider(_ref) {
|
|
10
|
+
var _ref$theme = _ref.theme,
|
|
11
|
+
theme = _ref$theme === void 0 ? DEFAULT_COMPONENT_THEME : _ref$theme,
|
|
12
|
+
children = _ref.children;
|
|
13
|
+
var themedChildren = react.Children.map(children, function (child) {
|
|
14
|
+
var _child$props;
|
|
15
|
+
|
|
16
|
+
if (! /*#__PURE__*/react.isValidElement(child)) {
|
|
17
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
18
|
+
// eslint-disable-next-line no-console
|
|
19
|
+
console.warn('[ThemeProvider] Trying to inject `className` to an invalid React element, this will be skipped!');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return child;
|
|
23
|
+
} // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
var classNames = ["np-theme-".concat(theme), child === null || child === void 0 ? void 0 : (_child$props = child.props) === null || _child$props === void 0 ? void 0 : _child$props.className].filter(Boolean).join(' ');
|
|
27
|
+
return /*#__PURE__*/react.cloneElement(child, {
|
|
28
|
+
className: classNames // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
29
|
+
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
return jsxRuntime.jsx(ThemeContext.Provider, {
|
|
33
|
+
value: theme,
|
|
34
|
+
children: themedChildren
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
var useTheme = function useTheme() {
|
|
39
|
+
var _useContext;
|
|
40
|
+
|
|
41
|
+
return (_useContext = react.useContext(ThemeContext)) !== null && _useContext !== void 0 ? _useContext : DEFAULT_COMPONENT_THEME;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
exports.ThemeProvider = ThemeProvider;
|
|
45
|
+
exports.useTheme = useTheme;
|
|
46
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/const.ts","../../src/ThemeProvider.tsx","../../src/useTheme.ts"],"sourcesContent":["export const componentThemes = ['light', 'personal'] as const;\n\nexport type ComponentTheme = typeof componentThemes[number];\n\nexport const DEFAULT_COMPONENT_THEME = 'light' as const;\n","import { Children, cloneElement, createContext, isValidElement, PropsWithChildren } from 'react';\n\nimport { ComponentTheme, DEFAULT_COMPONENT_THEME } from './const';\n\nexport const ThemeContext = createContext<ComponentTheme>(DEFAULT_COMPONENT_THEME);\n\ntype ThemeProviderProps = PropsWithChildren<{\n theme: ComponentTheme | undefined;\n}>;\n\nexport const ThemeProvider = ({\n theme = DEFAULT_COMPONENT_THEME,\n children,\n}: ThemeProviderProps) => {\n const themedChildren = Children.map(children, (child) => {\n if (!isValidElement(child)) {\n if (process.env.NODE_ENV !== 'production') {\n // eslint-disable-next-line no-console\n console.warn(\n '[ThemeProvider] Trying to inject `className` to an invalid React element, this will be skipped!',\n );\n }\n return child;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\n const classNames = [`np-theme-${theme}`, child?.props?.className].filter(Boolean).join(' ');\n\n return cloneElement(child, {\n className: classNames,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } as any);\n });\n\n return <ThemeContext.Provider value={theme}>{themedChildren}</ThemeContext.Provider>;\n};\n","import { useContext } from 'react';\n\nimport { ThemeContext } from './ThemeProvider';\nimport { ComponentTheme, DEFAULT_COMPONENT_THEME } from './const';\n\nexport const useTheme = (): ComponentTheme => {\n return useContext(ThemeContext) ?? DEFAULT_COMPONENT_THEME;\n};\n"],"names":["DEFAULT_COMPONENT_THEME","ThemeContext","createContext","ThemeProvider","theme","children","themedChildren","Children","map","child","isValidElement","process","env","NODE_ENV","console","warn","classNames","props","className","filter","Boolean","join","cloneElement","_jsx","Provider","value","useTheme","useContext"],"mappings":";;;;;AAIO,IAAMA,uBAAuB,GAAG,OAAhC;;ACAA,IAAMC,YAAY,gBAAGC,mBAAa,CAAiBF,uBAAjB,CAAlC,CAAA;AAMMG,IAAAA,aAAa,GAAG,SAAhBA,aAAgB,CAGJ,IAAA,EAAA;AAAA,EAAA,IAAA,UAAA,GAAA,IAAA,CAFvBC,KAEuB;MAFvBA,KAEuB,2BAFfJ,uBAEe,GAAA,UAAA;MADvBK,QACuB,QADvBA,QACuB,CAAA;EACvB,IAAMC,cAAc,GAAGC,cAAQ,CAACC,GAAT,CAAaH,QAAb,EAAuB,UAACI,KAAD,EAAU;AAAA,IAAA,IAAA,YAAA,CAAA;;AACtD,IAAA,IAAI,eAACC,oBAAc,CAACD,KAAD,CAAnB,EAA4B;AAC1B,MAAA,IAAIE,OAAO,CAACC,GAAR,CAAYC,QAAZ,KAAyB,YAA7B,EAA2C;AACzC;QACAC,OAAO,CAACC,IAAR,CACE,iGADF,CAAA,CAAA;AAGD,OAAA;;AACD,MAAA,OAAON,KAAP,CAAA;AACD,KATqD;;;IAYtD,IAAMO,UAAU,GAAG,CAAaZ,WAAAA,CAAAA,MAAAA,CAAAA,KAAb,GAAsBK,KAAtB,KAAA,IAAA,IAAsBA,KAAtB,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,CAAA,YAAA,GAAsBA,KAAK,CAAEQ,KAA7B,MAAsB,IAAA,IAAA,YAAA,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,YAAA,CAAcC,SAApC,CAAA,CAA+CC,MAA/C,CAAsDC,OAAtD,CAA+DC,CAAAA,IAA/D,CAAoE,GAApE,CAAnB,CAAA;IAEA,oBAAOC,kBAAY,CAACb,KAAD,EAAQ;MACzBS,SAAS,EAAEF,UADc;;AAAA,KAAR,CAAnB,CAAA;AAID,GAlBsB,CAAvB,CAAA;AAoBA,EAAA,OAAOO,cAACtB,CAAAA,YAAY,CAACuB,QAAd,EAAsB;AAACC,IAAAA,KAAK,EAAErB,KAAR;IAAaC,QAAGC,EAAAA,cAAAA;AAAhB,GAAtB,CAAP,CAAA;AACD;;AC9BYoB,IAAAA,QAAQ,GAAG,SAAXA,QAAW,GAAqB;AAAA,EAAA,IAAA,WAAA,CAAA;;AAC3C,EAAA,OAAA,CAAA,WAAA,GAAOC,gBAAU,CAAC1B,YAAD,CAAjB,qDAAmCD,uBAAnC,CAAA;AACD;;;;;"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { PropsWithChildren } from "react";
|
|
3
|
+
declare const componentThemes: readonly [
|
|
4
|
+
"light",
|
|
5
|
+
"personal"
|
|
6
|
+
];
|
|
7
|
+
type ComponentTheme = (typeof componentThemes)[number];
|
|
8
|
+
type ThemeProviderProps = PropsWithChildren<{
|
|
9
|
+
theme: ComponentTheme | undefined;
|
|
10
|
+
}>;
|
|
11
|
+
declare const ThemeProvider: ({ theme, children }: ThemeProviderProps) => JSX.Element;
|
|
12
|
+
declare const useTheme: () => ComponentTheme;
|
|
13
|
+
export type { ComponentTheme };
|
|
14
|
+
export { ThemeProvider, useTheme };
|
package/dist/es/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import { Children, isValidElement, cloneElement, createContext, useContext } from 'react';
|
|
3
|
+
|
|
4
|
+
var DEFAULT_COMPONENT_THEME = 'light';
|
|
5
|
+
|
|
6
|
+
var ThemeContext = /*#__PURE__*/createContext(DEFAULT_COMPONENT_THEME);
|
|
7
|
+
var ThemeProvider = function ThemeProvider(_ref) {
|
|
8
|
+
var _ref$theme = _ref.theme,
|
|
9
|
+
theme = _ref$theme === void 0 ? DEFAULT_COMPONENT_THEME : _ref$theme,
|
|
10
|
+
children = _ref.children;
|
|
11
|
+
var themedChildren = Children.map(children, function (child) {
|
|
12
|
+
var _child$props;
|
|
13
|
+
|
|
14
|
+
if (! /*#__PURE__*/isValidElement(child)) {
|
|
15
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
16
|
+
// eslint-disable-next-line no-console
|
|
17
|
+
console.warn('[ThemeProvider] Trying to inject `className` to an invalid React element, this will be skipped!');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return child;
|
|
21
|
+
} // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
var classNames = ["np-theme-".concat(theme), child === null || child === void 0 ? void 0 : (_child$props = child.props) === null || _child$props === void 0 ? void 0 : _child$props.className].filter(Boolean).join(' ');
|
|
25
|
+
return /*#__PURE__*/cloneElement(child, {
|
|
26
|
+
className: classNames // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
27
|
+
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
return jsx(ThemeContext.Provider, {
|
|
31
|
+
value: theme,
|
|
32
|
+
children: themedChildren
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
var useTheme = function useTheme() {
|
|
37
|
+
var _useContext;
|
|
38
|
+
|
|
39
|
+
return (_useContext = useContext(ThemeContext)) !== null && _useContext !== void 0 ? _useContext : DEFAULT_COMPONENT_THEME;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export { ThemeProvider, useTheme };
|
|
43
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/const.ts","../../src/ThemeProvider.tsx","../../src/useTheme.ts"],"sourcesContent":["export const componentThemes = ['light', 'personal'] as const;\n\nexport type ComponentTheme = typeof componentThemes[number];\n\nexport const DEFAULT_COMPONENT_THEME = 'light' as const;\n","import { Children, cloneElement, createContext, isValidElement, PropsWithChildren } from 'react';\n\nimport { ComponentTheme, DEFAULT_COMPONENT_THEME } from './const';\n\nexport const ThemeContext = createContext<ComponentTheme>(DEFAULT_COMPONENT_THEME);\n\ntype ThemeProviderProps = PropsWithChildren<{\n theme: ComponentTheme | undefined;\n}>;\n\nexport const ThemeProvider = ({\n theme = DEFAULT_COMPONENT_THEME,\n children,\n}: ThemeProviderProps) => {\n const themedChildren = Children.map(children, (child) => {\n if (!isValidElement(child)) {\n if (process.env.NODE_ENV !== 'production') {\n // eslint-disable-next-line no-console\n console.warn(\n '[ThemeProvider] Trying to inject `className` to an invalid React element, this will be skipped!',\n );\n }\n return child;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\n const classNames = [`np-theme-${theme}`, child?.props?.className].filter(Boolean).join(' ');\n\n return cloneElement(child, {\n className: classNames,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } as any);\n });\n\n return <ThemeContext.Provider value={theme}>{themedChildren}</ThemeContext.Provider>;\n};\n","import { useContext } from 'react';\n\nimport { ThemeContext } from './ThemeProvider';\nimport { ComponentTheme, DEFAULT_COMPONENT_THEME } from './const';\n\nexport const useTheme = (): ComponentTheme => {\n return useContext(ThemeContext) ?? DEFAULT_COMPONENT_THEME;\n};\n"],"names":["DEFAULT_COMPONENT_THEME","ThemeContext","createContext","ThemeProvider","theme","children","themedChildren","Children","map","child","isValidElement","process","env","NODE_ENV","console","warn","classNames","props","className","filter","Boolean","join","cloneElement","_jsx","Provider","value","useTheme","useContext"],"mappings":";;;AAIO,IAAMA,uBAAuB,GAAG,OAAhC;;ACAA,IAAMC,YAAY,gBAAGC,aAAa,CAAiBF,uBAAjB,CAAlC,CAAA;AAMMG,IAAAA,aAAa,GAAG,SAAhBA,aAAgB,CAGJ,IAAA,EAAA;AAAA,EAAA,IAAA,UAAA,GAAA,IAAA,CAFvBC,KAEuB;MAFvBA,KAEuB,2BAFfJ,uBAEe,GAAA,UAAA;MADvBK,QACuB,QADvBA,QACuB,CAAA;EACvB,IAAMC,cAAc,GAAGC,QAAQ,CAACC,GAAT,CAAaH,QAAb,EAAuB,UAACI,KAAD,EAAU;AAAA,IAAA,IAAA,YAAA,CAAA;;AACtD,IAAA,IAAI,eAACC,cAAc,CAACD,KAAD,CAAnB,EAA4B;AAC1B,MAAA,IAAIE,OAAO,CAACC,GAAR,CAAYC,QAAZ,KAAyB,YAA7B,EAA2C;AACzC;QACAC,OAAO,CAACC,IAAR,CACE,iGADF,CAAA,CAAA;AAGD,OAAA;;AACD,MAAA,OAAON,KAAP,CAAA;AACD,KATqD;;;IAYtD,IAAMO,UAAU,GAAG,CAAaZ,WAAAA,CAAAA,MAAAA,CAAAA,KAAb,GAAsBK,KAAtB,KAAA,IAAA,IAAsBA,KAAtB,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,CAAA,YAAA,GAAsBA,KAAK,CAAEQ,KAA7B,MAAsB,IAAA,IAAA,YAAA,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,YAAA,CAAcC,SAApC,CAAA,CAA+CC,MAA/C,CAAsDC,OAAtD,CAA+DC,CAAAA,IAA/D,CAAoE,GAApE,CAAnB,CAAA;IAEA,oBAAOC,YAAY,CAACb,KAAD,EAAQ;MACzBS,SAAS,EAAEF,UADc;;AAAA,KAAR,CAAnB,CAAA;AAID,GAlBsB,CAAvB,CAAA;AAoBA,EAAA,OAAOO,GAACtB,CAAAA,YAAY,CAACuB,QAAd,EAAsB;AAACC,IAAAA,KAAK,EAAErB,KAAR;IAAaC,QAAGC,EAAAA,cAAAA;AAAhB,GAAtB,CAAP,CAAA;AACD;;AC9BYoB,IAAAA,QAAQ,GAAG,SAAXA,QAAW,GAAqB;AAAA,EAAA,IAAA,WAAA,CAAA;;AAC3C,EAAA,OAAA,CAAA,WAAA,GAAOC,UAAU,CAAC1B,YAAD,CAAjB,qDAAmCD,uBAAnC,CAAA;AACD;;;;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wise/components-theming",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Provides theming support for the Wise Design system components",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"wise",
|
|
8
|
+
"jestconfig"
|
|
9
|
+
],
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"fullname": "transferwise/neptune-web",
|
|
13
|
+
"url": "git+https://github.com/transferwise/neptune-web.git"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@babel/plugin-syntax-import-assertions": "7.20.0",
|
|
17
|
+
"@rollup/plugin-typescript": "9.0.2",
|
|
18
|
+
"@types/react": "17.0.52",
|
|
19
|
+
"@types/react-dom": "17.0.18",
|
|
20
|
+
"rollup": "3.2.3",
|
|
21
|
+
"rollup-plugin-ts": "3.0.2",
|
|
22
|
+
"typescript": "4.8.4"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"react": ">=16.14",
|
|
26
|
+
"react-dom": ">=16.14"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "rollup --config --sourcemap --configPlugin rollup-plugin-ts",
|
|
30
|
+
"test": "jest --env=jsdom --maxWorkers=4 --config=../../node_modules/@transferwise/test-config/jest.config.js",
|
|
31
|
+
"test:watch": "jest --watch --env=jsdom --config=../../node_modules/@transferwise/test-config/jest.config.js"
|
|
32
|
+
},
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"import": "./dist/es/index.js",
|
|
36
|
+
"require": "./dist/cjs/index.js"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"main": "./dist/cjs/index.js",
|
|
40
|
+
"module": "./dist/es/index.js",
|
|
41
|
+
"types": "./dist/es/index.d.ts",
|
|
42
|
+
"files": [
|
|
43
|
+
"dist/",
|
|
44
|
+
"src/"
|
|
45
|
+
],
|
|
46
|
+
"gitHead": "e7f68c481710db55b16fbf9fbe22f3dd0014d29a"
|
|
47
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { render, screen } from '@testing-library/react';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
|
|
4
|
+
import { ThemeProvider } from './ThemeProvider';
|
|
5
|
+
import {} from './const';
|
|
6
|
+
|
|
7
|
+
const InValidElement = React.createContext<undefined>(undefined).Provider;
|
|
8
|
+
const ComponentWithClassname = ({
|
|
9
|
+
className,
|
|
10
|
+
...rest
|
|
11
|
+
}: {
|
|
12
|
+
className?: string;
|
|
13
|
+
[key: string]: string | undefined;
|
|
14
|
+
}) => <div className={className} {...rest} />;
|
|
15
|
+
|
|
16
|
+
describe('ThemeProvider', () => {
|
|
17
|
+
it('appends theme to children', () => {
|
|
18
|
+
render(
|
|
19
|
+
<ThemeProvider theme="light">
|
|
20
|
+
<div>children 1</div>
|
|
21
|
+
<div>children 2</div>
|
|
22
|
+
</ThemeProvider>,
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
expect(screen.getByText('children 1')).toHaveClass('np-theme-light');
|
|
26
|
+
expect(screen.getByText('children 2')).toHaveClass('np-theme-light');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('concatenate className with children', () => {
|
|
30
|
+
render(
|
|
31
|
+
<ThemeProvider theme="light">
|
|
32
|
+
<div className="other class names">children</div>
|
|
33
|
+
</ThemeProvider>,
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
expect(screen.getByText('children')).toHaveClass('np-theme-light other class names');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('can nest theme providers', () => {
|
|
40
|
+
render(
|
|
41
|
+
<ThemeProvider theme="light">
|
|
42
|
+
<div>light</div>
|
|
43
|
+
<ThemeProvider theme="personal">
|
|
44
|
+
<div>personal</div>
|
|
45
|
+
<ThemeProvider theme="light">
|
|
46
|
+
<div>light</div>
|
|
47
|
+
</ThemeProvider>
|
|
48
|
+
</ThemeProvider>
|
|
49
|
+
</ThemeProvider>,
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
expect(screen.getAllByText('light')[0]).toHaveClass('np-theme-light');
|
|
53
|
+
expect(screen.getByText('personal')).toHaveClass('np-theme-personal');
|
|
54
|
+
expect(screen.getAllByText('light')[1]).toHaveClass('np-theme-light');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('can append className to a component', () => {
|
|
58
|
+
render(
|
|
59
|
+
<ThemeProvider theme="personal">
|
|
60
|
+
<ComponentWithClassname data-testid="button">Click me!</ComponentWithClassname>
|
|
61
|
+
</ThemeProvider>,
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
expect(screen.getByTestId('button')).toHaveClass('np-theme-personal');
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('displays console warning when children is not a valid React element', () => {
|
|
68
|
+
jest.spyOn(console, 'warn').mockImplementation(() => jest.fn());
|
|
69
|
+
|
|
70
|
+
render(
|
|
71
|
+
<ThemeProvider theme="personal">
|
|
72
|
+
<InValidElement value={undefined}>
|
|
73
|
+
<ComponentWithClassname data-testid="button">Click me!</ComponentWithClassname>
|
|
74
|
+
</InValidElement>
|
|
75
|
+
,
|
|
76
|
+
</ThemeProvider>,
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
expect(screen.getByTestId('button')).not.toHaveClass('np-theme-personal');
|
|
80
|
+
// eslint-disable-next-line no-console
|
|
81
|
+
expect(console.warn).toHaveBeenCalledWith(
|
|
82
|
+
'[ThemeProvider] Trying to inject `className` to an invalid React element, this will be skipped!',
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Children, cloneElement, createContext, isValidElement, PropsWithChildren } from 'react';
|
|
2
|
+
|
|
3
|
+
import { ComponentTheme, DEFAULT_COMPONENT_THEME } from './const';
|
|
4
|
+
|
|
5
|
+
export const ThemeContext = createContext<ComponentTheme>(DEFAULT_COMPONENT_THEME);
|
|
6
|
+
|
|
7
|
+
type ThemeProviderProps = PropsWithChildren<{
|
|
8
|
+
theme: ComponentTheme | undefined;
|
|
9
|
+
}>;
|
|
10
|
+
|
|
11
|
+
export const ThemeProvider = ({
|
|
12
|
+
theme = DEFAULT_COMPONENT_THEME,
|
|
13
|
+
children,
|
|
14
|
+
}: ThemeProviderProps) => {
|
|
15
|
+
const themedChildren = Children.map(children, (child) => {
|
|
16
|
+
if (!isValidElement(child)) {
|
|
17
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
18
|
+
// eslint-disable-next-line no-console
|
|
19
|
+
console.warn(
|
|
20
|
+
'[ThemeProvider] Trying to inject `className` to an invalid React element, this will be skipped!',
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
return child;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
27
|
+
const classNames = [`np-theme-${theme}`, child?.props?.className].filter(Boolean).join(' ');
|
|
28
|
+
|
|
29
|
+
return cloneElement(child, {
|
|
30
|
+
className: classNames,
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
32
|
+
} as any);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
return <ThemeContext.Provider value={theme}>{themedChildren}</ThemeContext.Provider>;
|
|
36
|
+
};
|
package/src/const.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react-hooks';
|
|
2
|
+
|
|
3
|
+
import { ThemeProvider } from './ThemeProvider';
|
|
4
|
+
import { useTheme } from './useTheme';
|
|
5
|
+
|
|
6
|
+
describe('useTheme', () => {
|
|
7
|
+
it('returns default light theme', () => {
|
|
8
|
+
const {
|
|
9
|
+
result: { current },
|
|
10
|
+
} = renderHook(() => useTheme());
|
|
11
|
+
|
|
12
|
+
expect(current).toStrictEqual('light');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('returns provided theme', () => {
|
|
16
|
+
const {
|
|
17
|
+
result: { current },
|
|
18
|
+
} = renderHook(() => useTheme(), {
|
|
19
|
+
wrapper: ThemeProvider,
|
|
20
|
+
initialProps: {
|
|
21
|
+
theme: 'personal' as const,
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
expect(current).toStrictEqual('personal');
|
|
26
|
+
});
|
|
27
|
+
});
|
package/src/useTheme.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
|
|
3
|
+
import { ThemeContext } from './ThemeProvider';
|
|
4
|
+
import { ComponentTheme, DEFAULT_COMPONENT_THEME } from './const';
|
|
5
|
+
|
|
6
|
+
export const useTheme = (): ComponentTheme => {
|
|
7
|
+
return useContext(ThemeContext) ?? DEFAULT_COMPONENT_THEME;
|
|
8
|
+
};
|