@coldsmirk/ledger-mantine 0.10.3 → 0.11.1

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/README.md CHANGED
@@ -17,6 +17,8 @@ pnpm add @coldsmirk/ledger-mantine
17
17
 
18
18
  Peer dependencies: `@mantine/core`, `@mantine/dates`, and `@mantine/hooks` `^9.0.0`; `react` and `react-dom` `^19.2.0`; and `dayjs` as required by `@mantine/dates`. `@tanstack/react-table` and `@tanstack/react-virtual` are direct dependencies — everything a table needs is re-exported from the main entry.
19
19
 
20
+ **Browser support:** Chrome 111+, Edge 111+, Firefox 128+, and Safari / iOS Safari 16.4+.
21
+
20
22
  Import Mantine's styles and ledger's layered stylesheet once at the application entry point:
21
23
 
22
24
  ```tsx
package/dist/index.cjs CHANGED
@@ -3185,8 +3185,10 @@ function TableFooter({ table, ariaRowIndexStart, columnWindow, filler }) {
3185
3185
  //#region src/column-autosize.ts
3186
3186
  /**
3187
3187
  * Fitting columns to their rendered content (docs/sizing.md#autosize) — one measurement behind
3188
- * both of its uses: the resizer's double-click, which writes the fit as a `columnSizing` entry,
3189
- * and `meta.autosize`, which keeps a column fitted as its content changes (`use-autosize-widths.ts`).
3188
+ * each of its uses: the resizer's double-click, which writes the fit as a `columnSizing` entry,
3189
+ * `meta.autosize`, which keeps a column fitted as its content changes, and
3190
+ * `meta.minSizeFromHeader`, which keeps a column at least as wide as its header
3191
+ * (`use-autosize-widths.ts`).
3190
3192
  *
3191
3193
  * What is measured is what the column has to hold: the header cell, every RENDERED body cell —
3192
3194
  * under virtualization that is the current window, the industry contract for autosize (unrendered
@@ -3262,18 +3264,19 @@ function contentCells(main, region, selector) {
3262
3264
  return [...main.querySelectorAll(`:scope ${region} ${selector}`)].filter((cell) => cell.colSpan <= 1 && !Object.hasOwn(cell.dataset, "editing"));
3263
3265
  }
3264
3266
  /**
3265
- * The width each target's rendered content needs, slack included, in integer pixels — before the
3267
+ * The width each target's rendered extent needs, slack included, in integer pixels — before the
3266
3268
  * column's own `minSize`/`maxSize`, which are the caller's to apply. A column with nothing rendered
3267
3269
  * (one a column window left out) has no entry.
3268
3270
  */
3269
3271
  function measureColumnWidths(main, targets) {
3270
3272
  const columns = targets.map((target) => {
3271
3273
  const selector = `[data-ledger-column-id="${CSS.escape(target.columnId)}"]`;
3274
+ const withContent = target.extent === "content";
3272
3275
  return {
3273
3276
  target,
3274
3277
  header: main.querySelector(`:scope .ledger-header ${selector}`),
3275
- body: contentCells(main, ".ledger-tbody", selector),
3276
- footer: contentCells(main, ".ledger-footer", selector)
3278
+ body: withContent ? contentCells(main, ".ledger-tbody", selector) : [],
3279
+ footer: withContent ? contentCells(main, ".ledger-footer", selector) : []
3277
3280
  };
3278
3281
  });
3279
3282
  const headers = columns.map((column) => column.header).filter((header) => header !== null);
@@ -3312,7 +3315,10 @@ function measureColumnWidths(main, targets) {
3312
3315
  * arrive from the render that put the handle on screen, for the reason `use-column-resize.ts` gives.
3313
3316
  */
3314
3317
  function autosizeColumn(setColumnSizing, { minSize, maxSize, ...target }, main) {
3315
- const fitted = measureColumnWidths(main, [target]).get(target.columnId);
3318
+ const fitted = measureColumnWidths(main, [{
3319
+ ...target,
3320
+ extent: "content"
3321
+ }]).get(target.columnId);
3316
3322
  if (fitted === void 0) return;
3317
3323
  const next = Math.min(Math.max(fitted, minSize), maxSize);
3318
3324
  setColumnSizing((previous) => previous[target.columnId] === next ? previous : {
@@ -3954,8 +3960,13 @@ function HeaderCell({ header, table, reorder, resize, columnWidths, columnSizing
3954
3960
  //#endregion
3955
3961
  //#region src/use-autosize-widths.ts
3956
3962
  /**
3957
- * `meta.autosize` (docs/sizing.md#autosize): keeps each such column fitted to its rendered content
3958
- * and hands the width engine the fits, which rank between a user's resize and the author's `size`.
3963
+ * The widths ledger measures and keeps measured (docs/sizing.md#autosize), for the width engine:
3964
+ * - **fits** — `meta.autosize`: each such column fitted to its rendered content, a width that
3965
+ * ranks between a user's resize and the author's `size`;
3966
+ * - **header widths** — `meta.minSizeFromHeader`, or the table's `columnMinSizeFromHeader`: each
3967
+ * such column's header measured whole, a floor under the column's width
3968
+ * (docs/sizing.md#a-header-held-whole). A column fitted by `autosize` holds its header already
3969
+ * and is not measured twice.
3959
3970
  *
3960
3971
  * When a fit is taken, and what it may do:
3961
3972
  * - **A new view refits.** Different rows (a new page, a filter, a sort — anything that changes the
@@ -3980,16 +3991,38 @@ function HeaderCell({ header, table, reorder, resize, columnWidths, columnSizing
3980
3991
  */
3981
3992
  const SCROLL_SETTLE_MS = 150;
3982
3993
  const NO_WIDTHS = {};
3983
- function autosizeTargets(columns, columnSizing) {
3994
+ /**
3995
+ * What each column has measured: its content under `meta.autosize`, else its header where it asks
3996
+ * never to be narrower than it — nothing for an injected column or one a user resized.
3997
+ */
3998
+ function autosizeTargets(columns, columnSizing, minSizeFromHeader) {
3984
3999
  return columns.flatMap((column) => {
3985
4000
  const { meta } = column.columnDef;
3986
- return meta?.autosize === true && !isInternalColumn(column.id) && columnSizing[column.id] === void 0 ? [{
4001
+ if (isInternalColumn(column.id) || columnSizing[column.id] !== void 0) return [];
4002
+ const extent = meta?.autosize === true ? "content" : meta?.minSizeFromHeader ?? minSizeFromHeader ? "header" : void 0;
4003
+ return extent === void 0 ? [] : [{
3987
4004
  columnId: column.id,
3988
- hasFilter: meta.filter !== void 0,
3989
- truncate: meta.truncate === true
3990
- }] : [];
4005
+ extent,
4006
+ hasFilter: meta?.filter !== void 0,
4007
+ truncate: meta?.truncate === true
4008
+ }];
3991
4009
  });
3992
4010
  }
4011
+ /**
4012
+ * The measured widths, told apart by what each target measured.
4013
+ */
4014
+ function byExtent(widths, targets) {
4015
+ const fits = {};
4016
+ const headerWidths = {};
4017
+ for (const { columnId, extent } of targets) {
4018
+ const width = widths[columnId];
4019
+ if (width !== void 0) (extent === "content" ? fits : headerWidths)[columnId] = width;
4020
+ }
4021
+ return {
4022
+ fits,
4023
+ headerWidths
4024
+ };
4025
+ }
3993
4026
  function sameIds(previous, next) {
3994
4027
  return previous.length === next.length && previous.every((id, index) => id === next[index]);
3995
4028
  }
@@ -4021,8 +4054,8 @@ function useFontsVersion(enabled) {
4021
4054
  }, [enabled]);
4022
4055
  return version;
4023
4056
  }
4024
- function useAutosizeWidths({ columns, columnSizing, main, viewport, rows, appearance, virtualized }) {
4025
- const targets = autosizeTargets(columns, columnSizing);
4057
+ function useAutosizeWidths({ columns, columnSizing, main, viewport, rows, appearance, virtualized, minSizeFromHeader }) {
4058
+ const targets = autosizeTargets(columns, columnSizing, minSizeFromHeader);
4026
4059
  const enabled = targets.length > 0;
4027
4060
  const fontsVersion = useFontsVersion(enabled);
4028
4061
  const signature = JSON.stringify([
@@ -4103,7 +4136,7 @@ function useAutosizeWidths({ columns, columnSizing, main, viewport, rows, appear
4103
4136
  fit,
4104
4137
  fitWhenSettled
4105
4138
  ]);
4106
- return widths;
4139
+ return (0, react.useMemo)(() => byExtent(widths, targets), [widths, signature]);
4107
4140
  }
4108
4141
  //#endregion
4109
4142
  //#region src/use-column-widths.ts
@@ -4113,9 +4146,12 @@ function useAutosizeWidths({ columns, columnSizing, main, viewport, rows, appear
4113
4146
  * number system — the browser never redistributes anything.
4114
4147
  *
4115
4148
  * Rules, in order:
4149
+ * - a column's minimum is its declared `minSize`, raised to its measured header where it asks
4150
+ * never to be narrower than it (`meta.minSizeFromHeader`) — except under a user's resize, which
4151
+ * the declared bounds alone clamp;
4116
4152
  * - a column with an explicit width (user-resized `columnSizing` entry, else its fitted content
4117
4153
  * width under `meta.autosize`, else the author's `size`, else `defaultColumn.size`) is fixed at
4118
- * that width, clamped to its declared min/max;
4154
+ * that width, clamped to its minimum and its declared max;
4119
4155
  * - a column without one is a grow column with basis `minSize ?? 80`; container surplus is
4120
4156
  * distributed proportionally to the bases (integer floor, remainder in display order), capped
4121
4157
  * by maxSize; when the container is too small every grow column falls back to its basis and the
@@ -4144,6 +4180,13 @@ function clampWidth(value, min, max) {
4144
4180
  return Math.min(Math.max(Number.isFinite(value) ? Math.max(0, Math.round(value)) : 0, normalizedBound(min) ?? 0), normalizedBound(max) ?? Number.MAX_SAFE_INTEGER);
4145
4181
  }
4146
4182
  /**
4183
+ * The column's declared `minSize`, raised to its measured header where it has one.
4184
+ */
4185
+ function headerMinSize(column) {
4186
+ const headerWidth = finiteNumber(column.headerWidth);
4187
+ return headerWidth === void 0 ? column.minSize : Math.max(headerWidth, finiteNumber(column.minSize) ?? 0);
4188
+ }
4189
+ /**
4147
4190
  * Weighted water-filling over integer pixels. Columns that hit maxSize leave the active set and
4148
4191
  * the remaining space is redistributed over columns that can still grow. If every column reaches
4149
4192
  * its cap, maxSize wins and the table intentionally stops short of the viewport.
@@ -4183,16 +4226,18 @@ function resolveColumnWidths(columns, columnSizing, availableWidth) {
4183
4226
  const byId = {};
4184
4227
  const growColumns = [];
4185
4228
  for (const column of columns) {
4186
- const sized = finiteNumber(columnSizing[column.id]) ?? finiteNumber(column.fit) ?? finiteNumber(column.size);
4229
+ const resized = finiteNumber(columnSizing[column.id]);
4230
+ const sized = resized ?? finiteNumber(column.fit) ?? finiteNumber(column.size);
4231
+ const minSize = resized === void 0 ? headerMinSize(column) : column.minSize;
4187
4232
  if (sized === void 0) {
4188
- const basis = clampWidth(column.minSize ?? DEFAULT_FLEX_BASIS, column.minSize, column.maxSize);
4233
+ const basis = clampWidth(column.minSize ?? DEFAULT_FLEX_BASIS, minSize, column.maxSize);
4189
4234
  byId[column.id] = basis;
4190
4235
  growColumns.push({
4191
4236
  id: column.id,
4192
4237
  weight: basis,
4193
4238
  max: normalizedBound(column.maxSize)
4194
4239
  });
4195
- } else byId[column.id] = clampWidth(sized, column.minSize, column.maxSize);
4240
+ } else byId[column.id] = clampWidth(sized, minSize, column.maxSize);
4196
4241
  }
4197
4242
  const baseTotal = columns.reduce((total, column) => total + (byId[column.id] ?? 0), 0);
4198
4243
  const targetWidth = Number.isFinite(availableWidth) ? Math.max(0, Math.round(availableWidth)) : 0;
@@ -4251,13 +4296,15 @@ function useColumnWidths(table, { columns, viewport, tableMinWidth, injectedWidt
4251
4296
  if (isInternalColumn(column.id)) return {
4252
4297
  id: column.id,
4253
4298
  fit: void 0,
4299
+ headerWidth: void 0,
4254
4300
  size: raw?.size ?? injectedWidth,
4255
4301
  minSize: raw?.minSize ?? injectedWidth,
4256
4302
  maxSize: raw?.maxSize ?? injectedWidth
4257
4303
  };
4258
4304
  return {
4259
4305
  id: column.id,
4260
- fit: autosizeWidths[column.id],
4306
+ fit: autosizeWidths.fits[column.id],
4307
+ headerWidth: autosizeWidths.headerWidths[column.id],
4261
4308
  size: raw ? raw.size ?? defaultColumn?.size : column.columnDef.size,
4262
4309
  minSize: raw ? raw.minSize ?? defaultColumn?.minSize : column.columnDef.minSize,
4263
4310
  maxSize: raw ? raw.maxSize ?? defaultColumn?.maxSize : column.columnDef.maxSize
@@ -4470,7 +4517,7 @@ function normalizeColumnPinning(value) {
4470
4517
  if (!isRecord(value) || !isStringArray(value.start) || !isStringArray(value.end)) return value;
4471
4518
  const start = new Set(value.start);
4472
4519
  return {
4473
- end: [...new Set(value.end).difference(start)],
4520
+ end: [...new Set(value.end)].filter((id) => !start.has(id)),
4474
4521
  start: [...start]
4475
4522
  };
4476
4523
  }
@@ -7006,6 +7053,7 @@ const defaultProps = {
7006
7053
  withBottomBorder: false,
7007
7054
  size: "sm",
7008
7055
  tabularNums: false,
7056
+ columnMinSizeFromHeader: false,
7009
7057
  virtualizedRows: false,
7010
7058
  virtualizedColumns: false,
7011
7059
  endReachedOffset: 240,
@@ -7145,7 +7193,7 @@ function DataTableFromOptions({ props }) {
7145
7193
  });
7146
7194
  }
7147
7195
  function DataTableCore({ presentation, stylesProps, table }) {
7148
- const { striped, stripedColor: _stripedColor, highlightOnHover, highlightOnHoverColor: _highlightOnHoverColor, withTableBorder, withColumnBorders, withRowBorders, withBottomBorder, borderColor: _borderColor, radius: _radius, size: sizeProp, verticalSpacing, horizontalSpacing, tabularNums, tableMinWidth, virtualizedRows, virtualizedColumns, onEndReached, endReachedOffset, loadingMore, loadMoreError, loading, emptyState, error, onRetry, withColumnHeaders, withPaginationBar, pageSizeOptions, onRowClick, onRowActivate, onRowDoubleClick, onRowContextMenu, rowProps, headerRowProps, footerRowProps, viewportProps, labels: labelsProp, icons: iconsProp, handleRef, ref: rootRef, classNames, styles, unstyled, vars, className, style, attributes, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-describedby": ariaDescribedBy, ...others } = presentation;
7196
+ const { striped, stripedColor: _stripedColor, highlightOnHover, highlightOnHoverColor: _highlightOnHoverColor, withTableBorder, withColumnBorders, withRowBorders, withBottomBorder, borderColor: _borderColor, radius: _radius, size: sizeProp, verticalSpacing, horizontalSpacing, tabularNums, tableMinWidth, columnMinSizeFromHeader, virtualizedRows, virtualizedColumns, onEndReached, endReachedOffset, loadingMore, loadMoreError, loading, emptyState, error, onRetry, withColumnHeaders, withPaginationBar, pageSizeOptions, onRowClick, onRowActivate, onRowDoubleClick, onRowContextMenu, rowProps, headerRowProps, footerRowProps, viewportProps, labels: labelsProp, icons: iconsProp, handleRef, ref: rootRef, classNames, styles, unstyled, vars, className, style, attributes, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-describedby": ariaDescribedBy, ...others } = presentation;
7149
7197
  const getStyles = (0, _mantine_core.useStyles)({
7150
7198
  name: "DataTable",
7151
7199
  classes,
@@ -7208,6 +7256,7 @@ function DataTableCore({ presentation, stylesProps, table }) {
7208
7256
  columnSizing: table.state.columnSizing,
7209
7257
  columns: visibleLeafColumns,
7210
7258
  main,
7259
+ minSizeFromHeader: columnMinSizeFromHeader === true,
7211
7260
  rows: table.getRowModel().rows,
7212
7261
  viewport,
7213
7262
  virtualized: Boolean(virtualizedRows) || Boolean(virtualizedColumns)