@alulore/kumiko 0.20.2 → 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 (80) 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/button/button.css +21 -7
  5. package/dist/card/card.css +3 -2
  6. package/dist/card/item-card.d.ts +1 -1
  7. package/dist/card/item-card.js +3 -2
  8. package/dist/chip/chip.css +6 -10
  9. package/dist/chip/chip.js +1 -1
  10. package/dist/form/error-line.js +4 -3
  11. package/dist/input/input.css +99 -114
  12. package/dist/list-row/list-row-motion.js +29 -10
  13. package/dist/media-viewer/gesture-press.d.ts +1 -0
  14. package/dist/media-viewer/gesture-press.js +12 -0
  15. package/dist/media-viewer/media-slide.d.ts +3 -1
  16. package/dist/media-viewer/media-slide.js +3 -2
  17. package/dist/media-viewer/media-viewer.css +5 -1
  18. package/dist/media-viewer/media-viewer.js +41 -23
  19. package/dist/media-viewer/stage-rect.d.ts +16 -0
  20. package/dist/media-viewer/stage-rect.js +56 -0
  21. package/dist/media-viewer/view-gesture.d.ts +1 -2
  22. package/dist/media-viewer/view-gesture.js +39 -44
  23. package/dist/media-viewer/viewer-chrome.d.ts +2 -1
  24. package/dist/media-viewer/viewer-chrome.js +3 -1
  25. package/dist/media-viewer/viewer-motion.d.ts +1 -0
  26. package/dist/media-viewer/viewer-motion.js +20 -7
  27. package/dist/media-viewer/zoom-pan.d.ts +1 -0
  28. package/dist/media-viewer/zoom-pan.js +3 -0
  29. package/dist/media-viewer/zoom-store.d.ts +8 -0
  30. package/dist/media-viewer/zoom-store.js +30 -0
  31. package/dist/menu/menu.js +6 -3
  32. package/dist/menu/point-menu.js +32 -12
  33. package/dist/page/page-actions.js +2 -1
  34. package/dist/page/page-breadcrumbs.js +2 -1
  35. package/dist/page/use-measured-fit.d.ts +5 -0
  36. package/dist/page/use-measured-fit.js +47 -5
  37. package/dist/page/use-stuck.js +12 -2
  38. package/dist/pagination/pagination-fit.d.ts +3 -1
  39. package/dist/pagination/pagination-fit.js +4 -3
  40. package/dist/pagination/pagination-motion.js +18 -11
  41. package/dist/pagination/pagination.css +50 -6
  42. package/dist/pagination/pagination.js +20 -3
  43. package/dist/progress/progress.css +3 -2
  44. package/dist/progress/progress.js +15 -3
  45. package/dist/select/select-labels.d.ts +2 -0
  46. package/dist/select/select-labels.js +5 -1
  47. package/dist/select/select.css +7 -11
  48. package/dist/select/select.d.ts +1 -1
  49. package/dist/select/select.js +12 -6
  50. package/dist/shortcuts/hints.js +10 -4
  51. package/dist/shortcuts/provider.d.ts +4 -2
  52. package/dist/shortcuts/provider.js +14 -17
  53. package/dist/skeleton/skeleton.css +22 -9
  54. package/dist/slider/slider-feel.js +18 -6
  55. package/dist/slider/slider.css +9 -10
  56. package/dist/slider/slider.js +13 -10
  57. package/dist/smooth-caret/caret-controller.js +18 -6
  58. package/dist/smooth-caret/caret-geometry.js +18 -7
  59. package/dist/smooth-caret/caret-glide.js +3 -2
  60. package/dist/smooth-caret/remote-carets.js +20 -3
  61. package/dist/smooth-caret/smooth-caret.css +1 -1
  62. package/dist/switch/switch-gesture.d.ts +4 -4
  63. package/dist/switch/switch-gesture.js +52 -14
  64. package/dist/switch/switch.css +12 -13
  65. package/dist/switch/switch.d.ts +2 -1
  66. package/dist/switch/switch.js +4 -6
  67. package/dist/table/data-table.js +32 -12
  68. package/dist/table/table-cells.d.ts +20 -8
  69. package/dist/table/table-cells.js +18 -20
  70. package/dist/table/table-model.d.ts +0 -2
  71. package/dist/table/table-model.js +15 -9
  72. package/dist/table/table-search.d.ts +12 -0
  73. package/dist/table/table-search.js +34 -0
  74. package/dist/table/table-sections.d.ts +3 -2
  75. package/dist/table/table-sections.js +16 -13
  76. package/dist/table/table.css +2 -14
  77. package/dist/table/use-latest.d.ts +6 -0
  78. package/dist/table/use-latest.js +22 -0
  79. package/dist/table/use-table-view.js +36 -24
  80. package/package.json +1 -1
@@ -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
+ }
@@ -1,4 +1,4 @@
1
- import type { ReactNode } from "react";
1
+ import { type ReactNode } from "react";
2
2
  import { type RowContext } from "./table-cells";
3
3
  import type { TableColumn } from "./table-types";
4
4
  import type { TableView } from "./use-table-view";
@@ -9,7 +9,8 @@ export declare function Header<Row extends object>({ columns, view, selection }:
9
9
  selection: boolean;
10
10
  }): React.JSX.Element;
11
11
  /** The body. It comes from one decision: placeholders, the empty message, no results, or the rows. */
12
- export declare function Body<Row extends object>({ context, empty }: {
12
+ export declare function Body<Row extends object>({ view, context, empty }: {
13
+ view: TableView<Row>;
13
14
  context: RowContext<Row>;
14
15
  empty: ReactNode;
15
16
  }): React.JSX.Element;
@@ -1,4 +1,5 @@
1
1
  import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useMemo } from "react";
2
3
  import { Button } from "../button";
3
4
  import { Checkbox } from "../checkbox";
4
5
  import { EmptyState } from "../empty-state";
@@ -16,7 +17,7 @@ function ariaSort(rule) {
16
17
  function HeaderCell({ column, view }) {
17
18
  const rule = view.state.sort.find((item) => item.id === column.id);
18
19
  const actions = isActionsColumn(column);
19
- return (_jsxs("th", { role: "columnheader", scope: "col", "aria-sort": ariaSort(rule), "data-align": column.align, "data-priority": column.priority, "data-kind": actions ? "actions" : undefined, style: { inlineSize: column.width }, children: [column.sortable && (_jsxs("button", { type: "button", "data-slot": "table-sort", onClick: () => {
20
+ return (_jsxs("th", { role: "columnheader", scope: "col", "aria-sort": ariaSort(rule), "data-align": column.align, "data-priority": column.priority, "data-kind": actions ? "actions" : undefined, "data-sortable": column.sortable || undefined, style: { inlineSize: column.width }, children: [column.sortable && (_jsxs("button", { type: "button", "data-slot": "table-sort", onClick: () => {
20
21
  view.dispatch({ type: "sort", id: column.id });
21
22
  }, children: [_jsx("span", { children: column.label }), _jsx(SortIcon, {})] })), !column.sortable && !actions && column.label, actions && _jsx("span", { "data-slot": "table-visually-hidden", children: view.words.actions })] }));
22
23
  }
@@ -38,14 +39,23 @@ export function Header({ columns, view, selection }) {
38
39
  }
39
40
  const skeletonWidths = ["70%", "55%", "85%", "45%", "65%"];
40
41
  function SkeletonRows({ columns, view, selection }) {
41
- return (_jsx(_Fragment, { children: Array.from({ length: view.loadingRows }, (_, index) => (_jsxs("tr", { role: "row", "data-slot": "table-skeleton-row", children: [selection && _jsx("td", { role: "cell", "data-kind": "select" }), columns.map((column, at) => (_jsxs("td", { role: "cell", "data-priority": column.priority, "data-align": column.align, "data-card-label": "off", children: [index === 0 && at === 0 && (_jsx("span", { "data-slot": "table-visually-hidden", children: view.words.loading })), !isActionsColumn(column) && (_jsx(Skeleton, { shape: "text", width: skeletonWidths[(index + at) % skeletonWidths.length] }))] }, column.id)))] }, index))) }));
42
+ return (_jsx(_Fragment, { children: Array.from({ length: view.loadingRows }, (_, index) => (_jsxs("tr", { role: "row", "data-slot": "table-skeleton-row", children: [selection && _jsx("td", { role: "cell", "data-kind": "select" }), columns.map((column, at) => (_jsxs("td", { role: "cell", "data-priority": column.priority, "data-align": column.align, children: [index === 0 && at === 0 && (_jsx("span", { "data-slot": "table-visually-hidden", children: view.words.loading })), !isActionsColumn(column) && (_jsx(Skeleton, { shape: "text", width: skeletonWidths[(index + at) % skeletonWidths.length] }))] }, column.id)))] }, index))) }));
42
43
  }
43
44
  function MessageRow({ span, children }) {
44
45
  return (_jsx("tr", { role: "row", "data-slot": "table-message-row", children: _jsx("td", { role: "cell", colSpan: span, "data-slot": "table-message", children: children }) }));
45
46
  }
47
+ function NoResults({ view }) {
48
+ return (_jsx(EmptyState, { size: "inline", kind: "no-results", title: view.words.noResults, description: null, labels: { clear: view.words.clearSearch }, onClear: (event) => {
49
+ // The Clear button goes away with the empty state, so focus moves to the search box.
50
+ const table = event.currentTarget.closest('[data-slot="table"]');
51
+ view.dispatch({ type: "search", text: "" });
52
+ table?.querySelector('[data-slot="table-toolbar"] input')?.focus();
53
+ } }));
54
+ }
46
55
  /** The body. It comes from one decision: placeholders, the empty message, no results, or the rows. */
47
- export function Body({ context, empty }) {
48
- const { view, columns, selection } = context;
56
+ export function Body({ view, context, empty }) {
57
+ const { columns, selection } = context;
58
+ const chosen = useMemo(() => new Set(view.state.selected), [view.state.selected]);
49
59
  const span = columns.length + (selection ? 1 : 0);
50
60
  let content;
51
61
  switch (view.body) {
@@ -56,17 +66,10 @@ export function Body({ context, empty }) {
56
66
  content = (_jsx(MessageRow, { span: span, children: empty ?? _jsx(EmptyState, { size: "inline", kind: "empty", title: view.words.empty }) }));
57
67
  break;
58
68
  case "no-results":
59
- content = (_jsx(MessageRow, { span: span, children: _jsx(EmptyState, { size: "inline", kind: "no-results", title: view.words.noResults, description: null, labels: { clear: view.words.clearSearch }, onClear: (event) => {
60
- // The Clear button goes away with the empty state, so focus moves to the search box.
61
- const table = event.currentTarget.closest('[data-slot="table"]');
62
- view.dispatch({ type: "search", text: "" });
63
- table
64
- ?.querySelector('[data-slot="table-toolbar"] input')
65
- ?.focus();
66
- } }) }));
69
+ content = (_jsx(MessageRow, { span: span, children: _jsx(NoResults, { view: view }) }));
67
70
  break;
68
71
  case "rows":
69
- content = view.pageRows.map((item) => (_jsx(BodyRow, { context: context, item: item }, item.id)));
72
+ content = view.pageRows.map((item) => (_jsx(BodyRow, { context: context, row: item.row, id: item.id, name: view.rowName(item.row), selected: chosen.has(item.id) }, item.id)));
70
73
  break;
71
74
  }
72
75
  return _jsx("tbody", { role: "rowgroup", children: content });
@@ -239,7 +239,7 @@
239
239
  }
240
240
 
241
241
  /* Header buttons: the whole header cell is the target. */
242
- :where([data-slot="table"] th:has(> [data-slot="table-sort"])) {
242
+ :where([data-slot="table"] th[data-sortable]) {
243
243
  padding: 0;
244
244
  }
245
245
 
@@ -379,7 +379,7 @@
379
379
  --_row-bg: var(--kumiko-table-row-selected-hover-bg);
380
380
  }
381
381
 
382
- :where([data-slot="table"] th:has(> [data-slot="table-sort"]:hover)) {
382
+ :where([data-slot="table"] th[data-sortable]:hover) {
383
383
  color: var(--kumiko-table-fg);
384
384
  }
385
385
 
@@ -579,10 +579,6 @@
579
579
  text-align: start;
580
580
  }
581
581
 
582
- & [data-card-label="off"] > [data-slot="table-cell-label"] {
583
- display: none;
584
- }
585
-
586
582
  & [data-slot="table-cell-value"] {
587
583
  min-inline-size: 0;
588
584
  margin-inline-start: auto;
@@ -736,10 +732,6 @@
736
732
  text-align: start;
737
733
  }
738
734
 
739
- & [data-card-label="off"] > [data-slot="table-cell-label"] {
740
- display: none;
741
- }
742
-
743
735
  & [data-slot="table-cell-value"] {
744
736
  min-inline-size: 0;
745
737
  margin-inline-start: auto;
@@ -891,10 +883,6 @@
891
883
  text-align: start;
892
884
  }
893
885
 
894
- & [data-card-label="off"] > [data-slot="table-cell-label"] {
895
- display: none;
896
- }
897
-
898
886
  & [data-slot="table-cell-value"] {
899
887
  min-inline-size: 0;
900
888
  margin-inline-start: auto;
@@ -0,0 +1,6 @@
1
+ /** A function that never changes identity and always calls the latest `fn`, so memoised children needn't re-render. */
2
+ export declare function useLatestCallback<Args extends unknown[], Result>(fn: (...args: Args) => Result): (...args: Args) => Result;
3
+ /** Whether two objects hold the same entries. */
4
+ export declare function sameEntries(first: object, second: object): boolean;
5
+ /** The first value of this shape: a later one with the same entries gives back the first, so it can sit in a dependency list. */
6
+ export declare function useShallowStable<Value extends object>(value: Value): Value;