@coldsmirk/ledger-mantine 0.5.1 → 0.7.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.
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
- import { n as defaultLabels, t as DataTableLabels } from "./labels-D-6FCakH.cjs";
2
+ import { n as defaultLabels, t as DataTableLabels } from "./labels-D6GC8goU.cjs";
3
3
  import { BoxProps, ComboboxData, ElementProps, Factory, MantineBreakpoint, MantineColor, MantineRadius, MantineSize, MantineSpacing, PopoverProps, StylesApiProps, TableTdProps, TableThProps, TableTrProps, TextInputProps } from "@mantine/core";
4
4
  import { Cell as Cell$1, CellData, Column as Column$1, ColumnDef as ColumnDef$1, ColumnFiltersState, ColumnFiltersState as ColumnFiltersState$1, ColumnOrderState, ColumnOrderState as ColumnOrderState$1, ColumnPinningState, ColumnPinningState as ColumnPinningState$1, ColumnSizingState, ColumnSizingState as ColumnSizingState$1, ColumnVisibilityState, ColumnVisibilityState as ColumnVisibilityState$1, ExpandedState, ExpandedState as ExpandedState$1, FilterFn, FilterFnOption, GroupingState, GroupingState as GroupingState$1, Header as Header$1, HeaderGroup as HeaderGroup$1, PaginationState, PaginationState as PaginationState$1, ReactTable, Row as Row$1, RowData, RowData as RowData$1, RowPinningState, RowPinningState as RowPinningState$1, RowSelectionState, RowSelectionState as RowSelectionState$1, SortingState, SortingState as SortingState$1, TableFeatures, TableOptions, aggregationFn_count, aggregationFn_extent, aggregationFn_first, aggregationFn_last, aggregationFn_max, aggregationFn_mean, aggregationFn_median, aggregationFn_min, aggregationFn_sum, aggregationFn_unique, aggregationFn_uniqueCount, flexRender } from "@tanstack/react-table";
5
5
  import { CSSProperties, ComponentProps, ComponentType, JSX as JSX$1, KeyboardEvent, MouseEvent, ReactElement, ReactNode, Ref } from "react";
@@ -487,9 +487,12 @@ interface UseDataTableOptions<TData extends RowData$1> {
487
487
  onRowEditCommit?: (change: DataTableRowEditCommit<TData>) => void | Promise<void>;
488
488
  /**
489
489
  * A single keyboard-reachable current row (row click or ↑/↓/Home/End on the focused body;
490
- * Enter fires `onRowClick`), independent from checkbox selection. Off by default.
490
+ * Enter fires `onRowActivate`), independent from checkbox selection. Off by default. A
491
+ * predicate keeps the rows it rejects out of the cursor's reach — the keys pass over them and
492
+ * a click leaves the current row where it was — and so out of activation too: what the
493
+ * keyboard cannot reach, a click does not activate ([rows.md](../docs/rows.md#active-row)).
491
494
  */
492
- enableActiveRow?: boolean;
495
+ enableActiveRow?: boolean | ((row: Row<TData>) => boolean);
493
496
  /**
494
497
  * Drag-and-drop row reordering through an injected handle column. Row order IS data order —
495
498
  * there is no order state — so a completed drag hands the move to `onRowReorder` and the
@@ -809,6 +812,11 @@ interface LedgerMeta<TData extends RowData$1> {
809
812
  enabled: boolean;
810
813
  id: string | null;
811
814
  set: (id: string | null) => void;
815
+ /**
816
+ * Whether a row can be the current row, and so be activated: `enableActiveRow`'s predicate,
817
+ * or every row.
818
+ */
819
+ canActivate: (row: Row<TData>) => boolean;
812
820
  };
813
821
  /**
814
822
  * Header drag-reorder affordance (ledger-owned; TanStack has state but no switch).
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
- import { n as defaultLabels, t as DataTableLabels } from "./labels-D-6FCakH.js";
2
+ import { n as defaultLabels, t as DataTableLabels } from "./labels-D6GC8goU.js";
3
3
  import { BoxProps, ComboboxData, ElementProps, Factory, MantineBreakpoint, MantineColor, MantineRadius, MantineSize, MantineSpacing, PopoverProps, StylesApiProps, TableTdProps, TableThProps, TableTrProps, TextInputProps } from "@mantine/core";
4
4
  import { CSSProperties, ComponentProps, ComponentType, JSX as JSX$1, KeyboardEvent, MouseEvent, ReactElement, ReactNode, Ref } from "react";
5
5
  import { Cell as Cell$1, CellData, Column as Column$1, ColumnDef as ColumnDef$1, ColumnFiltersState, ColumnFiltersState as ColumnFiltersState$1, ColumnOrderState, ColumnOrderState as ColumnOrderState$1, ColumnPinningState, ColumnPinningState as ColumnPinningState$1, ColumnSizingState, ColumnSizingState as ColumnSizingState$1, ColumnVisibilityState, ColumnVisibilityState as ColumnVisibilityState$1, ExpandedState, ExpandedState as ExpandedState$1, FilterFn, FilterFnOption, GroupingState, GroupingState as GroupingState$1, Header as Header$1, HeaderGroup as HeaderGroup$1, PaginationState, PaginationState as PaginationState$1, ReactTable, Row as Row$1, RowData, RowData as RowData$1, RowPinningState, RowPinningState as RowPinningState$1, RowSelectionState, RowSelectionState as RowSelectionState$1, SortingState, SortingState as SortingState$1, TableFeatures, TableOptions, aggregationFn_count, aggregationFn_extent, aggregationFn_first, aggregationFn_last, aggregationFn_max, aggregationFn_mean, aggregationFn_median, aggregationFn_min, aggregationFn_sum, aggregationFn_unique, aggregationFn_uniqueCount, flexRender } from "@tanstack/react-table";
@@ -487,9 +487,12 @@ interface UseDataTableOptions<TData extends RowData$1> {
487
487
  onRowEditCommit?: (change: DataTableRowEditCommit<TData>) => void | Promise<void>;
488
488
  /**
489
489
  * A single keyboard-reachable current row (row click or ↑/↓/Home/End on the focused body;
490
- * Enter fires `onRowClick`), independent from checkbox selection. Off by default.
490
+ * Enter fires `onRowActivate`), independent from checkbox selection. Off by default. A
491
+ * predicate keeps the rows it rejects out of the cursor's reach — the keys pass over them and
492
+ * a click leaves the current row where it was — and so out of activation too: what the
493
+ * keyboard cannot reach, a click does not activate ([rows.md](../docs/rows.md#active-row)).
491
494
  */
492
- enableActiveRow?: boolean;
495
+ enableActiveRow?: boolean | ((row: Row<TData>) => boolean);
493
496
  /**
494
497
  * Drag-and-drop row reordering through an injected handle column. Row order IS data order —
495
498
  * there is no order state — so a completed drag hands the move to `onRowReorder` and the
@@ -809,6 +812,11 @@ interface LedgerMeta<TData extends RowData$1> {
809
812
  enabled: boolean;
810
813
  id: string | null;
811
814
  set: (id: string | null) => void;
815
+ /**
816
+ * Whether a row can be the current row, and so be activated: `enableActiveRow`'s predicate,
817
+ * or every row.
818
+ */
819
+ canActivate: (row: Row<TData>) => boolean;
812
820
  };
813
821
  /**
814
822
  * Header drag-reorder affordance (ledger-owned; TanStack has state but no switch).
package/dist/index.js CHANGED
@@ -428,6 +428,8 @@ function useRowReorder({ onRowReorder, labels, viewportRef, getCenterRows, nameR
428
428
  body.append(rowClone);
429
429
  ghostTable.append(colgroup, body);
430
430
  element.replaceChildren(ghostTable);
431
+ const surface = getComputedStyle(source).getPropertyValue("--ledger-body-bg").trim();
432
+ if (surface !== "") element.style.setProperty("--ledger-body-bg", surface);
431
433
  const rowRect = source.getBoundingClientRect();
432
434
  ghostOrigin.current = {
433
435
  x: rowRect.left - ghostPosition.current.x,
@@ -1071,7 +1073,8 @@ const defaultLabels = {
1071
1073
  selectRow: "Select row",
1072
1074
  selectedCount: (count) => `${count} selected`,
1073
1075
  clearSelection: "Clear selection",
1074
- rowNavigation: "Table rows — use the arrow keys to move between rows",
1076
+ rows: "Rows",
1077
+ rowNavigation: "Use the arrow keys to move between rows",
1075
1078
  currentRow: (row, index, count) => `${row}, row ${index} of ${count}`,
1076
1079
  expandRow: "Expand row",
1077
1080
  collapseRow: "Collapse row",
@@ -1567,6 +1570,66 @@ function warnOnce(key, message) {
1567
1570
  console.warn(`[ledger] ${message}`);
1568
1571
  }
1569
1572
  //#endregion
1573
+ //#region src/interactive.ts
1574
+ /**
1575
+ * An element that handles a click or a key itself: links, buttons, form controls and the labels
1576
+ * that forward to them, a disclosure's summary, editable content, and the interactive roles.
1577
+ */
1578
+ const INTERACTIVE = [
1579
+ "a[href]",
1580
+ "area[href]",
1581
+ "button",
1582
+ "input",
1583
+ "select",
1584
+ "textarea",
1585
+ "label",
1586
+ "summary",
1587
+ "[contenteditable]:not([contenteditable=\"false\"])",
1588
+ ...[
1589
+ "button",
1590
+ "checkbox",
1591
+ "combobox",
1592
+ "link",
1593
+ "menuitem",
1594
+ "menuitemcheckbox",
1595
+ "menuitemradio",
1596
+ "option",
1597
+ "radio",
1598
+ "searchbox",
1599
+ "slider",
1600
+ "spinbutton",
1601
+ "switch",
1602
+ "tab",
1603
+ "textbox",
1604
+ "treeitem"
1605
+ ].map((role) => `[role="${role}"]`)
1606
+ ].join(", ");
1607
+ /**
1608
+ * Whether an event belongs to `boundary` itself — the row a click reached, the body viewport a
1609
+ * key reached — rather than to something inside it that handles input of its own
1610
+ * (docs/rows.md#row-interaction):
1611
+ *
1612
+ * - it happened in the boundary's own DOM: React bubbles a portal's events through the tree
1613
+ * that rendered it, so a click on the item of a menu opened from a cell would otherwise reach
1614
+ * the row the menu's trigger sits in;
1615
+ * - and not on, or inside, an interactive element within the boundary, which owns it.
1616
+ */
1617
+ function isOwnEvent(event, boundary) {
1618
+ const { target } = event;
1619
+ if (!(target instanceof Node) || !boundary.contains(target)) return false;
1620
+ const interactive = (target instanceof Element ? target : target.parentElement)?.closest(INTERACTIVE);
1621
+ return !interactive || interactive === boundary || !boundary.contains(interactive);
1622
+ }
1623
+ /**
1624
+ * Whether a click is the end of a text selection made inside `boundary`: the pointer went down,
1625
+ * dragged across text and came up there, which selected the text and chose nothing. (A
1626
+ * double-click selects the word it lands on as its own effect, so it is not asked.)
1627
+ */
1628
+ function endsTextSelection(boundary) {
1629
+ const selection = boundary.ownerDocument.getSelection();
1630
+ return selection !== null && !selection.isCollapsed && boundary.contains(selection.anchorNode);
1631
+ }
1632
+ //#endregion
1570
1633
  //#region src/ledger-commands.ts
1571
1634
  function ledgerCommands(meta) {
1572
1635
  return meta;
@@ -2569,17 +2632,20 @@ function DataCell({ cell, editing, rowEditing, isFirstDataCell, depth, rowSpan,
2569
2632
  children: content
2570
2633
  });
2571
2634
  }
2572
- function DataRowImpl({ row, dataIndex, last, rowsBelow, editingColumnId, editingRow, dragging, dropSide, selected, active, expanded, depth, spanning, columnWindow, pinnedPosition, pinnedOffset, virtualIndex, measureRef, ariaRowIndex }) {
2635
+ function DataRowImpl({ row, dataIndex, last, rowsBelow, editingColumnId, editingRow, dragging, dropSide, selected, active, activatable, expanded, depth, spanning, columnWindow, pinnedPosition, pinnedOffset, virtualIndex, measureRef, ariaRowIndex }) {
2573
2636
  const { getStyles, onRowClick, onRowActivate, onRowDoubleClick, onRowContextMenu, rowProps, activeRowEnabled, setActiveRow } = useDataTableContext();
2574
2637
  const cells = row.getVisibleCells();
2575
2638
  const firstDataCellIndex = cells.findIndex((cell) => !isInternalColumn(cell.column.id));
2576
2639
  const pinnedStyle = pinnedPosition === "top" ? { top: `${pinnedOffset ?? 0}px` } : pinnedPosition === "bottom" ? { bottom: `${pinnedOffset ?? 0}px` } : void 0;
2577
- const handler = (callback) => callback ? (event) => callback(row, event) : void 0;
2578
- const handleClick = onRowClick || onRowActivate || activeRowEnabled ? (event) => {
2579
- if (activeRowEnabled) setActiveRow?.(row.id);
2580
- onRowClick?.(row, event);
2581
- onRowActivate?.(row, event);
2640
+ const handler = (callback, selecting) => callback ? (event) => {
2641
+ if (isOwnEvent(event, event.currentTarget) && !(selecting && endsTextSelection(event.currentTarget))) callback(row, event);
2582
2642
  } : void 0;
2643
+ const activates = !activeRowEnabled || activatable;
2644
+ const handleClick = handler(onRowClick || onRowActivate && activates || activeRowEnabled && activatable ? (_, event) => {
2645
+ if (activeRowEnabled && activatable) setActiveRow?.(row.id);
2646
+ onRowClick?.(row, event);
2647
+ if (activates) onRowActivate?.(row, event);
2648
+ } : void 0, true);
2583
2649
  return /* @__PURE__ */ jsx(Table.Tr, {
2584
2650
  ref: measureRef,
2585
2651
  ...mergeElementProps(resolveElementProps(rowProps, row), {
@@ -2600,8 +2666,8 @@ function DataRowImpl({ row, dataIndex, last, rowsBelow, editingColumnId, editing
2600
2666
  "data-selected": selected || void 0,
2601
2667
  role: "row",
2602
2668
  onClick: handleClick,
2603
- onContextMenu: handler(onRowContextMenu),
2604
- onDoubleClick: handler(onRowDoubleClick),
2669
+ onContextMenu: handler(onRowContextMenu, true),
2670
+ onDoubleClick: handler(onRowDoubleClick, false),
2605
2671
  ...getStyles("row", { style: pinnedStyle })
2606
2672
  }),
2607
2673
  children: renderRowCells()
@@ -2843,6 +2909,7 @@ function TableBody({ table, virtualization, columnWindow, viewportRef, loading,
2843
2909
  const dropSide = reorderTarget?.rowId === row.id && !(reorderTarget.side === "after" && withDetailPanels && row.getIsExpanded()) ? reorderTarget.side : null;
2844
2910
  const spanKey = spanningActive ? row.getVisibleCells().map((cell) => cell.getIsCovered() ? "x" : `${cell.getRowSpan()}:${cell.getColSpan()}`).join(" ") : "";
2845
2911
  return /* @__PURE__ */ jsx(DataRow, {
2912
+ activatable: ledger?.activeRow.canActivate(row) ?? true,
2846
2913
  active: activeRowId === row.id,
2847
2914
  ariaRowIndex,
2848
2915
  columnsKey,
@@ -2874,7 +2941,7 @@ function TableBody({ table, virtualization, columnWindow, viewportRef, loading,
2874
2941
  const firstItem = items[0];
2875
2942
  const lastItem = items.at(-1);
2876
2943
  const topSpace = firstItem ? firstItem.start : 0;
2877
- const bottomSpace = lastItem ? virtualizer.getTotalSize() - lastItem.end : 0;
2944
+ const bottomSpace = lastItem ? virtualizer.getTotalSize() - lastItem.end : virtualizer.getTotalSize();
2878
2945
  center = /* @__PURE__ */ jsxs(Fragment, { children: [
2879
2946
  topSpace > 0 && /* @__PURE__ */ jsx("tr", {
2880
2947
  "aria-hidden": true,
@@ -3858,7 +3925,7 @@ const builtInFilterFns = {
3858
3925
  * private `RangeValue` alias behind the min/max/extent value types becomes nameable only by a
3859
3926
  * deep path the package's `exports` map hides, and an inferred registry fails declaration emit
3860
3927
  * (TS2883). Naming each definition by its public export emits the type the upstream fix will
3861
- * emit anyway. Upstream: sxzz/rolldown-plugin-dts#312 (fix: #310), rolldown/tsdown#1087.
3928
+ * emit anyway. Upstream: TanStack/table#6619, sxzz/rolldown-plugin-dts#312 (fix: #310).
3862
3929
  */
3863
3930
  const builtInAggregationFns = {
3864
3931
  count: aggregationFn_count,
@@ -5878,6 +5945,10 @@ const NO_ROW_PINNING = {
5878
5945
  top: [],
5879
5946
  bottom: []
5880
5947
  };
5948
+ /**
5949
+ * `enableActiveRow` without a predicate: the cursor can rest on any row.
5950
+ */
5951
+ const everyRow = () => true;
5881
5952
  const DEFAULT_PAGINATION = {
5882
5953
  pageIndex: 0,
5883
5954
  pageSize: 20
@@ -6099,9 +6170,10 @@ function useDataTable(options) {
6099
6170
  renderDetailPanel,
6100
6171
  selectAllScope,
6101
6172
  activeRow: {
6102
- enabled: enableActiveRow,
6173
+ enabled: enableActiveRow !== false,
6103
6174
  id: activeRowId,
6104
- set: setActiveRowId
6175
+ set: setActiveRowId,
6176
+ canActivate: typeof enableActiveRow === "function" ? enableActiveRow : everyRow
6105
6177
  },
6106
6178
  enableColumnOrdering,
6107
6179
  enableColumnResizing,
@@ -7047,6 +7119,7 @@ function DataTableCore({ presentation, stylesProps, table }) {
7047
7119
  ]);
7048
7120
  const activeRowAnnouncer = useRef(null);
7049
7121
  const rowNavigationHintId = `${instanceId}-row-navigation`;
7122
+ const viewportFocusStop = activeRowEnabled || viewportOverflows;
7050
7123
  const viewportDescribedBy = activeRowEnabled ? [viewportProps?.["aria-describedby"], rowNavigationHintId].filter(Boolean).join(" ") : viewportProps?.["aria-describedby"];
7051
7124
  const announceActiveRow = useEventCallback(() => {
7052
7125
  const target = activeRowAnnouncer.current;
@@ -7076,7 +7149,7 @@ function DataTableCore({ presentation, stylesProps, table }) {
7076
7149
  ]);
7077
7150
  const handleActiveRowKeyDown = useEventCallback((event) => {
7078
7151
  if (!activeRowEnabled || !setActiveRow) return;
7079
- if (event.target.closest("input, button, select, textarea, a, [contenteditable]")) return;
7152
+ if (!isOwnEvent(event, event.currentTarget)) return;
7080
7153
  if (event.key === "F2") {
7081
7154
  if (!editing || activeRowId === null) return;
7082
7155
  const rowMode = editing.mode === "row";
@@ -7091,35 +7164,40 @@ function DataTableCore({ presentation, stylesProps, table }) {
7091
7164
  return;
7092
7165
  }
7093
7166
  const rows = displaySnapshot.ordered;
7094
- if (rows.length === 0) return;
7167
+ const canActivate = ledger?.activeRow.canActivate;
7168
+ if (rows.length === 0 || !canActivate) return;
7095
7169
  const currentIndex = activeRowId === null ? -1 : rows.findIndex((row) => row.id === activeRowId);
7096
- const moveTo = (index) => {
7097
- const row = rows[Math.min(Math.max(index, 0), rows.length - 1)];
7098
- if (row) {
7099
- setActiveRow(row.id);
7100
- scrollRowIntoView(row.id);
7170
+ const moveFrom = (from, step) => {
7171
+ for (let index = from; index >= 0 && index < rows.length; index += step) {
7172
+ const row = rows[index];
7173
+ if (canActivate(row)) {
7174
+ setActiveRow(row.id);
7175
+ scrollRowIntoView(row.id);
7176
+ return;
7177
+ }
7101
7178
  }
7179
+ if (currentIndex !== -1 && activeRowId !== null) scrollRowIntoView(activeRowId);
7102
7180
  };
7103
7181
  switch (event.key) {
7104
7182
  case "ArrowDown":
7105
7183
  event.preventDefault();
7106
- moveTo(currentIndex + 1);
7184
+ moveFrom(currentIndex + 1, 1);
7107
7185
  break;
7108
7186
  case "ArrowUp":
7109
7187
  event.preventDefault();
7110
- moveTo(currentIndex - 1);
7188
+ moveFrom(currentIndex === -1 ? 0 : currentIndex - 1, currentIndex === -1 ? 1 : -1);
7111
7189
  break;
7112
7190
  case "Home":
7113
7191
  event.preventDefault();
7114
- moveTo(0);
7192
+ moveFrom(0, 1);
7115
7193
  break;
7116
7194
  case "End":
7117
7195
  event.preventDefault();
7118
- moveTo(rows.length - 1);
7196
+ moveFrom(rows.length - 1, -1);
7119
7197
  break;
7120
7198
  case "Enter": {
7121
7199
  const row = currentIndex >= 0 ? rows[currentIndex] : null;
7122
- if (row && onRowActivate) {
7200
+ if (row && onRowActivate && canActivate(row)) {
7123
7201
  event.preventDefault();
7124
7202
  onRowActivate(row, event);
7125
7203
  }
@@ -7285,8 +7363,9 @@ function DataTableCore({ presentation, stylesProps, table }) {
7285
7363
  viewportProps: mergeElementProps(viewportProps, {
7286
7364
  style: { overscrollBehavior: "none" },
7287
7365
  role: "rowgroup",
7366
+ "aria-label": viewportProps?.["aria-label"] ?? (viewportFocusStop ? labels.rows : void 0),
7288
7367
  "aria-describedby": viewportDescribedBy,
7289
- tabIndex: activeRowEnabled || viewportOverflows ? 0 : void 0,
7368
+ tabIndex: viewportFocusStop ? 0 : void 0,
7290
7369
  ...activeRowEnabled && { onKeyDown: handleActiveRowKeyDown }
7291
7370
  }),
7292
7371
  onScrollPositionChange: handleScrollPositionChange,