@vendure-io/ui 3.0.0-next.0 → 3.0.0-next.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.
@@ -1,6 +1,7 @@
1
1
  import type { Column, ColumnDef, RowSelectionState, Table } from '@tanstack/react-table';
2
2
  import type {
3
3
  ControlledState,
4
+ DataTableClearFilters,
4
5
  DataTableColumnMeta,
5
6
  } from '@vendure-io/ui/components/molecules/data-table/data-table-types';
6
7
 
@@ -46,6 +47,25 @@ export function getHideableLeafColumns<TData>(table: Table<TData>): Column<TData
46
47
  }
47
48
 
48
49
  /** Resolve a render-prop-or-node slot against the live table instance. */
50
+ /**
51
+ * The "Clear filters" button after the chips, or undefined when it is off.
52
+ * `emptyCell`: the table shows its empty state (no rows, not loading). The
53
+ * button hides there unless the consumer sets `hidden`. Default click: remove
54
+ * every applied filter.
55
+ */
56
+ export function resolveClearFilters<TData>(
57
+ option: boolean | DataTableClearFilters | undefined,
58
+ table: Pick<Table<TData>, 'setColumnFilters'>,
59
+ emptyCell: boolean,
60
+ ): { hidden: boolean; onClear: () => void } | undefined {
61
+ if (!option) return undefined;
62
+ const config = option === true ? {} : option;
63
+ return {
64
+ hidden: config.hidden ?? emptyCell,
65
+ onClear: config.onClear ?? (() => table.setColumnFilters([])),
66
+ };
67
+ }
68
+
49
69
  export function resolveSlot<TData, TSlot>(
50
70
  slot: TSlot | ((table: Table<TData>) => TSlot) | undefined,
51
71
  table: Table<TData>,
@@ -122,6 +122,24 @@ export interface DataTableFilterColumn<TData = unknown> {
122
122
  * passes its human-readable-operator formatter here.
123
123
  */
124
124
  formatChip?: (value: DataTableFilterValue) => React.ReactNode;
125
+ /**
126
+ * Makes the column's applied-filter chip editable. Present → the chip label
127
+ * is a button that opens this editor, and the consumer renders the whole
128
+ * popup (a Popover, a Combobox, a Dialog) controlled by `open`. Anchor a
129
+ * positioned popup to `anchor`, the chip. Absent → the chip only removes.
130
+ */
131
+ renderEditor?: (ctx: DataTableFilterEditorContext<TData>) => React.ReactNode;
132
+ }
133
+
134
+ export interface DataTableFilterEditorContext<TData = unknown> {
135
+ /** Whether the editor is open. The chip label opens it. */
136
+ open: boolean;
137
+ onOpenChange: (open: boolean) => void;
138
+ /** The applied-filter chip, to anchor a positioned popup to. */
139
+ anchor: React.RefObject<HTMLElement | null>;
140
+ value: DataTableFilterValue | undefined;
141
+ onChange: (next: DataTableFilterValue | undefined) => void;
142
+ column: DataTableFilterColumn<TData>;
125
143
  }
126
144
 
127
145
  export type DataTableFilters<TData> = ControlledState<ColumnFiltersState> & {
@@ -141,8 +159,28 @@ export type DataTableFilters<TData> = ControlledState<ColumnFiltersState> & {
141
159
  showAddFilter?: boolean;
142
160
  /** Defaults to true. Set false when a consumer renders its own applied-filter UI. */
143
161
  showAppliedFilters?: boolean;
162
+ /**
163
+ * A "Clear filters" button after the applied-filter chips. Off by default.
164
+ * `true` or an object turns it on. It shows only with a chip: a search has
165
+ * the clear of its own field. While the table shows its empty state (no
166
+ * rows, not loading), the empty state holds the only "Clear filters", so the
167
+ * button hides. The chip line keeps the button's height either way, so the
168
+ * table does not move when it shows or hides.
169
+ */
170
+ clearFilters?: boolean | DataTableClearFilters;
144
171
  };
145
172
 
173
+ export interface DataTableClearFilters {
174
+ /** Runs on click. Default: removes every applied filter. */
175
+ onClear?: () => void;
176
+ /**
177
+ * Hides the button while a chip applies. Default: true while the table
178
+ * shows its empty state (no rows, not loading). Set it when the consumer
179
+ * knows better, e.g. loading passed through `emptyState`.
180
+ */
181
+ hidden?: boolean;
182
+ }
183
+
146
184
  export interface DataTableBulkActionContext<TData> {
147
185
  /**
148
186
  * `row.original` of the selected rows the table has loaded, from the core's
@@ -227,6 +265,7 @@ export interface DataTableLabels {
227
265
  columnsTrigger?: string; // 'Columns'
228
266
  columnsHeading?: string; // 'Toggle columns'
229
267
  addFilter?: string; // 'Add filter'
268
+ clearFilters?: string; // 'Clear filters'
230
269
  removeFilter?: (columnLabel: string) => string; // (l) => `Remove ${l} filter`
231
270
  sortLabel?: (columnLabel: string) => string; // (c) => `Sort by ${c}`
232
271
  filtersCollapsed?: (count: number) => string; // (n) => `${n} filters`
@@ -23,6 +23,7 @@ import {
23
23
  useReactTable,
24
24
  type VisibilityState,
25
25
  } from '@tanstack/react-table';
26
+ import { Button } from '@vendure-io/ui/components/atoms/button';
26
27
  import { Card, CardFooter, CardHeader, CardTable } from '@vendure-io/ui/components/atoms/card';
27
28
  import { Checkbox } from '@vendure-io/ui/components/atoms/checkbox';
28
29
  import {
@@ -74,6 +75,7 @@ import {
74
75
  columnAlign,
75
76
  getSelectedRowIds,
76
77
  measureColumnIndents,
78
+ resolveClearFilters,
77
79
  resolveSlot,
78
80
  sameIndents,
79
81
  } from './data-table-helpers.tsx';
@@ -338,15 +340,23 @@ const HEADER_LABEL_UNDER_BULK_CLASS =
338
340
  const ROW_CONTROL_SELECTOR =
339
341
  '[data-slot=data-table-select-area],a:not([data-slot=data-table-row-link]),button,input,select,textarea,[role=button],[role=checkbox],[role=menuitem],[tabindex]:not([data-slot=data-table-row-link]),[data-tooltip-trigger],[data-slot=id-chip],[data-slot=anonymized-token],[data-slot=copyable-text],[data-table-row-control]';
340
342
 
343
+ // The select gutter is `--spacing(10)`, 40px: the checkbox (8px in, 16px wide,
344
+ // ending at 24px), a 5px gap, then the 11px `--content-inset` of a sortable
345
+ // header (DataTableColumnHeader pulls its pill back by that much so its label
346
+ // starts with the cell text). A narrower gutter puts the hovered or sorted
347
+ // header pill over the select-all checkbox. Every rule that uses the gutter
348
+ // (the select area, the leading cell, the bulk bar over the header) writes
349
+ // `--spacing(10)`; change them together.
350
+ //
341
351
  // The click area of a select checkbox is its whole cell: the full height of
342
352
  // the row, from the table's start edge to where the leading cell's content
343
- // starts (its gutter, `--spacing(8)` plus its indent). The select cell is
353
+ // starts (its gutter plus its indent). The select cell is
344
354
  // zero-width, so the area is floated over that gutter, under the checkbox and
345
355
  // above the row link's stretched ::after. It is not focusable: keyboard focus
346
356
  // and the focus ring stay on the checkbox. The checkbox's own enlarged hit
347
357
  // area is turned off here, since it reached past the gutter into the content.
348
358
  const SELECT_AREA_CLASS =
349
- 'absolute inset-y-0 start-0 z-10 w-[calc(var(--spacing)*8+var(--column-indent,0px))]';
359
+ 'absolute inset-y-0 start-0 z-10 w-[calc(var(--spacing)*10+var(--column-indent,0px))]';
350
360
  const SELECT_CHECKBOX_HIT_CLASS = 'after:hidden';
351
361
 
352
362
  // Select all and clear all from the header checkbox ripple down the page: the
@@ -672,6 +682,7 @@ function DataTable<TData>({
672
682
  columnsTrigger: labels?.columnsTrigger ?? 'Columns',
673
683
  columnsHeading: labels?.columnsHeading ?? 'Toggle columns',
674
684
  addFilter: labels?.addFilter ?? 'Add filter',
685
+ clearFilters: labels?.clearFilters ?? 'Clear filters',
675
686
  removeFilter:
676
687
  labels?.removeFilter ?? ((columnLabel: string) => `Remove ${columnLabel} filter`),
677
688
  sortLabel: labels?.sortLabel ?? ((columnLabel: string) => `Sort by ${columnLabel}`),
@@ -934,7 +945,7 @@ function DataTable<TData>({
934
945
  // Every other cell adds its column's indent (`--column-indent`, see
935
946
  // `columnIndents`) to its start padding. CardTable's edge-cell padding
936
947
  // adds it too.
937
- if (id === leadingColumnId) return 'ps-[calc(var(--spacing)*8+var(--column-indent,0px))]';
948
+ if (id === leadingColumnId) return 'ps-[calc(var(--spacing)*10+var(--column-indent,0px))]';
938
949
  // Actions cells drop vertical padding so a row-action button (taller than a
939
950
  // text line) sits inside the natural row height rather than inflating every
940
951
  // row; `align-middle` on the cell keeps it centred.
@@ -1047,6 +1058,13 @@ function DataTable<TData>({
1047
1058
  filters.showAppliedFilters !== false &&
1048
1059
  table.getState().columnFilters.length > 0;
1049
1060
  const showHeader = header != null || showControls || showChips;
1061
+ // "Clear filters" after the chips. With no rows (and not loading) the empty
1062
+ // state has the only one.
1063
+ const clearFilters = resolveClearFilters(
1064
+ filters?.clearFilters,
1065
+ table,
1066
+ !(isLoading && rows.length === 0) && bodyRows.length === 0,
1067
+ );
1050
1068
  // The bulk bar exists only while rows are selected. It takes the controls
1051
1069
  // row's place, or, with no controls row, covers the column header row (see
1052
1070
  // `bulkBarFade`): it never opens a band of its own, so the table never
@@ -1185,7 +1203,10 @@ function DataTable<TData>({
1185
1203
  </MotionConfig>
1186
1204
  )}
1187
1205
  {showChips && (
1188
- <ListHeaderChips>
1206
+ // With "Clear filters" on, the line keeps the button's height
1207
+ // (Button sm, h-7; a Chip is h-6), so the table does not move
1208
+ // when the button shows or hides.
1209
+ <ListHeaderChips className={clearFilters ? 'min-h-7' : undefined}>
1189
1210
  <DataTableAppliedFilters
1190
1211
  table={table}
1191
1212
  columns={filters?.columns}
@@ -1193,6 +1214,16 @@ function DataTable<TData>({
1193
1214
  removeLabel={l.removeFilter}
1194
1215
  collapsedLabel={l.filtersCollapsed}
1195
1216
  />
1217
+ {clearFilters && !clearFilters.hidden && (
1218
+ <Button
1219
+ data-slot="data-table-clear-filters"
1220
+ variant="ghost"
1221
+ size="sm"
1222
+ onClick={clearFilters.onClear}
1223
+ >
1224
+ {l.clearFilters}
1225
+ </Button>
1226
+ )}
1196
1227
  </ListHeaderChips>
1197
1228
  )}
1198
1229
  </ListHeader>
@@ -1211,7 +1242,7 @@ function DataTable<TData>({
1211
1242
  <motion.div
1212
1243
  key="bulk-over-header"
1213
1244
  {...bulkBarFade}
1214
- className="pointer-events-none absolute inset-x-0 top-0 z-20 flex h-10 items-center ps-8 pe-(--card-px)"
1245
+ className="pointer-events-none absolute inset-x-0 top-0 z-20 flex h-10 items-center ps-10 pe-(--card-px)"
1215
1246
  >
1216
1247
  <DataTableBulkActions
1217
1248
  table={table}
@@ -1,29 +1,13 @@
1
1
  'use client';
2
2
 
3
3
  import { Kbd } from '@vendure-io/ui/components/atoms/kbd';
4
+ import { useIsApplePlatform } from '@vendure-io/ui/hooks/use-apple-platform';
4
5
  import { cn } from '@vendure-io/ui/lib/utils';
5
6
  import { SearchIcon } from 'lucide-react';
6
7
  import type * as React from 'react';
7
- import { useSyncExternalStore } from 'react';
8
8
 
9
9
  type SearchTriggerCollapse = 'md' | 'never' | 'always';
10
10
 
11
- const subscribe = () => () => {};
12
-
13
- // Apple platforms show ⌘, the rest show Ctrl. The server and the first
14
- // hydration render have no platform, so they show Ctrl; React swaps in the
15
- // client value right after hydration without a mismatch error.
16
- function isApplePlatform() {
17
- if (typeof navigator === 'undefined') return false;
18
- const nav = navigator as Navigator & { userAgentData?: { platform?: string } };
19
- const platform = nav.userAgentData?.platform || nav.platform || nav.userAgent;
20
- return /mac|iphone|ipad|ipod/i.test(platform);
21
- }
22
-
23
- function useIsApplePlatform() {
24
- return useSyncExternalStore(subscribe, isApplePlatform, () => false);
25
- }
26
-
27
11
  // The v3 look: a button that looks like a field. 28px high, 220px wide, on
28
12
  // the muted fill with a 1px inset hairline and the lg radius. The 13px text
29
13
  // is `foreground-secondary`: muted text on the muted fill is under 4.5:1.
@@ -45,10 +45,11 @@ const MASK = '••••••••';
45
45
 
46
46
  // The trailing buttons have the IdChip button look (`chipIconButtonClass`).
47
47
  // Each one is its own control (the chip has two), so its icon brightens on
48
- // its own hover, and its ::after widens the target to 21px.
48
+ // its own hover. Its ::after is a 24px target centred on the 13px icon
49
+ // (WCAG 2.5.8).
49
50
  const CHIP_BUTTON_CLASS = cn(
50
51
  chipIconButtonClass,
51
- 'relative after:absolute after:-inset-1 hover:text-foreground',
52
+ 'relative after:absolute after:top-1/2 after:left-1/2 after:size-6 after:-translate-1/2 hover:text-foreground',
52
53
  'hover:[&>svg:not([role=status])]:opacity-100',
53
54
  'hover:[&_[data-slot=copy-state-icon]:not([data-copied])>svg:first-child]:opacity-100',
54
55
  );
@@ -180,7 +181,9 @@ function SecretValue({
180
181
  onCopyError={onCopyError}
181
182
  copyLabel={copyLabel}
182
183
  copiedLabel={copiedLabel}
183
- className={CHIP_BUTTON_CLASS}
184
+ // 6px more than the chip gap puts the icon centres 25px apart, so
185
+ // the two 24px targets do not overlap.
186
+ className={cn(CHIP_BUTTON_CLASS, 'ms-1.5')}
184
187
  />
185
188
  ) : null}
186
189
  {state.status === 'error' ? (
@@ -8,25 +8,59 @@ import * as React from 'react';
8
8
  * first paint, or a label whose text changes, resizes the swap too.
9
9
  * `offsetWidth` ignores a scale on an ancestor (a tooltip's entrance, a
10
10
  * pressed card), which `getBoundingClientRect` would include.
11
+ *
12
+ * With `natural`, the width is the label's full text width, rounded up,
13
+ * also while the label is cut with an ellipsis: the swap keeps that width and
14
+ * its container shrinks it, so the cut never feeds back into the size.
11
15
  */
12
- export function useActiveLabelSize(active: number) {
16
+ export function useActiveLabelSize(
17
+ active: number,
18
+ { natural = false, key }: { natural?: boolean; key?: string } = {},
19
+ ) {
13
20
  const refs = React.useRef<(HTMLSpanElement | null)[]>([]);
14
21
  const [size, setSize] = React.useState<{ width: number; height: number }>();
22
+ // biome-ignore lint/correctness/useExhaustiveDependencies: `key` only re-runs the measure when the active label's text changes.
15
23
  React.useLayoutEffect(() => {
16
24
  const label = refs.current[active];
17
25
  if (!label) return;
18
26
  const measure = () => {
19
- const width = label.offsetWidth;
27
+ const width = natural ? naturalWidth(label) : label.offsetWidth;
20
28
  const height = label.offsetHeight;
21
29
  setSize((last) =>
22
30
  last?.width === width && last.height === height ? last : { width, height },
23
31
  );
24
32
  };
25
33
  measure();
26
- if (typeof ResizeObserver === 'undefined') return;
34
+ // A cut label's box does not grow when a web font arrives, so the
35
+ // ResizeObserver misses it: measure again when fonts load.
36
+ const fonts = natural && typeof document !== 'undefined' ? document.fonts : undefined;
37
+ fonts?.addEventListener('loadingdone', measure);
38
+ void fonts?.ready.then(measure);
39
+ if (typeof ResizeObserver === 'undefined') {
40
+ return () => fonts?.removeEventListener('loadingdone', measure);
41
+ }
27
42
  const observer = new ResizeObserver(measure);
28
43
  observer.observe(label);
29
- return () => observer.disconnect();
30
- }, [active]);
44
+ return () => {
45
+ observer.disconnect();
46
+ fonts?.removeEventListener('loadingdone', measure);
47
+ };
48
+ }, [active, natural, key]);
31
49
  return { refs, width: size?.width, height: size?.height };
32
50
  }
51
+
52
+ /**
53
+ * The full width of a label's text, also while it is cut, rounded up to a
54
+ * whole pixel: `scrollWidth` rounds down, and a text 0.4px wider than its
55
+ * box shows an ellipsis. A scale on an ancestor is taken out.
56
+ */
57
+ function naturalWidth(label: HTMLElement) {
58
+ const range = document.createRange();
59
+ range.selectNodeContents(label);
60
+ // The scale from the unrounded layout width: offsetWidth rounds, and a
61
+ // scale read from it moves the result by a pixel on every measure.
62
+ const box = label.getBoundingClientRect().width;
63
+ const layout = Number.parseFloat(getComputedStyle(label).width);
64
+ const scale = layout > 0 && box > 0 ? box / layout : 1;
65
+ return Math.max(label.offsetWidth, Math.ceil(range.getBoundingClientRect().width / scale - 0.01));
66
+ }
@@ -0,0 +1,26 @@
1
+ 'use client';
2
+
3
+ import { useSyncExternalStore } from 'react';
4
+
5
+ const subscribe = () => () => {};
6
+
7
+ // Apple platforms show ⌘, the rest show Ctrl. The server and the first
8
+ // hydration render have no platform, so they get false (Ctrl); React swaps in
9
+ // the client value right after hydration without a mismatch error.
10
+ function isApplePlatform() {
11
+ if (typeof navigator === 'undefined') return false;
12
+ const nav = navigator as Navigator & { userAgentData?: { platform?: string } };
13
+ const platform = nav.userAgentData?.platform || nav.platform || nav.userAgent;
14
+ return /mac|iphone|ipad|ipod/i.test(platform);
15
+ }
16
+
17
+ /**
18
+ * Whether the browser runs on an Apple platform, to label a shortcut with ⌘
19
+ * there and Ctrl elsewhere. False on the server and in the first hydration
20
+ * render.
21
+ */
22
+ function useIsApplePlatform() {
23
+ return useSyncExternalStore(subscribe, isApplePlatform, () => false);
24
+ }
25
+
26
+ export { useIsApplePlatform };
package/src/lib/utils.ts CHANGED
@@ -37,16 +37,9 @@ const twMerge = extendTailwindMerge<'text-style'>({
37
37
  'normal',
38
38
  'slow',
39
39
  'slower',
40
- 'palette-detail-delay',
41
- 'palette-result-delay',
42
- 'palette-detail-exit',
43
40
  'palette-exit',
44
- 'palette-tint',
45
- 'palette-result-exit',
46
41
  'palette-enter',
47
42
  'palette-move',
48
- 'palette-detail-move',
49
- 'palette-result-blur',
50
43
  'tooltip-enter',
51
44
  'tooltip-delay',
52
45
  'tooltip-label-fade',