strata-react 0.1.14 → 0.1.16

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.
package/dist/index.js CHANGED
@@ -1,9 +1,9 @@
1
- import { _ as HttpError, a as useLoad, b as cn, c as ThemeProvider, d as useSystemColorScheme, f as ChoiceGroup, g as Textarea, h as NativeSelect, i as toLoad, l as colorSchemeScript, m as Input, n as createQueryClient, o as COLOR_SCHEME_STORAGE_KEY, p as Field, r as fetchJson, s as ColorSchemeChoice, t as StrataQueryProvider, u as useColorScheme, v as describeActionError, x as typeSteps, y as describeHttpError } from "./query-CkOrBm3w.js";
1
+ import { S as typeSteps, _ as Textarea, a as toLoad, b as describeHttpError, c as ColorSchemeChoice, d as useColorScheme, f as useSystemColorScheme, g as NativeSelect, h as Input, i as staleOf, l as ThemeProvider, m as Field, n as createQueryClient, o as useLoad, p as ChoiceGroup, r as fetchJson, s as COLOR_SCHEME_STORAGE_KEY, t as StrataQueryProvider, u as colorSchemeScript, v as HttpError, x as cn, y as describeActionError } from "./query-BlPgaCBG.js";
2
2
  import * as React from "react";
3
3
  import { Slot } from "@radix-ui/react-slot";
4
4
  import { cva } from "class-variance-authority";
5
5
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
6
- import { ArrowDown, ArrowUp, ArrowUpDown, ArrowUpRight, Check, CircleAlert, ImageOff, LoaderCircle, RotateCcw, X } from "lucide-react";
6
+ import { ArrowDown, ArrowUp, ArrowUpDown, ArrowUpRight, Check, ChevronRight, CircleAlert, History, ImageOff, LoaderCircle, RotateCcw, X } from "lucide-react";
7
7
  import { Link, NavLink, useSearchParams } from "react-router";
8
8
  import { flexRender, getCoreRowModel, getSortedRowModel, useReactTable } from "@tanstack/react-table";
9
9
  import { useVirtualizer } from "@tanstack/react-virtual";
@@ -180,10 +180,12 @@ function RowAction({ label, subject, icon: Icon, variant = "ghost", size = "sm",
180
180
  /** A native checkbox and its label as one target. `label` is required; `hideLabel` keeps it as the accessible name only. */
181
181
  function Checkbox({ label, hideLabel = false, detail, checked, onCheckedChange, className, disabled, ...props }) {
182
182
  const ref = React.useRef(null);
183
+ const shift = useShiftNote();
183
184
  React.useEffect(() => {
184
185
  if (ref.current) ref.current.indeterminate = checked === "mixed";
185
186
  }, [checked]);
186
187
  return /* @__PURE__ */ jsxs("label", {
188
+ ...shift.handlers,
187
189
  className: cn("inline-flex min-h-6 min-w-6 items-start gap-3", hideLabel && "items-center justify-center", disabled ? "cursor-not-allowed opacity-50" : "cursor-pointer", className),
188
190
  children: [/* @__PURE__ */ jsx("input", {
189
191
  ref,
@@ -192,7 +194,7 @@ function Checkbox({ label, hideLabel = false, detail, checked, onCheckedChange,
192
194
  "aria-checked": checked === "mixed" ? "mixed" : void 0,
193
195
  "aria-label": hideLabel ? label : void 0,
194
196
  disabled,
195
- onChange: (e) => onCheckedChange(e.target.checked),
197
+ onChange: (e) => onCheckedChange(e.target.checked, shift.take()),
196
198
  className: cn("size-4.5 shrink-0 accent-primary", !hideLabel && "mt-0.5", disabled ? "cursor-not-allowed" : "cursor-pointer"),
197
199
  ...props
198
200
  }), !hideLabel && /* @__PURE__ */ jsxs("span", {
@@ -207,6 +209,21 @@ function Checkbox({ label, hideLabel = false, detail, checked, onCheckedChange,
207
209
  })]
208
210
  });
209
211
  }
212
+ /** Notes Shift on the press that leads to a checkbox's change event, which has no modifier keys of its own. */
213
+ function useShiftNote() {
214
+ const held = React.useRef(false);
215
+ return React.useMemo(() => ({
216
+ handlers: {
217
+ onMouseDown: (e) => void (held.current = e.shiftKey),
218
+ onKeyDown: (e) => void (held.current = e.shiftKey)
219
+ },
220
+ take: () => {
221
+ const shiftKey = held.current;
222
+ held.current = false;
223
+ return { shiftKey };
224
+ }
225
+ }), []);
226
+ }
210
227
  // src/components/async-button.tsx
211
228
  /** One button through idle, pending, done and error, with the label changing in place and its width fixed by the longest label. The outcome is announced politely, with the error's own words when `error` is given. */
212
229
  function AsyncButton({ phase, labels, error, disabled, ...props }) {
@@ -610,8 +627,8 @@ function LinkCardList({ label, children, className }) {
610
627
  });
611
628
  }
612
629
  // src/components/data-view.tsx
613
- /** Every view of remote data: renders `skeleton` while loading, an alert with Try again on error, `empty` when `isEmpty`, else `children(data)`. The skeleton and empty state are required. */
614
- function DataView({ state, isEmpty, skeleton, empty, children, className }) {
630
+ /** Every view of remote data: renders `skeleton` while loading, an alert with Try again on error, `empty` when `isEmpty`, else `children(data)`, under a `StaleNotice` when a refresh failed. The skeleton and empty state are required. */
631
+ function DataView({ state, isEmpty, skeleton, empty, children, staleNotice = (stale) => /* @__PURE__ */ jsx(StaleNotice, { stale }), className }) {
615
632
  return /* @__PURE__ */ jsxs("div", {
616
633
  className: cn("min-w-0", className),
617
634
  "aria-busy": state.status === "loading" || void 0,
@@ -636,7 +653,38 @@ function DataView({ state, isEmpty, skeleton, empty, children, className }) {
636
653
  })
637
654
  ]
638
655
  }),
639
- state.status === "ready" && (isEmpty(state.data) ? empty : children(state.data))
656
+ state.status === "ready" && /* @__PURE__ */ jsxs(Fragment, { children: [state.stale && staleNotice(state.stale), isEmpty(state.data) ? empty : children(state.data)] })
657
+ ]
658
+ });
659
+ }
660
+ /** When `readAt` was, for a person: the clock time today, else the weekday and time. */
661
+ function readTime(readAt, now = /* @__PURE__ */ new Date()) {
662
+ return readAt.toDateString() === now.toDateString() ? format.clock.format(readAt) : format.shortDay.format(readAt);
663
+ }
664
+ /** The notice over data whose refresh failed: what happened, how old the data shown is, and Try again. A polite status, not an alert: nothing was lost. */
665
+ function StaleNotice({ stale, shown, className }) {
666
+ return /* @__PURE__ */ jsxs("div", {
667
+ role: "status",
668
+ className: cn("mb-4 flex flex-wrap items-start gap-3 rounded-surface bg-warning-subtle p-3 text-warning", className),
669
+ children: [
670
+ /* @__PURE__ */ jsx(History, {
671
+ "aria-hidden": true,
672
+ className: "mt-0.5 size-4 shrink-0"
673
+ }),
674
+ /* @__PURE__ */ jsxs("p", {
675
+ className: "min-w-0 flex-1 type-body-sm",
676
+ children: [
677
+ stale.message,
678
+ " ",
679
+ shown ? shown(readTime(stale.readAt)) : `Showing data from ${readTime(stale.readAt)}.`
680
+ ]
681
+ }),
682
+ /* @__PURE__ */ jsxs(Button, {
683
+ size: "sm",
684
+ onClick: stale.retry,
685
+ disabled: stale.retrying,
686
+ children: [/* @__PURE__ */ jsx(RotateCcw, { "aria-hidden": true }), stale.retrying ? "Trying again…" : "Try again"]
687
+ })
640
688
  ]
641
689
  });
642
690
  }
@@ -767,6 +815,231 @@ function ThumbnailSkeleton({ aspect = 1, className }) {
767
815
  children: /* @__PURE__ */ jsx(Skeleton, { className: "absolute inset-0" })
768
816
  });
769
817
  }
818
+ // src/components/disclosure.tsx
819
+ /** Shows more of one thing in place: a button with `aria-expanded` around `summary`, and a body mounted only while open (so a request inside it runs on first open). Controlled with `open`/`onOpenChange` or `useExpanded`, or uncontrolled with `defaultOpen`. */
820
+ function Disclosure({ summary, children, actions, open, defaultOpen = false, onOpenChange, className }) {
821
+ const [own, setOwn] = React.useState(defaultOpen);
822
+ const isOpen = open ?? own;
823
+ const bodyId = React.useId();
824
+ const toggle = () => {
825
+ if (open === void 0) setOwn(!isOpen);
826
+ onOpenChange?.(!isOpen);
827
+ };
828
+ return /* @__PURE__ */ jsxs("div", {
829
+ className: cn("min-w-0", className),
830
+ children: [/* @__PURE__ */ jsxs("div", {
831
+ className: "flex items-center gap-3",
832
+ children: [/* @__PURE__ */ jsxs("button", {
833
+ type: "button",
834
+ "aria-expanded": isOpen,
835
+ "aria-controls": isOpen ? bodyId : void 0,
836
+ onClick: toggle,
837
+ className: "-mx-1 flex min-h-(--control-h-sm) min-w-0 flex-1 cursor-pointer items-center gap-2 rounded-control px-1 text-left hover:bg-muted",
838
+ children: [/* @__PURE__ */ jsx(Chevron, { open: isOpen }), /* @__PURE__ */ jsx("span", {
839
+ className: "min-w-0 flex-1",
840
+ children: summary
841
+ })]
842
+ }), actions && /* @__PURE__ */ jsx("div", {
843
+ className: "flex shrink-0 items-center gap-2",
844
+ children: actions
845
+ })]
846
+ }), isOpen && /* @__PURE__ */ jsx("div", {
847
+ id: bodyId,
848
+ className: "pt-2 pb-3 pl-6",
849
+ children
850
+ })]
851
+ });
852
+ }
853
+ /** The disclosure chevron: points right when closed, down when open. Shared with `DataTable`'s row toggle. */
854
+ function Chevron({ open, className }) {
855
+ return /* @__PURE__ */ jsx(ChevronRight, {
856
+ "aria-hidden": true,
857
+ className: cn("size-4 shrink-0 text-muted-foreground", open && "rotate-90", className)
858
+ });
859
+ }
860
+ /** Shared open state for the disclosures of one list or table, optionally kept in the URL (`param`). */
861
+ function useExpanded(options = {}) {
862
+ return options.param ? useUrlExpanded(options.param, options) : useLocalExpanded(options);
863
+ }
864
+ function api(expanded, set, single) {
865
+ return {
866
+ expanded,
867
+ onChange: (next) => {
868
+ const opened = single ? [...next].filter((k) => !expanded.has(k)) : [];
869
+ set(opened.length ? /* @__PURE__ */ new Set([opened[opened.length - 1]]) : next);
870
+ },
871
+ isOpen: (key) => expanded.has(key),
872
+ setOpen: (key, open) => {
873
+ const next = single ? /* @__PURE__ */ new Set() : new Set(expanded);
874
+ if (open) next.add(key);
875
+ else next.delete(key);
876
+ set(next);
877
+ },
878
+ collapseAll: () => set(/* @__PURE__ */ new Set())
879
+ };
880
+ }
881
+ function useLocalExpanded({ initial = [], single }) {
882
+ const [expanded, setExpanded] = React.useState(() => new Set(initial));
883
+ return React.useMemo(() => api(expanded, setExpanded, single), [expanded, single]);
884
+ }
885
+ function useUrlExpanded(param, { single }) {
886
+ const [params, setParams] = useSearchParams();
887
+ const fromUrl = params.getAll(param);
888
+ const joined = fromUrl.join("\0");
889
+ const expanded = React.useMemo(() => new Set(fromUrl), [joined]);
890
+ const set = React.useCallback((next) => setParams((p) => {
891
+ const out = new URLSearchParams(p);
892
+ out.delete(param);
893
+ for (const k of next) out.append(param, k);
894
+ return out;
895
+ }, { replace: true }), [param, setParams]);
896
+ return React.useMemo(() => api(expanded, set, single), [
897
+ expanded,
898
+ set,
899
+ single
900
+ ]);
901
+ }
902
+ // src/components/selection.tsx
903
+ function reconcile(prev, rows, keys, initial) {
904
+ const present = new Set(keys);
905
+ const raw = new Set([...prev.raw].filter((k) => present.has(k)));
906
+ const seen = new Set(prev.seen);
907
+ rows.forEach((row, i) => {
908
+ const key = keys[i];
909
+ if (seen.has(key)) return;
910
+ seen.add(key);
911
+ if (initial(row, key)) raw.add(key);
912
+ });
913
+ return {
914
+ keys,
915
+ seen,
916
+ raw
917
+ };
918
+ }
919
+ var sameKeys = (a, b) => a.length === b.length && a.every((k, i) => k === b[i]);
920
+ /**
921
+ * Chosen row keys, pruned to the rows that exist, and the calls that change them. Spread into
922
+ * `DataTable`'s or `SelectableGrid`'s `selection` with a `rowLabel`. `initial` (keys, or a test on
923
+ * the row) chooses each row the first time it appears, so a selection seeded from data that loads
924
+ * later, or a next page, still starts right.
925
+ */
926
+ function useSelection(rows, rowKey, initial = []) {
927
+ const initialRef = React.useRef(initial);
928
+ initialRef.current = initial;
929
+ const initialKeys = React.useRef(void 0);
930
+ const test = (row, key) => {
931
+ const init = initialRef.current;
932
+ if (typeof init === "function") return init(row);
933
+ if (initialKeys.current?.source !== init) initialKeys.current = {
934
+ source: init,
935
+ keys: new Set(init)
936
+ };
937
+ return initialKeys.current.keys.has(key);
938
+ };
939
+ const keys = rows.map(rowKey);
940
+ const [state, setState] = React.useState(() => reconcile({
941
+ keys: [],
942
+ seen: /* @__PURE__ */ new Set(),
943
+ raw: /* @__PURE__ */ new Set()
944
+ }, rows, keys, test));
945
+ let current = state;
946
+ if (!sameKeys(state.keys, keys)) {
947
+ current = reconcile(state, rows, keys, test);
948
+ setState(current);
949
+ }
950
+ const selected = current.raw;
951
+ const selectedRows = React.useMemo(() => rows.filter((r) => selected.has(rowKey(r))), [rows, selected]);
952
+ const onChange = React.useCallback((next) => setState((s) => {
953
+ const present = new Set(s.keys);
954
+ return {
955
+ ...s,
956
+ raw: new Set([...next].filter((k) => present.has(k)))
957
+ };
958
+ }), []);
959
+ const clear = React.useCallback(() => setState((s) => ({
960
+ ...s,
961
+ raw: /* @__PURE__ */ new Set()
962
+ })), []);
963
+ return {
964
+ selected,
965
+ selectedRows,
966
+ count: selected.size,
967
+ onChange,
968
+ clear
969
+ };
970
+ }
971
+ /**
972
+ * Selecting rows of a surface in the order it shows them: one row, a range with Shift, and all
973
+ * shown. Shared by `DataTable` and `SelectableGrid`, so a range and select-all behave the same on
974
+ * both. `rows` are in display order (after sorting) and include rows a virtualised surface has not
975
+ * rendered.
976
+ *
977
+ * A range runs from the last row clicked (the anchor) to this one, over the selectable rows
978
+ * between, and sets them all to this row's new state, as Gmail and Finder do: Shift-clicking an
979
+ * unchosen row chooses the run, Shift-clicking a chosen one clears it. Every click moves the
980
+ * anchor. An anchor that has left the rows (filtered, removed) makes the next Shift-click a plain
981
+ * one.
982
+ * @internal
983
+ */
984
+ function useSelectRows(rows, rowKey, selection) {
985
+ const anchor = React.useRef(void 0);
986
+ const keys = selection ? rows.filter((r) => selection.isSelectable?.(r) ?? true).map(rowKey) : [];
987
+ const chosen = selection ? keys.filter((k) => selection.selected.has(k)).length : 0;
988
+ const allChecked = chosen === 0 ? false : chosen === keys.length ? true : "mixed";
989
+ const toggle = (key, on, { shiftKey }) => {
990
+ if (!selection) return;
991
+ const next = new Set(selection.selected);
992
+ const from = shiftKey && anchor.current !== void 0 ? keys.indexOf(anchor.current) : -1;
993
+ const to = keys.indexOf(key);
994
+ if (from >= 0 && to >= 0) {
995
+ const [a, b] = from < to ? [from, to] : [to, from];
996
+ for (const k of keys.slice(a, b + 1)) if (on) next.add(k);
997
+ else next.delete(k);
998
+ } else if (on) next.add(key);
999
+ else next.delete(key);
1000
+ anchor.current = key;
1001
+ selection.onChange(next);
1002
+ };
1003
+ const toggleAll = () => {
1004
+ if (!selection) return;
1005
+ const next = new Set(selection.selected);
1006
+ if (allChecked === false) keys.forEach((k) => next.add(k));
1007
+ else keys.forEach((k) => next.delete(k));
1008
+ selection.onChange(next);
1009
+ };
1010
+ return {
1011
+ toggle,
1012
+ all: {
1013
+ checked: allChecked,
1014
+ toggle: toggleAll,
1015
+ disabled: keys.length === 0
1016
+ }
1017
+ };
1018
+ }
1019
+ /** The bar for a selection: a polite live count ("2 bookings selected"), the bulk actions, and Clear selection. Renders only the live region while nothing is chosen. */
1020
+ function BulkBar({ count, noun, onClear, children, format: format$1 = format, className }) {
1021
+ const [one, many] = typeof noun === "string" ? [noun, `${noun}s`] : noun;
1022
+ const label = `${format$1.count(count, one, many)} selected`;
1023
+ return /* @__PURE__ */ jsxs("div", {
1024
+ className: cn(count > 0 && "flex flex-wrap items-center gap-x-4 gap-y-2 rounded-surface bg-surface-sunken py-1.5 pr-1.5 pl-3", className),
1025
+ children: [/* @__PURE__ */ jsx("p", {
1026
+ "aria-live": "polite",
1027
+ className: count > 0 ? "type-label tabular-nums" : "sr-only",
1028
+ children: count > 0 ? label : `No ${many} selected`
1029
+ }), count > 0 && /* @__PURE__ */ jsxs("div", {
1030
+ role: "group",
1031
+ "aria-label": `Actions for the selected ${many}`,
1032
+ className: "flex flex-1 flex-wrap items-center gap-2",
1033
+ children: [children, /* @__PURE__ */ jsxs(Button, {
1034
+ size: "sm",
1035
+ variant: "ghost",
1036
+ className: "ml-auto",
1037
+ onClick: onClear,
1038
+ children: [/* @__PURE__ */ jsx(X, { "aria-hidden": true }), "Clear selection"]
1039
+ })]
1040
+ })]
1041
+ });
1042
+ }
770
1043
  // src/components/data-table.tsx
771
1044
  function useLocalSort(initial) {
772
1045
  const [sort, setSort] = React.useState(initial);
@@ -813,31 +1086,8 @@ function UrlSortedTable(props) {
813
1086
  sortState: useUrlSort(props.sortParam, props.initialSort)
814
1087
  });
815
1088
  }
816
- function useSelectAll(rows, rowKey, selection) {
817
- return React.useMemo(() => {
818
- if (!selection) return void 0;
819
- const keys = rows.filter((r) => selection.isSelectable?.(r) ?? true).map(rowKey);
820
- const chosen = keys.filter((k) => selection.selected.has(k)).length;
821
- const checked = chosen === 0 ? false : chosen === keys.length ? true : "mixed";
822
- const toggle = () => {
823
- const next = new Set(selection.selected);
824
- if (checked === false) keys.forEach((k) => next.add(k));
825
- else keys.forEach((k) => next.delete(k));
826
- selection.onChange(next);
827
- };
828
- return {
829
- checked,
830
- toggle,
831
- disabled: keys.length === 0
832
- };
833
- }, [
834
- rows,
835
- rowKey,
836
- selection
837
- ]);
838
- }
839
- function Table({ caption, columns, rows, rowKey, sortState: [sort, setSort], virtualizeFrom = 100, maxHeight = 480, selection, className }) {
840
- const selectAll = useSelectAll(rows, rowKey, selection);
1089
+ function Table({ caption, columns, rows, rowKey, sortState: [sort, setSort], virtualizeFrom = 100, maxHeight = 480, selection, expansion, className }) {
1090
+ const baseId = React.useId();
841
1091
  const defs = React.useMemo(() => columns.map((c) => ({
842
1092
  id: c.key,
843
1093
  accessorFn: (row) => row,
@@ -870,12 +1120,29 @@ function Table({ caption, columns, rows, rowKey, sortState: [sort, setSort], vir
870
1120
  dir: sort?.key === key && sort.dir === "ascending" ? "descending" : "ascending"
871
1121
  });
872
1122
  const modelRows = table.getRowModel().rows;
1123
+ const canExpand = (row) => expansion?.isExpandable?.(row) ?? true;
1124
+ const lines = React.useMemo(() => expansion ? modelRows.flatMap((row) => expansion.expanded.has(row.id) && canExpand(row.original) ? [{
1125
+ row,
1126
+ detail: false
1127
+ }, {
1128
+ row,
1129
+ detail: true
1130
+ }] : [{
1131
+ row,
1132
+ detail: false
1133
+ }]) : modelRows.map((row) => ({
1134
+ row,
1135
+ detail: false
1136
+ })), [modelRows, expansion]);
1137
+ const select = useSelectRows(React.useMemo(() => modelRows.map((r) => r.original), [modelRows]), rowKey, selection);
1138
+ const selectAll = selection ? select.all : void 0;
873
1139
  const virtual = modelRows.length >= virtualizeFrom;
874
1140
  const scroller = React.useRef(null);
875
1141
  const virtualizer = useVirtualizer({
876
- count: virtual ? modelRows.length : 0,
1142
+ count: virtual ? lines.length : 0,
877
1143
  getScrollElement: () => scroller.current,
878
- estimateSize: () => 41,
1144
+ estimateSize: (i) => lines[i]?.detail ? 160 : 41,
1145
+ getItemKey: (i) => lines[i] ? `${lines[i].row.id}${lines[i].detail ? "\0detail" : ""}` : i,
879
1146
  overscan: 8,
880
1147
  initialRect: {
881
1148
  width: 0,
@@ -885,7 +1152,14 @@ function Table({ caption, columns, rows, rowKey, sortState: [sort, setSort], vir
885
1152
  const items = virtual ? virtualizer.getVirtualItems() : [];
886
1153
  const padTop = items.length ? items[0].start : 0;
887
1154
  const padBottom = items.length ? virtualizer.getTotalSize() - items[items.length - 1].end : 0;
888
- const visible = virtual ? items.map((i) => modelRows[i.index]) : modelRows;
1155
+ const visible = virtual ? items.map((i) => ({
1156
+ ...lines[i.index],
1157
+ index: i.index
1158
+ })) : lines.map((l, index) => ({
1159
+ ...l,
1160
+ index
1161
+ }));
1162
+ const lead = (selection ? 1 : 0) + (expansion ? 1 : 0);
889
1163
  return /* @__PURE__ */ jsx("div", {
890
1164
  ref: scroller,
891
1165
  role: "region",
@@ -895,7 +1169,7 @@ function Table({ caption, columns, rows, rowKey, sortState: [sort, setSort], vir
895
1169
  style: virtual ? { maxHeight } : void 0,
896
1170
  children: /* @__PURE__ */ jsxs("table", {
897
1171
  className: "w-full border-collapse type-body-sm",
898
- "aria-rowcount": virtual ? modelRows.length + 1 : void 0,
1172
+ "aria-rowcount": virtual ? lines.length + 1 : void 0,
899
1173
  children: [
900
1174
  /* @__PURE__ */ jsx("caption", {
901
1175
  className: "sr-only",
@@ -903,74 +1177,115 @@ function Table({ caption, columns, rows, rowKey, sortState: [sort, setSort], vir
903
1177
  }),
904
1178
  /* @__PURE__ */ jsx("thead", {
905
1179
  className: cn(virtual && "sticky top-0 z-10 bg-background"),
906
- children: table.getHeaderGroups().map((group) => /* @__PURE__ */ jsxs("tr", { children: [selectAll && /* @__PURE__ */ jsx("th", {
907
- scope: "col",
908
- className: "w-8 py-2 pr-2 text-left align-bottom",
909
- children: /* @__PURE__ */ jsx(Checkbox, {
910
- className: "-my-1",
911
- label: "Select all shown",
912
- hideLabel: true,
913
- checked: selectAll.checked,
914
- disabled: selectAll.disabled,
915
- onCheckedChange: selectAll.toggle
916
- })
917
- }), group.headers.map((header) => {
918
- const meta = header.column.columnDef.meta;
919
- const active = sort?.key === header.id && header.column.getCanSort() ? sort.dir : void 0;
920
- const Icon = active === "ascending" ? ArrowUp : active === "descending" ? ArrowDown : ArrowUpDown;
921
- const rendered = flexRender(header.column.columnDef.header, header.getContext());
922
- const label = meta?.hideHeader ? /* @__PURE__ */ jsx("span", {
923
- className: "sr-only",
924
- children: rendered
925
- }) : rendered;
926
- return /* @__PURE__ */ jsx("th", {
1180
+ children: table.getHeaderGroups().map((group) => /* @__PURE__ */ jsxs("tr", { children: [
1181
+ selectAll && /* @__PURE__ */ jsx("th", {
927
1182
  scope: "col",
928
- "aria-sort": active,
929
- className: cn("py-2 pr-4 text-left align-bottom type-caption text-muted-foreground last:pr-0", meta?.numeric && "text-right"),
930
- children: header.column.getCanSort() ? /* @__PURE__ */ jsxs("button", {
931
- type: "button",
932
- onClick: () => toggle(header.id),
933
- className: cn("-mx-1 inline-flex cursor-pointer items-center gap-1 rounded-control px-1 transition-state hover:text-foreground", meta?.numeric && "flex-row-reverse", active && "text-foreground"),
934
- children: [label, /* @__PURE__ */ jsx(Icon, {
935
- "aria-hidden": true,
936
- className: cn("size-3.5", !active && "opacity-50")
937
- })]
938
- }) : label
939
- }, header.id);
940
- })] }, group.id))
1183
+ className: "w-8 py-2 pr-2 text-left align-bottom",
1184
+ children: /* @__PURE__ */ jsx(Checkbox, {
1185
+ className: "-my-1",
1186
+ label: "Select all shown",
1187
+ hideLabel: true,
1188
+ checked: selectAll.checked,
1189
+ disabled: selectAll.disabled,
1190
+ onCheckedChange: selectAll.toggle
1191
+ })
1192
+ }),
1193
+ expansion && /* @__PURE__ */ jsx("th", {
1194
+ scope: "col",
1195
+ className: "w-10 py-2 pr-2",
1196
+ children: /* @__PURE__ */ jsx("span", {
1197
+ className: "sr-only",
1198
+ children: "Details"
1199
+ })
1200
+ }),
1201
+ group.headers.map((header) => {
1202
+ const meta = header.column.columnDef.meta;
1203
+ const active = sort?.key === header.id && header.column.getCanSort() ? sort.dir : void 0;
1204
+ const Icon = active === "ascending" ? ArrowUp : active === "descending" ? ArrowDown : ArrowUpDown;
1205
+ const rendered = flexRender(header.column.columnDef.header, header.getContext());
1206
+ const label = meta?.hideHeader ? /* @__PURE__ */ jsx("span", {
1207
+ className: "sr-only",
1208
+ children: rendered
1209
+ }) : rendered;
1210
+ return /* @__PURE__ */ jsx("th", {
1211
+ scope: "col",
1212
+ "aria-sort": active,
1213
+ className: cn("py-2 pr-4 text-left align-bottom type-caption text-muted-foreground last:pr-0", meta?.numeric && "text-right"),
1214
+ children: header.column.getCanSort() ? /* @__PURE__ */ jsxs("button", {
1215
+ type: "button",
1216
+ onClick: () => toggle(header.id),
1217
+ className: cn("-mx-1 inline-flex cursor-pointer items-center gap-1 rounded-control px-1 transition-state hover:text-foreground", meta?.numeric && "flex-row-reverse", active && "text-foreground"),
1218
+ children: [label, /* @__PURE__ */ jsx(Icon, {
1219
+ "aria-hidden": true,
1220
+ className: cn("size-3.5", !active && "opacity-50")
1221
+ })]
1222
+ }) : label
1223
+ }, header.id);
1224
+ })
1225
+ ] }, group.id))
941
1226
  }),
942
1227
  /* @__PURE__ */ jsxs("tbody", { children: [
943
1228
  padTop > 0 && /* @__PURE__ */ jsx("tr", {
944
1229
  "aria-hidden": true,
945
1230
  style: { height: padTop }
946
1231
  }),
947
- visible.map((row, i) => {
1232
+ visible.map(({ row, detail, index }) => {
948
1233
  const chosen = selection?.selected.has(row.id) ?? false;
1234
+ const detailId = `${baseId}-detail-${row.id}`;
1235
+ if (detail && expansion) return /* @__PURE__ */ jsxs("tr", {
1236
+ ref: virtual ? virtualizer.measureElement : void 0,
1237
+ "data-index": index,
1238
+ className: cn(chosen && "bg-surface-sunken"),
1239
+ "aria-rowindex": virtual ? index + 2 : void 0,
1240
+ children: [lead > 0 && /* @__PURE__ */ jsx("td", { colSpan: lead }), /* @__PURE__ */ jsx("td", {
1241
+ id: detailId,
1242
+ colSpan: columns.length,
1243
+ className: "pt-0.5 pb-4 align-top",
1244
+ children: expansion.detail(row.original)
1245
+ })]
1246
+ }, `${row.id}-detail`);
1247
+ const open = expansion?.expanded.has(row.id) ?? false;
949
1248
  return /* @__PURE__ */ jsxs("tr", {
950
1249
  className: cn("border-t border-border", chosen && "bg-surface-sunken"),
951
- "aria-rowindex": virtual ? (items[i]?.index ?? 0) + 2 : void 0,
952
- children: [selection && /* @__PURE__ */ jsx("td", {
953
- className: "w-8 py-2.5 pr-2 align-middle",
954
- children: /* @__PURE__ */ jsx(Checkbox, {
955
- className: "-my-0.5",
956
- label: `Select ${selection.rowLabel(row.original)}`,
957
- hideLabel: true,
958
- checked: chosen,
959
- disabled: !(selection.isSelectable?.(row.original) ?? true),
960
- onCheckedChange: (on) => {
961
- const next = new Set(selection.selected);
962
- if (on) next.add(row.id);
963
- else next.delete(row.id);
964
- selection.onChange(next);
965
- }
1250
+ "aria-rowindex": virtual ? index + 2 : void 0,
1251
+ children: [
1252
+ selection && /* @__PURE__ */ jsx("td", {
1253
+ className: "w-8 py-2.5 pr-2 align-middle",
1254
+ children: /* @__PURE__ */ jsx(Checkbox, {
1255
+ className: "-my-0.5",
1256
+ label: `Select ${selection.rowLabel(row.original)}`,
1257
+ hideLabel: true,
1258
+ checked: chosen,
1259
+ disabled: !(selection.isSelectable?.(row.original) ?? true),
1260
+ onCheckedChange: (on, gesture) => select.toggle(row.id, on, gesture)
1261
+ })
1262
+ }),
1263
+ expansion && /* @__PURE__ */ jsx("td", {
1264
+ className: "w-10 py-2.5 pr-2 align-middle",
1265
+ children: canExpand(row.original) && /* @__PURE__ */ jsx("button", {
1266
+ type: "button",
1267
+ "aria-expanded": open,
1268
+ "aria-controls": open ? detailId : void 0,
1269
+ "aria-label": expansion.rowLabel(row.original),
1270
+ title: expansion.rowLabel(row.original),
1271
+ onClick: () => {
1272
+ const next = new Set(expansion.expanded);
1273
+ if (open) next.delete(row.id);
1274
+ else next.add(row.id);
1275
+ expansion.onChange(next);
1276
+ },
1277
+ className: "-my-1.5 grid size-(--control-h-sm) cursor-pointer place-items-center rounded-control hover:bg-muted",
1278
+ children: /* @__PURE__ */ jsx(Chevron, { open })
1279
+ })
1280
+ }),
1281
+ row.getVisibleCells().map((cell) => {
1282
+ const meta = cell.column.columnDef.meta;
1283
+ return /* @__PURE__ */ jsx("td", {
1284
+ className: cn("py-2.5 pr-4 align-middle last:pr-0", meta?.numeric && "text-right tabular-nums", meta?.truncate && "max-w-48 truncate", meta?.className),
1285
+ children: flexRender(cell.column.columnDef.cell, cell.getContext())
1286
+ }, cell.id);
966
1287
  })
967
- }), row.getVisibleCells().map((cell) => {
968
- const meta = cell.column.columnDef.meta;
969
- return /* @__PURE__ */ jsx("td", {
970
- className: cn("py-2.5 pr-4 align-middle last:pr-0", meta?.numeric && "text-right tabular-nums", meta?.truncate && "max-w-48 truncate", meta?.className),
971
- children: flexRender(cell.column.columnDef.cell, cell.getContext())
972
- }, cell.id);
973
- })]
1288
+ ]
974
1289
  }, row.id);
975
1290
  }),
976
1291
  padBottom > 0 && /* @__PURE__ */ jsx("tr", {
@@ -996,52 +1311,155 @@ function TableSkeleton({ rows = 4, columns = 5 }) {
996
1311
  }, r))]
997
1312
  });
998
1313
  }
999
- // src/components/selection.tsx
1000
- /** Chosen row keys, pruned to the rows that exist, and the calls that change them. Spread into `DataTable`'s `selection` with a `rowLabel`. */
1001
- function useSelection(rows, rowKey, initial = []) {
1002
- const [raw, setRaw] = React.useState(() => new Set(initial));
1003
- const selected = React.useMemo(() => {
1004
- const present = new Set(rows.map(rowKey));
1005
- return new Set([...raw].filter((k) => present.has(k)));
1006
- }, [raw, rows]);
1007
- const selectedRows = React.useMemo(() => rows.filter((r) => selected.has(rowKey(r))), [rows, selected]);
1008
- React.useEffect(() => {
1009
- if (selected.size !== raw.size) setRaw(selected);
1010
- }, [selected, raw]);
1011
- const onChange = React.useCallback((next) => setRaw(next), []);
1012
- const clear = React.useCallback(() => setRaw(/* @__PURE__ */ new Set()), []);
1013
- return {
1014
- selected,
1015
- selectedRows,
1016
- count: selected.size,
1017
- onChange,
1018
- clear
1314
+ // src/components/selectable-grid.tsx
1315
+ var minWidth = {
1316
+ sm: 96,
1317
+ md: 144,
1318
+ lg: 192
1319
+ };
1320
+ var aspectClass = {
1321
+ square: "aspect-square",
1322
+ portrait: "aspect-3/4",
1323
+ landscape: "aspect-4/3"
1324
+ };
1325
+ var aspectRatio = {
1326
+ square: 1,
1327
+ portrait: 4 / 3,
1328
+ landscape: 3 / 4
1329
+ };
1330
+ var GAP = 12;
1331
+ var TILE_CHROME = 40;
1332
+ /** A grid of pictures to choose from: each tile one labelled target, "Select all shown", Shift-click ranges, virtualised from `virtualizeFrom` tiles. Pair with `useSelection` and a `BulkBar`. */
1333
+ function SelectableGrid({ label, rows, rowKey, renderImage, detail, selection, aspect = "square", tileSize = "md", virtualizeFrom = 100, maxHeight = 480, className }) {
1334
+ const select = useSelectRows(rows, rowKey, selection);
1335
+ const virtual = rows.length >= virtualizeFrom;
1336
+ const tile = (row, index) => {
1337
+ const key = rowKey(row);
1338
+ return /* @__PURE__ */ jsx(Tile, {
1339
+ name: `Select ${selection.rowLabel(row)}`,
1340
+ chosen: selection.selected.has(key),
1341
+ disabled: !(selection.isSelectable?.(row) ?? true),
1342
+ onChange: (on, gesture) => select.toggle(key, on, gesture),
1343
+ aspect,
1344
+ image: renderImage(row),
1345
+ detail: detail?.(row),
1346
+ index
1347
+ });
1019
1348
  };
1020
- }
1021
- /** The bar for a selection: a polite live count ("2 bookings selected"), the bulk actions, and Clear selection. Renders only the live region while nothing is chosen. */
1022
- function BulkBar({ count, noun, onClear, children, format: format$1 = format, className }) {
1023
- const [one, many] = typeof noun === "string" ? [noun, `${noun}s`] : noun;
1024
- const label = `${format$1.count(count, one, many)} selected`;
1025
1349
  return /* @__PURE__ */ jsxs("div", {
1026
- className: cn(count > 0 && "flex flex-wrap items-center gap-x-4 gap-y-2 rounded-surface bg-surface-sunken py-1.5 pr-1.5 pl-3", className),
1027
- children: [/* @__PURE__ */ jsx("p", {
1028
- "aria-live": "polite",
1029
- className: count > 0 ? "type-label tabular-nums" : "sr-only",
1030
- children: count > 0 ? label : `No ${many} selected`
1031
- }), count > 0 && /* @__PURE__ */ jsxs("div", {
1032
- role: "group",
1033
- "aria-label": `Actions for the selected ${many}`,
1034
- className: "flex flex-1 flex-wrap items-center gap-2",
1035
- children: [children, /* @__PURE__ */ jsxs(Button, {
1036
- size: "sm",
1037
- variant: "ghost",
1038
- className: "ml-auto",
1039
- onClick: onClear,
1040
- children: [/* @__PURE__ */ jsx(X, { "aria-hidden": true }), "Clear selection"]
1041
- })]
1350
+ className: cn("grid min-w-0 gap-3", className),
1351
+ children: [/* @__PURE__ */ jsx(Checkbox, {
1352
+ className: "justify-self-start",
1353
+ label: "Select all shown",
1354
+ checked: select.all.checked,
1355
+ disabled: select.all.disabled,
1356
+ onCheckedChange: select.all.toggle
1357
+ }), virtual ? /* @__PURE__ */ jsx(VirtualTiles, {
1358
+ label,
1359
+ rows,
1360
+ rowKey,
1361
+ tile,
1362
+ aspect,
1363
+ tileSize,
1364
+ maxHeight
1365
+ }) : /* @__PURE__ */ jsx("ul", {
1366
+ "aria-label": label,
1367
+ className: "grid gap-3",
1368
+ style: { gridTemplateColumns: `repeat(auto-fill, minmax(min(${minWidth[tileSize]}px, 100%), 1fr))` },
1369
+ children: rows.map((row, i) => /* @__PURE__ */ jsx("li", { children: tile(row, i) }, rowKey(row)))
1042
1370
  })]
1043
1371
  });
1044
1372
  }
1373
+ function VirtualTiles({ label, rows, rowKey, tile, aspect, tileSize, maxHeight }) {
1374
+ const scroller = React.useRef(null);
1375
+ const [width, setWidth] = React.useState(0);
1376
+ React.useLayoutEffect(() => {
1377
+ const el = scroller.current;
1378
+ if (!el || typeof ResizeObserver === "undefined") return;
1379
+ const observer = new ResizeObserver(([entry]) => setWidth(entry.contentRect.width));
1380
+ observer.observe(el);
1381
+ return () => observer.disconnect();
1382
+ }, []);
1383
+ const min = minWidth[tileSize];
1384
+ const columns = Math.max(1, Math.floor((width + GAP) / (min + GAP)));
1385
+ const tileWidth = width > 0 ? (width - GAP * (columns - 1)) / columns : min;
1386
+ const lines = Math.ceil(rows.length / columns);
1387
+ const virtualizer = useVirtualizer({
1388
+ count: lines,
1389
+ getScrollElement: () => scroller.current,
1390
+ estimateSize: () => Math.round(tileWidth * aspectRatio[aspect]) + TILE_CHROME + GAP,
1391
+ overscan: 3,
1392
+ getItemKey: (i) => `${columns}:${i}`,
1393
+ initialRect: {
1394
+ width: 0,
1395
+ height: maxHeight
1396
+ }
1397
+ });
1398
+ const items = virtualizer.getVirtualItems();
1399
+ return /* @__PURE__ */ jsx("div", {
1400
+ ref: scroller,
1401
+ role: "region",
1402
+ "aria-label": label,
1403
+ tabIndex: 0,
1404
+ "data-strata-scroll": "",
1405
+ className: "min-w-0 overflow-y-auto",
1406
+ style: { height: maxHeight },
1407
+ children: /* @__PURE__ */ jsx("div", {
1408
+ role: "list",
1409
+ "aria-label": label,
1410
+ className: "relative w-full",
1411
+ style: { height: virtualizer.getTotalSize() },
1412
+ children: items.map((v) => {
1413
+ const start = v.index * columns;
1414
+ return /* @__PURE__ */ jsx("div", {
1415
+ ref: virtualizer.measureElement,
1416
+ "data-index": v.index,
1417
+ role: "none",
1418
+ className: "absolute inset-x-0 top-0 grid gap-3 pb-3",
1419
+ style: {
1420
+ translate: `0 ${v.start}px`,
1421
+ gridTemplateColumns: `repeat(${columns}, minmax(0, 1fr))`
1422
+ },
1423
+ children: rows.slice(start, start + columns).map((row, i) => /* @__PURE__ */ jsx("div", {
1424
+ role: "listitem",
1425
+ "aria-setsize": rows.length,
1426
+ "aria-posinset": start + i + 1,
1427
+ children: tile(row, start + i)
1428
+ }, rowKey(row)))
1429
+ }, v.key);
1430
+ })
1431
+ })
1432
+ });
1433
+ }
1434
+ function Tile({ name, chosen, disabled, onChange, aspect, image, detail, index }) {
1435
+ const shift = useShiftNote();
1436
+ return /* @__PURE__ */ jsxs("label", {
1437
+ ...shift.handlers,
1438
+ "data-index": index,
1439
+ className: cn("relative grid min-w-0 gap-1.5 rounded-surface border border-transparent p-1.5 select-none transition-state", chosen && "border-primary bg-surface-sunken", disabled ? "cursor-not-allowed opacity-50" : "cursor-pointer hover:bg-surface-sunken"),
1440
+ children: [
1441
+ /* @__PURE__ */ jsx("span", {
1442
+ className: cn("block overflow-hidden rounded-control bg-muted *:size-full *:object-cover", aspectClass[aspect]),
1443
+ children: image
1444
+ }),
1445
+ /* @__PURE__ */ jsx("span", {
1446
+ className: "absolute top-3 left-3 grid size-7 place-items-center rounded-control bg-background",
1447
+ children: /* @__PURE__ */ jsx("input", {
1448
+ type: "checkbox",
1449
+ "aria-label": name,
1450
+ checked: chosen,
1451
+ disabled,
1452
+ onChange: (e) => onChange(e.target.checked, shift.take()),
1453
+ className: cn("size-4.5 accent-primary", disabled ? "cursor-not-allowed" : "cursor-pointer")
1454
+ })
1455
+ }),
1456
+ detail != null && /* @__PURE__ */ jsx("span", {
1457
+ className: "truncate type-body-sm text-muted-foreground",
1458
+ children: detail
1459
+ })
1460
+ ]
1461
+ });
1462
+ }
1045
1463
  // src/components/virtual-list.tsx
1046
1464
  /** A list that renders only its visible rows (TanStack Virtual) from `virtualizeFrom` items, keeping list semantics for screen readers. */
1047
1465
  function VirtualList({ label, items, itemKey, children, estimateSize = 48, height = 480, virtualizeFrom = 100, bounded = false, footer, className }) {
@@ -2148,11 +2566,15 @@ function useInfiniteLoad({ queryKey, queryFn, items, itemKey, hasMore, cursorAft
2148
2566
  dismiss,
2149
2567
  restore
2150
2568
  };
2151
- if (list) return {
2152
- status: "ready",
2153
- data: list,
2154
- ...extra
2155
- };
2569
+ if (list) {
2570
+ const stale = query.isRefetchError ? staleOf(query, describeError) : void 0;
2571
+ return {
2572
+ status: "ready",
2573
+ data: list,
2574
+ ...stale && { stale },
2575
+ ...extra
2576
+ };
2577
+ }
2156
2578
  if (query.status === "error") return {
2157
2579
  status: "error",
2158
2580
  message: describeError(query.error),
@@ -2193,6 +2615,6 @@ function nextAfter(pages, items, itemKey, hidden) {
2193
2615
  if (held.length === 0 || pages.length > 1 && !lastPageAddedAny) return last;
2194
2616
  return held[held.length - 1];
2195
2617
  }
2196
- export { ActiveList, AppShell, AsyncButton, Breadcrumb, BulkBar, Button, COLOR_SCHEME_STORAGE_KEY, Checkbox, ChoiceGroup, ColorSchemeChoice, CompositionBar, DataTable, DataView, Done, EmptyState, Field, HttpError, IconButton, InfiniteList, Input, KeysLegend, LineChart, LinkCard, LinkCardList, Meter, Metric, MetricStrip, NBSP, NativeSelect, PageHeader, RowAction, SideNav, Skeleton, Status, StrataQueryProvider, TableSkeleton, Tabs, Textarea, ThemeProvider, Thumbnail, ThumbnailSkeleton, UndoBar, VirtualList, buttonVariants, cn, colorSchemeScript, createFormat, createQueryClient, crossedThreshold, describeActionError, describeHttpError, fetchJson, format, isTypingTarget, parseSort, serialiseSort, toDate, toLoad, typeSteps, useActiveItem, useAsyncAction, useColorScheme, useHotkeys, useInfiniteLoad, useLoad, useSelection, useSystemColorScheme, useUndoable };
2618
+ export { ActiveList, AppShell, AsyncButton, Breadcrumb, BulkBar, Button, COLOR_SCHEME_STORAGE_KEY, Checkbox, ChoiceGroup, ColorSchemeChoice, CompositionBar, DataTable, DataView, Disclosure, Done, EmptyState, Field, HttpError, IconButton, InfiniteList, Input, KeysLegend, LineChart, LinkCard, LinkCardList, Meter, Metric, MetricStrip, NBSP, NativeSelect, PageHeader, RowAction, SelectableGrid, SideNav, Skeleton, StaleNotice, Status, StrataQueryProvider, TableSkeleton, Tabs, Textarea, ThemeProvider, Thumbnail, ThumbnailSkeleton, UndoBar, VirtualList, buttonVariants, cn, colorSchemeScript, createFormat, createQueryClient, crossedThreshold, describeActionError, describeHttpError, fetchJson, format, isTypingTarget, parseSort, serialiseSort, staleOf, toDate, toLoad, typeSteps, useActiveItem, useAsyncAction, useColorScheme, useExpanded, useHotkeys, useInfiniteLoad, useLoad, useSelection, useSystemColorScheme, useUndoable };
2197
2619
 
2198
2620
  //# sourceMappingURL=index.js.map