@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
 
@@ -27,17 +27,32 @@ module.exports = __toCommonJS(useRootColors_exports);
27
27
  var import_react = require("react");
28
28
  function useRootColors(colors = []) {
29
29
  const [state, dispatch] = (0, import_react.useReducer)(rootColorsReducer, {});
30
+ const handleRefetch = (0, import_react.useCallback)(() => {
31
+ return new Promise((resolve) => {
32
+ dispatch(formatColors(colors));
33
+ resolve();
34
+ });
35
+ }, []);
30
36
  (0, import_react.useEffect)(() => {
31
37
  if (Object.keys(state).length === colors.length) return;
32
- const rootStyles = getComputedStyle(document.body);
33
- const rootColors = colors.reduce((acc, color) => {
38
+ dispatch(formatColors(colors));
39
+ console.log("updating colors in root hook");
40
+ }, [colors]);
41
+ return (0, import_react.useMemo)(
42
+ () => ({ colors: state, refetch: handleRefetch }),
43
+ [state, handleRefetch]
44
+ );
45
+ }
46
+ function formatColors(colors) {
47
+ const rootStyles = getComputedStyle(document.body);
48
+ return colors.reduce(
49
+ (acc, color) => {
34
50
  const formattedColor = color.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase().replaceAll(".", "-");
35
51
  acc[color] = rootStyles.getPropertyValue(`--cerberus-colors-${formattedColor}`).trim();
36
52
  return acc;
37
- }, {});
38
- dispatch(rootColors);
39
- }, [colors]);
40
- return state;
53
+ },
54
+ {}
55
+ );
41
56
  }
42
57
  function rootColorsReducer(state, action) {
43
58
  return { ...state, ...action };
@@ -1 +1 @@
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":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,mBAAsC;AAgB/B,SAAS,cAAc,SAAmB,CAAC,GAAqB;AACrE,QAAM,CAAC,OAAO,QAAQ,QAAI,yBAAW,mBAAmB,CAAC,CAAC;AAE1D,8BAAU,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":[]}
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":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,mBAA4D;AAyBrD,SAAS,cAAc,SAAmB,CAAC,GAAqB;AACrE,QAAM,CAAC,OAAO,QAAQ,QAAI,yBAAW,mBAAmB,CAAC,CAAC;AAE1D,QAAM,oBAAgB,0BAAY,MAAM;AACtC,WAAO,IAAI,QAAc,CAAC,YAAY;AACpC,eAAS,aAAa,MAAM,CAAC;AAC7B,cAAQ;AAAA,IACV,CAAC;AAAA,EACH,GAAG,CAAC,CAAC;AAEL,8BAAU,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,aAAO;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":[]}
@@ -3125,17 +3125,32 @@ function useToggle(options) {
3125
3125
  var import_react25 = require("react");
3126
3126
  function useRootColors(colors = []) {
3127
3127
  const [state, dispatch] = (0, import_react25.useReducer)(rootColorsReducer, {});
3128
+ const handleRefetch = (0, import_react25.useCallback)(() => {
3129
+ return new Promise((resolve) => {
3130
+ dispatch(formatColors(colors));
3131
+ resolve();
3132
+ });
3133
+ }, []);
3128
3134
  (0, import_react25.useEffect)(() => {
3129
3135
  if (Object.keys(state).length === colors.length) return;
3130
- const rootStyles = getComputedStyle(document.body);
3131
- const rootColors = colors.reduce((acc, color) => {
3136
+ dispatch(formatColors(colors));
3137
+ console.log("updating colors in root hook");
3138
+ }, [colors]);
3139
+ return (0, import_react25.useMemo)(
3140
+ () => ({ colors: state, refetch: handleRefetch }),
3141
+ [state, handleRefetch]
3142
+ );
3143
+ }
3144
+ function formatColors(colors) {
3145
+ const rootStyles = getComputedStyle(document.body);
3146
+ return colors.reduce(
3147
+ (acc, color) => {
3132
3148
  const formattedColor = color.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase().replaceAll(".", "-");
3133
3149
  acc[color] = rootStyles.getPropertyValue(`--cerberus-colors-${formattedColor}`).trim();
3134
3150
  return acc;
3135
- }, {});
3136
- dispatch(rootColors);
3137
- }, [colors]);
3138
- return state;
3151
+ },
3152
+ {}
3153
+ );
3139
3154
  }
3140
3155
  function rootColorsReducer(state, action) {
3141
3156
  return { ...state, ...action };