@overdoser/react-toolkit 0.19.0 → 0.20.0

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.
@@ -1,4 +1,4 @@
1
1
  export { Table } from './Table';
2
- export type { TableProps, TableClasses, ColumnDef, PaginationConfig, TableRowKey, TableBulkActionsContext } from './Table';
2
+ export type { TableProps, TableClasses, ColumnDef, DataColumnDef, DisplayColumnDef, PaginationConfig, TableRowKey, TableBulkActionsContext } from './Table';
3
3
  export { useTableSort } from './useTableSort';
4
4
  export type { SortConfig } from './useTableSort';
@@ -26,7 +26,7 @@ export interface SortConfig {
26
26
  * const { sortedData, sortConfig, requestSort } = useTableSort(users);
27
27
  * ```
28
28
  */
29
- export declare function useTableSort<T extends Record<string, unknown>>(data: T[], initialSort?: SortConfig[]): {
29
+ export declare function useTableSort<T extends object>(data: T[], initialSort?: SortConfig[]): {
30
30
  sortedData: T[];
31
31
  sortConfig: SortConfig[];
32
32
  requestSort: (key: keyof T & string, startDirection?: 'asc' | 'desc') => void;
@@ -24,6 +24,8 @@ export interface ThemeToggleProps extends Omit<ComponentPropsWithRef<'button'>,
24
24
  showLabel?: boolean;
25
25
  /** localStorage key passed to `useTheme`. @default 'crk-theme' */
26
26
  storageKey?: string;
27
+ /** Theme used when nothing is stored yet. Match `THEME_INIT_SCRIPT`'s `defaultTheme`. @default 'system' */
28
+ defaultTheme?: 'light' | 'dark' | 'system';
27
29
  /** Override class names on internal elements. */
28
30
  classes?: Partial<ThemeToggleClasses>;
29
31
  }
@@ -1,9 +1,9 @@
1
1
  import { jsx as r, jsxs as p } from "react/jsx-runtime";
2
- import { forwardRef as D } from "react";
2
+ import { forwardRef as S } from "react";
3
3
  import a from "clsx";
4
4
  import t from "./ThemeToggle.module.scss.js";
5
- import { useTheme as S } from "../../hooks/useTheme.js";
6
- const B = { light: "Light", dark: "Dark", system: "System" }, T = {
5
+ import { useTheme as B } from "../../hooks/useTheme.js";
6
+ const $ = { light: "Light", dark: "Dark", system: "System" }, T = {
7
7
  light: /* @__PURE__ */ p("svg", { viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: [
8
8
  /* @__PURE__ */ r("circle", { cx: "12", cy: "12", r: "4" }),
9
9
  /* @__PURE__ */ r("path", { d: "M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M4.93 19.07l1.41-1.41M17.66 6.34l1.41-1.41" })
@@ -13,7 +13,7 @@ const B = { light: "Light", dark: "Dark", system: "System" }, T = {
13
13
  /* @__PURE__ */ r("rect", { x: "2", y: "3", width: "20", height: "14", rx: "2" }),
14
14
  /* @__PURE__ */ r("path", { d: "M8 21h8M12 17v4" })
15
15
  ] })
16
- }, $ = D(
16
+ }, E = S(
17
17
  ({
18
18
  allowSystem: w = !0,
19
19
  variant: L = "icon",
@@ -21,15 +21,16 @@ const B = { light: "Light", dark: "Dark", system: "System" }, T = {
21
21
  labels: A,
22
22
  showLabel: c = !1,
23
23
  storageKey: N,
24
+ defaultTheme: j,
24
25
  classes: n,
25
26
  className: k,
26
27
  style: f,
27
- onClick: j,
28
+ onClick: C,
28
29
  ...v
29
30
  }, x) => {
30
- const { theme: y, setTheme: u } = S({ storageKey: N }), i = { ...B, ...A }, e = w ? ["light", "dark", "system"] : ["light", "dark"], l = e.includes(y) ? y : "light", b = e[(e.indexOf(l) + 1) % e.length];
31
+ const { theme: y, setTheme: u } = B({ storageKey: N, defaultTheme: j }), i = { ...$, ...A }, e = w ? ["light", "dark", "system"] : ["light", "dark"], l = e.includes(y) ? y : "light", b = e[(e.indexOf(l) + 1) % e.length];
31
32
  if (L === "segmented") {
32
- const { "aria-label": s, ...C } = v;
33
+ const { "aria-label": s, ...D } = v;
33
34
  return /* @__PURE__ */ r(
34
35
  "div",
35
36
  {
@@ -38,7 +39,7 @@ const B = { light: "Light", dark: "Dark", system: "System" }, T = {
38
39
  "aria-label": s ?? "Theme",
39
40
  className: a(t.group, t[g], n?.root, k),
40
41
  style: f,
41
- ...C,
42
+ ...D,
42
43
  children: e.map((o) => {
43
44
  const m = o === l;
44
45
  return /* @__PURE__ */ p(
@@ -80,7 +81,7 @@ const B = { light: "Light", dark: "Dark", system: "System" }, T = {
80
81
  className: a(t.root, t[g], c && t.withLabel, n?.root, k),
81
82
  style: f,
82
83
  onClick: (s) => {
83
- j?.(s), s.defaultPrevented || u(b);
84
+ C?.(s), s.defaultPrevented || u(b);
84
85
  },
85
86
  ...v,
86
87
  children: [
@@ -91,7 +92,7 @@ const B = { light: "Light", dark: "Dark", system: "System" }, T = {
91
92
  );
92
93
  }
93
94
  );
94
- $.displayName = "ThemeToggle";
95
+ E.displayName = "ThemeToggle";
95
96
  export {
96
- $ as ThemeToggle
97
+ E as ThemeToggle
97
98
  };
@@ -1,94 +1,97 @@
1
- import { useMemo as A, useCallback as f, useSyncExternalStore as J } from "react";
2
- const i = /* @__PURE__ */ new Map(), d = /* @__PURE__ */ new Map();
1
+ import { useMemo as A, useCallback as w, useSyncExternalStore as J } from "react";
2
+ const f = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map();
3
3
  function I(e, s) {
4
4
  return `${e}:${s}`;
5
5
  }
6
- function v(e) {
6
+ function p(e) {
7
7
  try {
8
8
  return typeof window > "u" ? null : e === "local" ? window.localStorage : window.sessionStorage;
9
9
  } catch {
10
10
  return null;
11
11
  }
12
12
  }
13
- function S(e, s) {
14
- i.get(I(e, s))?.forEach((u) => u());
13
+ function v(e, s) {
14
+ f.get(I(e, s))?.forEach((u) => u());
15
15
  }
16
16
  let E = !1;
17
17
  function N() {
18
18
  E || typeof window > "u" || (E = !0, window.addEventListener("storage", (e) => {
19
19
  if (e.key === null) {
20
- i.forEach((u) => u.forEach((n) => n()));
20
+ f.forEach((u) => u.forEach((r) => r()));
21
21
  return;
22
22
  }
23
23
  const s = e.storageArea === window.sessionStorage ? "session" : "local";
24
- S(s, e.key);
24
+ v(s, e.key);
25
25
  }));
26
26
  }
27
27
  function $(e, s, u = {}) {
28
- const { storage: n = "local", serialize: m = JSON.stringify, deserialize: p = JSON.parse, sync: y = !0 } = u, r = I(n, e), l = A(
28
+ const { storage: r = "local", serialize: S = JSON.stringify, deserialize: m = JSON.parse, sync: y = !0 } = u, n = I(r, e), d = A(
29
29
  () => typeof s == "function" ? s() : s,
30
30
  // Only the first initial value matters, as with useState.
31
31
  // eslint-disable-next-line react-hooks/exhaustive-deps
32
32
  []
33
- ), w = f(() => {
34
- const o = v(n);
33
+ ), g = w(() => {
34
+ const o = p(r);
35
35
  let t = null;
36
36
  try {
37
37
  t = o ? o.getItem(e) : null;
38
38
  } catch {
39
39
  t = null;
40
40
  }
41
- const a = d.get(r);
41
+ if (t === null) {
42
+ const l = i.get(n);
43
+ return l && l.raw === null ? l.value : d;
44
+ }
45
+ const a = i.get(n);
42
46
  if (a && a.raw === t) return a.value;
43
- let c = l;
44
- if (t !== null)
45
- try {
46
- c = p(t);
47
- } catch {
48
- c = l;
49
- }
50
- return d.set(r, { raw: t, value: c }), c;
51
- }, [n, e, r, l, p]), b = f(
47
+ let c;
48
+ try {
49
+ c = m(t);
50
+ } catch {
51
+ return d;
52
+ }
53
+ return i.set(n, { raw: t, value: c }), c;
54
+ }, [r, e, n, d, m]), b = w(
52
55
  (o) => {
53
- let t = i.get(r);
54
- return t || (t = /* @__PURE__ */ new Set(), i.set(r, t)), t.add(o), y && N(), () => {
55
- t.delete(o), t.size === 0 && i.delete(r);
56
+ let t = f.get(n);
57
+ return t || (t = /* @__PURE__ */ new Set(), f.set(n, t)), t.add(o), y && N(), () => {
58
+ t.delete(o), t.size === 0 && f.delete(n);
56
59
  };
57
60
  },
58
- [r, y]
59
- ), z = J(b, w, () => l), L = f(
61
+ [n, y]
62
+ ), z = J(b, g, () => d), L = w(
60
63
  (o) => {
61
- const t = w(), a = typeof o == "function" ? o(t) : o;
64
+ const t = g(), a = typeof o == "function" ? o(t) : o;
62
65
  let c = null;
63
66
  try {
64
- c = m(a);
67
+ c = S(a);
65
68
  } catch {
66
69
  c = null;
67
70
  }
68
- const g = v(n);
71
+ const l = p(r);
69
72
  let h = null;
70
- if (g) {
73
+ if (l) {
71
74
  try {
72
- c !== null && g.setItem(e, c);
75
+ c !== null && l.setItem(e, c);
73
76
  } catch {
74
77
  }
75
78
  try {
76
- h = g.getItem(e);
79
+ h = l.getItem(e);
77
80
  } catch {
78
81
  h = null;
79
82
  }
80
83
  }
81
- d.set(r, { raw: h, value: a }), S(n, e);
84
+ i.set(n, { raw: h, value: a }), v(r, e);
82
85
  },
83
- [w, m, n, e, r]
84
- ), M = f(() => {
85
- const o = v(n);
86
+ [g, S, r, e, n]
87
+ ), M = w(() => {
88
+ const o = p(r);
86
89
  try {
87
90
  o?.removeItem(e);
88
91
  } catch {
89
92
  }
90
- d.set(r, { raw: null, value: l }), S(n, e);
91
- }, [n, e, r, l]);
93
+ i.delete(n), v(r, e);
94
+ }, [r, e, n]);
92
95
  return [z, L, M];
93
96
  }
94
97
  export {
@@ -21,8 +21,8 @@ export interface UseThemeResult {
21
21
  * Light / dark / system theme switch for the `--crk-*` tokens.
22
22
  *
23
23
  * Persists the choice (localStorage, cross-tab), writes `data-theme` on
24
- * `<html>` (`system` removes it so the stylesheet follows the OS
25
- * preference), and reports the `resolved` scheme. Every hook instance stays
24
+ * `<html>` (`light` / `dark` / `system` — the stylesheet follows the OS
25
+ * preference only for `system`), and reports the `resolved` scheme. Every hook instance stays
26
26
  * in sync. Pair with `THEME_INIT_SCRIPT` in `<head>` to avoid a flash of the
27
27
  * wrong theme on load.
28
28
  *
package/hooks/useTheme.js CHANGED
@@ -1,27 +1,25 @@
1
1
  import { useEffect as p, useCallback as i } from "react";
2
2
  import { usePersistedState as y } from "./usePersistedState.js";
3
3
  import { useMediaQuery as g } from "./useMediaQuery.js";
4
- const o = ["light", "dark", "system"];
5
- function f(s, r) {
6
- if (typeof document > "u") return;
7
- const e = document.documentElement;
8
- r === "system" ? e.removeAttribute(s) : e.setAttribute(s, r);
4
+ const s = ["light", "dark", "system"];
5
+ function a(r, o) {
6
+ typeof document > "u" || document.documentElement.setAttribute(r, o);
9
7
  }
10
- function E(s = {}) {
11
- const { storageKey: r = "crk-theme", attribute: e = "data-theme", defaultTheme: m = "system" } = s, [c, u] = y(r, m), t = o.includes(c) ? c : m, l = g("(prefers-color-scheme: dark)"), a = t === "system" ? l ? "dark" : "light" : t;
8
+ function T(r = {}) {
9
+ const { storageKey: o = "crk-theme", attribute: t = "data-theme", defaultTheme: m = "system" } = r, [c, u] = y(o, m), e = s.includes(c) ? c : m, f = g("(prefers-color-scheme: dark)"), l = e === "system" ? f ? "dark" : "light" : e;
12
10
  p(() => {
13
- f(e, t);
14
- }, [e, t]);
11
+ a(t, e);
12
+ }, [t, e]);
15
13
  const n = i(
16
14
  (d) => {
17
- u(d), f(e, d);
15
+ u(d), a(t, d);
18
16
  },
19
- [u, e]
17
+ [u, t]
20
18
  ), h = i(() => {
21
- n(o[(o.indexOf(t) + 1) % o.length]);
22
- }, [t, n]);
23
- return { theme: t, resolved: a, setTheme: n, toggle: h };
19
+ n(s[(s.indexOf(e) + 1) % s.length]);
20
+ }, [e, n]);
21
+ return { theme: e, resolved: l, setTheme: n, toggle: h };
24
22
  }
25
23
  export {
26
- E as useTheme
24
+ T as useTheme
27
25
  };