@inkeep/cxkit-color-mode 0.0.0-dev-20250221234142

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 Chakra UI
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 @@
1
+ # @inkeep
@@ -0,0 +1 @@
1
+ "use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const x=require("react/jsx-runtime"),n=require("react"),L=["light","dark"],P="(prefers-color-scheme: dark)",T=typeof window>"u",D=n.createContext(void 0),_={setColorMode:e=>{},colorModes:[]},I=()=>n.useContext(D)??_;function q(e,l){const t=I();return(t.forcedColorMode??t.resolvedColorMode)==="dark"?l:e}const z=e=>(n.useContext(D),x.jsx(N,{...e})),J=()=>{},Q={setColorMode:J,colorModes:["dark"],colorMode:"dark",forcedColorMode:"dark",resolvedColorMode:"dark",systemColorMode:"dark"},F=({children:e})=>x.jsx(D.Provider,{value:Q,children:x.jsx("div",{"data-theme":"dark",children:e})}),G=["light","dark"],N=({forcedColorMode:e,disableTransitionOnChange:l=!1,enableSystem:t=!1,enableColorScheme:y=!0,storageKey:m="inkeep-colorMode",colorModes:r=G,defaultColorMode:o=t?"system":"light",attribute:M="data-theme",value:$,children:S,nonce:j,shadowHostId:p,rootId:h})=>{const[i,a]=n.useState(()=>R(m,o)),[w,d]=n.useState(()=>R(m)),u=$?Object.values($):r,g=n.useCallback(s=>{let c=s;if(!c)return;s==="system"&&t&&(c=B());const C=$?$[c]:c,O=l?W():null,A=p&&h&&document.getElementById(p)?.shadowRoot,E=A?A.getElementById(h)??document.documentElement:document.documentElement;if(M==="class"?(E.classList.remove(...u),C&&E.classList.add(C)):C?E.setAttribute(M,C):E.removeAttribute(M),y){const V=L.includes(o)?o:null,b=L.includes(c)?c:V;E.style.colorScheme=b}O?.()},[]),f=n.useCallback(s=>{a(s);try{localStorage.setItem(m,s)}catch{}},[e]),v=n.useCallback(s=>{const c=B(s);d(c),i==="system"&&t&&!e&&g("system")},[i,e]);n.useEffect(()=>{const s=window.matchMedia(P);return s.addListener(v),v(s),()=>s.removeListener(v)},[v]),n.useEffect(()=>{const s=c=>{if(c.key!==m)return;const C=c.newValue||o;f(C)};return window.addEventListener("storage",s),()=>window.removeEventListener("storage",s)},[f]),n.useEffect(()=>{g(e??i)},[e,i]);const k=n.useMemo(()=>({colorMode:i,setColorMode:f,forcedColorMode:e,resolvedColorMode:i==="system"?w:i,colorModes:t?[...r,"system"]:r,systemColorMode:t?w:void 0}),[i,f,e,w,t,r]);return x.jsxs(D.Provider,{value:k,children:[x.jsx(U,{forcedColorMode:e,disableTransitionOnChange:l,enableSystem:t,enableColorScheme:y,storageKey:m,colorModes:r,defaultColorMode:o,attribute:M,value:$,children:S,attrs:u,nonce:j,shadowHostId:p,rootId:h}),S]})},U=n.memo(({forcedColorMode:e,storageKey:l,attribute:t,enableSystem:y,enableColorScheme:m,defaultColorMode:r,value:o,attrs:M,nonce:$,shadowHostId:S,rootId:j})=>{const p=r==="system",h=(()=>{const d=S&&j?`document.getElementById('${S}').shadowRoot.getElementById('${j}')`:"document.documentElement";if(t==="class"){const u=`c.remove(${M.map(g=>`'${g}'`).join(",")})`;return`var d=${d},c=d.classList;${u};`}return`var d=${d},n='${t}',s='setAttribute';`})(),i=m?(L.includes(r)?r:null)?`if(e==='light'||e==='dark'||!e)d.style.colorScheme=e||'${r}'`:"if(e==='light'||e==='dark')d.style.colorScheme=e":"",a=(d,u=!1,g=!0)=>{const f=o?o[d]:d,v=u?`${d}|| ''`:`'${f}'`;let k="";return m&&g&&!u&&L.includes(d)&&(k+=`d.style.colorScheme = '${d}';`),t==="class"?u||f?k+=`c.add(${v})`:k+="null":f&&(k+=`d[s](n,${v})`),k},w=e?`!function(){${h}${a(e)}}()`:y?`!function(){try{${h}var e=localStorage.getItem('${l}');if('system'===e||(!e&&${p})){var t='${P}',m=window.matchMedia(t);if(m.media!==t||m.matches){${a("dark")}}else{${a("light")}}}else if(e){${o?`var x=${JSON.stringify(o)};`:""}${a(o?"x[e]":"e",!0)}}${p?"":`else{${a(r,!1,!1)}}`}${i}}catch(e){}}()`:`!function(){try{${h}var e=localStorage.getItem('${l}');if(e){${o?`var x=${JSON.stringify(o)};`:""}${a(o?"x[e]":"e",!0)}}else{${a(r,!1,!1)};}${i}}catch(t){}}();`;return x.jsx("script",{nonce:$,dangerouslySetInnerHTML:{__html:w}})},()=>!0),R=(e,l)=>{if(T)return;let t;try{t=localStorage.getItem(e)||void 0}catch{}return t||l},W=()=>{const e=document.createElement("style");return e.appendChild(document.createTextNode("*{-webkit-transition:none!important;-moz-transition:none!important;-o-transition:none!important;-ms-transition:none!important;transition:none!important}")),document.head.appendChild(e),()=>{window.getComputedStyle(document.body),setTimeout(()=>{document.head.removeChild(e)},1)}},B=e=>(e||(e=window.matchMedia(P)),e.matches?"dark":"light");exports.ColorMode=N;exports.ColorModeProvider=z;exports.DarkModeProvider=F;exports.useColorMode=I;exports.useColorModeValue=q;
@@ -0,0 +1,21 @@
1
+ import { ColorModeProviderProps } from '@inkeep/cxkit-types';
2
+ export type ColorMode = 'light' | 'dark';
3
+ export declare const useColorMode: () => any;
4
+ /**
5
+ * Change value based on color mode.
6
+ *
7
+ * @param light the light mode value
8
+ * @param dark the dark mode value
9
+ *
10
+ * @example
11
+ *
12
+ * ```js
13
+ * const Icon = useColorModeValue(MoonIcon, SunIcon)
14
+ * ```
15
+ */
16
+ export declare function useColorModeValue<TLight = unknown, TDark = unknown>(light: TLight, dark: TDark): TLight | TDark;
17
+ export declare const ColorModeProvider: React.FC<ColorModeProviderProps>;
18
+ export declare const DarkModeProvider: ({ children }: {
19
+ children: React.ReactNode;
20
+ }) => import("react/jsx-runtime").JSX.Element;
21
+ export declare const ColorMode: React.FC<ColorModeProviderProps>;
@@ -0,0 +1,21 @@
1
+ import { ColorModeProviderProps } from '@inkeep/cxkit-types';
2
+ export type ColorMode = 'light' | 'dark';
3
+ export declare const useColorMode: () => any;
4
+ /**
5
+ * Change value based on color mode.
6
+ *
7
+ * @param light the light mode value
8
+ * @param dark the dark mode value
9
+ *
10
+ * @example
11
+ *
12
+ * ```js
13
+ * const Icon = useColorModeValue(MoonIcon, SunIcon)
14
+ * ```
15
+ */
16
+ export declare function useColorModeValue<TLight = unknown, TDark = unknown>(light: TLight, dark: TDark): TLight | TDark;
17
+ export declare const ColorModeProvider: React.FC<ColorModeProviderProps>;
18
+ export declare const DarkModeProvider: ({ children }: {
19
+ children: React.ReactNode;
20
+ }) => import("react/jsx-runtime").JSX.Element;
21
+ export declare const ColorMode: React.FC<ColorModeProviderProps>;
@@ -0,0 +1,171 @@
1
+ "use client";
2
+ import { jsx as x, jsxs as J } from "react/jsx-runtime";
3
+ import { useContext as O, createContext as T, useState as N, useCallback as A, useEffect as j, useMemo as Q, memo as q } from "react";
4
+ const L = ["light", "dark"], B = "(prefers-color-scheme: dark)", F = typeof window > "u", D = T(void 0), G = {
5
+ setColorMode: (e) => {
6
+ },
7
+ colorModes: []
8
+ }, U = () => O(D) ?? G;
9
+ function te(e, d) {
10
+ const t = U();
11
+ return (t.forcedColorMode ?? t.resolvedColorMode) === "dark" ? d : e;
12
+ }
13
+ const oe = (e) => (O(D), /* @__PURE__ */ x(Z, { ...e })), W = () => {
14
+ }, X = {
15
+ setColorMode: W,
16
+ colorModes: ["dark"],
17
+ colorMode: "dark",
18
+ forcedColorMode: "dark",
19
+ resolvedColorMode: "dark",
20
+ systemColorMode: "dark"
21
+ }, se = ({ children: e }) => /* @__PURE__ */ x(D.Provider, { value: X, children: /* @__PURE__ */ x("div", { "data-theme": "dark", children: e }) }), Y = ["light", "dark"], Z = ({
22
+ forcedColorMode: e,
23
+ disableTransitionOnChange: d = !1,
24
+ enableSystem: t = !1,
25
+ enableColorScheme: v = !0,
26
+ storageKey: a = "inkeep-colorMode",
27
+ colorModes: n = Y,
28
+ defaultColorMode: o = t ? "system" : "light",
29
+ attribute: k = "data-theme",
30
+ value: f,
31
+ children: w,
32
+ nonce: E,
33
+ shadowHostId: y,
34
+ rootId: $
35
+ }) => {
36
+ const [c, l] = N(() => P(a, o)), [C, i] = N(() => P(a)), m = f ? Object.values(f) : n, g = A((s) => {
37
+ let r = s;
38
+ if (!r) return;
39
+ s === "system" && t && (r = R());
40
+ const M = f ? f[r] : r, V = d ? H() : null, I = y && $ && document.getElementById(y)?.shadowRoot, S = I ? I.getElementById($) ?? document.documentElement : document.documentElement;
41
+ if (k === "class" ? (S.classList.remove(...m), M && S.classList.add(M)) : M ? S.setAttribute(k, M) : S.removeAttribute(k), v) {
42
+ const _ = L.includes(o) ? o : null, z = L.includes(r) ? r : _;
43
+ S.style.colorScheme = z;
44
+ }
45
+ V?.();
46
+ }, []), u = A(
47
+ (s) => {
48
+ l(s);
49
+ try {
50
+ localStorage.setItem(a, s);
51
+ } catch {
52
+ }
53
+ },
54
+ [e]
55
+ ), h = A(
56
+ (s) => {
57
+ const r = R(s);
58
+ i(r), c === "system" && t && !e && g("system");
59
+ },
60
+ [c, e]
61
+ );
62
+ j(() => {
63
+ const s = window.matchMedia(B);
64
+ return s.addListener(h), h(s), () => s.removeListener(h);
65
+ }, [h]), j(() => {
66
+ const s = (r) => {
67
+ if (r.key !== a)
68
+ return;
69
+ const M = r.newValue || o;
70
+ u(M);
71
+ };
72
+ return window.addEventListener("storage", s), () => window.removeEventListener("storage", s);
73
+ }, [u]), j(() => {
74
+ g(e ?? c);
75
+ }, [e, c]);
76
+ const p = Q(
77
+ () => ({
78
+ colorMode: c,
79
+ setColorMode: u,
80
+ forcedColorMode: e,
81
+ resolvedColorMode: c === "system" ? C : c,
82
+ colorModes: t ? [...n, "system"] : n,
83
+ systemColorMode: t ? C : void 0
84
+ }),
85
+ [c, u, e, C, t, n]
86
+ );
87
+ return /* @__PURE__ */ J(D.Provider, { value: p, children: [
88
+ /* @__PURE__ */ x(
89
+ b,
90
+ {
91
+ forcedColorMode: e,
92
+ disableTransitionOnChange: d,
93
+ enableSystem: t,
94
+ enableColorScheme: v,
95
+ storageKey: a,
96
+ colorModes: n,
97
+ defaultColorMode: o,
98
+ attribute: k,
99
+ value: f,
100
+ children: w,
101
+ attrs: m,
102
+ nonce: E,
103
+ shadowHostId: y,
104
+ rootId: $
105
+ }
106
+ ),
107
+ w
108
+ ] });
109
+ }, b = q(
110
+ ({
111
+ forcedColorMode: e,
112
+ storageKey: d,
113
+ attribute: t,
114
+ enableSystem: v,
115
+ enableColorScheme: a,
116
+ defaultColorMode: n,
117
+ value: o,
118
+ attrs: k,
119
+ nonce: f,
120
+ shadowHostId: w,
121
+ rootId: E
122
+ }) => {
123
+ const y = n === "system", $ = (() => {
124
+ const i = w && E ? `document.getElementById('${w}').shadowRoot.getElementById('${E}')` : "document.documentElement";
125
+ if (t === "class") {
126
+ const m = `c.remove(${k.map((g) => `'${g}'`).join(",")})`;
127
+ return `var d=${i},c=d.classList;${m};`;
128
+ }
129
+ return `var d=${i},n='${t}',s='setAttribute';`;
130
+ })(), c = a ? (L.includes(n) ? n : null) ? `if(e==='light'||e==='dark'||!e)d.style.colorScheme=e||'${n}'` : "if(e==='light'||e==='dark')d.style.colorScheme=e" : "", l = (i, m = !1, g = !0) => {
131
+ const u = o ? o[i] : i, h = m ? `${i}|| ''` : `'${u}'`;
132
+ let p = "";
133
+ return a && g && !m && L.includes(i) && (p += `d.style.colorScheme = '${i}';`), t === "class" ? m || u ? p += `c.add(${h})` : p += "null" : u && (p += `d[s](n,${h})`), p;
134
+ }, C = e ? `!function(){${$}${l(e)}}()` : v ? `!function(){try{${$}var e=localStorage.getItem('${d}');if('system'===e||(!e&&${y})){var t='${B}',m=window.matchMedia(t);if(m.media!==t||m.matches){${l(
135
+ "dark"
136
+ )}}else{${l("light")}}}else if(e){${o ? `var x=${JSON.stringify(o)};` : ""}${l(o ? "x[e]" : "e", !0)}}${y ? "" : `else{${l(n, !1, !1)}}`}${c}}catch(e){}}()` : `!function(){try{${$}var e=localStorage.getItem('${d}');if(e){${o ? `var x=${JSON.stringify(o)};` : ""}${l(o ? "x[e]" : "e", !0)}}else{${l(
137
+ n,
138
+ !1,
139
+ !1
140
+ )};}${c}}catch(t){}}();`;
141
+ return /* @__PURE__ */ x("script", { nonce: f, dangerouslySetInnerHTML: { __html: C } });
142
+ },
143
+ // Never re-render this component
144
+ () => !0
145
+ ), P = (e, d) => {
146
+ if (F) return;
147
+ let t;
148
+ try {
149
+ t = localStorage.getItem(e) || void 0;
150
+ } catch {
151
+ }
152
+ return t || d;
153
+ }, H = () => {
154
+ const e = document.createElement("style");
155
+ return e.appendChild(
156
+ document.createTextNode(
157
+ "*{-webkit-transition:none!important;-moz-transition:none!important;-o-transition:none!important;-ms-transition:none!important;transition:none!important}"
158
+ )
159
+ ), document.head.appendChild(e), () => {
160
+ window.getComputedStyle(document.body), setTimeout(() => {
161
+ document.head.removeChild(e);
162
+ }, 1);
163
+ };
164
+ }, R = (e) => (e || (e = window.matchMedia(B)), e.matches ? "dark" : "light");
165
+ export {
166
+ Z as ColorMode,
167
+ oe as ColorModeProvider,
168
+ se as DarkModeProvider,
169
+ U as useColorMode,
170
+ te as useColorModeValue
171
+ };
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("./color-mode-provider.cjs");exports.ColorMode=o.ColorMode;exports.ColorModeProvider=o.ColorModeProvider;exports.DarkModeProvider=o.DarkModeProvider;exports.useColorMode=o.useColorMode;exports.useColorModeValue=o.useColorModeValue;
@@ -0,0 +1 @@
1
+ export * from './color-mode-provider';
@@ -0,0 +1 @@
1
+ export * from './color-mode-provider';
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ "use client";
2
+ import { ColorMode as r, ColorModeProvider as d, DarkModeProvider as l, useColorMode as M, useColorModeValue as u } from "./color-mode-provider.js";
3
+ export {
4
+ r as ColorMode,
5
+ d as ColorModeProvider,
6
+ l as DarkModeProvider,
7
+ M as useColorMode,
8
+ u as useColorModeValue
9
+ };
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@inkeep/cxkit-color-mode",
3
+ "version": "0.0.0-dev-20250221234142",
4
+ "description": "",
5
+ "license": "Inkeep, Inc. Customer License (IICL) v1.1",
6
+ "homepage": "",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/inkeep/widget",
10
+ "directory": "packages/color-mode"
11
+ },
12
+ "type": "module",
13
+ "main": "dist/index.cjs",
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "clean-package": "../../clean-package.config.json",
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "sideEffects": false,
22
+ "devDependencies": {
23
+ "@biomejs/biome": "1.9.4",
24
+ "@types/react": "19.0.7",
25
+ "@types/react-dom": "19.0.3",
26
+ "@vitejs/plugin-react": "4.3.4",
27
+ "clean-package": "2.2.0",
28
+ "globby": "14.0.2",
29
+ "react": "19.0.0",
30
+ "react-dom": "19.0.0",
31
+ "typescript": "5.7.3",
32
+ "vite": "5.4.11",
33
+ "vite-plugin-dts": "4.4.0"
34
+ },
35
+ "module": "dist/index.js",
36
+ "types": "dist/index.d.ts",
37
+ "exports": {
38
+ ".": {
39
+ "source": "./src/index.ts",
40
+ "import": {
41
+ "types": "./dist/index.d.ts",
42
+ "default": "./dist/index.js"
43
+ },
44
+ "require": {
45
+ "types": "./dist/index.d.cts",
46
+ "default": "./dist/index.cjs"
47
+ }
48
+ },
49
+ "./package.json": "./package.json"
50
+ },
51
+ "scripts": {
52
+ "build": "vite build",
53
+ "lint": "biome lint ./src",
54
+ "typecheck": "tsc"
55
+ }
56
+ }