basalt-ui 2.13.0 → 2.13.2
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/CHANGELOG.md +23 -0
- package/MIGRATING.md +32 -12
- package/README.md +1 -1
- package/dist/charts/compile.d.ts.map +1 -1
- package/dist/charts/compile.js +6 -5
- package/dist/charts/cursor.d.ts +33 -7
- package/dist/charts/cursor.d.ts.map +1 -1
- package/dist/charts/cursor.js +31 -10
- package/dist/charts/legend.d.ts +19 -3
- package/dist/charts/legend.d.ts.map +1 -1
- package/dist/charts/legend.js +33 -15
- package/dist/charts/marks.d.ts.map +1 -1
- package/dist/charts/marks.js +24 -28
- package/dist/charts/panel.d.ts.map +1 -1
- package/dist/charts/panel.js +4 -4
- package/dist/charts/types.d.ts +3 -2
- package/dist/charts/types.d.ts.map +1 -1
- package/dist/router-tanstack/nav.d.ts +5 -0
- package/dist/router-tanstack/nav.d.ts.map +1 -1
- package/dist/router-tanstack/use-nav.d.ts +4 -2
- package/dist/router-tanstack/use-nav.d.ts.map +1 -1
- package/dist/router-tanstack/use-nav.js +55 -18
- package/dist/shell/app-breadcrumbs.d.ts +1 -3
- package/dist/shell/app-breadcrumbs.d.ts.map +1 -1
- package/dist/shell/app-breadcrumbs.js +1 -6
- package/dist/shell/index.d.ts +14 -5
- package/dist/shell/index.d.ts.map +1 -1
- package/dist/shell/index.js +6 -29
- package/dist/shell/nav-types.d.ts +7 -0
- package/dist/shell/nav-types.d.ts.map +1 -1
- package/dist/shell/page-aside.d.ts +4 -2
- package/dist/shell/page-aside.d.ts.map +1 -1
- package/dist/shell/page-aside.js +41 -23
- package/dist/shell/page-bar.d.ts +1 -1
- package/dist/shell/page-bar.d.ts.map +1 -1
- package/dist/shell/page-bar.js +39 -33
- package/dist/theme/index.d.ts.map +1 -1
- package/dist/theme/index.js +4 -1
- package/llms.txt +2 -2
- package/package.json +1 -1
- package/src/charts/compile.ts +8 -10
- package/src/charts/cursor.ts +69 -19
- package/src/charts/legend.tsx +40 -19
- package/src/charts/marks.ts +28 -29
- package/src/charts/panel.tsx +4 -4
- package/src/charts/types.ts +3 -2
- package/src/router-tanstack/nav.ts +5 -0
- package/src/router-tanstack/use-nav.ts +116 -76
- package/src/shell/app-breadcrumbs.tsx +1 -12
- package/src/shell/index.tsx +22 -55
- package/src/shell/nav-types.ts +8 -0
- package/src/shell/page-aside.tsx +59 -26
- package/src/shell/page-bar.tsx +63 -59
- package/src/theme/__snapshots__/plain-theme.test.ts.snap +2 -0
- package/src/theme/index.ts +4 -1
|
@@ -9,10 +9,16 @@
|
|
|
9
9
|
* No JSX — anchors are built with `createElement`, so `./router-tanstack` stays a headless,
|
|
10
10
|
* no-JSX `.ts` module per its own barrel contract.
|
|
11
11
|
*/
|
|
12
|
-
import { createElement, forwardRef, useMemo } from 'react'
|
|
12
|
+
import { createElement, forwardRef, useMemo, useState } from 'react'
|
|
13
13
|
import type { AnchorHTMLAttributes, ElementType, ReactNode } from 'react'
|
|
14
14
|
import { Link, useMatchRoute, useRouterState } from '@tanstack/react-router'
|
|
15
|
-
import type {
|
|
15
|
+
import type {
|
|
16
|
+
ActiveCrumb,
|
|
17
|
+
MobileNavConfig,
|
|
18
|
+
NavAnchor,
|
|
19
|
+
SidebarItem,
|
|
20
|
+
SidebarSection,
|
|
21
|
+
} from '../shell/nav-types'
|
|
16
22
|
import { flattenNav } from './nav-stash'
|
|
17
23
|
import type { AnyNavGroup, AnyNavItem, NavConfig, NavItemId, NavTabId } from './nav'
|
|
18
24
|
|
|
@@ -23,7 +29,7 @@ export type UseNavOptions<G extends ReadonlyArray<AnyNavGroup>> = {
|
|
|
23
29
|
* Anything else becomes `item.badge` (desktop only).
|
|
24
30
|
*/
|
|
25
31
|
badges?: Partial<Record<NavItemId<G>, number | ReactNode>>
|
|
26
|
-
/** Override active detection entirely. */
|
|
32
|
+
/** Override active detection entirely. Keep it stable (module scope or memoized). */
|
|
27
33
|
isActive?: (item: AnyNavItem & { id: NavItemId<G> }) => boolean
|
|
28
34
|
getScrollElement?: () => HTMLElement | null
|
|
29
35
|
}
|
|
@@ -35,15 +41,10 @@ type OverriddenAnchorProps = AnchorHTMLAttributes<HTMLAnchorElement> & {
|
|
|
35
41
|
}
|
|
36
42
|
|
|
37
43
|
/**
|
|
38
|
-
* The host `Link` renders as when `isActive` overrides the router
|
|
39
|
-
* `
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
* its host element in place of `'a'` (same file, `Link`: `const host = props._asChild || "a"`) —
|
|
43
|
-
* an underscore-prefixed, undocumented seam, so `MAINTAINING.md` names it for re-verification on
|
|
44
|
-
* every router bump. This host drops the router's two attributes and re-applies only basalt's own
|
|
45
|
-
* verdict, which the anchor smuggles through `data-basalt-current`; `data-status` is re-stamped
|
|
46
|
-
* for it so both paths expose the same hooks on the winner.
|
|
44
|
+
* The host `Link` renders as when `isActive` overrides the router (via the undocumented
|
|
45
|
+
* `props._asChild` seam, re-verified per router bump in `MAINTAINING.md`). It drops the router's
|
|
46
|
+
* `aria-current` / `data-status` and re-applies only basalt's verdict, passed as
|
|
47
|
+
* `data-basalt-current`, so a router-matched loser cannot claim the page.
|
|
47
48
|
*/
|
|
48
49
|
const OverriddenAnchor = forwardRef<HTMLAnchorElement, OverriddenAnchorProps>((props, ref) => {
|
|
49
50
|
const {
|
|
@@ -62,7 +63,88 @@ const OverriddenAnchor = forwardRef<HTMLAnchorElement, OverriddenAnchorProps>((p
|
|
|
62
63
|
OverriddenAnchor.displayName = 'OverriddenAnchor'
|
|
63
64
|
|
|
64
65
|
/** What this resolver turns a definition into: `ShellFrame`'s sidebar sections and mobile-bar tuning. */
|
|
65
|
-
export type ResolvedNav = {
|
|
66
|
+
export type ResolvedNav = {
|
|
67
|
+
sections: SidebarSection[]
|
|
68
|
+
mobileNav: MobileNavConfig
|
|
69
|
+
/** The breadcrumb for the one active destination; `undefined` when nothing matches. */
|
|
70
|
+
activeCrumb: ActiveCrumb | undefined
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* One anchor per destination for the life of its `config`: a fresh function per resolve is a new
|
|
75
|
+
* component TYPE and remounts every nav `<a>` on each navigation. Nothing per-render is closed over;
|
|
76
|
+
* active state arrives as the `aria-current` prop. Keyed by override mode because `isActive` swaps
|
|
77
|
+
* the host element (`OverriddenAnchor`).
|
|
78
|
+
*/
|
|
79
|
+
const anchorCache = new WeakMap<object, Map<boolean, Map<string, NavAnchor>>>()
|
|
80
|
+
|
|
81
|
+
function anchorFor(config: object, item: AnyNavItem, overridden: boolean): NavAnchor {
|
|
82
|
+
let byMode = anchorCache.get(config)
|
|
83
|
+
if (!byMode) anchorCache.set(config, (byMode = new Map()))
|
|
84
|
+
let byId = byMode.get(overridden)
|
|
85
|
+
if (!byId) byMode.set(overridden, (byId = new Map()))
|
|
86
|
+
const cached = byId.get(item.id)
|
|
87
|
+
if (cached) return cached
|
|
88
|
+
// `exact: true` makes `resolveState` the only authority on active: a `Link` spreads its own
|
|
89
|
+
// `aria-current` LAST and cannot be overridden from outside, so a prefix-matching parent would
|
|
90
|
+
// otherwise claim the page beside its child. `Link as ElementType` is the one cast in the chain.
|
|
91
|
+
const Anchor: NavAnchor = (props) =>
|
|
92
|
+
createElement(Link as ElementType, {
|
|
93
|
+
...item.link,
|
|
94
|
+
activeOptions: { exact: true, includeSearch: true },
|
|
95
|
+
...(overridden && {
|
|
96
|
+
_asChild: OverriddenAnchor,
|
|
97
|
+
'data-basalt-current': props['aria-current'],
|
|
98
|
+
}),
|
|
99
|
+
...props,
|
|
100
|
+
})
|
|
101
|
+
byId.set(item.id, Anchor)
|
|
102
|
+
return Anchor
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const NO_BADGES: Readonly<Record<string, unknown>> = {}
|
|
106
|
+
|
|
107
|
+
/** Shallow equality over own keys; `undefined` and `{}` are the same (no badges). */
|
|
108
|
+
function shallowEqual(
|
|
109
|
+
a: Readonly<Record<string, unknown>> = NO_BADGES,
|
|
110
|
+
b: Readonly<Record<string, unknown>> = NO_BADGES,
|
|
111
|
+
): boolean {
|
|
112
|
+
if (a === b) return true
|
|
113
|
+
const keys = Object.keys(a)
|
|
114
|
+
return (
|
|
115
|
+
keys.length === Object.keys(b).length &&
|
|
116
|
+
keys.every((k) => Object.hasOwn(b, k) && Object.is(a[k], b[k]))
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The breadcrumb for `winnerId`: its group, and its immediate parent when nested. */
|
|
121
|
+
function crumbOf(
|
|
122
|
+
config: { groups: ReadonlyArray<AnyNavGroup> },
|
|
123
|
+
winnerId: string | undefined,
|
|
124
|
+
overridden: boolean,
|
|
125
|
+
): ActiveCrumb | undefined {
|
|
126
|
+
const find = (
|
|
127
|
+
items: ReadonlyArray<AnyNavItem>,
|
|
128
|
+
section: string,
|
|
129
|
+
parent?: AnyNavItem,
|
|
130
|
+
): ActiveCrumb | undefined => {
|
|
131
|
+
for (const item of items) {
|
|
132
|
+
if (item.id === winnerId) {
|
|
133
|
+
const parentCrumb = parent && {
|
|
134
|
+
parent: parent.label,
|
|
135
|
+
parentAnchor: anchorFor(config, parent, overridden),
|
|
136
|
+
}
|
|
137
|
+
return { section, page: item.label, ...parentCrumb }
|
|
138
|
+
}
|
|
139
|
+
const nested = item.children && find(item.children, section, item)
|
|
140
|
+
if (nested) return nested
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
for (const group of config.groups) {
|
|
144
|
+
const found = find(group.items, group.label)
|
|
145
|
+
if (found) return found
|
|
146
|
+
}
|
|
147
|
+
}
|
|
66
148
|
|
|
67
149
|
/**
|
|
68
150
|
* Resolve a nav definition against the live router into `BasaltShell`'s internal `ShellFrame`
|
|
@@ -79,17 +161,18 @@ export function useNav<
|
|
|
79
161
|
// The change signal: one string that moves on every navigation, including search-param-only ones.
|
|
80
162
|
const href = useRouterState({ select: (s) => s.location.href })
|
|
81
163
|
|
|
82
|
-
|
|
164
|
+
// Keep the previous badges object while every value is unchanged, so an inline literal never
|
|
165
|
+
// rebuilds the sections below. Adjusting state during render is React's sanctioned form of this.
|
|
166
|
+
const [badges, setBadges] = useState(opts?.badges)
|
|
167
|
+
if (!shallowEqual(badges, opts?.badges)) setBadges(opts?.badges)
|
|
83
168
|
const isActiveOverride = opts?.isActive
|
|
84
169
|
|
|
85
|
-
const
|
|
170
|
+
const resolved = useMemo<Pick<ResolvedNav, 'sections' | 'activeCrumb'>>(() => {
|
|
86
171
|
const badgeOf = (id: string): number | ReactNode =>
|
|
87
|
-
(badges as Record<string, number | ReactNode
|
|
172
|
+
(badges as Readonly<Record<string, number | ReactNode>> | undefined)?.[id]
|
|
88
173
|
|
|
89
|
-
//
|
|
90
|
-
//
|
|
91
|
-
// `active` on its own — it only says "on the path", and the pass below picks ONE winner
|
|
92
|
-
// across the whole definition so a parent and its child can no longer both read `active`.
|
|
174
|
+
// "On the path" only: the override wins, else a router prefix match (`'/'` stays exact). The
|
|
175
|
+
// pass below picks the ONE `active` winner.
|
|
93
176
|
const matchOf = (item: AnyNavItem): boolean =>
|
|
94
177
|
isActiveOverride?.(item as AnyNavItem & { id: NavItemId<G> }) ??
|
|
95
178
|
Boolean(
|
|
@@ -100,13 +183,8 @@ export function useNav<
|
|
|
100
183
|
}),
|
|
101
184
|
)
|
|
102
185
|
|
|
103
|
-
// One pass over the
|
|
104
|
-
//
|
|
105
|
-
// per-item independent check, which is what let a parent prefix-match stay lit alongside an
|
|
106
|
-
// exact-matching child. The most SPECIFIC match (most non-empty `/`-segments in `link.to`)
|
|
107
|
-
// wins; a tie (the playground's `reports`/`components` shape, both pointing at `/components`)
|
|
108
|
-
// keeps the FIRST item in definition order, since only a strictly later, strictly longer match
|
|
109
|
-
// ever replaces the current winner below.
|
|
186
|
+
// One pass over the flattened definition: the most SPECIFIC match (most `/`-segments in
|
|
187
|
+
// `link.to`) wins, and a tie keeps the FIRST item in definition order.
|
|
110
188
|
const allItems = flattenNav(config)
|
|
111
189
|
const specificity = (to: string): number => to.split('/').filter(Boolean).length
|
|
112
190
|
const matchedById = new Map<string, boolean>()
|
|
@@ -124,11 +202,8 @@ export function useNav<
|
|
|
124
202
|
}
|
|
125
203
|
}
|
|
126
204
|
|
|
127
|
-
// `active` is
|
|
128
|
-
//
|
|
129
|
-
// tie loser matches nothing here (its own `to` cannot be a strict prefix of an equally long
|
|
130
|
-
// `winnerTo`), so it reads as neither active nor an ancestor, which is the point: two
|
|
131
|
-
// destinations sharing one route are siblings, not a hierarchy.
|
|
205
|
+
// `active` is the winner only; `ancestor` is a matched item strictly on the winner's path. Two
|
|
206
|
+
// destinations sharing one route are siblings, so a tie loser is neither.
|
|
132
207
|
const resolveState = (item: AnyNavItem): { active: boolean; ancestor: boolean } => {
|
|
133
208
|
if (item.id === winnerId) return { active: true, ancestor: false }
|
|
134
209
|
const matched = matchedById.get(item.id) ?? false
|
|
@@ -136,45 +211,12 @@ export function useNav<
|
|
|
136
211
|
return { active: false, ancestor }
|
|
137
212
|
}
|
|
138
213
|
|
|
139
|
-
const
|
|
140
|
-
// One anchor per destination. Basalt renders every pixel of chrome around it (desktop row,
|
|
141
|
-
// 56px slot, 44px sheet row) and only hosts this component, so `preload`, middle-click and
|
|
142
|
-
// back/forward all keep working. `Link as ElementType` is the single cast in the whole
|
|
143
|
-
// chain: the widened `AnyNavLink['search']` is `unknown`, which `LinkComponentProps` rejects.
|
|
144
|
-
// The consumer gets zero casts, which is the point.
|
|
145
|
-
//
|
|
146
|
-
// `activeOptions: { exact: true, includeSearch: true }` is what makes `resolveState` above
|
|
147
|
-
// the ONLY authority on active, and it is not a preference — a `Link` computes its own
|
|
148
|
-
// `isActive` (default `activeOptions.exact: false`, i.e. prefix) and spreads
|
|
149
|
-
// `{ 'data-status': 'active', 'aria-current': 'page' }` LAST, after `activeProps` and after
|
|
150
|
-
// every caller prop (`@tanstack/react-router/dist/esm/link.js` — `...isActive &&
|
|
151
|
-
// STATIC_ACTIVE_PROPS` is the final entry of the returned object). So `aria-current` cannot
|
|
152
|
-
// be overridden from outside; the only lever is `isActive` itself. Left at its default, the
|
|
153
|
-
// router re-derived the exact fuzzy match this resolver exists to replace, and at
|
|
154
|
-
// `/dashboard/sessions` BOTH the parent and the child anchor carried `aria-current="page"`
|
|
155
|
-
// even with basalt's own model marking one of them an ancestor — verified in Chrome, and the
|
|
156
|
-
// reason the first attempt at this fix measured as no fix at all. `includeSearch` already
|
|
157
|
-
// defaults to `true` upstream; naming it here is belt-and-braces so a future default change
|
|
158
|
-
// cannot silently reopen the gap.
|
|
159
|
-
//
|
|
160
|
-
// Exact here does NOT narrow basalt's own law. `resolveState` still matches fuzzily and still
|
|
161
|
-
// lights an item whose exact route is never visited; that item is simply named by the
|
|
162
|
-
// `aria-current` `app-sidebar.tsx` stamps itself, which survives because the router's spread
|
|
163
|
-
// is conditional on ITS `isActive` being true. The CSS side no longer treats `aria-current`
|
|
164
|
-
// as a style hook either (`theme/nav-link.module.css`, `shell/app-sidebar.module.css`), so
|
|
165
|
-
// `use-nav`'s single `data-active` winner is the one visual authority even if the router's
|
|
166
|
-
// own `isActive` ever disagreed.
|
|
167
|
-
const Anchor: NavAnchor = (props) =>
|
|
168
|
-
createElement(Link as ElementType, {
|
|
169
|
-
...item.link,
|
|
170
|
-
activeOptions: { exact: true, includeSearch: true },
|
|
171
|
-
...(isActiveOverride !== undefined && {
|
|
172
|
-
_asChild: OverriddenAnchor,
|
|
173
|
-
'data-basalt-current': props['aria-current'],
|
|
174
|
-
}),
|
|
175
|
-
...props,
|
|
176
|
-
})
|
|
214
|
+
const overridden = isActiveOverride !== undefined
|
|
177
215
|
|
|
216
|
+
const toSidebarItem = (item: AnyNavItem): SidebarItem => {
|
|
217
|
+
// One anchor per destination, hosted by basalt's own chrome (desktop row, 56px slot, 44px
|
|
218
|
+
// sheet row), so `preload`, middle-click and back/forward all keep working.
|
|
219
|
+
const Anchor = anchorFor(config, item, overridden)
|
|
178
220
|
const badge = badgeOf(item.id)
|
|
179
221
|
const children = item.children?.map(toSidebarItem)
|
|
180
222
|
const { active, ancestor } = resolveState(item)
|
|
@@ -184,10 +226,7 @@ export function useNav<
|
|
|
184
226
|
label: item.label,
|
|
185
227
|
icon: item.icon,
|
|
186
228
|
Anchor,
|
|
187
|
-
// Exactly one destination
|
|
188
|
-
// Rolling that up into a mobile SLOT (any descendant active → the slot reads active) is
|
|
189
|
-
// still `projectMobileNav`'s job, which reads `children` for that. `ancestor` is the
|
|
190
|
-
// desktop-only middle state: a matched item that sits on the winner's path but isn't it.
|
|
229
|
+
// Exactly one destination reads `active`; `projectMobileNav` rolls it up into mobile slots.
|
|
191
230
|
active,
|
|
192
231
|
...(ancestor && { ancestor }),
|
|
193
232
|
...(item.short !== undefined && { short: item.short }),
|
|
@@ -202,7 +241,7 @@ export function useNav<
|
|
|
202
241
|
}
|
|
203
242
|
}
|
|
204
243
|
|
|
205
|
-
|
|
244
|
+
const sections = config.groups.map((group) => ({
|
|
206
245
|
label: group.label,
|
|
207
246
|
items: group.items.map(toSidebarItem),
|
|
208
247
|
...(group.icon !== undefined && { icon: group.icon }),
|
|
@@ -210,6 +249,7 @@ export function useNav<
|
|
|
210
249
|
...(group.defaultCollapsed !== undefined && { defaultCollapsed: group.defaultCollapsed }),
|
|
211
250
|
...(group.mobile !== undefined && { mobile: group.mobile }),
|
|
212
251
|
}))
|
|
252
|
+
return { sections, activeCrumb: crumbOf(config, winnerId, overridden) }
|
|
213
253
|
// `href` is deliberately an unread dep, and the reason the memo is correct: `matchRoute` has a
|
|
214
254
|
// STABLE identity (`useCallback(fn, [router])`), so nothing else in this list moves on a
|
|
215
255
|
// navigation — while the `matchRoute` calls above read router state that just changed. `href`
|
|
@@ -239,5 +279,5 @@ export function useNav<
|
|
|
239
279
|
}
|
|
240
280
|
}, [config, mobile, getScrollElement])
|
|
241
281
|
|
|
242
|
-
return {
|
|
282
|
+
return { ...resolved, mobileNav }
|
|
243
283
|
}
|
|
@@ -41,8 +41,6 @@ export type AppBreadcrumbsProps = BasaltProps & {
|
|
|
41
41
|
* client-side. basalt still owns every pixel of the crumb — the anchor only hosts the label.
|
|
42
42
|
*/
|
|
43
43
|
parentAnchor?: NavAnchor | undefined
|
|
44
|
-
/** Parent item href — the no-router fallback; ignored when `parentAnchor` is present. */
|
|
45
|
-
parentHref?: string | undefined
|
|
46
44
|
page?: string
|
|
47
45
|
}
|
|
48
46
|
|
|
@@ -50,7 +48,6 @@ export function AppBreadcrumbs({
|
|
|
50
48
|
section,
|
|
51
49
|
parent,
|
|
52
50
|
parentAnchor,
|
|
53
|
-
parentHref,
|
|
54
51
|
page,
|
|
55
52
|
className,
|
|
56
53
|
style,
|
|
@@ -89,15 +86,7 @@ export function AppBreadcrumbs({
|
|
|
89
86
|
<Text style={separatorStyle}>/</Text>
|
|
90
87
|
</>
|
|
91
88
|
)}
|
|
92
|
-
{parent && !parentAnchor &&
|
|
93
|
-
<>
|
|
94
|
-
<Anchor style={crumbStyle} underline="never" href={parentHref} truncate>
|
|
95
|
-
{parent}
|
|
96
|
-
</Anchor>
|
|
97
|
-
<Text style={separatorStyle}>/</Text>
|
|
98
|
-
</>
|
|
99
|
-
)}
|
|
100
|
-
{parent && !parentAnchor && !parentHref && (
|
|
89
|
+
{parent && !parentAnchor && (
|
|
101
90
|
<>
|
|
102
91
|
<Text style={crumbStyle} truncate>
|
|
103
92
|
{parent}
|
package/src/shell/index.tsx
CHANGED
|
@@ -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`;
|
|
259
|
-
*
|
|
260
|
-
*
|
|
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
|
-
|
|
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
|
|
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
|
|
package/src/shell/nav-types.ts
CHANGED
|
@@ -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
|
+
}
|
package/src/shell/page-aside.tsx
CHANGED
|
@@ -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
|
-
|
|
85
|
-
|
|
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
|
-
|
|
117
|
-
|
|
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((
|
|
122
|
-
return () =>
|
|
123
|
-
setClaims((
|
|
124
|
-
|
|
125
|
-
|
|
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
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|