@tristan2828/ui-foundation 4.112.0 → 4.114.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/README.md CHANGED
@@ -21,7 +21,7 @@ than installing this by hand:
21
21
  | `@tristan2828/ui-foundation` | `AppShell`, `DataTable`, `EntityView` (with `EditableValue`, `editInPlace`, printing its notes and `usePrinting`, `KeyStats`, `IconAction`), `EntityForm`, `Markdown` (with `countTasks`, `toggleTaskAt`), `RichTextEditor`, `ErrorState`, `MultiChoice`, `MultiReference`, `ListEditor`, `PasswordInput`, `RouteErrorBoundary`, `FoundationProviders`, `DataEnvironmentBanner`, `LoginRoute`, `RegisterRoute`, `useAuth`, `useTableUrlState`, `useRecordUpdate`, `useDebouncedValue`, `createQueryClient`, `cn`; types `Page`, `AppError`, `QuerySpec`. For timed screens (`docs/foundation/timed-screens.md`): `SessionView`, `useSessionClock`, `useWakeLock`, `useMuted`, `usePrefersReducedMotion`, and the audio cues `unlockAudio`, `chime`, `sequence`, `cancelCues`, `onCuesRecovered`, `cuesInSync`, `keepAudioAlive`, `say`, `hush`, `vibrate`, `canVibrate`. For the page standards (`docs/foundation/design-language.md` "Parts of the page standards"): `ValueList`, `ListHeader`, `Timeline`, `NeedsYou`, `Freshness`, `PartEmpty`. For a module's overview ("An overview page" there): `OverviewPage`, and `WidgetCard`'s `to`, `value`, `line` and `freshness`. For a list's tools (`docs/foundation/cell-patterns.md` 25 and 26): `FilterBar`, `FilterSelect`, `RelatedTable`, and DataTable's `views`, `columnsMenu`, `rowActions`, `phoneRow`, `limit` and `viewAll`. For reminders (`docs/foundation/reminders.md`): `AllowReminders`, `usePushPermission`. For a record opened in a dialog over its list (`docs/foundation/add-an-entity.md`): `RecordDialogRoute`, `RecordLink`, `useOpenRecord`, `useRecordDialog`, `recordDialogState`, and for a new record over it `useNewRecordDialog`. A small form in a dialog: `FormDialog`. For the dialogs and feedback standard (`docs/foundation/add-an-entity.md` "Toasts and feedback"): `DeleteAction`, `notify`, `useAction`, `ActionError`, `JobStatus`, `OfflineNotice`, `useOffline`; from `/ui/skeleton`, `Skeleton` takes `delay`. For the trail and search in `AppShell`'s bar (`docs/foundation/design-language.md` "The sidebar and the phone's bar"): `usePageTrail`, `useRecentRecord`, `PageTrail`; types `AppShellSearch`, `SearchRecord`, `RecentRecord`. For the form standard (`docs/foundation/add-an-entity.md` "Forms"): `FormSection`, `OptionSelect`, `ChoiceButtons`, `ReferenceCombobox`, `countFormErrors`, `fieldRows`, `parsePlainDate`, and for a record's rows edited in place `editRowClass`, `onEditRowClick` and `inPlace.choice`; from `/ui/field`, `Field` takes `size`, `solo` and `kind`, and `FieldLabel` takes `required`. From `/ui/dialog`, `DialogContent` takes `size` and `dirty`, and `DiscardChangesDialog` is the "Discard your changes?" prompt |
22
22
  | `@tristan2828/ui-foundation/ui/<name>` | shadcn primitives: badge, button, calendar, card, checkbox, combobox, dialog, empty, field, input, input-group, label, popover, select, separator, sheet, sidebar, skeleton, sonner, spinner, switch, table, textarea, tooltip |
23
23
  | `@tristan2828/ui-foundation/gateway` | `safeFetch`, `toAppError`, `networkError` — for an app's `src/api/gateway/` only |
24
- | `@tristan2828/ui-foundation/mocks` | MSW: `authHandlers`, `environmentHandlers`, `resetMockAuth`, `getMockCurrentUser`, `MOCK_USER`, `exposeMswForE2E` |
24
+ | `@tristan2828/ui-foundation/mocks` | MSW: `authHandlers`, `environmentHandlers`, `resetMockAuth`, `getMockCurrentUser`, `MOCK_USER`, `startMockWorker` (mock mode's boot), `exposeMswForE2E` |
25
25
  | `@tristan2828/ui-foundation/testing` | Playwright: `test` and `expect` (a page whose `goto()`/`reload()` wait for the app's boot; `withBootWait` for a page of your own), `defineA11ySuite`, `defineMockModeBannerSuite`, `defineDataEnvironmentBannerSuite`, `forceMswOverride`, `forceLoggedOut`, `waitForMswReady`, `expectDataViewsLoaded` |
26
26
  | `@tristan2828/ui-foundation/eslint` | the lint rules, as a flat config: `export default [...uiFoundation()]` |
27
27
  | `@tristan2828/ui-foundation/styles.css` | tokens and base styles, after `@import "tailwindcss";` |
@@ -37,7 +37,7 @@
37
37
  - [Picking at random](#picking-at-random) · lines 1713–1744
38
38
  - [Filling in what's missing](#filling-in-whats-missing) · lines 1746–1813
39
39
  - [Values edited in place: the presets' looks](#values-edited-in-place-the-presets-looks) · lines 1815–1840
40
- - [The sidebar and the phone's bar](#the-sidebar-and-the-phones-bar) · lines 1842–1931
40
+ - [The sidebar and the phone's bar](#the-sidebar-and-the-phones-bar) · lines 1842–1938
41
41
 
42
42
  <!-- /index -->
43
43
 
@@ -1874,8 +1874,15 @@ const bottomBar: BottomBarItem[] = [
1874
1874
  - **Counts** (`count`, `countLabel`): a plain number at the entry's end
1875
1875
  in the muted text tone, never a coloured badge; on the icon rail a dot
1876
1876
  in `--primary` on the icon. Only on entries that ask you to act
1877
- (unread, to fill in, new); 0 shows nothing. `countLabel` is what the
1878
- number counts, read after the entry's name ("News 24 unread").
1877
+ (unread, to fill in, new); 0 shows nothing, nor does a count still
1878
+ loading, and past 99 it reads "99+". `countLabel` is what the number
1879
+ counts, read after the entry's name ("News 24 unread"). On a phone
1880
+ (4.113) the bottom bar has no room at an item's end, so its count is
1881
+ a small `--primary` badge on the item's icon, ringed in the bar's
1882
+ background: a page's own count, or a section's sum of its entries'
1883
+ (with their `countLabel` when they share one). The sheet's tiles carry
1884
+ theirs in the corner and its list links at their end, as the sidebar
1885
+ does. Each is read after its name ("News, 7 new").
1879
1886
  - **A section at the foot** (`placement: 'bottom'`): under every other
1880
1887
  section and `sidebarExtra`, pushed to the sidebar's foot when there's
1881
1888
  room; for tools rather than modules.
@@ -1,4 +1,4 @@
1
- import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  // AppShell's bottom bar on a phone (#240): Home and a few of the nav's
3
3
  // sections in thumb reach, where the sidebar is a sheet behind the
4
4
  // header's menu button. An item is a link, or a nav section by its label,
@@ -7,16 +7,20 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
7
7
  // `sheet: 'tiles'` a grid of the entries as tiles, icon over label, like
8
8
  // a phone's home screen. The sheet closes on navigation, as the
9
9
  // sidebar's does. The item holding the current page is marked
10
- // (`aria-current`). Shown below the md breakpoint only (the sidebar's own
10
+ // (`aria-current`). An entry's `count` shows on the phone as it does in
11
+ // the sidebar: a badge on a bar item's icon (a page's own, or the sum of a
12
+ // section's), in a tile's corner, at a sheet link's end; nothing at 0 or
13
+ // while loading, "99+" past 99, and read after the label ("News, 7 new").
14
+ // Shown below the md breakpoint only (the sidebar's own
11
15
  // mobile breakpoint); the shell pads its content by the bar's height and
12
16
  // the phone's safe area, so a page's last row clears it.
13
17
  import { useId, useState } from 'react';
14
18
  import { Link, useLocation } from 'react-router';
15
- import { bottomBarCurrent, bottomBarSection, bottomBarTiles, childItems, childRuns, childrenOpen, isNavItemGroup, } from './app-shell-nav.js';
19
+ import { bottomBarCount, bottomBarCurrent, bottomBarSection, bottomBarTiles, childItems, childRuns, childrenOpen, isNavItemGroup, navCountSpoken, navCountText, } from './app-shell-nav.js';
16
20
  import { Sheet, SheetContent, SheetHeader, SheetTitle } from '../ui/sheet.js';
17
21
  const ITEM_CLASS = 'flex min-w-0 flex-1 flex-col items-center justify-center gap-0.5 rounded-md px-1 type-caption text-muted-foreground ' +
18
22
  'outline-none focus-visible:ring-3 focus-visible:ring-ring/50 aria-[current]:text-foreground aria-[current]:font-medium ' +
19
- '[&>svg]:size-5 [&>svg]:shrink-0';
23
+ '[&_svg]:size-5 [&_svg]:shrink-0';
20
24
  const SHEET_LINK_CLASS = 'flex min-h-11 items-center gap-3 rounded-md px-3 type-body text-foreground outline-none ' +
21
25
  'hover:bg-accent focus-visible:ring-3 focus-visible:ring-ring/50 aria-[current=page]:bg-accent aria-[current=page]:font-medium ' +
22
26
  '[&>svg]:size-4 [&>svg]:shrink-0 [&>svg]:text-muted-foreground';
@@ -25,13 +29,39 @@ const SHEET_LINK_CLASS = 'flex min-h-11 items-center gap-3 rounded-md px-3 type-
25
29
  * plus the bar (4rem and its 1px border) and the phone's safe area.
26
30
  */
27
31
  export const BOTTOM_BAR_PADDING = 'max-md:pb-[calc(--spacing(22)+1px+env(safe-area-inset-bottom))]';
32
+ // A link's label, and with a count its name: "News, 7 new". The name is
33
+ // one hidden text, the drawn label and count aria-hidden, so it reads
34
+ // exactly that (a hidden span beside the label would add a space before
35
+ // the comma).
36
+ function CountedLabel({ label, count, countLabel, className, }) {
37
+ const spoken = navCountSpoken(count, countLabel);
38
+ if (spoken === undefined)
39
+ return _jsx("span", { className: className, children: label });
40
+ return (_jsxs(_Fragment, { children: [_jsx("span", { "aria-hidden": "true", className: className, children: label }), _jsx("span", { className: "sr-only", children: `${label}, ${spoken}` })] }));
41
+ }
42
+ // A tile's count in its corner, as the sidebar's tiles have it.
43
+ // Its link's name reads it (CountedLabel).
44
+ function TileCount({ count }) {
45
+ const text = navCountText(count);
46
+ if (text === undefined)
47
+ return null;
48
+ return (_jsx("span", { "aria-hidden": "true", "data-slot": "nav-count", className: "absolute top-1 right-1.5 type-caption leading-none text-foreground tabular-nums", children: text }));
49
+ }
50
+ // A bar item's icon, with its count as a badge on its corner: the
51
+ // primary's fill, ringed in the bar's background so it stands off the
52
+ // icon. Sized in em, so it grows with the type at large density.
53
+ function BarIcon({ icon: Icon, count }) {
54
+ const text = navCountText(count);
55
+ return (_jsxs("span", { className: "relative inline-flex", children: [_jsx(Icon, { "aria-hidden": "true" }), text !== undefined && (_jsx("span", { "aria-hidden": "true", "data-slot": "bottom-bar-count", className: "absolute -top-1.5 left-[calc(100%-0.5rem)] inline-flex h-[1.5em] min-w-[1.5em] items-center justify-center rounded-full bg-primary px-[0.35em] type-caption leading-none font-medium text-primary-foreground tabular-nums ring-2 ring-background", children: text }))] }));
56
+ }
28
57
  // A tile: the entry's icon over its label, a finger's target at least.
29
- const TILE_CLASS = 'flex min-h-11 min-w-0 flex-col items-center justify-center gap-1 rounded-lg border border-border bg-background px-1.5 py-2.5 ' +
58
+ const TILE_CLASS = 'relative flex min-h-11 min-w-0 flex-col items-center justify-center gap-1 rounded-lg border border-border bg-background px-1.5 py-2.5 ' +
30
59
  'text-center type-caption text-foreground outline-none hover:bg-accent focus-visible:ring-3 focus-visible:ring-ring/50 ' +
31
60
  'aria-[current]:border-primary aria-[current]:font-medium [&>svg]:size-5 [&>svg]:shrink-0 [&>svg]:text-link';
32
61
  function SheetLink({ item, activeTo }) {
33
- const { to, label, icon: Icon, children } = item;
34
- return (_jsxs("li", { children: [_jsxs(Link, { to: to, "aria-current": to === activeTo ? 'page' : undefined, className: SHEET_LINK_CLASS, children: [_jsx(Icon, {}), _jsx("span", { className: "truncate", children: label })] }), children && children.length > 0 && (_jsx("div", { className: "ml-5 border-l border-border pl-2", children: _jsx(SheetChildren, { items: children, activeTo: activeTo }) }))] }));
62
+ const { to, label, icon: Icon, children, count, countLabel } = item;
63
+ const text = navCountText(count);
64
+ return (_jsxs("li", { children: [_jsxs(Link, { to: to, "aria-current": to === activeTo ? 'page' : undefined, className: SHEET_LINK_CLASS, children: [_jsx(Icon, {}), _jsx(CountedLabel, { label: label, count: count, countLabel: countLabel, className: "truncate" }), text !== undefined && (_jsx("span", { "aria-hidden": "true", "data-slot": "nav-count", className: "ml-auto shrink-0 type-caption text-muted-foreground tabular-nums", children: text }))] }), children && children.length > 0 && (_jsx("div", { className: "ml-5 border-l border-border pl-2", children: _jsx(SheetChildren, { items: children, activeTo: activeTo }) }))] }));
35
65
  }
36
66
  function SheetGroup({ label, items, activeTo }) {
37
67
  const headingId = useId();
@@ -42,7 +72,7 @@ function SheetChildren({ items, activeTo }) {
42
72
  }
43
73
  // A page tile, under a tiles section's tiles: smaller, four to a row,
44
74
  // still a finger's target.
45
- const PAGE_TILE_CLASS = 'flex min-h-11 min-w-0 flex-1 flex-col items-center justify-center gap-0.5 rounded-md px-1 py-1.5 text-center type-caption ' +
75
+ const PAGE_TILE_CLASS = 'relative flex min-h-11 min-w-0 flex-1 flex-col items-center justify-center gap-0.5 rounded-md px-1 py-1.5 text-center type-caption ' +
46
76
  'text-foreground outline-none hover:bg-accent focus-visible:ring-3 focus-visible:ring-ring/50 ' +
47
77
  'aria-[current=page]:bg-accent aria-[current=page]:font-medium [&>svg]:size-4 [&>svg]:shrink-0 [&>svg]:text-muted-foreground';
48
78
  // The current entry's pages under a tiles section's tiles (#6C): a
@@ -50,7 +80,7 @@ const PAGE_TILE_CLASS = 'flex min-h-11 min-w-0 flex-1 flex-col items-center just
50
80
  // own with its list still named by it.
51
81
  function SheetPageTiles({ items, activeTo }) {
52
82
  const open = items.filter((item) => childrenOpen(item, activeTo));
53
- return open.map((item) => (_jsx("div", { role: "group", "aria-label": `${item.label} pages`, "data-slot": "app-shell-sheet-pages", className: "flex flex-col", children: childRuns(item).map((run, runIndex) => (_jsxs("div", { className: "flex flex-col", children: [_jsx("div", { role: "separator", className: "my-2 h-px bg-border" }), _jsx("ul", { "aria-label": run.label, className: "grid grid-cols-4 gap-1.5", children: run.items.map(({ to, label, icon: Icon }) => (_jsx("li", { className: "flex min-w-0", children: _jsxs(Link, { to: to, "aria-current": to === activeTo ? 'page' : undefined, className: PAGE_TILE_CLASS, children: [_jsx(Icon, { "aria-hidden": "true" }), _jsx("span", { className: "max-w-full break-words", children: label })] }) }, to))) })] }, run.label ?? `run:${runIndex}`))) }, item.to)));
83
+ return open.map((item) => (_jsx("div", { role: "group", "aria-label": `${item.label} pages`, "data-slot": "app-shell-sheet-pages", className: "flex flex-col", children: childRuns(item).map((run, runIndex) => (_jsxs("div", { className: "flex flex-col", children: [_jsx("div", { role: "separator", className: "my-2 h-px bg-border" }), _jsx("ul", { "aria-label": run.label, className: "grid grid-cols-4 gap-1.5", children: run.items.map(({ to, label, icon: Icon, count, countLabel }) => (_jsx("li", { className: "flex min-w-0", children: _jsxs(Link, { to: to, "aria-current": to === activeTo ? 'page' : undefined, className: PAGE_TILE_CLASS, children: [_jsx(Icon, { "aria-hidden": "true" }), _jsx(CountedLabel, { label: label, count: count, countLabel: countLabel, className: "max-w-full break-words" }), _jsx(TileCount, { count: count })] }) }, to))) })] }, run.label ?? `run:${runIndex}`))) }, item.to)));
54
84
  }
55
85
  // The tiles (#N8): one per entry. Its pages are left to the module itself,
56
86
  // unless the section's look is tiles (#6C): then the current entry's are
@@ -58,10 +88,10 @@ function SheetPageTiles({ items, activeTo }) {
58
88
  // its pages, is marked.
59
89
  function SheetTiles({ items, activeTo, withPages, }) {
60
90
  return (_jsxs(_Fragment, { children: [_jsx("ul", { "data-slot": "app-shell-sheet-tiles", className: "grid grid-cols-3 gap-2", children: items.map((item) => {
61
- const { to, label, icon: Icon } = item;
91
+ const { to, label, icon: Icon, count, countLabel } = item;
62
92
  // "page" on the module's own page, "true" on one of its pages.
63
93
  const current = to === activeTo ? 'page' : childItems(item).some((child) => child.to === activeTo) ? 'true' : undefined;
64
- return (_jsx("li", { className: "flex min-w-0", children: _jsxs(Link, { to: to, "aria-current": current, className: `${TILE_CLASS} flex-1`, children: [_jsx(Icon, { "aria-hidden": "true" }), _jsx("span", { className: "max-w-full break-words", children: label })] }) }, to));
94
+ return (_jsx("li", { className: "flex min-w-0", children: _jsxs(Link, { to: to, "aria-current": current, className: `${TILE_CLASS} flex-1`, children: [_jsx(Icon, { "aria-hidden": "true" }), _jsx(CountedLabel, { label: label, count: count, countLabel: countLabel, className: "max-w-full break-words" }), _jsx(TileCount, { count: count })] }) }, to));
65
95
  }) }), withPages && _jsx(SheetPageTiles, { items: items, activeTo: activeTo })] }));
66
96
  }
67
97
  export function BottomBar({ items, nav, activeTo, navLook = 'list', }) {
@@ -80,12 +110,12 @@ export function BottomBar({ items, nav, activeTo, navLook = 'list', }) {
80
110
  // The current entry's pages show under the tiles when the section's own look is tiles.
81
111
  const withPages = (section?.look ?? navLook) === 'tiles';
82
112
  return (_jsxs(_Fragment, { children: [_jsx("nav", { "aria-label": "Bottom bar", "data-slot": "app-shell-bottom-bar", className: "fixed inset-x-0 bottom-0 z-20 border-t border-border bg-background pb-[env(safe-area-inset-bottom)] md:hidden", children: _jsx("ul", { className: "flex h-16 items-stretch gap-1 px-2 py-1.5", children: shown.map((item, index) => {
83
- const Icon = item.icon;
84
113
  const isCurrent = index === current;
114
+ const { count, countLabel } = bottomBarCount(item, nav);
85
115
  if ('to' in item) {
86
- return (_jsx("li", { className: "flex min-w-0 flex-1", children: _jsxs(Link, { to: item.to, "aria-current": isCurrent ? 'page' : undefined, className: ITEM_CLASS, children: [_jsx(Icon, { "aria-hidden": "true" }), _jsx("span", { className: "max-w-full truncate", children: item.label })] }) }, `to:${item.to}`));
116
+ return (_jsx("li", { className: "flex min-w-0 flex-1", children: _jsxs(Link, { to: item.to, "aria-current": isCurrent ? 'page' : undefined, className: ITEM_CLASS, children: [_jsx(BarIcon, { icon: item.icon, count: count }), _jsx(CountedLabel, { label: item.label, count: count, countLabel: countLabel, className: "max-w-full truncate" })] }) }, `to:${item.to}`));
87
117
  }
88
118
  const label = item.label ?? item.group;
89
- return (_jsx("li", { className: "flex min-w-0 flex-1", children: _jsxs("button", { type: "button", "aria-haspopup": "dialog", "aria-expanded": openGroup === item.group, "aria-current": isCurrent ? 'true' : undefined, onClick: () => setOpenGroup(item.group), className: ITEM_CLASS, children: [_jsx(Icon, { "aria-hidden": "true" }), _jsx("span", { className: "max-w-full truncate", children: label })] }) }, `group:${item.group}`));
119
+ return (_jsx("li", { className: "flex min-w-0 flex-1", children: _jsxs("button", { type: "button", "aria-haspopup": "dialog", "aria-expanded": openGroup === item.group, "aria-current": isCurrent ? 'true' : undefined, onClick: () => setOpenGroup(item.group), className: ITEM_CLASS, children: [_jsx(BarIcon, { icon: item.icon, count: count }), _jsx(CountedLabel, { label: label, count: count, countLabel: countLabel, className: "max-w-full truncate" })] }) }, `group:${item.group}`));
90
120
  }) }) }), _jsx(Sheet, { open: section !== undefined, onOpenChange: (open) => !open && setOpenGroup(null), children: _jsx(SheetContent, { side: "bottom", className: "max-h-[80dvh] gap-2 pb-[calc(--spacing(4)+env(safe-area-inset-bottom))] md:hidden", children: section && (_jsxs(_Fragment, { children: [_jsx(SheetHeader, { className: "pb-0", children: _jsx(SheetTitle, { children: section.label }) }), _jsx("nav", { "aria-label": section.label, className: "overflow-y-auto px-2", children: tiles ? (_jsx(SheetTiles, { items: section.items, activeTo: activeTo, withPages: withPages })) : (_jsx(SheetChildren, { items: section.items, activeTo: activeTo })) })] })) }) })] }));
91
121
  }
@@ -220,3 +220,23 @@ export declare function bottomBarSection(nav: readonly NavEntry[], label: string
220
220
  * the page's own link, or else the section it's in, children included.
221
221
  */
222
222
  export declare function bottomBarCurrent(items: readonly BottomBarItem[], nav: readonly NavEntry[], activeTo: string | undefined): number | undefined;
223
+ /**
224
+ * A count as drawn: nothing at 0, below it or while it's loading (no
225
+ * count yet), and "99+" past 99, so a badge keeps its size.
226
+ */
227
+ export declare function navCountText(count: number | undefined): string | undefined;
228
+ /**
229
+ * A count as read, after its entry's label: "7 new" with its
230
+ * `countLabel`, the number alone without. The real number, past 99 too.
231
+ * Nothing where nothing is drawn.
232
+ */
233
+ export declare function navCountSpoken(count: number | undefined, countLabel?: string): string | undefined;
234
+ /**
235
+ * A bottom bar item's count: a page's, from its nav entry, or a
236
+ * section's, the sum of its entries' and their pages'. A section's
237
+ * `countLabel` is its entries' when they share one, and none otherwise.
238
+ */
239
+ export declare function bottomBarCount(item: BottomBarItem, nav: readonly NavEntry[]): {
240
+ count?: number;
241
+ countLabel?: string;
242
+ };
@@ -215,3 +215,41 @@ export function bottomBarCurrent(items, nav, activeTo) {
215
215
  const group = items.findIndex((item) => 'group' in item && withChildren(bottomBarSection(nav, item.group)?.items ?? []).some((entry) => entry.to === activeTo));
216
216
  return group === -1 ? undefined : group;
217
217
  }
218
+ /**
219
+ * A count as drawn: nothing at 0, below it or while it's loading (no
220
+ * count yet), and "99+" past 99, so a badge keeps its size.
221
+ */
222
+ export function navCountText(count) {
223
+ if (count === undefined || !Number.isFinite(count) || count <= 0)
224
+ return undefined;
225
+ return count > 99 ? '99+' : String(Math.floor(count));
226
+ }
227
+ /**
228
+ * A count as read, after its entry's label: "7 new" with its
229
+ * `countLabel`, the number alone without. The real number, past 99 too.
230
+ * Nothing where nothing is drawn.
231
+ */
232
+ export function navCountSpoken(count, countLabel) {
233
+ if (navCountText(count) === undefined)
234
+ return undefined;
235
+ const n = Math.floor(count);
236
+ return countLabel ? `${n} ${countLabel}` : String(n);
237
+ }
238
+ /**
239
+ * A bottom bar item's count: a page's, from its nav entry, or a
240
+ * section's, the sum of its entries' and their pages'. A section's
241
+ * `countLabel` is its entries' when they share one, and none otherwise.
242
+ */
243
+ export function bottomBarCount(item, nav) {
244
+ if ('to' in item) {
245
+ const entry = navItems(nav).find((other) => other.to === item.to);
246
+ return entry ? { count: entry.count, countLabel: entry.countLabel } : {};
247
+ }
248
+ const counted = withChildren(bottomBarSection(nav, item.group)?.items ?? []).filter((entry) => navCountText(entry.count) !== undefined);
249
+ if (!counted.length)
250
+ return {};
251
+ const count = counted.reduce((sum, entry) => sum + Math.floor(entry.count), 0);
252
+ const labels = new Set(counted.map((entry) => entry.countLabel));
253
+ const [only] = labels;
254
+ return { count, countLabel: labels.size === 1 ? only : undefined };
255
+ }
@@ -5,7 +5,7 @@ import { useCallback, useEffect, useId, useMemo, useState } from 'react';
5
5
  import { Link, Navigate, Outlet, useLocation } from 'react-router';
6
6
  import { useAuth } from '../../auth/use-auth.js';
7
7
  import { BottomBar, BOTTOM_BAR_PADDING } from './app-shell-bottom-bar.js';
8
- import { activeNavTo, childItems, childRuns, childrenOpen, isNavItemGroup, navGroups, navPageLabel, navTooltip, navTrail, trailBackTarget, withChildren, } from './app-shell-nav.js';
8
+ import { activeNavTo, childItems, childRuns, childrenOpen, isNavItemGroup, navCountSpoken, navCountText, navGroups, navPageLabel, navTooltip, navTrail, trailBackTarget, withChildren, } from './app-shell-nav.js';
9
9
  import { versionHref, versionLabel } from './app-shell-version.js';
10
10
  import { ErrorState } from './error-state.js';
11
11
  import { PageTrail } from './page-trail.js';
@@ -98,9 +98,10 @@ const ACTIVE_MARK = 'data-active:shadow-[inset_3px_0_0_0_var(--primary)]';
98
98
  // An entry's count (#N3): a plain number at its end in the muted tone,
99
99
  // read with what it counts; on the icon rail, a dot on the icon.
100
100
  function NavCount({ count, countLabel }) {
101
- if (!count)
101
+ const text = navCountText(count);
102
+ if (text === undefined)
102
103
  return null;
103
- return (_jsxs(_Fragment, { children: [_jsx("span", { "aria-hidden": "true", "data-slot": "nav-count", className: "ml-auto shrink-0 type-caption text-muted-foreground tabular-nums group-data-[collapsible=icon]:hidden", children: count }), _jsx("span", { className: "sr-only", children: countLabel ? `${count} ${countLabel}` : count }), _jsx("span", { "aria-hidden": "true", "data-slot": "nav-count-dot", className: "pointer-events-none absolute top-1 right-1 hidden size-1.5 rounded-full bg-primary group-data-[collapsible=icon]:block" })] }));
104
+ return (_jsxs(_Fragment, { children: [_jsx("span", { "aria-hidden": "true", "data-slot": "nav-count", className: "ml-auto shrink-0 type-caption text-muted-foreground tabular-nums group-data-[collapsible=icon]:hidden", children: text }), _jsx("span", { className: "sr-only", children: navCountSpoken(count, countLabel) }), _jsx("span", { "aria-hidden": "true", "data-slot": "nav-count-dot", className: "pointer-events-none absolute top-1 right-1 hidden size-1.5 rounded-full bg-primary group-data-[collapsible=icon]:block" })] }));
104
105
  }
105
106
  function NavSubLink({ item, activeTo }) {
106
107
  const { to, label, icon: Icon, count, countLabel } = item;
@@ -144,7 +145,7 @@ function NavTile({ item, activeTo }) {
144
145
  const { to, label, icon: Icon, count, countLabel } = item;
145
146
  // "page" on the entry's own page, "true" on one of its pages.
146
147
  const current = to === activeTo ? 'page' : childItems(item).some((child) => child.to === activeTo) ? 'true' : undefined;
147
- return (_jsx("li", { className: "flex min-w-0", children: _jsxs(Link, { to: to, "aria-current": current, "data-slot": "nav-tile", "data-active": current ? '' : undefined, className: NAV_TILE_CLASS, children: [_jsx(Icon, { "aria-hidden": "true" }), _jsx("span", { className: "max-w-full break-words", children: label }), count ? (_jsxs(_Fragment, { children: [_jsx("span", { "aria-hidden": "true", "data-slot": "nav-count", className: "absolute top-1 right-1.5 type-caption leading-none text-sidebar-foreground tabular-nums", children: count }), _jsx("span", { className: "sr-only", children: countLabel ? `${count} ${countLabel}` : count })] })) : null] }) }));
148
+ return (_jsx("li", { className: "flex min-w-0", children: _jsxs(Link, { to: to, "aria-current": current, "data-slot": "nav-tile", "data-active": current ? '' : undefined, className: NAV_TILE_CLASS, children: [_jsx(Icon, { "aria-hidden": "true" }), _jsx("span", { className: "max-w-full break-words", children: label }), navCountText(count) !== undefined ? (_jsxs(_Fragment, { children: [_jsx("span", { "aria-hidden": "true", "data-slot": "nav-count", className: "absolute top-1 right-1.5 type-caption leading-none text-sidebar-foreground tabular-nums", children: navCountText(count) }), _jsx("span", { className: "sr-only", children: navCountSpoken(count, countLabel) })] })) : null] }) }));
148
149
  }
149
150
  // A group in the tiles look (#6C): its entries as a three-column grid,
150
151
  // its label for screen readers only (the grid is a list named by it), and
@@ -3,4 +3,5 @@ export { clientErrorHandlers, getMockClientErrors, resetMockClientErrors } from
3
3
  export { environmentHandlers } from './environment.js';
4
4
  export { getMockPushSubscriptions, MOCK_PUSH_PUBLIC_KEY, pushHandlers, resetMockPush } from './push.js';
5
5
  export { exposeMswForE2E } from './e2e-hooks.js';
6
+ export { MOCK_PROBE_PATH, startMockWorker } from './start-worker.js';
6
7
  export type { MswHandle, MswOverride } from './override.js';
@@ -6,3 +6,4 @@ export { clientErrorHandlers, getMockClientErrors, resetMockClientErrors } from
6
6
  export { environmentHandlers } from './environment.js';
7
7
  export { getMockPushSubscriptions, MOCK_PUSH_PUBLIC_KEY, pushHandlers, resetMockPush } from './push.js';
8
8
  export { exposeMswForE2E } from './e2e-hooks.js';
9
+ export { MOCK_PROBE_PATH, startMockWorker } from './start-worker.js';
@@ -0,0 +1,10 @@
1
+ import type { SetupWorker, StartOptions } from 'msw/browser';
2
+ /** Answered only by the handler startMockWorker adds for the probe. */
3
+ export declare const MOCK_PROBE_PATH = "/__ui-foundation-msw-probe";
4
+ /**
5
+ * Starts MSW for an app's mock mode. Resolves once requests are answered by
6
+ * the mocks, with the worker exposed for Playwright (`exposeMswForE2E`).
7
+ * Render the app only after it resolves. `options` go to `worker.start()`;
8
+ * unhandled requests are bypassed unless they say otherwise.
9
+ */
10
+ export declare function startMockWorker(worker: SetupWorker, options?: StartOptions): Promise<void>;
@@ -0,0 +1,77 @@
1
+ // Mock mode's boot, shared by every app: starts the MSW worker and returns
2
+ // only once the page's requests really reach the mocks, then exposes the
3
+ // worker for Playwright (./e2e-hooks.ts).
4
+ //
5
+ // Two waits after worker.start():
6
+ //
7
+ // 1. The controller. On a first visit start() can resolve before the new
8
+ // service worker controls the page, and a request made in that gap goes
9
+ // to the network.
10
+ // 2. A probe. Being controlled still isn't enough: MSW's worker mocks only
11
+ // for the clients in its in-memory list, which the page joins with a
12
+ // MOCK_ACTIVATE message during start(). A worker the browser stops and
13
+ // starts again (it may, under load, at any time) has forgotten that list
14
+ // and passes every request through, so the app's first queries came back
15
+ // from the network (no `main`, "data is undefined"). So one request that
16
+ // only a runtime handler answers is made first; until it comes back
17
+ // mocked, the page joins the list again.
18
+ import { http, HttpResponse } from 'msw';
19
+ import { exposeMswForE2E } from './e2e-hooks.js';
20
+ /** Answered only by the handler startMockWorker adds for the probe. */
21
+ export const MOCK_PROBE_PATH = '/__ui-foundation-msw-probe';
22
+ const PROBE_ANSWER = 'mocked';
23
+ const PROBE_TRIES = 20;
24
+ const PROBE_PAUSE_MS = 100;
25
+ /**
26
+ * Starts MSW for an app's mock mode. Resolves once requests are answered by
27
+ * the mocks, with the worker exposed for Playwright (`exposeMswForE2E`).
28
+ * Render the app only after it resolves. `options` go to `worker.start()`;
29
+ * unhandled requests are bypassed unless they say otherwise.
30
+ */
31
+ export async function startMockWorker(worker, options = {}) {
32
+ await worker.start({ onUnhandledRequest: 'bypass', ...options });
33
+ await untilControlled();
34
+ await untilMocked(worker);
35
+ // After the probe's resetHandlers(), so a spec's pre-navigation override
36
+ // (applied here) stays in place.
37
+ exposeMswForE2E(worker);
38
+ }
39
+ // Bounded: a hard reload leaves the page uncontrolled for its whole life,
40
+ // and then the probe below says so instead of the boot hanging.
41
+ const CONTROLLER_WAIT_MS = 3000;
42
+ function untilControlled() {
43
+ if (navigator.serviceWorker.controller)
44
+ return Promise.resolve();
45
+ return new Promise((resolve) => {
46
+ const timer = setTimeout(resolve, CONTROLLER_WAIT_MS);
47
+ navigator.serviceWorker.addEventListener('controllerchange', () => {
48
+ clearTimeout(timer);
49
+ resolve();
50
+ }, { once: true });
51
+ });
52
+ }
53
+ async function untilMocked(worker) {
54
+ worker.use(http.get(MOCK_PROBE_PATH, () => HttpResponse.text(PROBE_ANSWER)));
55
+ try {
56
+ for (let attempt = 0; attempt < PROBE_TRIES; attempt += 1) {
57
+ if ((await probe()) === PROBE_ANSWER)
58
+ return;
59
+ // The worker doesn't know this page: join its client list again.
60
+ navigator.serviceWorker.controller?.postMessage('MOCK_ACTIVATE');
61
+ await new Promise((resolve) => setTimeout(resolve, PROBE_PAUSE_MS));
62
+ }
63
+ console.error(`[ui-foundation] The mock service worker still passes this page's requests to the network after ${PROBE_TRIES} tries. ` +
64
+ 'API calls may not be mocked; reload the page.');
65
+ }
66
+ finally {
67
+ // The probe's handler goes: the runtime handlers are the specs' alone.
68
+ worker.resetHandlers();
69
+ }
70
+ }
71
+ function probe() {
72
+ // The one bare fetch in mocks (eslint.config.js): it tests the worker
73
+ // itself, before any app code runs, and never reaches a server.
74
+ return fetch(MOCK_PROBE_PATH, { cache: 'no-store' })
75
+ .then((response) => (response.ok ? response.text() : ''))
76
+ .catch(() => '');
77
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tristan2828/ui-foundation",
3
- "version": "4.112.0",
3
+ "version": "4.114.0",
4
4
  "description": "Shared UI foundation for contract-first, database-backed React apps: shadcn primitives, DataTable/EntityForm composites, auth, the gateway error seam, design tokens, lint rules and the AI playbook.",
5
5
  "license": "MIT",
6
6
  "repository": {