cyhip-dynamic-themes 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 CyberHippie-io
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,2 @@
1
+ # cyhip-dynamic-themes
2
+ Dynamic Color Themes for React Apps
@@ -0,0 +1 @@
1
+ export declare const version: string;
@@ -0,0 +1,2 @@
1
+ import packageJson from "./package.json";
2
+ export var version = packageJson.version;
@@ -0,0 +1,6 @@
1
+ export { consistentChroma } from "./lib/tw-dynamic-themes/runtime";
2
+ export { dynamicTwClasses } from "./lib/tw-dynamic-themes/twPlugin";
3
+ export { hueScheme as defaultHueScheme } from "./src/hue-scheme";
4
+ export { currentAccentValue, getThemeProperties } from "./src/theme-helpers";
5
+ export { useColorTheme } from "./src/theme-hook";
6
+ export { version } from "./__version__";
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export { consistentChroma } from "./lib/tw-dynamic-themes/runtime";
2
+ export { dynamicTwClasses } from "./lib/tw-dynamic-themes/twPlugin";
3
+ export { hueScheme as defaultHueScheme } from "./src/hue-scheme";
4
+ export { currentAccentValue, getThemeProperties } from "./src/theme-helpers";
5
+ export { useColorTheme } from "./src/theme-hook";
6
+ export { version } from "./__version__";
@@ -0,0 +1,7 @@
1
+ export const shades: number[];
2
+ export function makeVariable({ fallbackValue, name, shade, withVar }: {
3
+ fallbackValue: any;
4
+ name: any;
5
+ shade: any;
6
+ withVar: any;
7
+ }): string;
@@ -0,0 +1,20 @@
1
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
2
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
3
+ if (ar || !(i in from)) {
4
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
5
+ ar[i] = from[i];
6
+ }
7
+ }
8
+ return to.concat(ar || Array.prototype.slice.call(from));
9
+ };
10
+ export var shades = __spreadArray(__spreadArray([
11
+ 50
12
+ ], Array.from({ length: 9 }).map(function (_, i) { return (i + 1) * 100; }), true), [
13
+ 950,
14
+ ], false);
15
+ export var makeVariable = function (_a) {
16
+ var fallbackValue = _a.fallbackValue, name = _a.name, shade = _a.shade, withVar = _a.withVar;
17
+ var variable = "--".concat(name, "-").concat(shade);
18
+ var value = fallbackValue ? variable + ", " + fallbackValue : variable;
19
+ return withVar ? "var(".concat(value, ")") : variable;
20
+ };
@@ -0,0 +1,14 @@
1
+ /**
2
+ * A map of CSS varable name to color
3
+ */
4
+ type SingleVariable = [string, string];
5
+ export declare function getVariables({ baseName, hue, mode, monoCromatic, }: {
6
+ baseName: string;
7
+ hue: number;
8
+ mode?: "bright" | "consistent";
9
+ monoCromatic?: boolean;
10
+ }): SingleVariable[];
11
+ export declare function updateVariables(variables: SingleVariable[], el?: HTMLElement): void;
12
+ export declare const highestChroma: (shadeIndex: number, hue: number) => string;
13
+ export declare const consistentChroma: (i: number, hue: number, monoCromatic?: boolean) => string;
14
+ export {};
@@ -0,0 +1,88 @@
1
+ import { toGamut as _toGamut, converter, differenceEuclidean, } from "culori";
2
+ import { makeVariable, shades } from "./common";
3
+ var toGamut = _toGamut;
4
+ export function getVariables(_a) {
5
+ var baseName = _a.baseName, hue = _a.hue, _b = _a.mode, mode = _b === void 0 ? "consistent" : _b, _c = _a.monoCromatic, monoCromatic = _c === void 0 ? false : _c;
6
+ var calculator = mode === "bright" ? highestChroma : consistentChroma;
7
+ return shades.map(function (shade, shadeIndex) { return [
8
+ makeVariable({ name: baseName, shade: shade }),
9
+ calculator(shadeIndex, hue, monoCromatic),
10
+ ]; });
11
+ }
12
+ export function updateVariables(variables, el) {
13
+ var target = el !== null && el !== void 0 ? el : document.documentElement;
14
+ for (var _i = 0, variables_1 = variables; _i < variables_1.length; _i++) {
15
+ var _a = variables_1[_i], varName = _a[0], value = _a[1];
16
+ target.style.setProperty(varName, value + "");
17
+ }
18
+ }
19
+ var lightnessForShade = function (shade) {
20
+ var highestL = 100;
21
+ var lowestL = 13;
22
+ var diffL = highestL - lowestL;
23
+ var shadeDiff = shades[shades.length - 1] - shades[0];
24
+ // Maintaining the proximity of colors with a step of 50 and 100
25
+ var multiplier = shade / shadeDiff;
26
+ return (lowestL + (highestL - diffL * multiplier)) / 100;
27
+ };
28
+ var lightness = shades.map(lightnessForShade);
29
+ export var highestChroma = function (shadeIndex, hue) {
30
+ var oklch = converter("oklch");
31
+ // Setting an obsurdly high chroma
32
+ var color = "oklch(".concat(lightness[shadeIndex], " 0.4 ").concat(hue, ")");
33
+ // Clamping it to the highest chroma possible
34
+ return serializeColor(oklch(toGamut("p3", "oklch", differenceEuclidean("oklch"), 0)(color)));
35
+ };
36
+ export var consistentChroma = function (i, hue, monoCromatic) {
37
+ if (monoCromatic === void 0) { monoCromatic = false; }
38
+ var oklch = converter("oklch");
39
+ // Using a pinned chroma
40
+ var chroma = monoCromatic ? chromaData[i] * 0.00001 : chromaData[i];
41
+ var light = monoCromatic ? lightness[i] * 1.0 : lightness[i] * 0.95;
42
+ // if (monoCromatic && i > 4) {
43
+ // light = 0.24 + i / 10;
44
+ // chroma = 0;
45
+ // }
46
+ var color = "oklch(".concat(light, " ").concat(chroma, " ").concat(hue, ")");
47
+ return serializeColor(oklch(toGamut("p3", "oklch", differenceEuclidean("oklch"), 0)(color)));
48
+ };
49
+ // const chromaData: Record<number, number> = {
50
+ // 0: 0.0108,
51
+ // 1: 0.0321,
52
+ // 2: 0.0609,
53
+ // 3: 0.0908,
54
+ // 4: 0.1398,
55
+ // 5: 0.1472,
56
+ // 6: 0.1299,
57
+ // 7: 0.1067,
58
+ // 8: 0.0898,
59
+ // 9: 0.0726,
60
+ // 10: 0.054,
61
+ // };
62
+ var chromaData = {
63
+ 0: 0.0114,
64
+ 1: 0.0331,
65
+ 2: 0.0774,
66
+ 3: 0.1275,
67
+ 4: 0.1547,
68
+ 5: 0.1355,
69
+ 6: 0.1164,
70
+ 7: 0.0974,
71
+ 8: 0.0782,
72
+ 9: 0.0588,
73
+ 10: 0.0491,
74
+ };
75
+ // const chromaData: Record<number, number> = {
76
+ // 0: 0.0,
77
+ // 1: 0.0,
78
+ // 2: 0.0,
79
+ // 3: 0.0,
80
+ // 4: 0.0,
81
+ // 5: 0.0,
82
+ // 6: 0.0,
83
+ // 7: 0.0,
84
+ // 8: 0.0,
85
+ // 9: 0.0,
86
+ // 10: 0.0,
87
+ // };
88
+ var serializeColor = function (c) { var _a; return "".concat(c.l.toFixed(3), " ").concat(c.c.toFixed(3), " ").concat((_a = c.h) === null || _a === void 0 ? void 0 : _a.toFixed(3)); };
@@ -0,0 +1,3 @@
1
+ export function dynamicTwClasses(baseName: any, baseHue: any): {
2
+ [k: string]: string;
3
+ };
@@ -0,0 +1,16 @@
1
+ import { makeVariable, shades } from "./common";
2
+ import { consistentChroma } from "./runtime";
3
+ export function dynamicTwClasses(baseName, baseHue) {
4
+ return Object.fromEntries(shades.map(function (shade, i) {
5
+ var color = consistentChroma(i, baseHue);
6
+ return [
7
+ shade,
8
+ "oklch(".concat(makeVariable({
9
+ fallbackValue: color,
10
+ name: baseName,
11
+ shade: shade,
12
+ withVar: true,
13
+ }), " / <alpha-value>)"),
14
+ ];
15
+ }));
16
+ }
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "cyhip-dynamic-themes",
3
+ "version": "0.1.1",
4
+ "description": "Tailwind-powered dynamic color themes for React apps.",
5
+ "author": "@KassioRF, @CyberHippie-io",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/CyberHippie-io/cyhip-dynamic-themes"
10
+ },
11
+ "keywords": [
12
+ "react",
13
+ "tailwind",
14
+ "theme",
15
+ "color",
16
+ "dynamic"
17
+ ],
18
+ "files": [
19
+ "dist",
20
+ "README.md",
21
+ "LICENSE"
22
+ ],
23
+ "main": "./dist/index.js",
24
+ "module": "./dist/index.esm.js",
25
+ "types": "./dist/index.d.ts",
26
+ "scripts": {
27
+ "build": "tsc --project tsconfig.json",
28
+ "start": "tsx src/index.ts",
29
+ "prepublishOnly": "yarn build"
30
+ },
31
+ "devDependencies": {
32
+ "@types/culori": "^2.1.1",
33
+ "@types/react": "^18.3.11",
34
+ "tsx": "^4.19.1",
35
+ "typescript": "^5.6.3"
36
+ },
37
+ "dependencies": {
38
+ "culori": "^4.0.1",
39
+ "react": "^18.3.1",
40
+ "react-dom": "^18.3.1"
41
+ },
42
+ "peerDependencies": {
43
+ "react": "^18.0.0",
44
+ "react-dom": "^18.0.0"
45
+ },
46
+ "exports": {
47
+ ".": {
48
+ "import": {
49
+ "types": "./dist/index.d.ts",
50
+ "default": "./dist/index.js"
51
+ },
52
+ "require": {
53
+ "types": "./dist/index.d.ts",
54
+ "default": "./dist/index.js"
55
+ }
56
+ }
57
+ }
58
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * HUE THEMES
3
+ *
4
+ * Define the available color palettes here!
5
+ *
6
+ * The palettes are based on HUE values.
7
+ *
8
+ * To add or modify a HUE palette, explore and preview colors at:
9
+ * https://oklch.com/#70,0.1,250,100
10
+ *
11
+ */
12
+ declare const hueScheme: Record<string, string>;
13
+ export { hueScheme };
@@ -0,0 +1,20 @@
1
+ /**
2
+ * HUE THEMES
3
+ *
4
+ * Define the available color palettes here!
5
+ *
6
+ * The palettes are based on HUE values.
7
+ *
8
+ * To add or modify a HUE palette, explore and preview colors at:
9
+ * https://oklch.com/#70,0.1,250,100
10
+ *
11
+ */
12
+ var hueScheme = {
13
+ monoCromatic: "-1",
14
+ blue: "250",
15
+ green: "150",
16
+ orange: "35",
17
+ pink: "0",
18
+ purple: "316",
19
+ };
20
+ export { hueScheme };
@@ -0,0 +1,5 @@
1
+ export { consistentChroma } from "../lib/tw-dynamic-themes/runtime";
2
+ export { dynamicTwClasses } from "../lib/tw-dynamic-themes/twPlugin";
3
+ export { hueScheme as defaultHueScheme } from "./hue-scheme";
4
+ export { currentAccentValue, getThemeProperties } from "./theme-helpers";
5
+ export { useColorTheme } from "./theme-hook";
@@ -0,0 +1,5 @@
1
+ export { consistentChroma } from "../lib/tw-dynamic-themes/runtime";
2
+ export { dynamicTwClasses } from "../lib/tw-dynamic-themes/twPlugin";
3
+ export { hueScheme as defaultHueScheme } from "./hue-scheme";
4
+ export { currentAccentValue, getThemeProperties } from "./theme-helpers";
5
+ export { useColorTheme } from "./theme-hook";
@@ -0,0 +1,25 @@
1
+ /**
2
+ * getThemeProperties
3
+ *
4
+ * Defines CSS class and style properties based on the provided HUE value and dark mode setting.
5
+ *
6
+ * - If `hue` is "-1", the theme is monochromatic, which affects the accent color behavior.
7
+ * - Returns an object with:
8
+ * - `className`: A string containing the class for dark mode ("dark") or an empty string for light mode.
9
+ * - `style`: A record with dynamically generated CSS variables for the accent colors.
10
+ *
11
+ */
12
+ export declare const getThemeProperties: (hue: string, darkMode: boolean) => {
13
+ className: string;
14
+ style: Record<string, string>;
15
+ };
16
+ /**
17
+ * Retrieves the current value of a specified CSS variable from the root element.
18
+ *
19
+ * - If the function is executed in a non-browser environment (e.g., during server-side rendering), it returns `null`.
20
+ * - If the CSS variable is not found or its value is empty, it also returns `null`.
21
+ *
22
+ * @param variableName - The name of the CSS variable to retrieve, e.g., "--accent-500".
23
+ * @returns The OKLCH color value for the provided CSS variable, e.g., "0.614 0.136 250.000", or `null` if not available.
24
+ */
25
+ export declare const currentAccentValue: (variableName: string) => string | null;
@@ -0,0 +1,52 @@
1
+ import { getVariables } from "../lib/tw-dynamic-themes/runtime";
2
+ /**
3
+ * getThemeProperties
4
+ *
5
+ * Defines CSS class and style properties based on the provided HUE value and dark mode setting.
6
+ *
7
+ * - If `hue` is "-1", the theme is monochromatic, which affects the accent color behavior.
8
+ * - Returns an object with:
9
+ * - `className`: A string containing the class for dark mode ("dark") or an empty string for light mode.
10
+ * - `style`: A record with dynamically generated CSS variables for the accent colors.
11
+ *
12
+ */
13
+ export var getThemeProperties = function (hue, darkMode) {
14
+ var monoCromatic = hue == "-1";
15
+ var accent = getVariables({
16
+ baseName: "accent",
17
+ hue: +hue,
18
+ monoCromatic: monoCromatic,
19
+ });
20
+ // MonoCromatic have a different accent behavior for accent values
21
+ if (monoCromatic) {
22
+ accent.push([
23
+ "--accent-500",
24
+ darkMode ? "1.000 0.000 89.876" : "0.212 0.000 359.000",
25
+ ]);
26
+ accent.push([
27
+ "--accent-50",
28
+ darkMode ? "0.212 0.000 359.000" : "1.000 0.000 89.876",
29
+ ]);
30
+ }
31
+ return {
32
+ className: darkMode ? "dark" : "",
33
+ style: Object.fromEntries(accent),
34
+ };
35
+ };
36
+ /**
37
+ * Retrieves the current value of a specified CSS variable from the root element.
38
+ *
39
+ * - If the function is executed in a non-browser environment (e.g., during server-side rendering), it returns `null`.
40
+ * - If the CSS variable is not found or its value is empty, it also returns `null`.
41
+ *
42
+ * @param variableName - The name of the CSS variable to retrieve, e.g., "--accent-500".
43
+ * @returns The OKLCH color value for the provided CSS variable, e.g., "0.614 0.136 250.000", or `null` if not available.
44
+ */
45
+ export var currentAccentValue = function (variableName) {
46
+ if (typeof window === "undefined") {
47
+ return null;
48
+ }
49
+ var rootElement = document.documentElement;
50
+ var computedStyle = getComputedStyle(rootElement);
51
+ return computedStyle.getPropertyValue(variableName).trim() || null;
52
+ };
@@ -0,0 +1,17 @@
1
+ /**
2
+ * useColorTheme
3
+ *
4
+ * A custom hook that manages the application of color themes based on
5
+ * the provided HUE value and dark mode setting.
6
+ *
7
+ * It updates the document's root element with the appropriate
8
+ * class name and CSS variables for the theme.
9
+ *
10
+ * Note: This hook dispatches a custom event 'themeChange' when the theme changes.
11
+ * You can listen for this event to update components that depend on the theme.
12
+ *
13
+ */
14
+ declare const useColorTheme: (hue: string, darkMode: boolean) => {
15
+ setTheme: (newHue: string, newDarkMode: boolean) => void;
16
+ };
17
+ export { useColorTheme };
@@ -0,0 +1,35 @@
1
+ import { useCallback, useEffect } from "react";
2
+ import { getThemeProperties } from "./theme-helpers";
3
+ /**
4
+ * useColorTheme
5
+ *
6
+ * A custom hook that manages the application of color themes based on
7
+ * the provided HUE value and dark mode setting.
8
+ *
9
+ * It updates the document's root element with the appropriate
10
+ * class name and CSS variables for the theme.
11
+ *
12
+ * Note: This hook dispatches a custom event 'themeChange' when the theme changes.
13
+ * You can listen for this event to update components that depend on the theme.
14
+ *
15
+ */
16
+ var useColorTheme = function (hue, darkMode) {
17
+ var setTheme = useCallback(function (newHue, newDarkMode) {
18
+ var _a = getThemeProperties(newHue, newDarkMode), className = _a.className, style = _a.style;
19
+ document.documentElement.className = className;
20
+ document.documentElement.style.setProperty("color-scheme", newDarkMode ? "dark" : "light");
21
+ Object.entries(style).forEach(function (_a) {
22
+ var key = _a[0], value = _a[1];
23
+ document.documentElement.style.setProperty(key, value);
24
+ });
25
+ var themeChangeEvent = new CustomEvent("themeChange", {
26
+ detail: { hue: newHue, darkMode: newDarkMode },
27
+ });
28
+ window.dispatchEvent(themeChangeEvent);
29
+ }, []);
30
+ useEffect(function () {
31
+ setTheme(hue, darkMode);
32
+ }, [hue, darkMode, setTheme]);
33
+ return { setTheme: setTheme };
34
+ };
35
+ export { useColorTheme };
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "cyhip-dynamic-themes",
3
+ "version": "0.1.1",
4
+ "description": "Tailwind-powered dynamic color themes for React apps.",
5
+ "author": "@KassioRF, @CyberHippie-io",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/CyberHippie-io/cyhip-dynamic-themes"
10
+ },
11
+ "keywords": [
12
+ "react",
13
+ "tailwind",
14
+ "theme",
15
+ "color",
16
+ "dynamic"
17
+ ],
18
+ "files": [
19
+ "dist",
20
+ "README.md",
21
+ "LICENSE"
22
+ ],
23
+ "main": "./dist/index.js",
24
+ "module": "./dist/index.esm.js",
25
+ "types": "./dist/index.d.ts",
26
+ "devDependencies": {
27
+ "@types/culori": "^2.1.1",
28
+ "@types/react": "^18.3.11",
29
+ "tsx": "^4.19.1",
30
+ "typescript": "^5.6.3"
31
+ },
32
+ "dependencies": {
33
+ "culori": "^4.0.1",
34
+ "react": "^18.3.1",
35
+ "react-dom": "^18.3.1"
36
+ },
37
+ "peerDependencies": {
38
+ "react": "^18.0.0",
39
+ "react-dom": "^18.0.0"
40
+ },
41
+ "exports": {
42
+ ".": {
43
+ "import": {
44
+ "types": "./dist/index.d.ts",
45
+ "default": "./dist/index.js"
46
+ },
47
+ "require": {
48
+ "types": "./dist/index.d.ts",
49
+ "default": "./dist/index.js"
50
+ }
51
+ }
52
+ },
53
+ "scripts": {
54
+ "build": "tsc --project tsconfig.json",
55
+ "start": "tsx src/index.ts"
56
+ }
57
+ }