@equinor/cpl-theme-react 0.0.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/LICENCE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Equinor ASA
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ [<-- Go back](../../README.md)
2
+
3
+ # @equinor/cpl-theme-react
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ yarn add @equinor/cpl-theme-react
9
+ ```
10
+
11
+ ## Components
12
+
13
+ - `ThemeProvider` - A provider component which will only be rendered on the client side that wraps your application and manages theme mode state.
package/dist/index.css ADDED
@@ -0,0 +1,41 @@
1
+ /* src/styles.css */
2
+ :root[data-equinor-cpl-theme=light] {
3
+ --color-text: rgba(19, 38, 52, 1);
4
+ }
5
+ :root[data-equinor-cpl-theme=dark] {
6
+ --background-default: rgba(19, 38, 52, 1);
7
+ --background-raised: #243746;
8
+ --color-text: #ffffff;
9
+ --color-text-secondary: rgba(222, 229, 231, 1);
10
+ --color-text-tertiary: rgba(156, 166, 172, 1);
11
+ --color-text-static-icons-primary: rgba(255, 255, 255, 1);
12
+ --color-text-static-primary-black: rgba(0, 0, 0, 1);
13
+ --color-text-static-icons-secondary: rgba(222, 229, 231, 1);
14
+ --color-text-static-icons-tertiary: rgba(156, 166, 172, 1);
15
+ --interactive-primary-resting: rgba(151, 202, 206, 1);
16
+ --interactive-primary-hover-alt: rgba(173, 226, 230, 0.1);
17
+ --interactive-primary-hover: rgba(173, 226, 230, 1);
18
+ --interactive-secondary-resting: rgba(222, 229, 231, 1);
19
+ --interactive-secondary-highlight: rgba(255, 255, 255, 0.1);
20
+ --interactive-secondary-hover: rgba(255, 255, 255, 1);
21
+ --interactive-focus-border: #97cace;
22
+ --interactive-disabled-text: #637583;
23
+ --interactive-disabled-border: #405462;
24
+ --interactive-focus: rgba(0, 112, 121, 1);
25
+ --interactive-danger-text: rgba(255, 102, 112, 1);
26
+ --interactive-warning-text: rgba(255, 198, 122, 1);
27
+ --interactive-success-text: rgba(161, 218, 160, 1);
28
+ --interactive-table-header-fill-resting: #243746;
29
+ --line-color: #444c52;
30
+ --line-color-2: #394955;
31
+ --background-lighten: rgba(2, 1, 1, 0.16);
32
+ --eds-color-surface-input: rgba(0, 0, 0, 0.28);
33
+ --eds-color-text-input-placeholder: rgba(100, 114, 125, 1);
34
+ --eds-color-icon-interactive: rgba(166, 175, 181, 1);
35
+ --eds-color-text-static: rgba(166, 175, 181, 1);
36
+ --eds-color-text-static-2: rgba(194, 200, 204, 1);
37
+ }
38
+ body {
39
+ background-color: var(--background-default);
40
+ color: var(--color-text);
41
+ }
@@ -0,0 +1,87 @@
1
+ import { tokens } from '@equinor/eds-tokens';
2
+ import React from 'react';
3
+
4
+ /**
5
+ * Utility for deep partial
6
+ */
7
+ type DeepPartial<T> = {
8
+ [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
9
+ };
10
+ type EdsTokensType = typeof tokens;
11
+ type CplTokensType = DeepPartial<EdsTokensType>;
12
+
13
+ type ThemeMode = 'light' | 'dark' | undefined;
14
+ type CustomToken = Record<string, string>;
15
+ type ThemeContextType = {
16
+ mode: ThemeMode;
17
+ tokens?: CplTokensType & {
18
+ custom?: CustomToken;
19
+ };
20
+ toggleTheme: (mode: ThemeMode) => void;
21
+ };
22
+ /**
23
+ * `ThemeContext`
24
+ * The context used to provide and consume the current theme mode ("light" | "dark" | undefined).
25
+ * Exposes `mode` as the current theme and `toggleTheme` as a function to switch themes.
26
+ *
27
+ * Usage:
28
+ * ```tsx
29
+ * const { mode, toggleTheme, tokens } = useTheme();
30
+ * ```
31
+ */
32
+ declare const ThemeContext: React.Context<ThemeContextType | undefined>;
33
+ type ThemeProviderProps = {
34
+ mode?: ThemeMode;
35
+ children: React.ReactNode | null | undefined;
36
+ tokens?: CplTokensType & {
37
+ custom?: CustomToken;
38
+ };
39
+ };
40
+ /**
41
+ * `ThemeProvider`
42
+ *
43
+ * A provider component which will only be rendered on the client side that wraps your application and manages theme mode state.
44
+ * It takes an initial `mode` as an optional prop (default is undefined).
45
+ *
46
+ * - Sets a `data-equinor-cpl-theme` attribute on the document root to enable theme-based styling in CSS.
47
+ * - Exposes `mode`, tokens (if defined ), and `toggleTheme` for managing theme switching.
48
+ *
49
+ * Usage:
50
+ * ```tsx
51
+ * <ThemeProvider mode="dark" tokens={{custom:{},ui:{},...}} >
52
+ * <App />
53
+ * </ThemeProvider>
54
+ *
55
+ * ```
56
+ */
57
+ declare const ThemeProvider: ({ children, mode, tokens, }: ThemeProviderProps) => React.JSX.Element;
58
+
59
+ /**
60
+ * `useTheme`
61
+ * A custom hook to access the current theme and theme toggle function.
62
+ *
63
+ * - This hook returns `mode` (the current theme, either "light" or "dark") ,
64
+ * tokens ( to access the tokens in JSON format, if defined)
65
+ * and `toggleTheme` (a function to switch themes).
66
+ *
67
+ * - Must be used within a `ThemeProvider`, or it will throw an error.
68
+ *
69
+ * Usage:
70
+ * ```tsx
71
+ * const { mode, toggleTheme, tokens } = useTheme();
72
+ * toggleTheme("dark"); // Example: Switch to dark mode
73
+ * ```
74
+ *
75
+ * @throws Error if `useTheme` is called outside of a `ThemeProvider`.
76
+ */
77
+ declare const useTheme: () => {
78
+ mode: "light" | "dark" | undefined;
79
+ tokens?: CplTokensType & {
80
+ custom?: {
81
+ [x: string]: string;
82
+ };
83
+ };
84
+ toggleTheme: (mode: "light" | "dark" | undefined) => void;
85
+ };
86
+
87
+ export { type CplTokensType, type DeepPartial, ThemeContext, ThemeProvider, useTheme };
@@ -0,0 +1,87 @@
1
+ import { tokens } from '@equinor/eds-tokens';
2
+ import React from 'react';
3
+
4
+ /**
5
+ * Utility for deep partial
6
+ */
7
+ type DeepPartial<T> = {
8
+ [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
9
+ };
10
+ type EdsTokensType = typeof tokens;
11
+ type CplTokensType = DeepPartial<EdsTokensType>;
12
+
13
+ type ThemeMode = 'light' | 'dark' | undefined;
14
+ type CustomToken = Record<string, string>;
15
+ type ThemeContextType = {
16
+ mode: ThemeMode;
17
+ tokens?: CplTokensType & {
18
+ custom?: CustomToken;
19
+ };
20
+ toggleTheme: (mode: ThemeMode) => void;
21
+ };
22
+ /**
23
+ * `ThemeContext`
24
+ * The context used to provide and consume the current theme mode ("light" | "dark" | undefined).
25
+ * Exposes `mode` as the current theme and `toggleTheme` as a function to switch themes.
26
+ *
27
+ * Usage:
28
+ * ```tsx
29
+ * const { mode, toggleTheme, tokens } = useTheme();
30
+ * ```
31
+ */
32
+ declare const ThemeContext: React.Context<ThemeContextType | undefined>;
33
+ type ThemeProviderProps = {
34
+ mode?: ThemeMode;
35
+ children: React.ReactNode | null | undefined;
36
+ tokens?: CplTokensType & {
37
+ custom?: CustomToken;
38
+ };
39
+ };
40
+ /**
41
+ * `ThemeProvider`
42
+ *
43
+ * A provider component which will only be rendered on the client side that wraps your application and manages theme mode state.
44
+ * It takes an initial `mode` as an optional prop (default is undefined).
45
+ *
46
+ * - Sets a `data-equinor-cpl-theme` attribute on the document root to enable theme-based styling in CSS.
47
+ * - Exposes `mode`, tokens (if defined ), and `toggleTheme` for managing theme switching.
48
+ *
49
+ * Usage:
50
+ * ```tsx
51
+ * <ThemeProvider mode="dark" tokens={{custom:{},ui:{},...}} >
52
+ * <App />
53
+ * </ThemeProvider>
54
+ *
55
+ * ```
56
+ */
57
+ declare const ThemeProvider: ({ children, mode, tokens, }: ThemeProviderProps) => React.JSX.Element;
58
+
59
+ /**
60
+ * `useTheme`
61
+ * A custom hook to access the current theme and theme toggle function.
62
+ *
63
+ * - This hook returns `mode` (the current theme, either "light" or "dark") ,
64
+ * tokens ( to access the tokens in JSON format, if defined)
65
+ * and `toggleTheme` (a function to switch themes).
66
+ *
67
+ * - Must be used within a `ThemeProvider`, or it will throw an error.
68
+ *
69
+ * Usage:
70
+ * ```tsx
71
+ * const { mode, toggleTheme, tokens } = useTheme();
72
+ * toggleTheme("dark"); // Example: Switch to dark mode
73
+ * ```
74
+ *
75
+ * @throws Error if `useTheme` is called outside of a `ThemeProvider`.
76
+ */
77
+ declare const useTheme: () => {
78
+ mode: "light" | "dark" | undefined;
79
+ tokens?: CplTokensType & {
80
+ custom?: {
81
+ [x: string]: string;
82
+ };
83
+ };
84
+ toggleTheme: (mode: "light" | "dark" | undefined) => void;
85
+ };
86
+
87
+ export { type CplTokensType, type DeepPartial, ThemeContext, ThemeProvider, useTheme };
package/dist/index.js ADDED
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ ThemeContext: () => ThemeContext,
34
+ ThemeProvider: () => ThemeProvider,
35
+ useTheme: () => useTheme
36
+ });
37
+ module.exports = __toCommonJS(src_exports);
38
+
39
+ // src/tokens.ts
40
+ var tokens = {
41
+ /**
42
+ * Generic CplTokens in json format which can be overridden for project specific
43
+ */
44
+ /**
45
+ @ Example
46
+ colors:{
47
+ ui:{
48
+ background__default: {
49
+ hex:''
50
+ hsla:''
51
+ rgba:''
52
+ }
53
+ }
54
+ }
55
+ */
56
+ };
57
+ var tokens_default = tokens;
58
+
59
+ // src/ThemeProvider.tsx
60
+ var import_react = __toESM(require("react"));
61
+ var attributeSelector = `data-equinor-cpl-theme`;
62
+ var ThemeContext = import_react.default.createContext(void 0);
63
+ var ThemeProvider = ({
64
+ children,
65
+ mode = void 0,
66
+ tokens: tokens2 = tokens_default
67
+ }) => {
68
+ const [initialMode, setInitialModeMode] = import_react.default.useState(mode);
69
+ const toggleTheme = import_react.default.useCallback((newMode) => setInitialModeMode(newMode), []);
70
+ import_react.default.useEffect(() => {
71
+ if (initialMode) {
72
+ document.documentElement.setAttribute(attributeSelector, initialMode);
73
+ }
74
+ }, [initialMode]);
75
+ const applyCustomTokensToCSS = import_react.default.useCallback(
76
+ (props) => {
77
+ const selector = `:root[${attributeSelector}="${mode}"]`;
78
+ for (const stylesheet of Array.from(document.styleSheets)) {
79
+ try {
80
+ for (const rule of Array.from(stylesheet.cssRules)) {
81
+ if (rule instanceof CSSStyleRule && rule.selectorText === selector) {
82
+ Object.keys(props.tokens).forEach((key) => {
83
+ rule.style.setProperty(`${key}`, props.tokens[key]);
84
+ });
85
+ }
86
+ }
87
+ } catch (_) {
88
+ }
89
+ }
90
+ },
91
+ [mode]
92
+ );
93
+ import_react.default.useEffect(() => {
94
+ if (initialMode && tokens2.custom) {
95
+ applyCustomTokensToCSS({ tokens: tokens2.custom, mode: initialMode });
96
+ }
97
+ }, [tokens2, initialMode, applyCustomTokensToCSS]);
98
+ return /* @__PURE__ */ import_react.default.createElement(ThemeContext.Provider, { value: { mode: initialMode, toggleTheme, tokens: tokens2 } }, children);
99
+ };
100
+
101
+ // src/useTheme.ts
102
+ var import_react2 = require("react");
103
+ var useTheme = () => {
104
+ const context = (0, import_react2.useContext)(ThemeContext);
105
+ if (!context) {
106
+ throw new Error("useTheme must be used within a ThemeProvider");
107
+ }
108
+ return context;
109
+ };
110
+ // Annotate the CommonJS export names for ESM import in node:
111
+ 0 && (module.exports = {
112
+ ThemeContext,
113
+ ThemeProvider,
114
+ useTheme
115
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,76 @@
1
+ // src/tokens.ts
2
+ var tokens = {
3
+ /**
4
+ * Generic CplTokens in json format which can be overridden for project specific
5
+ */
6
+ /**
7
+ @ Example
8
+ colors:{
9
+ ui:{
10
+ background__default: {
11
+ hex:''
12
+ hsla:''
13
+ rgba:''
14
+ }
15
+ }
16
+ }
17
+ */
18
+ };
19
+ var tokens_default = tokens;
20
+
21
+ // src/ThemeProvider.tsx
22
+ import React from "react";
23
+ var attributeSelector = `data-equinor-cpl-theme`;
24
+ var ThemeContext = React.createContext(void 0);
25
+ var ThemeProvider = ({
26
+ children,
27
+ mode = void 0,
28
+ tokens: tokens2 = tokens_default
29
+ }) => {
30
+ const [initialMode, setInitialModeMode] = React.useState(mode);
31
+ const toggleTheme = React.useCallback((newMode) => setInitialModeMode(newMode), []);
32
+ React.useEffect(() => {
33
+ if (initialMode) {
34
+ document.documentElement.setAttribute(attributeSelector, initialMode);
35
+ }
36
+ }, [initialMode]);
37
+ const applyCustomTokensToCSS = React.useCallback(
38
+ (props) => {
39
+ const selector = `:root[${attributeSelector}="${mode}"]`;
40
+ for (const stylesheet of Array.from(document.styleSheets)) {
41
+ try {
42
+ for (const rule of Array.from(stylesheet.cssRules)) {
43
+ if (rule instanceof CSSStyleRule && rule.selectorText === selector) {
44
+ Object.keys(props.tokens).forEach((key) => {
45
+ rule.style.setProperty(`${key}`, props.tokens[key]);
46
+ });
47
+ }
48
+ }
49
+ } catch (_) {
50
+ }
51
+ }
52
+ },
53
+ [mode]
54
+ );
55
+ React.useEffect(() => {
56
+ if (initialMode && tokens2.custom) {
57
+ applyCustomTokensToCSS({ tokens: tokens2.custom, mode: initialMode });
58
+ }
59
+ }, [tokens2, initialMode, applyCustomTokensToCSS]);
60
+ return /* @__PURE__ */ React.createElement(ThemeContext.Provider, { value: { mode: initialMode, toggleTheme, tokens: tokens2 } }, children);
61
+ };
62
+
63
+ // src/useTheme.ts
64
+ import { useContext } from "react";
65
+ var useTheme = () => {
66
+ const context = useContext(ThemeContext);
67
+ if (!context) {
68
+ throw new Error("useTheme must be used within a ThemeProvider");
69
+ }
70
+ return context;
71
+ };
72
+ export {
73
+ ThemeContext,
74
+ ThemeProvider,
75
+ useTheme
76
+ };
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@equinor/cpl-theme-react",
3
+ "version": "0.0.1",
4
+ "license": "MIT",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist/**"
10
+ ],
11
+ "dependencies": {
12
+ "@equinor/eds-icons": "^0.21.0",
13
+ "@equinor/eds-tokens": "^0.9.2"
14
+ },
15
+ "devDependencies": {
16
+ "@equinor/eds-core-react": "^0.42.1",
17
+ "@storybook/react": "^8.1.11",
18
+ "@storybook/test": "^8.1.11",
19
+ "@types/react": "^18.3.3",
20
+ "@types/react-dom": "^18.3.0",
21
+ "@types/styled-components": "^5.1.34",
22
+ "eslint": "^8.56.0",
23
+ "react": "^18.2.0",
24
+ "react-dom": "^18.2.0",
25
+ "styled-components": "^6.1.11",
26
+ "tsup": "^8.1.0",
27
+ "eslint-config-custom": "0.0.5",
28
+ "tsconfig": "0.0.1"
29
+ },
30
+ "peerDependencies": {
31
+ "@equinor/eds-core-react": ">=0.42.1",
32
+ "react": ">=18.2.0",
33
+ "react-dom": ">=18.2.0",
34
+ "styled-components": ">=5.3.11"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public"
38
+ },
39
+ "scripts": {
40
+ "build": "tsup src/index.ts --format esm,cjs --dts --external react",
41
+ "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
42
+ "dev": "tsup src/index.ts --format esm,cjs --watch --dts --external react",
43
+ "lint": "TIMING=1 eslint . --fix"
44
+ }
45
+ }