@cerberus-design/react 0.13.1-next-394d7c4 → 0.13.1-next-b2ca1e0

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.
@@ -2101,7 +2101,16 @@ export { rectIntersection }
2101
2101
  * This module provides a hook to get Cerberus colors from the document root.
2102
2102
  * @module useRootColors
2103
2103
  */
2104
- declare type RootColorsResult = Record<string, string>;
2104
+ declare interface RootColorsResult {
2105
+ /**
2106
+ * A record of Cerberus colors where the key is the token name provided and the value is the color hex.
2107
+ */
2108
+ colors: Record<string, string>;
2109
+ /**
2110
+ * A function to refetch the Cerberus colors from the document root. Useful when you need the latest colors after a theme/mode change.
2111
+ */
2112
+ refetch: () => Promise<void>;
2113
+ }
2105
2114
  export { RootColorsResult }
2106
2115
  export { RootColorsResult as RootColorsResult_alias_1 }
2107
2116
 
@@ -0,0 +1,39 @@
1
+ // src/hooks/useRootColors.ts
2
+ import { useCallback, useEffect, useMemo, useReducer } from "react";
3
+ function useRootColors(colors = []) {
4
+ const [state, dispatch] = useReducer(rootColorsReducer, {});
5
+ const handleRefetch = useCallback(() => {
6
+ return new Promise((resolve) => {
7
+ dispatch(formatColors(colors));
8
+ resolve();
9
+ });
10
+ }, []);
11
+ useEffect(() => {
12
+ if (Object.keys(state).length === colors.length) return;
13
+ dispatch(formatColors(colors));
14
+ console.log("updating colors in root hook");
15
+ }, [colors]);
16
+ return useMemo(
17
+ () => ({ colors: state, refetch: handleRefetch }),
18
+ [state, handleRefetch]
19
+ );
20
+ }
21
+ function formatColors(colors) {
22
+ const rootStyles = getComputedStyle(document.body);
23
+ return colors.reduce(
24
+ (acc, color) => {
25
+ const formattedColor = color.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase().replaceAll(".", "-");
26
+ acc[color] = rootStyles.getPropertyValue(`--cerberus-colors-${formattedColor}`).trim();
27
+ return acc;
28
+ },
29
+ {}
30
+ );
31
+ }
32
+ function rootColorsReducer(state, action) {
33
+ return { ...state, ...action };
34
+ }
35
+
36
+ export {
37
+ useRootColors
38
+ };
39
+ //# sourceMappingURL=chunk-RMVJK26W.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/hooks/useRootColors.ts"],"sourcesContent":["'use client'\n\nimport { useCallback, useEffect, useMemo, useReducer } from 'react'\n\n/**\n * This module provides a hook to get Cerberus colors from the document root.\n * @module useRootColors\n */\n\nexport interface RootColorsResult {\n /**\n * A record of Cerberus colors where the key is the token name provided and the value is the color hex.\n */\n colors: Record<string, string>\n /**\n * A function to refetch the Cerberus colors from the document root. Useful when you need the latest colors after a theme/mode change.\n */\n refetch: () => Promise<void>\n}\n\n/**\n * This hook returns a record of Cerberus colors from the document root.\n * This is useful when you are working with a component that uses the `<canvas>`\n * element.\n * @param colors - An array of Cerberus tokens to get from the document root (i.e. `['dataViz.diverging.50', 'dataViz.diverging.200']`).\n * @returns A record of Cerberus colors where the key is the token name provided and the value is the color hex.\n */\nexport function useRootColors(colors: string[] = []): RootColorsResult {\n const [state, dispatch] = useReducer(rootColorsReducer, {})\n\n const handleRefetch = useCallback(() => {\n return new Promise<void>((resolve) => {\n dispatch(formatColors(colors))\n resolve()\n })\n }, [])\n\n useEffect(() => {\n if (Object.keys(state).length === colors.length) return\n dispatch(formatColors(colors))\n console.log('updating colors in root hook')\n }, [colors])\n\n // reducer is already memoized\n return useMemo(\n () => ({ colors: state, refetch: handleRefetch }),\n [state, handleRefetch],\n )\n}\n\nfunction formatColors(colors: string[]): Record<string, string> {\n const rootStyles = getComputedStyle(document.body)\n return colors.reduce(\n (acc, color) => {\n const formattedColor = color\n .replace(/([a-z])([A-Z])/g, '$1-$2')\n .toLowerCase()\n .replaceAll('.', '-')\n acc[color] = rootStyles\n .getPropertyValue(`--cerberus-colors-${formattedColor}`)\n .trim()\n return acc\n },\n {} as Record<string, string>,\n )\n}\n\nfunction rootColorsReducer(\n state: Record<string, string>,\n action: Record<string, string>,\n): Record<string, string> {\n return { ...state, ...action }\n}\n"],"mappings":";AAEA,SAAS,aAAa,WAAW,SAAS,kBAAkB;AAyBrD,SAAS,cAAc,SAAmB,CAAC,GAAqB;AACrE,QAAM,CAAC,OAAO,QAAQ,IAAI,WAAW,mBAAmB,CAAC,CAAC;AAE1D,QAAM,gBAAgB,YAAY,MAAM;AACtC,WAAO,IAAI,QAAc,CAAC,YAAY;AACpC,eAAS,aAAa,MAAM,CAAC;AAC7B,cAAQ;AAAA,IACV,CAAC;AAAA,EACH,GAAG,CAAC,CAAC;AAEL,YAAU,MAAM;AACd,QAAI,OAAO,KAAK,KAAK,EAAE,WAAW,OAAO,OAAQ;AACjD,aAAS,aAAa,MAAM,CAAC;AAC7B,YAAQ,IAAI,8BAA8B;AAAA,EAC5C,GAAG,CAAC,MAAM,CAAC;AAGX,SAAO;AAAA,IACL,OAAO,EAAE,QAAQ,OAAO,SAAS,cAAc;AAAA,IAC/C,CAAC,OAAO,aAAa;AAAA,EACvB;AACF;AAEA,SAAS,aAAa,QAA0C;AAC9D,QAAM,aAAa,iBAAiB,SAAS,IAAI;AACjD,SAAO,OAAO;AAAA,IACZ,CAAC,KAAK,UAAU;AACd,YAAM,iBAAiB,MACpB,QAAQ,mBAAmB,OAAO,EAClC,YAAY,EACZ,WAAW,KAAK,GAAG;AACtB,UAAI,KAAK,IAAI,WACV,iBAAiB,qBAAqB,cAAc,EAAE,EACtD,KAAK;AACR,aAAO;AAAA,IACT;AAAA,IACA,CAAC;AAAA,EACH;AACF;AAEA,SAAS,kBACP,OACA,QACwB;AACxB,SAAO,EAAE,GAAG,OAAO,GAAG,OAAO;AAC/B;","names":[]}
@@ -1,7 +1,7 @@
1
1
  "use client";
2
2
  import {
3
3
  useRootColors
4
- } from "../chunk-RO4AJOVX.js";
4
+ } from "../chunk-RMVJK26W.js";
5
5
  export {
6
6
  useRootColors
7
7
  };
@@ -171,7 +171,7 @@ import {
171
171
  } from "./chunk-KGQG5JGW.js";
172
172
  import {
173
173
  useRootColors
174
- } from "./chunk-RO4AJOVX.js";
174
+ } from "./chunk-RMVJK26W.js";
175
175
  import {
176
176
  MODE_KEY,
177
177
  THEME_KEY,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cerberus-design/react",
3
- "version": "0.13.1-next-394d7c4",
3
+ "version": "0.13.1-next-b2ca1e0",
4
4
  "description": "The Cerberus Design React component library.",
5
5
  "browserslist": "> 0.25%, not dead",
6
6
  "sideEffects": false,
@@ -26,8 +26,8 @@
26
26
  "react": "^18",
27
27
  "react-dom": "^18",
28
28
  "tsup": "^8.1.0",
29
- "@cerberus-design/styled-system": "0.13.1-next-394d7c4",
30
- "@cerberus-design/configs": "0.0.0"
29
+ "@cerberus-design/configs": "0.0.0",
30
+ "@cerberus-design/styled-system": "0.13.1-next-b2ca1e0"
31
31
  },
32
32
  "publishConfig": {
33
33
  "access": "public"
@@ -1,13 +1,22 @@
1
1
  'use client'
2
2
 
3
- import { useEffect, useReducer } from 'react'
3
+ import { useCallback, useEffect, useMemo, useReducer } from 'react'
4
4
 
5
5
  /**
6
6
  * This module provides a hook to get Cerberus colors from the document root.
7
7
  * @module useRootColors
8
8
  */
9
9
 
10
- export type RootColorsResult = Record<string, string>
10
+ export interface RootColorsResult {
11
+ /**
12
+ * A record of Cerberus colors where the key is the token name provided and the value is the color hex.
13
+ */
14
+ colors: Record<string, string>
15
+ /**
16
+ * A function to refetch the Cerberus colors from the document root. Useful when you need the latest colors after a theme/mode change.
17
+ */
18
+ refetch: () => Promise<void>
19
+ }
11
20
 
12
21
  /**
13
22
  * This hook returns a record of Cerberus colors from the document root.
@@ -19,26 +28,41 @@ export type RootColorsResult = Record<string, string>
19
28
  export function useRootColors(colors: string[] = []): RootColorsResult {
20
29
  const [state, dispatch] = useReducer(rootColorsReducer, {})
21
30
 
31
+ const handleRefetch = useCallback(() => {
32
+ return new Promise<void>((resolve) => {
33
+ dispatch(formatColors(colors))
34
+ resolve()
35
+ })
36
+ }, [])
37
+
22
38
  useEffect(() => {
23
39
  if (Object.keys(state).length === colors.length) return
40
+ dispatch(formatColors(colors))
41
+ console.log('updating colors in root hook')
42
+ }, [colors])
43
+
44
+ // reducer is already memoized
45
+ return useMemo(
46
+ () => ({ colors: state, refetch: handleRefetch }),
47
+ [state, handleRefetch],
48
+ )
49
+ }
24
50
 
25
- const rootStyles = getComputedStyle(document.body)
26
- const rootColors = colors.reduce((acc, color) => {
51
+ function formatColors(colors: string[]): Record<string, string> {
52
+ const rootStyles = getComputedStyle(document.body)
53
+ return colors.reduce(
54
+ (acc, color) => {
27
55
  const formattedColor = color
28
56
  .replace(/([a-z])([A-Z])/g, '$1-$2')
29
57
  .toLowerCase()
30
58
  .replaceAll('.', '-')
31
- acc[color as keyof typeof acc] = rootStyles
59
+ acc[color] = rootStyles
32
60
  .getPropertyValue(`--cerberus-colors-${formattedColor}`)
33
61
  .trim()
34
62
  return acc
35
- }, {} as RootColorsResult)
36
-
37
- dispatch(rootColors)
38
- }, [colors])
39
-
40
- // reducer is already memoized
41
- return state
63
+ },
64
+ {} as Record<string, string>,
65
+ )
42
66
  }
43
67
 
44
68
  function rootColorsReducer(
@@ -1,24 +0,0 @@
1
- // src/hooks/useRootColors.ts
2
- import { useEffect, useReducer } from "react";
3
- function useRootColors(colors = []) {
4
- const [state, dispatch] = useReducer(rootColorsReducer, {});
5
- useEffect(() => {
6
- if (Object.keys(state).length === colors.length) return;
7
- const rootStyles = getComputedStyle(document.body);
8
- const rootColors = colors.reduce((acc, color) => {
9
- const formattedColor = color.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase().replaceAll(".", "-");
10
- acc[color] = rootStyles.getPropertyValue(`--cerberus-colors-${formattedColor}`).trim();
11
- return acc;
12
- }, {});
13
- dispatch(rootColors);
14
- }, [colors]);
15
- return state;
16
- }
17
- function rootColorsReducer(state, action) {
18
- return { ...state, ...action };
19
- }
20
-
21
- export {
22
- useRootColors
23
- };
24
- //# sourceMappingURL=chunk-RO4AJOVX.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/hooks/useRootColors.ts"],"sourcesContent":["'use client'\n\nimport { useEffect, useReducer } from 'react'\n\n/**\n * This module provides a hook to get Cerberus colors from the document root.\n * @module useRootColors\n */\n\nexport type RootColorsResult = Record<string, string>\n\n/**\n * This hook returns a record of Cerberus colors from the document root.\n * This is useful when you are working with a component that uses the `<canvas>`\n * element.\n * @param colors - An array of Cerberus tokens to get from the document root (i.e. `['dataViz.diverging.50', 'dataViz.diverging.200']`).\n * @returns A record of Cerberus colors where the key is the token name provided and the value is the color hex.\n */\nexport function useRootColors(colors: string[] = []): RootColorsResult {\n const [state, dispatch] = useReducer(rootColorsReducer, {})\n\n useEffect(() => {\n if (Object.keys(state).length === colors.length) return\n\n const rootStyles = getComputedStyle(document.body)\n const rootColors = colors.reduce((acc, color) => {\n const formattedColor = color\n .replace(/([a-z])([A-Z])/g, '$1-$2')\n .toLowerCase()\n .replaceAll('.', '-')\n acc[color as keyof typeof acc] = rootStyles\n .getPropertyValue(`--cerberus-colors-${formattedColor}`)\n .trim()\n return acc\n }, {} as RootColorsResult)\n\n dispatch(rootColors)\n }, [colors])\n\n // reducer is already memoized\n return state\n}\n\nfunction rootColorsReducer(\n state: Record<string, string>,\n action: Record<string, string>,\n): Record<string, string> {\n return { ...state, ...action }\n}\n"],"mappings":";AAEA,SAAS,WAAW,kBAAkB;AAgB/B,SAAS,cAAc,SAAmB,CAAC,GAAqB;AACrE,QAAM,CAAC,OAAO,QAAQ,IAAI,WAAW,mBAAmB,CAAC,CAAC;AAE1D,YAAU,MAAM;AACd,QAAI,OAAO,KAAK,KAAK,EAAE,WAAW,OAAO,OAAQ;AAEjD,UAAM,aAAa,iBAAiB,SAAS,IAAI;AACjD,UAAM,aAAa,OAAO,OAAO,CAAC,KAAK,UAAU;AAC/C,YAAM,iBAAiB,MACpB,QAAQ,mBAAmB,OAAO,EAClC,YAAY,EACZ,WAAW,KAAK,GAAG;AACtB,UAAI,KAAyB,IAAI,WAC9B,iBAAiB,qBAAqB,cAAc,EAAE,EACtD,KAAK;AACR,aAAO;AAAA,IACT,GAAG,CAAC,CAAqB;AAEzB,aAAS,UAAU;AAAA,EACrB,GAAG,CAAC,MAAM,CAAC;AAGX,SAAO;AACT;AAEA,SAAS,kBACP,OACA,QACwB;AACxB,SAAO,EAAE,GAAG,OAAO,GAAG,OAAO;AAC/B;","names":[]}