@alulore/kumiko 0.20.1 → 0.21.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.
Files changed (83) hide show
  1. package/dist/avatar/avatar.js +1 -1
  2. package/dist/badge/badge.css +41 -3
  3. package/dist/badge/badge.js +11 -4
  4. package/dist/board/board-slots.js +2 -1
  5. package/dist/board/scrollbar-gutter.js +26 -2
  6. package/dist/button/button.css +21 -7
  7. package/dist/card/card.css +3 -2
  8. package/dist/card/item-card.d.ts +1 -1
  9. package/dist/card/item-card.js +3 -2
  10. package/dist/chip/chip.css +6 -10
  11. package/dist/chip/chip.js +1 -1
  12. package/dist/form/error-line.js +4 -3
  13. package/dist/input/input.css +99 -114
  14. package/dist/list-row/list-row-motion.js +29 -10
  15. package/dist/media-viewer/gesture-press.d.ts +1 -0
  16. package/dist/media-viewer/gesture-press.js +12 -0
  17. package/dist/media-viewer/media-slide.d.ts +3 -1
  18. package/dist/media-viewer/media-slide.js +3 -2
  19. package/dist/media-viewer/media-viewer.css +5 -1
  20. package/dist/media-viewer/media-viewer.js +41 -23
  21. package/dist/media-viewer/stage-rect.d.ts +16 -0
  22. package/dist/media-viewer/stage-rect.js +56 -0
  23. package/dist/media-viewer/view-gesture.d.ts +1 -2
  24. package/dist/media-viewer/view-gesture.js +39 -44
  25. package/dist/media-viewer/viewer-chrome.d.ts +2 -1
  26. package/dist/media-viewer/viewer-chrome.js +3 -1
  27. package/dist/media-viewer/viewer-motion.d.ts +1 -0
  28. package/dist/media-viewer/viewer-motion.js +20 -7
  29. package/dist/media-viewer/zoom-pan.d.ts +1 -0
  30. package/dist/media-viewer/zoom-pan.js +3 -0
  31. package/dist/media-viewer/zoom-store.d.ts +8 -0
  32. package/dist/media-viewer/zoom-store.js +30 -0
  33. package/dist/menu/menu.js +6 -3
  34. package/dist/menu/point-menu.js +32 -12
  35. package/dist/motion/flip.js +33 -20
  36. package/dist/page/page-actions.js +2 -1
  37. package/dist/page/page-breadcrumbs.js +2 -1
  38. package/dist/page/use-measured-fit.d.ts +5 -0
  39. package/dist/page/use-measured-fit.js +47 -5
  40. package/dist/page/use-stuck.js +12 -2
  41. package/dist/pagination/pagination-fit.d.ts +3 -1
  42. package/dist/pagination/pagination-fit.js +4 -3
  43. package/dist/pagination/pagination-motion.js +18 -11
  44. package/dist/pagination/pagination.css +50 -6
  45. package/dist/pagination/pagination.js +20 -3
  46. package/dist/progress/progress.css +3 -2
  47. package/dist/progress/progress.js +15 -3
  48. package/dist/select/select-labels.d.ts +2 -0
  49. package/dist/select/select-labels.js +5 -1
  50. package/dist/select/select.css +7 -11
  51. package/dist/select/select.d.ts +1 -1
  52. package/dist/select/select.js +12 -6
  53. package/dist/shortcuts/hints.js +10 -4
  54. package/dist/shortcuts/provider.d.ts +4 -2
  55. package/dist/shortcuts/provider.js +14 -17
  56. package/dist/skeleton/skeleton.css +22 -9
  57. package/dist/slider/slider-feel.js +18 -6
  58. package/dist/slider/slider.css +9 -10
  59. package/dist/slider/slider.js +13 -10
  60. package/dist/smooth-caret/caret-controller.js +18 -6
  61. package/dist/smooth-caret/caret-geometry.js +18 -7
  62. package/dist/smooth-caret/caret-glide.js +3 -2
  63. package/dist/smooth-caret/remote-carets.js +20 -3
  64. package/dist/smooth-caret/smooth-caret.css +1 -1
  65. package/dist/switch/switch-gesture.d.ts +4 -4
  66. package/dist/switch/switch-gesture.js +52 -14
  67. package/dist/switch/switch.css +12 -13
  68. package/dist/switch/switch.d.ts +2 -1
  69. package/dist/switch/switch.js +4 -6
  70. package/dist/table/data-table.js +32 -12
  71. package/dist/table/table-cells.d.ts +20 -8
  72. package/dist/table/table-cells.js +18 -20
  73. package/dist/table/table-model.d.ts +0 -2
  74. package/dist/table/table-model.js +15 -9
  75. package/dist/table/table-search.d.ts +12 -0
  76. package/dist/table/table-search.js +34 -0
  77. package/dist/table/table-sections.d.ts +3 -2
  78. package/dist/table/table-sections.js +16 -13
  79. package/dist/table/table.css +2 -14
  80. package/dist/table/use-latest.d.ts +6 -0
  81. package/dist/table/use-latest.js +22 -0
  82. package/dist/table/use-table-view.js +36 -24
  83. package/package.json +1 -1
@@ -85,25 +85,36 @@ function displayedText(field, text) {
85
85
  }
86
86
  function fillMirror(field, style) {
87
87
  const host = ensureMirror();
88
- for (const property of MIRRORED_PROPERTIES) {
89
- host.style.setProperty(property, style.getPropertyValue(property));
90
- }
88
+ /*
89
+ * Everything is read from the field before the mirror is written: a read after a write would force
90
+ * layout and style with the mirror dirty.
91
+ */
92
+ const values = MIRRORED_PROPERTIES.map((property) => style.getPropertyValue(property));
93
+ const fieldWidth = field.offsetWidth;
91
94
  /*
92
95
  * Only a textarea can show a scrollbar. Edge 157 leaves an input's padding out of its clientWidth; read as a
93
96
  * scrollbar, it narrowed the mirror, so centred and right-to-left text measured from the wrong edge.
94
97
  */
95
98
  const scrollbar = field instanceof HTMLTextAreaElement
96
- ? Math.max(0, field.offsetWidth -
99
+ ? Math.max(0, fieldWidth -
97
100
  field.clientWidth -
98
101
  pixels(style.borderLeftWidth) -
99
102
  pixels(style.borderRightWidth))
100
103
  : 0;
104
+ const paddingRight = pixels(style.paddingRight) + scrollbar;
105
+ const whiteSpace = field instanceof HTMLTextAreaElement ? style.whiteSpace : "pre";
106
+ MIRRORED_PROPERTIES.forEach((property, index) => {
107
+ const value = values[index] ?? "";
108
+ if (host.style.getPropertyValue(property) !== value) {
109
+ host.style.setProperty(property, value);
110
+ }
111
+ });
101
112
  host.style.borderStyle = "solid";
102
113
  host.style.borderColor = "transparent";
103
114
  host.style.boxSizing = "border-box";
104
- host.style.width = `${field.offsetWidth}px`;
105
- host.style.paddingRight = `${pixels(style.paddingRight) + scrollbar}px`;
106
- host.style.whiteSpace = field instanceof HTMLTextAreaElement ? style.whiteSpace : "pre";
115
+ host.style.width = `${fieldWidth}px`;
116
+ host.style.paddingRight = `${paddingRight}px`;
117
+ host.style.whiteSpace = whiteSpace;
107
118
  const caret = field.selectionStart ?? 0;
108
119
  const marker = document.createElement("span");
109
120
  marker.textContent = MARKER_TEXT;
@@ -23,10 +23,11 @@ export function leash(axis, target) {
23
23
  export function atRest(axis, target) {
24
24
  return Math.abs(target - axis.at) < REST_DISTANCE && Math.abs(axis.speed) < REST_SPEED;
25
25
  }
26
+ let reducedMotionQuery = null;
26
27
  /* Reduced motion and the `still` preset both turn the glide off: the caret snaps. */
27
28
  export function prefersReducedMotion(from) {
28
- return (window.matchMedia("(prefers-reduced-motion: reduce)").matches ||
29
- Boolean(from?.closest('[data-kumiko-motion="still"]')));
29
+ reducedMotionQuery ??= window.matchMedia("(prefers-reduced-motion: reduce)");
30
+ return reducedMotionQuery.matches || Boolean(from?.closest('[data-kumiko-motion="still"]'));
30
31
  }
31
32
  export function createGlider(paint) {
32
33
  let axes = [];
@@ -26,11 +26,25 @@ function createEntry(layer) {
26
26
  tag.dataset.slot = "remote-caret-tag";
27
27
  root.append(bar, tag);
28
28
  layer.append(root);
29
+ let writtenHeight = "";
29
30
  const glider = createGlider((x, y, height) => {
30
31
  root.style.transform = `translate3d(${x}px, ${y}px, 0)`;
31
- root.style.height = `${height}px`;
32
+ const written = `${height}px`;
33
+ if (written !== writtenHeight) {
34
+ writtenHeight = written;
35
+ root.style.height = written;
36
+ }
32
37
  });
33
- return { root, tag, glider, spot: null, shown: false, stillTimer: 0, leaveTimer: 0 };
38
+ return {
39
+ root,
40
+ tag,
41
+ glider,
42
+ spot: null,
43
+ colour: "",
44
+ shown: false,
45
+ stillTimer: 0,
46
+ leaveTimer: 0
47
+ };
34
48
  }
35
49
  function hide(entry) {
36
50
  delete entry.root.dataset.visible;
@@ -46,7 +60,10 @@ function settleTag(entry, stillMs) {
46
60
  }, stillMs);
47
61
  }
48
62
  function dress(entry, caret) {
49
- entry.root.style.setProperty("--kumiko-remote-caret-color", caret.colour);
63
+ if (entry.colour !== caret.colour) {
64
+ entry.colour = caret.colour;
65
+ entry.root.style.setProperty("--kumiko-remote-caret-color", caret.colour);
66
+ }
50
67
  if (entry.tag.textContent !== caret.name) {
51
68
  entry.tag.textContent = caret.name;
52
69
  }
@@ -35,7 +35,7 @@
35
35
  block-size: 1rem;
36
36
  pointer-events: none;
37
37
  opacity: 0;
38
- will-change: transform, height;
38
+ will-change: transform;
39
39
  transition: opacity var(--kumiko-fade-duration) var(--kumiko-fade-ease);
40
40
  }
41
41
 
@@ -14,12 +14,12 @@ export interface Swallow {
14
14
  }
15
15
  export declare function createSwallow(): Swallow;
16
16
  /**
17
- * The drag of a switch's thumb as state a switch can render: where the thumb is while it is dragged (or null),
18
- * and the pointer handlers that move it. A drag ends on the side where the thumb's centre is, through the
19
- * controller, and swallows the click that follows it so the switch does not flip twice.
17
+ * The drag of a switch's thumb: whether it is being dragged, and the pointer handlers that move it. A drag ends
18
+ * on the side where the thumb's centre is, through the controller, and swallows the click that follows it so the
19
+ * switch does not flip twice.
20
20
  */
21
21
  export declare function useSwitchDrag(controller: SwitchController): {
22
- offset: number | null;
22
+ dragging: boolean;
23
23
  down: (event: PointerEvent<HTMLElement>) => void;
24
24
  move: (event: PointerEvent<HTMLElement>) => void;
25
25
  up: (event: PointerEvent<HTMLElement>) => void;
@@ -1,13 +1,13 @@
1
- import { useRef, useState } from "react";
1
+ import { useCallback, useEffect, useRef, useState } from "react";
2
2
  import { endsOn, intentOf, thumbOffset } from "./switch-drag";
3
3
  const idle = { phase: "idle" };
4
4
  const px = (value) => Number(value.replace("px", ""));
5
+ const dragProperty = "--kumiko-switch-drag-x";
5
6
  /** How far the thumb can travel along the track, in pixels, from layout so a hover or press scale cannot change it. */
6
- function travelOf(root) {
7
- const thumb = root.querySelector('[data-slot="switch-thumb"]');
8
- const found = getComputedStyle(root);
7
+ function travelOf(root, found) {
8
+ const thumb = px(getComputedStyle(root, "::before").width);
9
9
  const inset = px(found.paddingInlineStart) + px(found.paddingInlineEnd);
10
- return Math.max(0, root.clientWidth - inset - (thumb?.offsetWidth ?? 0));
10
+ return Math.max(0, root.clientWidth - inset - thumb);
11
11
  }
12
12
  /** The gesture a press begins, or none when the switch cannot be changed or the press is not a primary one. */
13
13
  function begin(event, checked) {
@@ -16,6 +16,7 @@ function begin(event, checked) {
16
16
  if (blocked || event.button !== 0 || !event.isPrimary) {
17
17
  return idle;
18
18
  }
19
+ const found = getComputedStyle(root);
19
20
  return {
20
21
  phase: "pressed",
21
22
  id: event.pointerId,
@@ -23,8 +24,8 @@ function begin(event, checked) {
23
24
  y: event.clientY,
24
25
  start: {
25
26
  checked,
26
- travel: travelOf(root),
27
- direction: getComputedStyle(root).direction === "rtl" ? -1 : 1
27
+ travel: travelOf(root, found),
28
+ direction: found.direction === "rtl" ? -1 : 1
28
29
  }
29
30
  };
30
31
  }
@@ -85,20 +86,57 @@ function endedOn(gesture, event) {
85
86
  return endsOn(thumbOffset(gesture.start, event.clientX - gesture.x), gesture.start.travel);
86
87
  }
87
88
  /**
88
- * The drag of a switch's thumb as state a switch can render: where the thumb is while it is dragged (or null),
89
- * and the pointer handlers that move it. A drag ends on the side where the thumb's centre is, through the
90
- * controller, and swallows the click that follows it so the switch does not flip twice.
89
+ * Where the dragged thumb is, kept on the switch as a custom property: the first place is written at once, the
90
+ * later ones once a frame, so a move does not render the switch. Only whether it is dragged is state.
91
+ */
92
+ function useThumbOffset() {
93
+ const [dragging, setDragging] = useState(false);
94
+ const target = useRef(null);
95
+ const frame = useRef(0);
96
+ const pending = useRef(0);
97
+ const stopFrame = useCallback(() => {
98
+ cancelAnimationFrame(frame.current);
99
+ frame.current = 0;
100
+ }, []);
101
+ useEffect(() => stopFrame, [stopFrame]);
102
+ const place = (element, offset) => {
103
+ pending.current = offset;
104
+ if (target.current === null) {
105
+ target.current = element;
106
+ element.style.setProperty(dragProperty, `${offset}px`);
107
+ setDragging(true);
108
+ }
109
+ else if (frame.current === 0) {
110
+ frame.current = requestAnimationFrame(() => {
111
+ frame.current = 0;
112
+ target.current?.style.setProperty(dragProperty, `${pending.current}px`);
113
+ });
114
+ }
115
+ };
116
+ const clear = () => {
117
+ stopFrame();
118
+ target.current?.style.removeProperty(dragProperty);
119
+ target.current = null;
120
+ setDragging(false);
121
+ };
122
+ return { dragging, place, clear };
123
+ }
124
+ /**
125
+ * The drag of a switch's thumb: whether it is being dragged, and the pointer handlers that move it. A drag ends
126
+ * on the side where the thumb's centre is, through the controller, and swallows the click that follows it so the
127
+ * switch does not flip twice.
91
128
  */
92
129
  export function useSwitchDrag(controller) {
93
130
  const gesture = useRef(idle);
94
131
  const [swallow] = useState(createSwallow);
95
- const [offset, setOffset] = useState(null);
132
+ const thumb = useThumbOffset();
133
+ useEffect(() => () => swallow.clear(), [swallow]);
96
134
  const finish = () => {
97
135
  gesture.current = idle;
98
- setOffset(null);
136
+ thumb.clear();
99
137
  };
100
138
  return {
101
- offset,
139
+ dragging: thumb.dragging,
102
140
  down: (event) => {
103
141
  swallow.clear();
104
142
  gesture.current = begin(event, controller.getChecked());
@@ -107,7 +145,7 @@ export function useSwitchDrag(controller) {
107
145
  const next = follow(gesture.current, event);
108
146
  gesture.current = next.gesture;
109
147
  if (next.offset !== null) {
110
- setOffset(next.offset);
148
+ thumb.place(event.currentTarget, next.offset);
111
149
  }
112
150
  },
113
151
  up: (event) => {
@@ -89,7 +89,8 @@
89
89
  border-color: var(--kumiko-switch-readonly-checked-border, var(--kumiko-switch-checked-bg));
90
90
  }
91
91
 
92
- [data-slot="switch-thumb"] {
92
+ [data-slot="switch"]::before {
93
+ content: "";
93
94
  display: block;
94
95
  inline-size: var(--_thumb);
95
96
  block-size: var(--_thumb);
@@ -102,32 +103,31 @@
102
103
  scale var(--kumiko-press-duration) var(--kumiko-press-ease);
103
104
  }
104
105
 
105
- [data-slot="switch"][data-checked] [data-slot="switch-thumb"] {
106
+ [data-slot="switch"][data-checked]::before {
106
107
  background: var(--kumiko-switch-checked-thumb-bg);
107
108
  translate: calc(var(--_travel) * var(--_direction)) 0;
108
109
  }
109
110
 
110
- [data-slot="switch"][data-readonly] [data-slot="switch-thumb"] {
111
+ [data-slot="switch"][data-readonly]::before {
111
112
  background: var(--kumiko-switch-readonly-thumb-bg, var(--kumiko-switch-border));
112
113
  }
113
114
 
114
- [data-slot="switch"][data-readonly][data-checked] [data-slot="switch-thumb"] {
115
+ [data-slot="switch"][data-readonly][data-checked]::before {
115
116
  background: var(--kumiko-switch-readonly-checked-thumb-bg, var(--kumiko-switch-checked-bg));
116
117
  }
117
118
 
118
119
  /* While the thumb is dragged it sits where the pointer put it, so it never lags behind. */
119
- [data-slot="switch"][data-dragging] [data-slot="switch-thumb"] {
120
+ [data-slot="switch"][data-dragging]::before {
120
121
  translate: calc(var(--kumiko-switch-drag-x, 0px) * var(--_direction)) 0;
121
122
  transition-property: background-color, scale;
122
123
  }
123
124
 
124
- [data-slot="switch"]:not([data-disabled], [data-readonly]):active [data-slot="switch-thumb"] {
125
+ [data-slot="switch"]:not([data-disabled], [data-readonly]):active::before {
125
126
  scale: var(--kumiko-squash-x) var(--kumiko-squash-y);
126
127
  }
127
128
 
128
129
  @media (hover: hover) and (pointer: fine) {
129
- [data-slot="switch"]:not([data-disabled], [data-readonly]):hover:not(:active)
130
- [data-slot="switch-thumb"] {
130
+ [data-slot="switch"]:not([data-disabled], [data-readonly]):hover:not(:active)::before {
131
131
  scale: calc(1 + (var(--kumiko-hover-scale) - 1) * 1.5);
132
132
  }
133
133
  }
@@ -138,8 +138,7 @@
138
138
  border-color: ButtonText;
139
139
  background: Canvas;
140
140
  }
141
-
142
- [data-slot="switch-thumb"] {
141
+ [data-slot="switch"]::before {
143
142
  background: ButtonText;
144
143
  }
145
144
 
@@ -148,7 +147,7 @@
148
147
  background: Highlight;
149
148
  }
150
149
 
151
- [data-slot="switch"][data-checked] [data-slot="switch-thumb"] {
150
+ [data-slot="switch"][data-checked]::before {
152
151
  background: HighlightText;
153
152
  }
154
153
 
@@ -157,7 +156,7 @@
157
156
  background: Canvas;
158
157
  }
159
158
 
160
- [data-slot="switch"][data-readonly] [data-slot="switch-thumb"] {
159
+ [data-slot="switch"][data-readonly]::before {
161
160
  background: ButtonText;
162
161
  }
163
162
 
@@ -165,7 +164,7 @@
165
164
  border-color: Highlight;
166
165
  }
167
166
 
168
- [data-slot="switch"][data-readonly][data-checked] [data-slot="switch-thumb"] {
167
+ [data-slot="switch"][data-readonly][data-checked]::before {
169
168
  background: Highlight;
170
169
  }
171
170
  }
@@ -9,7 +9,8 @@ export type SwitchProps = Omit<SdkSwitchProps, "children"> & {
9
9
  };
10
10
  /**
11
11
  * The ui-sdk switch with Kumiko's look and motion. A tap flips it, and the thumb can also be dragged: it
12
- * follows the pointer along the track, and the switch ends on the side where the thumb's centre is.
12
+ * follows the pointer along the track, and the switch ends on the side where the thumb's centre is. The thumb
13
+ * is drawn by the switch's `::before`, so it costs no element.
13
14
  */
14
15
  export declare function Switch({ size, controller, ...props }: SwitchProps): React.JSX.Element;
15
16
  export {};
@@ -1,10 +1,7 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
- import { createSwitchController, Switch as SdkSwitch, SwitchThumb } from "@ui-sdk/react/switch";
2
+ import { createSwitchController, Switch as SdkSwitch } from "@ui-sdk/react/switch";
3
3
  import { useState } from "react";
4
4
  import { useSwitchDrag } from "./switch-gesture";
5
- function dragStyle(offset) {
6
- return { "--kumiko-switch-drag-x": `${offset}px` };
7
- }
8
5
  /** Runs the caller's handler first, then Kumiko's, so the caller can still see every event. */
9
6
  function alongside(callers, ours) {
10
7
  return (event) => {
@@ -14,11 +11,12 @@ function alongside(callers, ours) {
14
11
  }
15
12
  /**
16
13
  * The ui-sdk switch with Kumiko's look and motion. A tap flips it, and the thumb can also be dragged: it
17
- * follows the pointer along the track, and the switch ends on the side where the thumb's centre is.
14
+ * follows the pointer along the track, and the switch ends on the side where the thumb's centre is. The thumb
15
+ * is drawn by the switch's `::before`, so it costs no element.
18
16
  */
19
17
  export function Switch({ size = "md", controller, ...props }) {
20
18
  const [own] = useState(createSwitchController);
21
19
  const used = controller ?? own;
22
20
  const drag = useSwitchDrag(used);
23
- return (_jsx(SdkSwitch, { "data-size": size, "data-dragging": drag.offset === null ? undefined : "", ...props, controller: used, onPointerDown: alongside(props.onPointerDown, drag.down), onPointerMove: alongside(props.onPointerMove, drag.move), onPointerUp: alongside(props.onPointerUp, drag.up), onPointerCancel: alongside(props.onPointerCancel, drag.cancel), onClick: alongside(props.onClick, drag.click), children: _jsx(SwitchThumb, { style: drag.offset === null ? undefined : dragStyle(drag.offset) }) }));
21
+ return (_jsx(SdkSwitch, { "data-size": size, "data-dragging": drag.dragging ? "" : undefined, ...props, controller: used, onPointerDown: alongside(props.onPointerDown, drag.down), onPointerMove: alongside(props.onPointerMove, drag.move), onPointerUp: alongside(props.onPointerUp, drag.up), onPointerCancel: alongside(props.onPointerCancel, drag.cancel), onClick: alongside(props.onClick, drag.click) }));
24
22
  }
@@ -1,5 +1,5 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { useId } from "react";
2
+ import { useId, useMemo } from "react";
3
3
  import { Pagination } from "../pagination";
4
4
  import { Body, Header, Toolbar } from "./table-sections";
5
5
  import { useTableView } from "./use-table-view";
@@ -36,6 +36,35 @@ function selectionOf(props, view) {
36
36
  const actions = typeof props.selection === "object" ? props.selection.actions : undefined;
37
37
  return { actions: actions?.(view.selectedRows) };
38
38
  }
39
+ /** What every row needs, kept the same between renders unless one of its parts changes. */
40
+ function useRowContext(props, view, hasSelection) {
41
+ const { words, dispatch, rowName, linkColumnId } = view;
42
+ const { columns, locale, rowLink, onRowPress } = props;
43
+ const overlayId = overlayColumnId(props, view);
44
+ return useMemo(() => ({
45
+ words,
46
+ dispatch,
47
+ rowName,
48
+ linkColumnId,
49
+ columns,
50
+ locale,
51
+ selection: hasSelection,
52
+ rowLink,
53
+ onRowPress,
54
+ overlayId
55
+ }), [
56
+ words,
57
+ dispatch,
58
+ rowName,
59
+ linkColumnId,
60
+ columns,
61
+ locale,
62
+ hasSelection,
63
+ rowLink,
64
+ onRowPress,
65
+ overlayId
66
+ ]);
67
+ }
39
68
  /**
40
69
  * A data table over TanStack Table, drawn with Kumiko's look: sorting, search, paging, row selection, loading
41
70
  * rows and row actions. Every row is one link (or button) with a single tab stop, and the whole row can be
@@ -45,15 +74,6 @@ export function DataTable(props) {
45
74
  const captionId = useId();
46
75
  const view = useTableView(props);
47
76
  const selection = selectionOf(props, view);
48
- const context = {
49
- view,
50
- columns: props.columns,
51
- locale: props.locale,
52
- selection: selection !== undefined,
53
- selected: new Set(view.state.selected),
54
- rowLink: props.rowLink,
55
- onRowPress: props.onRowPress,
56
- overlayId: overlayColumnId(props, view)
57
- };
58
- return (_jsxs("div", { "data-slot": "table", "data-look": props.look, "data-density": props.density, "data-narrow": props.narrow, "data-narrow-at": props.narrowAt, "data-sticky": props.stickyHeader === undefined ? undefined : String(props.stickyHeader), children: [_jsx(Toolbar, { view: view, search: searchOf(props, view), selection: selection }), _jsx("div", { "data-slot": "table-scroll", role: "region", "aria-labelledby": captionId, tabIndex: 0, children: _jsxs("table", { role: "table", "aria-busy": view.body === "loading" || undefined, children: [_jsx("caption", { id: captionId, "data-slot": "table-visually-hidden", children: props.label }), _jsx(Header, { columns: props.columns, view: view, selection: selection !== undefined }), _jsx(Body, { context: context, empty: props.empty })] }) }), _jsx(PagingBar, { props: props, view: view })] }));
77
+ const context = useRowContext(props, view, selection !== undefined);
78
+ return (_jsxs("div", { "data-slot": "table", "data-look": props.look, "data-density": props.density, "data-narrow": props.narrow, "data-narrow-at": props.narrowAt, "data-sticky": props.stickyHeader === undefined ? undefined : String(props.stickyHeader), children: [_jsx(Toolbar, { view: view, search: searchOf(props, view), selection: selection }), _jsx("div", { "data-slot": "table-scroll", role: "region", "aria-labelledby": captionId, tabIndex: 0, children: _jsxs("table", { role: "table", "aria-busy": view.body === "loading" || undefined, children: [_jsx("caption", { id: captionId, "data-slot": "table-visually-hidden", children: props.label }), _jsx(Header, { columns: props.columns, view: view, selection: selection !== undefined }), _jsx(Body, { view: view, context: context, empty: props.empty })] }) }), _jsx(PagingBar, { props: props, view: view })] }));
59
79
  }
@@ -1,19 +1,31 @@
1
+ import { type ReactNode } from "react";
2
+ import type { TableLabels } from "./table-labels";
3
+ import type { TableEvent } from "./table-state";
1
4
  import type { RowLink, TableColumn } from "./table-types";
2
- import type { PageRow, TableView } from "./use-table-view";
3
- /** What every row needs besides its own data. */
5
+ /** What every row needs besides its own data. Keep it stable between renders, or every row draws again. */
4
6
  export interface RowContext<Row extends object> {
5
- view: TableView<Row>;
7
+ words: TableLabels;
8
+ dispatch: (event: TableEvent) => void;
9
+ rowName: (row: Row) => string;
10
+ linkColumnId: string | undefined;
6
11
  columns: readonly TableColumn<Row>[];
7
12
  locale: string | undefined;
8
13
  selection: boolean;
9
- selected: ReadonlySet<string>;
10
14
  rowLink: RowLink<Row> | undefined;
11
15
  onRowPress: ((row: Row) => void) | undefined;
12
16
  /** The column that holds the hidden copy of the link, which covers the whole row. */
13
17
  overlayId: string | undefined;
14
18
  }
15
- /** A body row: the selection box, then a cell for each column. */
16
- export declare function BodyRow<Row extends object>({ context, item }: {
19
+ interface BodyRowProps<Row extends object> {
17
20
  context: RowContext<Row>;
18
- item: PageRow<Row>;
19
- }): React.JSX.Element;
21
+ row: Row;
22
+ id: string;
23
+ name: string;
24
+ selected: boolean;
25
+ }
26
+ /**
27
+ * A body row: the selection box, then a cell for each column. It draws again only when its own row, its name,
28
+ * whether it is selected or the shared context changes.
29
+ */
30
+ export declare const BodyRow: <Row extends object>(props: BodyRowProps<Row>) => ReactNode;
31
+ export {};
@@ -1,5 +1,5 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { cloneElement } from "react";
2
+ import { cloneElement, memo } from "react";
3
3
  import { Checkbox } from "../checkbox";
4
4
  import { RowActions } from "./table-actions";
5
5
  import { isActionsColumn } from "./table-columns";
@@ -43,25 +43,23 @@ function RowOverlay({ context, row }) {
43
43
  }
44
44
  return null;
45
45
  }
46
- function DataCell({ context, column, item }) {
47
- const { view } = context;
48
- const content = column.render(item.row, { locale: context.locale, labels: view.words });
49
- const linked = column.id === view.linkColumnId;
50
- return (_jsxs("td", { role: "cell", "data-align": column.align, "data-priority": column.priority, "data-link": linked || undefined, "data-card-label": column.cardLabel ? undefined : "off", children: [_jsx("span", { "data-slot": "table-cell-label", "aria-hidden": "true", children: column.label }), _jsx("span", { "data-slot": "table-cell-value", children: linked ? (_jsx(LinkedContent, { context: context, row: item.row, children: content })) : (content) }), column.id === context.overlayId && _jsx(RowOverlay, { context: context, row: item.row })] }));
46
+ function DataCell({ context, column, row }) {
47
+ const content = column.render(row, { locale: context.locale, labels: context.words });
48
+ const linked = column.id === context.linkColumnId;
49
+ return (_jsxs("td", { role: "cell", "data-align": column.align, "data-priority": column.priority, "data-link": linked || undefined, children: [column.cardLabel && (_jsx("span", { "data-slot": "table-cell-label", "aria-hidden": "true", children: column.label })), _jsx("span", { "data-slot": "table-cell-value", children: linked ? (_jsx(LinkedContent, { context: context, row: row, children: content })) : (content) }), column.id === context.overlayId && _jsx(RowOverlay, { context: context, row: row })] }));
51
50
  }
52
- function ActionsCell({ context, column, item }) {
53
- const { view } = context;
54
- return (_jsxs("td", { role: "cell", "data-kind": "actions", "data-align": "end", "data-priority": column.priority, children: [isActionsColumn(column) && (_jsx(RowActions, { column: column, row: item.row, name: view.rowName(item.row), words: view.words })), column.id === context.overlayId && _jsx(RowOverlay, { context: context, row: item.row })] }));
51
+ function ActionsCell({ context, column, row, name }) {
52
+ return (_jsxs("td", { role: "cell", "data-kind": "actions", "data-align": "end", "data-priority": column.priority, children: [isActionsColumn(column) && (_jsx(RowActions, { column: column, row: row, name: name, words: context.words })), column.id === context.overlayId && _jsx(RowOverlay, { context: context, row: row })] }));
55
53
  }
56
- /** A body row: the selection box, then a cell for each column. */
57
- export function BodyRow({ context, item }) {
58
- const { view } = context;
59
- const selected = context.selected.has(item.id);
60
- return (_jsxs("tr", { role: "row", "data-selected": selected || undefined, "data-pressable": pressable(context) || undefined, children: [context.selection && (_jsx("td", { role: "cell", "data-kind": "select", children: _jsx("span", { "data-slot": "table-control", children: _jsx(Checkbox, { size: "sm", "aria-label": view.words.selectRow(view.rowName(item.row)), checked: selected, onCheckedChange: (next) => {
61
- view.dispatch({
62
- type: "select",
63
- ids: [item.id],
64
- on: next === true
65
- });
66
- } }) }) })), context.columns.map((column) => isActionsColumn(column) ? (_jsx(ActionsCell, { context: context, column: column, item: item }, column.id)) : (_jsx(DataCell, { context: context, column: column, item: item }, column.id)))] }));
54
+ function BodyRowView({ context, row, id, name, selected }) {
55
+ const { words, dispatch } = context;
56
+ return (_jsxs("tr", { role: "row", "data-selected": selected || undefined, "data-pressable": pressable(context) || undefined, children: [context.selection && (_jsx("td", { role: "cell", "data-kind": "select", children: _jsx("span", { "data-slot": "table-control", children: _jsx(Checkbox, { size: "sm", "aria-label": words.selectRow(name), checked: selected, onCheckedChange: (next) => {
57
+ dispatch({ type: "select", ids: [id], on: next === true });
58
+ } }) }) })), context.columns.map((column) => isActionsColumn(column) ? (_jsx(ActionsCell, { context: context, column: column, row: row, name: name }, column.id)) : (_jsx(DataCell, { context: context, column: column, row: row, name: name }, column.id)))] }));
67
59
  }
60
+ const memoRow = memo;
61
+ /**
62
+ * A body row: the selection box, then a cell for each column. It draws again only when its own row, its name,
63
+ * whether it is selected or the shared context changes.
64
+ */
65
+ export const BodyRow = memoRow(BodyRowView);
@@ -9,8 +9,6 @@ declare const features: {
9
9
  };
10
10
  type Features = typeof features;
11
11
  type Rows<Row extends object> = TanRow<Features, Row>[];
12
- /** Folds case and accents away, so "jose" finds "José". */
13
- export declare function foldText(text: string): string;
14
12
  export interface ModelOptions<Row extends object> {
15
13
  data: readonly Row[];
16
14
  columns: readonly TableColumn<Row>[];
@@ -1,6 +1,7 @@
1
1
  import { createSortedRowModel, createFilteredRowModel, columnFilteringFeature, globalFilteringFeature, rowSortingFeature, tableFeatures, useTable } from "@tanstack/react-table";
2
2
  import { useCallback, useMemo, useRef } from "react";
3
3
  import { compareSortValues, isActionsColumn } from "./table-columns";
4
+ import { createSearchMatcher } from "./table-search";
4
5
  const features = tableFeatures({
5
6
  rowSortingFeature,
6
7
  columnFilteringFeature,
@@ -8,10 +9,6 @@ const features = tableFeatures({
8
9
  filteredRowModel: createFilteredRowModel(),
9
10
  sortedRowModel: createSortedRowModel()
10
11
  });
11
- /** Folds case and accents away, so "jose" finds "José". */
12
- export function foldText(text) {
13
- return text.normalize("NFD").replaceAll(/\p{M}/gu, "").toLocaleLowerCase();
14
- }
15
12
  function present(value) {
16
13
  return value === null || value === "" ? undefined : value;
17
14
  }
@@ -36,6 +33,14 @@ function useColumnDefs(columns, latest, locale) {
36
33
  // The key stands for the ids and sortable flags; the columns themselves are read through the ref.
37
34
  [key, collator, latest]);
38
35
  }
36
+ /** The search matcher, started afresh whenever the data is another array. */
37
+ function useMatcher(data) {
38
+ const held = useRef(null);
39
+ if (held.current === null || held.current.data !== data) {
40
+ held.current = { data, matcher: createSearchMatcher() };
41
+ }
42
+ return held.current.matcher;
43
+ }
39
44
  /**
40
45
  * The rows after search and sort, from TanStack. It gets stable inputs: its column definitions are rebuilt
41
46
  * only when the columns' ids or sortable flags change, and read the latest columns through a ref, so a
@@ -46,12 +51,13 @@ export function useTableModel(options) {
46
51
  const latest = useRef({ columns, rowId });
47
52
  latest.current = { columns, rowId };
48
53
  const defs = useColumnDefs(columns, latest, locale);
54
+ const matcher = useMatcher(data);
49
55
  const globalFilterFn = useCallback((row, columnId, needle) => {
50
- const text = latest.current.columns
51
- .find((column) => column.id === columnId)
52
- ?.text(row.original);
53
- return text ? foldText(text).includes(foldText(needle).trim()) : false;
54
- }, []);
56
+ const column = latest.current.columns.find((item) => item.id === columnId);
57
+ return column
58
+ ? matcher.matches(row.original, column, () => column.text(row.original), needle)
59
+ : false;
60
+ }, [matcher]);
55
61
  const getRowId = useCallback((row) => latest.current.rowId(row), []);
56
62
  const table = useTable({
57
63
  features,
@@ -0,0 +1,12 @@
1
+ /** Folds case and accents away, so "jose" finds "José". */
2
+ export declare function foldText(text: string): string;
3
+ export interface SearchMatcher<Row extends object, Column extends object> {
4
+ /** Whether the cell's text holds the needle. A cell with no text never matches. */
5
+ matches: (row: Row, column: Column, text: () => string | null, needle: string) => boolean;
6
+ }
7
+ /**
8
+ * Matches search text against cells. The folded needle is kept until the search changes, and the folded text
9
+ * of each cell is kept by row and column object, so a keystroke folds only what it has not seen. A new matcher
10
+ * starts empty: make one when the data changes.
11
+ */
12
+ export declare function createSearchMatcher<Row extends object, Column extends object>(): SearchMatcher<Row, Column>;
@@ -0,0 +1,34 @@
1
+ /** Folds case and accents away, so "jose" finds "José". */
2
+ export function foldText(text) {
3
+ return text.normalize("NFD").replaceAll(/\p{M}/gu, "").toLocaleLowerCase();
4
+ }
5
+ /**
6
+ * Matches search text against cells. The folded needle is kept until the search changes, and the folded text
7
+ * of each cell is kept by row and column object, so a keystroke folds only what it has not seen. A new matcher
8
+ * starts empty: make one when the data changes.
9
+ */
10
+ export function createSearchMatcher() {
11
+ let search;
12
+ let folded = "";
13
+ const cells = new WeakMap();
14
+ return {
15
+ matches(row, column, text, needle) {
16
+ if (needle !== search) {
17
+ search = needle;
18
+ folded = foldText(needle).trim();
19
+ }
20
+ let byRow = cells.get(column);
21
+ if (!byRow) {
22
+ byRow = new WeakMap();
23
+ cells.set(column, byRow);
24
+ }
25
+ let cell = byRow.get(row);
26
+ if (cell === undefined) {
27
+ const plain = text();
28
+ cell = plain ? foldText(plain) : null;
29
+ byRow.set(row, cell);
30
+ }
31
+ return cell !== null && cell.includes(folded);
32
+ }
33
+ };
34
+ }