basalt-ui 2.13.0 → 2.13.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.
@@ -41,10 +41,10 @@ import type { GlobalAction } from '../controls/actions'
41
41
  import type { AccountMenuItem, BasaltAccountProps } from './account-types'
42
42
  import type { SidebarSearchActions, SidebarSearchConfig } from './sidebar-search'
43
43
  import type {
44
+ ActiveCrumb,
44
45
  MobileNavConfig,
45
46
  NavAnchor,
46
47
  SidebarBlock,
47
- SidebarItem,
48
48
  SidebarSection,
49
49
  } from './nav-types'
50
50
  import { CtlSlot, useBasaltSpacing } from '../theme'
@@ -167,7 +167,8 @@ export type ShellBaseProps = BasaltProps & {
167
167
  * the rest fold into the header's single kebab (`mobile: 'bar' | 'more' | 'hidden'`, see
168
168
  * `GlobalAction`). That kebab is the SAME one `PageBar`'s ROW 1 opens — a route whose bar has
169
169
  * row-1 actions (or `filtersEnd`) lends it, any other route gets it from the shell. An
170
- * `ActionGroup` a consumer mounts in some other home never inherits these rows.
170
+ * `ActionGroup` a consumer mounts in some other home never inherits these rows. Keep the array
171
+ * stable (module scope or memoized): a new one every render re-derives the header and kebab.
171
172
  */
172
173
  globalActions?: GlobalAction[]
173
174
  /**
@@ -255,58 +256,20 @@ export type BasaltShellProps<
255
256
  /**
256
257
  * The `defineNav` definition — the ONE nav, resolved against the router here: the sidebar, the
257
258
  * mobile bar (its tuning rides `defineNav`'s `mobile`) and the breadcrumb all derive from it.
258
- * Alongside it: `badges` (a `number` becomes `item.count`, anything else `item.badge`; hoist or
259
- * memoize the object), `isActive` (override active detection) and `getScrollElement` (the
260
- * mobile bar's scroll container).
259
+ * Alongside it: `badges` (a `number` becomes `item.count`, anything else `item.badge`; an
260
+ * inline `{ … }` literal is fine for numbers and strings, but a JSX `ReactNode` value is a new
261
+ * element every render and rebuilds the sections — memoize it), `isActive` (override active
262
+ * detection) and `getScrollElement` (the mobile bar's scroll container).
263
+ *
264
+ * Define `nav` at module scope: its anchors are cached per config object, so a `defineNav`
265
+ * call inside a component is a new config every render and remounts the links again.
266
+ * `isActive` must likewise be a module-scope or memoized function; a new one every render
267
+ * rebuilds the sections.
261
268
  */
262
269
  nav: NavConfig<G, T>
263
270
  }
264
271
 
265
- /** Active nav item across all sections → the breadcrumb's `{ section, parent?, …, page }`. */
266
- function findActiveCrumb(sections: SidebarSection[]): ActiveCrumb | undefined {
267
- for (const section of sections) {
268
- const found = findActiveWithParent(section.items)
269
- if (found)
270
- return {
271
- section: section.label,
272
- parent: found.parent,
273
- parentAnchor: found.parentAnchor,
274
- parentHref: found.parentHref,
275
- page: found.page,
276
- }
277
- }
278
- return undefined
279
- }
280
-
281
- type ActiveCrumb = {
282
- section: string
283
- parent?: string | undefined
284
- parentAnchor?: NavAnchor | undefined
285
- parentHref?: string | undefined
286
- page: string
287
- }
288
-
289
- /**
290
- * Recursively search for the deepest active item, returning the parent's label and its ROUTER
291
- * ANCHOR (not just an href) when nested — the crumb navigates client-side through the same seam
292
- * every nav row uses, so no second render callback is needed for it.
293
- */
294
- function findActiveWithParent(
295
- items: SidebarItem[],
296
- parentLabel?: string | undefined,
297
- parentAnchor?: NavAnchor | undefined,
298
- parentHref?: string | undefined,
299
- ): Omit<ActiveCrumb, 'section'> | undefined {
300
- for (const item of items) {
301
- // Recurse into children first — deeper active match wins over a prefix-matched parent.
302
- if (item.children) {
303
- const found = findActiveWithParent(item.children, item.label, item.Anchor, item.href)
304
- if (found) return found
305
- }
306
- if (item.active) return { parent: parentLabel, parentAnchor, parentHref, page: item.label }
307
- }
308
- return undefined
309
- }
272
+ const NO_ACTIONS: readonly GlobalAction[] = []
310
273
 
311
274
  /** Envelope version for the persisted collapse flag. Bump only if the value stops being a boolean. */
312
275
  const COLLAPSE_VERSION = 1
@@ -347,12 +310,14 @@ export function BasaltShell<
347
310
  >(props: BasaltShellProps<G, T>) {
348
311
  assertRequiredProps('BasaltShell', props, ['brand', 'nav'])
349
312
  const { nav, badges, isActive, getScrollElement, ...rest } = props
350
- const { sections, mobileNav } = useNav(nav, {
313
+ const { sections, mobileNav, activeCrumb } = useNav(nav, {
351
314
  ...(badges !== undefined && { badges }),
352
315
  ...(isActive !== undefined && { isActive }),
353
316
  ...(getScrollElement !== undefined && { getScrollElement }),
354
317
  })
355
- return <ShellFrame {...rest} sections={sections} mobileNav={mobileNav} />
318
+ return (
319
+ <ShellFrame {...rest} sections={sections} mobileNav={mobileNav} activeCrumb={activeCrumb} />
320
+ )
356
321
  }
357
322
 
358
323
  /**
@@ -365,6 +330,8 @@ export type ShellFrameProps = ShellBaseProps & {
365
330
  sections: SidebarSection[]
366
331
  /** Mobile bar tuning; every field optional (see `projectMobileNav`'s zero-config fallback). */
367
332
  mobileNav?: MobileNavConfig
333
+ /** The breadcrumb for the active destination, resolved by the nav resolver. */
334
+ activeCrumb: ActiveCrumb | undefined
368
335
  }
369
336
 
370
337
  /**
@@ -384,7 +351,7 @@ export type ShellFrameProps = ShellBaseProps & {
384
351
  */
385
352
  export function ShellFrame(props: ShellFrameProps) {
386
353
  return (
387
- <PageBarProvider globalActions={props.globalActions ?? []}>
354
+ <PageBarProvider globalActions={props.globalActions ?? NO_ACTIONS}>
388
355
  <AsideProvider>
389
356
  <ShellBody {...props} />
390
357
  </AsideProvider>
@@ -396,6 +363,7 @@ function ShellBody({
396
363
  brand,
397
364
  sections,
398
365
  mobileNav,
366
+ activeCrumb,
399
367
  globalActions,
400
368
  sidebarBlocks,
401
369
  settingsMenuItems,
@@ -495,7 +463,6 @@ function ShellBody({
495
463
  return () => registerSidebarToggle(null)
496
464
  }, [toggleCollapse])
497
465
 
498
- const activeCrumb = findActiveCrumb(sections)
499
466
  // The account row, every settings entry and every mobile-reachable sidebar block render as flat
500
467
  // rows in the More surface (there is no mobile sidebar to reach them through any more), so they
501
468
  // count toward BOTH `needsMore` and the menu-vs-sheet threshold. `accountRowCount` and
@@ -569,7 +536,7 @@ function ShellBody({
569
536
  <AppBreadcrumbs {...activeCrumb} />
570
537
  </div>
571
538
  <PageBarOutlet className={headerClasses.pageBar} />
572
- <HeaderGlobalActions actions={globalActions ?? []} />
539
+ <HeaderGlobalActions actions={globalActions ?? NO_ACTIONS} />
573
540
  </div>
574
541
  </AppShell.Header>
575
542
 
@@ -222,3 +222,11 @@ export type MobileNavConfig = {
222
222
  */
223
223
  getScrollElement?: () => HTMLElement | null
224
224
  }
225
+
226
+ /** The breadcrumb for the one active destination (the nav resolver builds it; the header renders it). */
227
+ export type ActiveCrumb = {
228
+ section: string
229
+ parent?: string | undefined
230
+ parentAnchor?: NavAnchor | undefined
231
+ page: string
232
+ }
@@ -54,6 +54,8 @@ import {
54
54
  useCallback,
55
55
  useContext,
56
56
  useDeferredValue,
57
+ useEffect,
58
+ useId,
57
59
  useMemo,
58
60
  useRef,
59
61
  useState,
@@ -81,8 +83,10 @@ type AsideRegion = {
81
83
  claimed: boolean
82
84
  /** The claiming page's fold state, published back so the shell can size the region. */
83
85
  folded: boolean
84
- claim: () => () => void
85
- publishFolded: (folded: boolean) => void
86
+ /** Registers the claimant `id`; the returned release ignores everything but that id. */
87
+ claim: (id: string) => () => void
88
+ /** Publishes `id`'s fold. Only the current owner's counts (see `AsideProvider`). */
89
+ publishFolded: (id: string, folded: boolean) => void
86
90
  /**
87
91
  * Bumped by `requestClose` — never 0 again once a close has been requested, so `PageAside` can
88
92
  * treat "changed since mount" as "fold now" without a ref of its own. `ShellBody` is the one
@@ -113,32 +117,60 @@ const AsideContext = createContext<AsideRegion>(NO_REGION)
113
117
  */
114
118
  export function AsideProvider({ children }: { children: ReactNode }): ReactNode {
115
119
  const [target, setTarget] = useState<HTMLElement | null>(null)
116
- const [claims, setClaims] = useState(0)
117
- const [folded, setFolded] = useState(false)
120
+ // Claimants in claim order; the OWNER is the last one (the most recently committed claim, which
121
+ // during a route transition is the incoming aside). Folds are stored per claimant and the region
122
+ // reads the owner's entry only. One state object, so a release drops id and fold together.
123
+ const [{ ids, folds }, setClaims] = useState<{
124
+ ids: readonly string[]
125
+ folds: Readonly<Record<string, boolean>>
126
+ }>({ ids: [], folds: {} })
118
127
  const [closeSignal, setCloseSignal] = useState(0)
119
128
 
120
- const claim = useCallback(() => {
121
- setClaims((n) => n + 1)
122
- return () => {
123
- setClaims((n) => n - 1)
124
- // A route that leaves folded must not hand the next one a folded, unclaimed region.
125
- setFolded(false)
126
- }
129
+ const claim = useCallback((id: string) => {
130
+ setClaims((s) => ({ ...s, ids: [...s.ids, id] }))
131
+ return () =>
132
+ setClaims((s) => ({
133
+ ids: s.ids.filter((x) => x !== id),
134
+ folds: Object.fromEntries(Object.entries(s.folds).filter(([k]) => k !== id)),
135
+ }))
136
+ }, [])
137
+
138
+ // An id that is not (or no longer) a claimant publishes nothing, so a late publish cannot
139
+ // resurrect the fold of an aside that already released.
140
+ const publishFolded = useCallback((id: string, folded: boolean) => {
141
+ setClaims((s) =>
142
+ !s.ids.includes(id) || s.folds[id] === folded
143
+ ? s
144
+ : { ...s, folds: { ...s.folds, [id]: folded } },
145
+ )
127
146
  }, [])
128
147
 
129
148
  const requestClose = useCallback(() => setCloseSignal((n) => n + 1), [])
130
149
 
131
- const value: AsideRegion = {
132
- target,
133
- setTarget,
134
- inShell: true,
135
- claimed: claims > 0,
136
- folded,
137
- claim,
138
- publishFolded: setFolded,
139
- closeSignal,
140
- requestClose,
141
- }
150
+ useEffect(() => {
151
+ if (process.env.NODE_ENV === 'production' || ids.length < 2) return
152
+ console.warn(
153
+ '[basalt] more than one PageAside is mounted at once; the newest claimant owns the region. Render one per page.',
154
+ )
155
+ }, [ids.length])
156
+
157
+ const owner = ids.at(-1)
158
+ const folded = owner !== undefined && folds[owner] === true
159
+
160
+ const value = useMemo<AsideRegion>(
161
+ () => ({
162
+ target,
163
+ setTarget,
164
+ inShell: true,
165
+ claimed: owner !== undefined,
166
+ folded,
167
+ claim,
168
+ publishFolded,
169
+ closeSignal,
170
+ requestClose,
171
+ }),
172
+ [target, owner, folded, claim, publishFolded, closeSignal, requestClose],
173
+ )
142
174
 
143
175
  return <AsideContext.Provider value={value}>{children}</AsideContext.Provider>
144
176
  }
@@ -257,6 +289,7 @@ export function PageAside(props: PageAsideProps): ReactNode {
257
289
  const { title, persistKey, defaultFolded, children, className, style, classNames } = props
258
290
  const { target, inShell, claim, publishFolded, closeSignal } = useContext(AsideContext)
259
291
  const docks = useContext(AsideDocksContext)
292
+ const id = useId()
260
293
  // Destructured, not held as one object: the hook returns a fresh literal every render, and the
261
294
  // claim effect below would then re-run (claim → release → claim) forever. `claimPanel` is a
262
295
  // `useCallback` on the provider, so it is the stable half.
@@ -309,13 +342,13 @@ export function PageAside(props: PageAsideProps): ReactNode {
309
342
 
310
343
  useIsomorphicLayoutEffect(() => {
311
344
  if (!portalled) return
312
- return claim()
313
- }, [portalled, claim])
345
+ return claim(id)
346
+ }, [portalled, claim, id])
314
347
 
315
348
  useIsomorphicLayoutEffect(() => {
316
349
  if (!portalled) return
317
- publishFolded(folded)
318
- }, [portalled, folded, publishFolded])
350
+ publishFolded(id, folded)
351
+ }, [portalled, folded, publishFolded, id])
319
352
 
320
353
  // The Escape hatch (`shell/index.tsx`'s `asideOverlay` effect): `closeSignal` is monotonic and
321
354
  // shared across every `PageAside` the shell ever mounts, so "past the initial 0" is the wrong
@@ -22,7 +22,7 @@
22
22
  * Originally extracted from argo's `apps/dashboard/src/components/app-shell/page-header.tsx`;
23
23
  * linewatch's `page-header.tsx` measure-and-publish effect became the framework behaviour below.
24
24
  */
25
- import { createContext, useCallback, useContext, useEffect, useRef, useState } from 'react'
25
+ import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'
26
26
  import type { ReactNode } from 'react'
27
27
  import { createPortal } from 'react-dom'
28
28
  import { cx } from '../common/props'
@@ -50,7 +50,7 @@ export const PAGE_BAR_HEIGHT_VAR = '--basalt-page-bar-h'
50
50
  * What a `PageAside` publishes when it projects itself into row 2 below `sm`
51
51
  * (`docs/ASIDE-SPEC.md` §0 "Desktop and mobile are one declaration"). Metadata only — the aside's
52
52
  * CHILDREN never travel through this context: they are portalled into the sheet's own node
53
- * ({@link PageBarSlots.panelTarget}), because a `ReactNode` in state would be a fresh object on
53
+ * ({@link AsidePanelSlot.panelTarget}), because a `ReactNode` in state would be a fresh object on
54
54
  * every parent render and the claim effect would loop.
55
55
  */
56
56
  export type AsidePanelClaim = {
@@ -60,49 +60,36 @@ export type AsidePanelClaim = {
60
60
  readonly icon: ReactNode
61
61
  }
62
62
 
63
- type PageBarSlots = {
63
+ /** Narrow, memoized contexts: a reader of one is not re-rendered by a change to another. */
64
+ type PortalTargets = {
64
65
  /** The header node row 1 portals into. `null` outside a shell, and until the outlet mounts. */
65
66
  target: HTMLElement | null
66
67
  setTarget: (el: HTMLElement | null) => void
67
- /**
68
- * The BAND node row 2 portals into — a shell-owned box between the header and the scrollport.
69
- * Same mechanism as `target` one region down, and it exists for the same reason: where a row
70
- * renders must not depend on where the consumer wrote `<PageBar>` in its page tree. Row 2 used to
71
- * render in flow at the top of Main and stick there, which meant a bar nested inside a `Stack`
72
- * (every real page) had `position: sticky` clamped to that wrapper and sat one shell padding low.
73
- * `null` outside a shell, and until the outlet mounts.
74
- */
68
+ /** The shell-owned band between the header and the scrollport that row 2 portals into. */
75
69
  bandTarget: HTMLElement | null
76
70
  setBandTarget: (el: HTMLElement | null) => void
77
- /** True only under `BasaltShell` — the provider is shell-internal, so the context IS the probe. */
78
- inShell: boolean
79
- /** Shell `globalActions` marked `mobile: 'more'`, already shaped as kebab rows. */
80
- mobileMoreActions: readonly BarAction[]
81
- /** How many page kebabs are mounted. `> 0` means the shell must not render one of its own. */
82
- kebabClaims: number
83
- claimKebab: () => () => void
84
- /** True while a mounted `PageBar` is rendering a row 2 — the row that can host the aside's pill.
85
- * Without one the aside keeps its in-flow mobile form; there is nowhere to put the trigger. */
71
+ }
72
+
73
+ type AsidePanelSlot = {
74
+ /** True while a mounted `PageBar` renders a row 2 (the only host for the aside's pill). */
86
75
  panelHost: boolean
87
76
  claimPanelHost: () => () => void
88
- /** The projecting aside, or `null`. At most one — the aside region holds one page at a time. */
77
+ /** The projecting aside, or `null`. */
89
78
  panel: AsidePanelClaim | null
90
79
  claimPanel: (panel: AsidePanelClaim) => () => void
91
- /** The node inside the open panel sheet the aside's children portal into. `null` while closed —
92
- * the Drawer unmounts its body, which is what keeps ONE node mounted at a time (law C9). */
80
+ /** The node inside the open panel sheet the aside's children portal into; `null` while closed. */
93
81
  panelTarget: HTMLElement | null
94
82
  setPanelTarget: (el: HTMLElement | null) => void
95
83
  }
96
84
 
97
- const NO_SLOTS: PageBarSlots = {
85
+ const NO_TARGETS: PortalTargets = {
98
86
  target: null,
99
87
  setTarget: () => {},
100
88
  bandTarget: null,
101
89
  setBandTarget: () => {},
102
- inShell: false,
103
- mobileMoreActions: [],
104
- kebabClaims: 0,
105
- claimKebab: () => () => {},
90
+ }
91
+
92
+ const NO_PANEL_SLOT: AsidePanelSlot = {
106
93
  panelHost: false,
107
94
  claimPanelHost: () => () => {},
108
95
  panel: null,
@@ -111,7 +98,12 @@ const NO_SLOTS: PageBarSlots = {
111
98
  setPanelTarget: () => {},
112
99
  }
113
100
 
114
- const PageBarContext = createContext<PageBarSlots>(NO_SLOTS)
101
+ const PortalTargetsContext = createContext<PortalTargets>(NO_TARGETS)
102
+ const AsidePanelContext = createContext<AsidePanelSlot>(NO_PANEL_SLOT)
103
+ /** True only under `BasaltShell` — the provider is shell-internal, so the context IS the probe. */
104
+ const InShellContext = createContext(false)
105
+ /** True while a page's `ActionGroup` holds the header's one mobile kebab. */
106
+ const KebabClaimedContext = createContext(false)
115
107
 
116
108
  /**
117
109
  * Internal — mounted by `BasaltShell`. Owns the portal target, and the single-kebab claim that
@@ -151,37 +143,47 @@ export function PageBarProvider({
151
143
  }
152
144
  }, [])
153
145
 
154
- const mobileMoreActions = globalActions
155
- .filter((action, index) => (action.mobile ?? (index < 2 ? 'bar' : 'more')) === 'more')
156
- .map(globalActionAsBarAction)
157
-
158
- const value: PageBarSlots = {
159
- target,
160
- setTarget,
161
- bandTarget,
162
- setBandTarget,
163
- inShell: true,
164
- mobileMoreActions,
165
- kebabClaims,
166
- claimKebab,
167
- panelHost: panelHostClaims > 0,
168
- claimPanelHost,
169
- panel,
170
- claimPanel,
171
- panelTarget,
172
- setPanelTarget,
173
- }
146
+ const mobileMoreActions = useMemo(
147
+ () =>
148
+ globalActions
149
+ .filter((action, index) => (action.mobile ?? (index < 2 ? 'bar' : 'more')) === 'more')
150
+ .map(globalActionAsBarAction),
151
+ [globalActions],
152
+ )
153
+
154
+ const targets = useMemo(
155
+ () => ({ target, setTarget, bandTarget, setBandTarget }),
156
+ [target, bandTarget],
157
+ )
158
+ const panelSlot = useMemo(
159
+ () => ({
160
+ panelHost: panelHostClaims > 0,
161
+ claimPanelHost,
162
+ panel,
163
+ claimPanel,
164
+ panelTarget,
165
+ setPanelTarget,
166
+ }),
167
+ [panelHostClaims, claimPanelHost, panel, claimPanel, panelTarget],
168
+ )
169
+ const extras = useMemo(() => ({ mobileMoreActions, claimKebab }), [mobileMoreActions, claimKebab])
174
170
 
175
171
  return (
176
- <PageBarContext.Provider value={value}>
177
- <BarExtrasProvider value={{ mobileMoreActions, claimKebab }}>{children}</BarExtrasProvider>
178
- </PageBarContext.Provider>
172
+ <InShellContext.Provider value>
173
+ <PortalTargetsContext.Provider value={targets}>
174
+ <KebabClaimedContext.Provider value={kebabClaims > 0}>
175
+ <AsidePanelContext.Provider value={panelSlot}>
176
+ <BarExtrasProvider value={extras}>{children}</BarExtrasProvider>
177
+ </AsidePanelContext.Provider>
178
+ </KebabClaimedContext.Provider>
179
+ </PortalTargetsContext.Provider>
180
+ </InShellContext.Provider>
179
181
  )
180
182
  }
181
183
 
182
184
  /** Internal — the app-shell header node the active page's row 1 portals into. */
183
185
  export function PageBarOutlet({ className }: { className?: string }) {
184
- const { setTarget } = useContext(PageBarContext)
186
+ const { setTarget } = useContext(PortalTargetsContext)
185
187
  return <div ref={setTarget} className={className} />
186
188
  }
187
189
 
@@ -195,7 +197,7 @@ export function PageBarOutlet({ className }: { className?: string }) {
195
197
  * `app-main.module.css` is what makes "nothing mounted" and "nothing painted" the same state.
196
198
  */
197
199
  export function PageBarBandOutlet({ className }: { className?: string }) {
198
- const { setBandTarget } = useContext(PageBarContext)
200
+ const { setBandTarget } = useContext(PortalTargetsContext)
199
201
  return <div ref={setBandTarget} className={className} />
200
202
  }
201
203
 
@@ -205,12 +207,12 @@ export function PageBarBandOutlet({ className }: { className?: string }) {
205
207
  * sticky in the document and `Section` must clear it.
206
208
  */
207
209
  export function useInPageBarShell(): boolean {
208
- return useContext(PageBarContext).inShell
210
+ return useContext(InShellContext)
209
211
  }
210
212
 
211
213
  /** Internal — `true` while a page's `ActionGroup` owns the header's one mobile kebab. */
212
214
  export function usePageKebabClaimed(): boolean {
213
- return useContext(PageBarContext).kebabClaims > 0
215
+ return useContext(KebabClaimedContext)
214
216
  }
215
217
 
216
218
  /**
@@ -236,7 +238,8 @@ export function useAsidePanelSlot(): {
236
238
  claim: (panel: AsidePanelClaim) => () => void
237
239
  target: HTMLElement | null
238
240
  } {
239
- const { panelHost, claimPanel, panelTarget, inShell } = useContext(PageBarContext)
241
+ const { panelHost, claimPanel, panelTarget } = useContext(AsidePanelContext)
242
+ const inShell = useContext(InShellContext)
240
243
  const [settled, setSettled] = useState(false)
241
244
  useIsomorphicLayoutEffect(() => {
242
245
  setSettled(true)
@@ -398,8 +401,9 @@ export function PageBar({
398
401
  style,
399
402
  classNames,
400
403
  }: PageBarProps): ReactNode {
401
- const { target, bandTarget, inShell, panel, claimPanelHost, setPanelTarget } =
402
- useContext(PageBarContext)
404
+ const { target, bandTarget } = useContext(PortalTargetsContext)
405
+ const { panel, claimPanelHost, setPanelTarget } = useContext(AsidePanelContext)
406
+ const inShell = useContext(InShellContext)
403
407
  // An overlay's open flag, like `FilterPill`'s and `FilterSet`'s — not page state (C3).
404
408
  const [panelOpened, setPanelOpened] = useState(false)
405
409