basalt-ui 2.11.1 → 2.12.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/AGENTS.md +1 -1
- package/CHANGELOG.md +16 -0
- package/MIGRATING.md +24 -12
- package/README.md +2 -0
- package/agent/rules/basalt-mantine.md +1 -1
- package/agent/skills/basalt-app/SKILL.md +1 -1
- package/dist/cli/doctor-upgrade.d.ts +2 -3
- package/dist/cli/doctor-upgrade.d.ts.map +1 -1
- package/dist/cli/doctor-upgrade.js +71 -26
- package/dist/cli/index.d.ts +3 -1
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +3 -1
- package/dist/cli/sync.d.ts.map +1 -1
- package/dist/cli/sync.js +2 -1
- package/dist/commands/command-stash.d.ts +15 -0
- package/dist/commands/command-stash.d.ts.map +1 -0
- package/dist/commands/command-stash.js +31 -0
- package/dist/commands/define-commands.d.ts +2 -3
- package/dist/commands/define-commands.d.ts.map +1 -1
- package/dist/commands/define-commands.js +9 -7
- package/dist/commands/define-overlays.d.ts +6 -12
- package/dist/commands/define-overlays.d.ts.map +1 -1
- package/dist/commands/define-overlays.js +28 -34
- package/dist/commands/projectors.js +1 -1
- package/dist/provider/hotkeys.d.ts +2 -6
- package/dist/provider/hotkeys.d.ts.map +1 -1
- package/dist/provider/hotkeys.js +10 -15
- package/dist/provider/index.d.ts +5 -3
- package/dist/provider/index.d.ts.map +1 -1
- package/dist/provider/index.js +12 -35
- package/dist/provider/modals-mount.d.ts +16 -0
- package/dist/provider/modals-mount.d.ts.map +1 -0
- package/dist/provider/modals-mount.js +42 -0
- package/dist/provider/notifier.d.ts +22 -0
- package/dist/provider/notifier.d.ts.map +1 -0
- package/dist/provider/notifier.js +36 -0
- package/dist/provider/route-transition.d.ts +1 -1
- package/dist/provider/route-transition.d.ts.map +1 -1
- package/dist/provider/route-transition.js +1 -1
- package/dist/provider/slots.d.ts +25 -0
- package/dist/provider/slots.d.ts.map +1 -0
- package/dist/provider/slots.js +29 -0
- package/dist/provider/spotlight-actions.d.ts +1 -1
- package/dist/provider/spotlight-actions.d.ts.map +1 -1
- package/dist/provider/spotlight-actions.js +2 -3
- package/dist/provider/spotlight-mount.d.ts +3 -0
- package/dist/provider/spotlight-mount.d.ts.map +1 -0
- package/dist/provider/spotlight-mount.js +26 -0
- package/dist/router-tanstack/nav-stash.d.ts +24 -0
- package/dist/router-tanstack/nav-stash.d.ts.map +1 -0
- package/dist/router-tanstack/nav-stash.js +23 -0
- package/dist/router-tanstack/nav.d.ts +5 -15
- package/dist/router-tanstack/nav.d.ts.map +1 -1
- package/dist/router-tanstack/nav.js +6 -16
- package/dist/router-tanstack/use-nav.js +1 -1
- package/dist/surfaces.d.ts +1 -1
- package/dist/surfaces.js +1 -1
- package/llms.txt +4 -4
- package/package.json +2 -2
- package/src/cli/doctor-upgrade.ts +101 -35
- package/src/cli/index.ts +5 -3
- package/src/cli/sync.ts +2 -2
- package/src/commands/command-stash.ts +44 -0
- package/src/commands/define-commands.ts +10 -17
- package/src/commands/define-overlays.ts +40 -68
- package/src/commands/projectors.ts +1 -1
- package/src/provider/hotkeys.ts +14 -22
- package/src/provider/index.tsx +28 -52
- package/src/provider/modals-mount.tsx +66 -0
- package/src/provider/notifier.ts +52 -0
- package/src/provider/route-transition.ts +1 -1
- package/src/provider/slots.ts +59 -0
- package/src/provider/spotlight-actions.tsx +3 -4
- package/src/provider/spotlight-mount.tsx +33 -0
- package/src/router-tanstack/nav-stash.ts +41 -0
- package/src/router-tanstack/nav.ts +11 -31
- package/src/router-tanstack/use-nav.ts +1 -1
- package/src/surfaces.ts +1 -1
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* `ShortcutsHelp` renders. Reads the runtime stash (populated by `defineCommands`), not the
|
|
4
4
|
* type-erased `BasaltRegister` slot; safe to call before any commands are registered.
|
|
5
5
|
*/
|
|
6
|
-
import { getActiveCommands } from './
|
|
6
|
+
import { getActiveCommands } from './command-stash'
|
|
7
7
|
|
|
8
8
|
// ── ShortcutEntry ─────────────────────────────────────────────────────────────
|
|
9
9
|
|
package/src/provider/hotkeys.ts
CHANGED
|
@@ -3,32 +3,18 @@
|
|
|
3
3
|
* `shortcut`. `@tanstack/react-hotkeys` is subpath-required for `./commands`, so it is reached
|
|
4
4
|
* through `import()` only and its runtime stays off first paint (V2-SPEC § 2).
|
|
5
5
|
*
|
|
6
|
-
* `defineCommands`
|
|
7
|
-
* module
|
|
6
|
+
* `defineCommands` registers `HotkeyBinder` as the provider's `hotkeys` slot when a command has a
|
|
7
|
+
* shortcut, so this module (and the peer behind its `import()`) is reachable only from there.
|
|
8
8
|
*/
|
|
9
9
|
import type { HotkeyRegistrationHandle } from '@tanstack/react-hotkeys'
|
|
10
|
-
import { useEffect } from 'react'
|
|
11
|
-
import { getActiveCommands } from '../commands/
|
|
12
|
-
import type { Command } from '../commands/define-commands'
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Runs a command, swallowing a sync throw or async rejection so a failing command never becomes an
|
|
16
|
-
* unhandled rejection (which the provider would report as a `window.error`). Dev logs it.
|
|
17
|
-
*/
|
|
18
|
-
export function runCommandSafely(cmd: Command): void {
|
|
19
|
-
const report = (error: unknown): void => {
|
|
20
|
-
if (process.env.NODE_ENV !== 'production') console.error('[basalt] command failed', error)
|
|
21
|
-
}
|
|
22
|
-
try {
|
|
23
|
-
Promise.resolve(cmd.run()).catch(report)
|
|
24
|
-
} catch (error) {
|
|
25
|
-
report(error)
|
|
26
|
-
}
|
|
27
|
-
}
|
|
10
|
+
import { useEffect, useSyncExternalStore } from 'react'
|
|
11
|
+
import { getActiveCommands, runCommandSafely, subscribeCommands } from '../commands/command-stash'
|
|
28
12
|
|
|
29
13
|
export function useRegistryHotkeys(): void {
|
|
14
|
+
// A late `defineCommands` (a lazily loaded route) replaces the map; re-bind against the new one.
|
|
15
|
+
const registry = useSyncExternalStore(subscribeCommands, getActiveCommands, getActiveCommands)
|
|
30
16
|
useEffect(() => {
|
|
31
|
-
const commands = Object.values(
|
|
17
|
+
const commands = Object.values(registry).filter((c) => c.shortcut !== undefined)
|
|
32
18
|
if (commands.length === 0) return
|
|
33
19
|
|
|
34
20
|
let cancelled = false
|
|
@@ -66,5 +52,11 @@ export function useRegistryHotkeys(): void {
|
|
|
66
52
|
cancelled = true
|
|
67
53
|
for (const handle of handles) handle.unregister()
|
|
68
54
|
}
|
|
69
|
-
}, [])
|
|
55
|
+
}, [registry])
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The `hotkeys` slot mount: renders nothing, binds the registry's shortcuts. */
|
|
59
|
+
export function HotkeyBinder(): null {
|
|
60
|
+
useRegistryHotkeys()
|
|
61
|
+
return null
|
|
70
62
|
}
|
package/src/provider/index.tsx
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* BasaltProvider — the one provider tree (V2-SPEC § 2): `MantineProvider` with the Basalt theme and
|
|
3
3
|
* resolver, the `--vx-*` palette `<style>`, and then
|
|
4
|
-
* `QueryClientProvider >
|
|
5
|
-
*
|
|
4
|
+
* `QueryClientProvider > RouterProvider` with `Notifications` as a sibling. Spotlight, the modals
|
|
5
|
+
* layer and the command hotkeys are slots: each mounts as a sibling only once the app's `defineNav` /
|
|
6
|
+
* `defineCommands` / `defineOverlays` call registered it (`./slots`), so an app defining none ships none
|
|
7
|
+
* of their runtimes. The consumer writes the route tree, the nav and the registries.
|
|
6
8
|
*
|
|
7
9
|
* It also owns the root error boundary, the `window` error listeners and the telemetry seam
|
|
8
10
|
* (V2-SPEC § 7), the command hotkeys and Spotlight actions, and — in dev only — the dev dock.
|
|
@@ -12,15 +14,12 @@
|
|
|
12
14
|
*/
|
|
13
15
|
import { MantineProvider, useMantineColorScheme, useMantineTheme } from '@mantine/core'
|
|
14
16
|
import type { MantineProviderProps } from '@mantine/core'
|
|
15
|
-
import { ModalsProvider } from '@mantine/modals'
|
|
16
17
|
import { Notifications } from '@mantine/notifications'
|
|
17
|
-
import { Spotlight } from '@mantine/spotlight'
|
|
18
|
-
import type { SpotlightActionData } from '@mantine/spotlight'
|
|
19
18
|
import { QueryClientProvider } from '@tanstack/react-query'
|
|
20
19
|
import type { QueryClient } from '@tanstack/react-query'
|
|
21
20
|
import { RouterProvider, useRouter } from '@tanstack/react-router'
|
|
22
21
|
import type { AnyRouter } from '@tanstack/react-router'
|
|
23
|
-
import { Component, Suspense, lazy,
|
|
22
|
+
import { Component, Suspense, lazy, useEffect, useMemo, useRef, useState } from 'react'
|
|
24
23
|
import type { ErrorInfo, ReactNode } from 'react'
|
|
25
24
|
import fallbackClasses from './error-fallback.module.css'
|
|
26
25
|
import { cssVariablesResolver } from '../theme'
|
|
@@ -30,7 +29,6 @@ import type { BuildPaletteOpts } from '../tokens/core'
|
|
|
30
29
|
import { isDefaultDeriveConfig } from '../tokens/derive'
|
|
31
30
|
import { buildPaletteData } from '../tokens/palette'
|
|
32
31
|
import { useMediaQuery } from '../common/use-media-query'
|
|
33
|
-
import { registerModalsProvider } from '../commands/define-overlays'
|
|
34
32
|
import { registerColorSchemeSetter } from '../commands/shell-bridge'
|
|
35
33
|
import { useIsomorphicLayoutEffect } from '../common/isomorphic-layout-effect'
|
|
36
34
|
import { PageTitle } from '../shell/page-title'
|
|
@@ -46,9 +44,9 @@ import {
|
|
|
46
44
|
import type { Telemetry } from '../telemetry'
|
|
47
45
|
import type { BasaltDevDockTool } from '../dev-dock'
|
|
48
46
|
import type { BasaltRouter, BasaltRouterContext } from './create-router'
|
|
49
|
-
import {
|
|
50
|
-
import
|
|
51
|
-
import {
|
|
47
|
+
import type { FlatNavItem } from '../router-tanstack/nav-stash'
|
|
48
|
+
import { trackProvider, useSlots } from './slots'
|
|
49
|
+
import type { SlotProps } from './slots'
|
|
52
50
|
|
|
53
51
|
type BasaltProviderCommonProps = {
|
|
54
52
|
/** Theme override merged onto the Basalt base. Defaults to `createBasaltTheme()`. */
|
|
@@ -91,8 +89,10 @@ type BasaltProviderCommonProps = {
|
|
|
91
89
|
*/
|
|
92
90
|
navCommandLabel?: (item: FlatNavItem) => string
|
|
93
91
|
/**
|
|
94
|
-
* Mount Basalt's `<Spotlight>` (Cmd+K) with the generated nav + command actions. Default `true
|
|
95
|
-
*
|
|
92
|
+
* Mount Basalt's `<Spotlight>` (Cmd+K) with the generated nav + command actions. Default `true`, and
|
|
93
|
+
* it mounts only when the app calls `defineNav` or `defineCommands` (nav entries and commands are
|
|
94
|
+
* what it lists). Set `false` when the app owns its own palette: neither the component nor its
|
|
95
|
+
* action wiring mounts.
|
|
96
96
|
*/
|
|
97
97
|
spotlight?: boolean
|
|
98
98
|
} & Omit<MantineProviderProps, 'children' | 'theme' | 'defaultColorScheme' | 'cssVariablesResolver'>
|
|
@@ -478,31 +478,22 @@ export function BasaltProvider({
|
|
|
478
478
|
})
|
|
479
479
|
const emit = useMemo(() => createLiveEmitter(() => sinkRef.current), [])
|
|
480
480
|
useWindowErrors(emit)
|
|
481
|
-
|
|
482
|
-
//
|
|
483
|
-
useEffect(
|
|
481
|
+
const slots = useSlots()
|
|
482
|
+
// Tells the imperative overlay calls a provider is mounted to commit a slot they register late.
|
|
483
|
+
useEffect(trackProvider, [])
|
|
484
484
|
|
|
485
485
|
const router = useEffectiveRouter(mountedRouter)
|
|
486
486
|
const client = readQueryClient(router)
|
|
487
487
|
useDataTelemetry(router, client, emit)
|
|
488
488
|
|
|
489
489
|
const [devTool, setDevTool] = useState<BasaltDevDockTool | null>(null)
|
|
490
|
-
// Rebuilt on mount and again at every open, so a command's `when()` is read when the palette
|
|
491
|
-
// appears, not frozen at mount — and `detectMac` (inside the builder) never runs during render.
|
|
492
|
-
const [actions, setActions] = useState<SpotlightActionData[]>([])
|
|
493
|
-
const refreshActions = useCallback(
|
|
494
|
-
() => setActions(buildSpotlightActions(router, setDevTool, navCommandLabel)),
|
|
495
|
-
[router, navCommandLabel],
|
|
496
|
-
)
|
|
497
|
-
useEffect(() => {
|
|
498
|
-
if (spotlight) refreshActions()
|
|
499
|
-
}, [spotlight, refreshActions])
|
|
500
|
-
|
|
501
490
|
// `createBasaltTheme(theme)` — plus, when the theme lab's "Apply" switch is on, the delta of its
|
|
502
491
|
// persisted config, so the dev tool moves the numeric `defaultProps` and `theme.spacing` a `<style>`
|
|
503
492
|
// tag cannot reach. No lab override (every production app) → the consumer theme merged onto the
|
|
504
493
|
// Basalt base and nothing else. See `./lab-theme.ts`.
|
|
505
494
|
const resolvedTheme = useLabTheme(theme)
|
|
495
|
+
const { modals: Modals, hotkeys: Hotkeys, spotlight: Spotlight } = slots
|
|
496
|
+
const slotProps: SlotProps = { router, navCommandLabel, openDevTool: setDevTool }
|
|
506
497
|
|
|
507
498
|
return (
|
|
508
499
|
// `...rest` spreads FIRST so a consumer's `MantineProviderProps` can never shadow the three
|
|
@@ -519,34 +510,19 @@ export function BasaltProvider({
|
|
|
519
510
|
<TelemetryContext.Provider value={emit}>
|
|
520
511
|
<SizeClassHintContext.Provider value={sizeClassHint}>
|
|
521
512
|
<QueryClientProvider client={client}>
|
|
522
|
-
{
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
<Suspense fallback={null}>
|
|
532
|
-
<LazyDevDock
|
|
533
|
-
tool={devTool}
|
|
534
|
-
onClose={() => setDevTool(null)}
|
|
535
|
-
router={router}
|
|
536
|
-
/>
|
|
537
|
-
</Suspense>
|
|
538
|
-
)}
|
|
539
|
-
</ModalsProvider>
|
|
513
|
+
{mountedRouter === undefined ? children : <RouterProvider router={mountedRouter} />}
|
|
514
|
+
{/* A sibling, not a wrapper: a late `defineOverlays` mounts it without re-parenting the app. */}
|
|
515
|
+
{Modals !== undefined && <Modals {...slotProps} />}
|
|
516
|
+
{/* `LazyDevDock` is `null` in production (module-scope gate above), so nothing mounts. */}
|
|
517
|
+
{LazyDevDock !== null && (
|
|
518
|
+
<Suspense fallback={null}>
|
|
519
|
+
<LazyDevDock tool={devTool} onClose={() => setDevTool(null)} router={router} />
|
|
520
|
+
</Suspense>
|
|
521
|
+
)}
|
|
540
522
|
</QueryClientProvider>
|
|
541
523
|
<Notifications position="bottom-right" autoClose={4000} limit={5} />
|
|
542
|
-
{
|
|
543
|
-
|
|
544
|
-
actions={actions}
|
|
545
|
-
onSpotlightOpen={refreshActions}
|
|
546
|
-
shortcut="mod + K"
|
|
547
|
-
maxHeight={420}
|
|
548
|
-
/>
|
|
549
|
-
)}
|
|
524
|
+
{Hotkeys !== undefined && <Hotkeys {...slotProps} />}
|
|
525
|
+
{spotlight && Spotlight !== undefined && <Spotlight {...slotProps} />}
|
|
550
526
|
</SizeClassHintContext.Provider>
|
|
551
527
|
</TelemetryContext.Provider>
|
|
552
528
|
</BasaltBridge>
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The modals mount: `ModalsProvider` as a SIBLING of the router, plus the live-provider declaration
|
|
3
|
+
* the imperative overlay calls wait on. Registered by `defineOverlays` / `overlays.*` (never imported
|
|
4
|
+
* by the provider), so an app using none ships none of `@mantine/modals`.
|
|
5
|
+
*/
|
|
6
|
+
import { ModalsProvider } from '@mantine/modals'
|
|
7
|
+
import { useEffect } from 'react'
|
|
8
|
+
import { createLiveCounter } from './notifier'
|
|
9
|
+
import { hasMountedProvider, registerSlot } from './slots'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The live `ModalsProvider`s. `@mantine/modals`' imperative API is a window event bus: with nothing
|
|
13
|
+
* subscribed a dialog never appears and a confirm promise never settles, so the overlay calls wait for
|
|
14
|
+
* this to be non-zero.
|
|
15
|
+
*/
|
|
16
|
+
const liveProviders = createLiveCounter()
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Internal — `ModalsMount` declares its `ModalsProvider` live for as long as it is mounted and gets
|
|
20
|
+
* back the retraction. There is no way to detect the provider from outside React (its context is only
|
|
21
|
+
* readable through `useModals()`, and the overlay calls are imperative), so the registration is explicit.
|
|
22
|
+
* Exported for the confirm tests, which mount their own host.
|
|
23
|
+
*/
|
|
24
|
+
export const registerModalsProvider = liveProviders.acquire
|
|
25
|
+
|
|
26
|
+
/** How long a call waits for a mounted `BasaltProvider` to commit the modals mount it asked for. */
|
|
27
|
+
const COMMIT_WAIT_MS = 5000
|
|
28
|
+
/** With no `BasaltProvider` mounted nothing will commit it; this is only the grace for one mounting now. */
|
|
29
|
+
const NO_PROVIDER_WAIT_MS = 500
|
|
30
|
+
|
|
31
|
+
/** Resolves `true` once a `ModalsProvider` is live, `false` after `timeoutMs` without one. */
|
|
32
|
+
function whenModalsLive(timeoutMs: number): Promise<boolean> {
|
|
33
|
+
if (liveProviders.count() > 0) return Promise.resolve(true)
|
|
34
|
+
return new Promise((resolve) => {
|
|
35
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
36
|
+
let unsubscribe: (() => void) | undefined
|
|
37
|
+
const finish = (isLive: boolean): void => {
|
|
38
|
+
clearTimeout(timer)
|
|
39
|
+
unsubscribe?.()
|
|
40
|
+
resolve(isLive)
|
|
41
|
+
}
|
|
42
|
+
timer = setTimeout(() => finish(false), timeoutMs)
|
|
43
|
+
unsubscribe = liveProviders.subscribe(() => {
|
|
44
|
+
if (liveProviders.count() > 0) finish(true)
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** @internal — put the modals mount in the provider's slots; synchronous, nothing awaited. */
|
|
50
|
+
export function registerModalsMount(): void {
|
|
51
|
+
registerSlot('modals', ModalsMount)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @internal — register the mount, then wait for it to go live: up to 5 s when a `BasaltProvider` is
|
|
56
|
+
* mounted (it commits the slot), 500 ms when none is. `false` means no dialog layer will serve the call.
|
|
57
|
+
*/
|
|
58
|
+
export function ensureModalsMount(): Promise<boolean> {
|
|
59
|
+
registerModalsMount()
|
|
60
|
+
return whenModalsLive(hasMountedProvider() ? COMMIT_WAIT_MS : NO_PROVIDER_WAIT_MS)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function ModalsMount() {
|
|
64
|
+
useEffect(() => registerModalsProvider(), [])
|
|
65
|
+
return <ModalsProvider>{null}</ModalsProvider>
|
|
66
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one subscribe/notify pair behind every module-level store here (slots, the command stash, the
|
|
3
|
+
* modals-live waiters) — shaped for `useSyncExternalStore`. A leaf: no React, nothing at import.
|
|
4
|
+
*/
|
|
5
|
+
export type Notifier = { subscribe: (listener: () => void) => () => void; notify: () => void }
|
|
6
|
+
|
|
7
|
+
export function createNotifier(): Notifier {
|
|
8
|
+
const listeners = new Set<() => void>()
|
|
9
|
+
return {
|
|
10
|
+
subscribe(listener) {
|
|
11
|
+
listeners.add(listener)
|
|
12
|
+
return () => void listeners.delete(listener)
|
|
13
|
+
},
|
|
14
|
+
// Deleting from a Set mid-iteration is safe, so a listener may unsubscribe itself here.
|
|
15
|
+
notify() {
|
|
16
|
+
for (const listener of listeners) listener()
|
|
17
|
+
},
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type LiveCounter = {
|
|
22
|
+
count: () => number
|
|
23
|
+
/** Count one more live thing, notify subscribers, and return its retraction (idempotent). */
|
|
24
|
+
acquire: () => () => void
|
|
25
|
+
subscribe: Notifier['subscribe']
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A COUNTER of live things — not a flag: two overlapping mounts are normal (a route swap mounts the
|
|
30
|
+
* next one before unmounting the previous), and a last-write-wins boolean would report the OLD one's
|
|
31
|
+
* teardown as "none live" while the new one serves.
|
|
32
|
+
*/
|
|
33
|
+
export function createLiveCounter(): LiveCounter {
|
|
34
|
+
let live = 0
|
|
35
|
+
const changes = createNotifier()
|
|
36
|
+
return {
|
|
37
|
+
count: () => live,
|
|
38
|
+
subscribe: changes.subscribe,
|
|
39
|
+
acquire() {
|
|
40
|
+
live++
|
|
41
|
+
changes.notify()
|
|
42
|
+
let released = false
|
|
43
|
+
return () => {
|
|
44
|
+
// A cleanup React runs twice (StrictMode remount) must not drive the count below the real one.
|
|
45
|
+
if (released) return
|
|
46
|
+
released = true
|
|
47
|
+
live--
|
|
48
|
+
changes.notify()
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* so `ready` and `finished` are dropped on the floor.
|
|
9
9
|
*/
|
|
10
10
|
import type { AnyRouter } from '@tanstack/react-router'
|
|
11
|
-
import { flattenNav, getActiveNav } from '../router-tanstack/nav'
|
|
11
|
+
import { flattenNav, getActiveNav } from '../router-tanstack/nav-stash'
|
|
12
12
|
|
|
13
13
|
/** The three `:active-view-transition-type()` names `styles.css` animates. Namespaced: a consumer's own cannot collide. */
|
|
14
14
|
export const ROUTE_TRANSITION_TYPES = {
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The provider's feature slots — a tiny leaf store (React types only, no Mantine, no peers, nothing
|
|
3
|
+
* touched at import so it is SSR-safe). "The bundle follows the definitions": a feature's runtime
|
|
4
|
+
* (hotkeys, Spotlight, modals) enters the import graph only through the `define*` call that needs
|
|
5
|
+
* it, and that call registers its mount here. `BasaltProvider` reads the store with
|
|
6
|
+
* `useSyncExternalStore`, so a registration made after first render (a `define*` call inside a lazily
|
|
7
|
+
* loaded route) mounts a sibling instead of re-parenting the app tree.
|
|
8
|
+
*/
|
|
9
|
+
import { useSyncExternalStore } from 'react'
|
|
10
|
+
import type { ComponentType } from 'react'
|
|
11
|
+
import type { AnyRouter } from '@tanstack/react-router'
|
|
12
|
+
import type { BasaltDevDockTool } from '../dev-dock'
|
|
13
|
+
import type { FlatNavItem } from '../router-tanstack/nav-stash'
|
|
14
|
+
import { createLiveCounter, createNotifier } from './notifier'
|
|
15
|
+
|
|
16
|
+
/** What the provider hands every mount; a mount that needs none of it ignores it. */
|
|
17
|
+
export type SlotProps = {
|
|
18
|
+
router: AnyRouter
|
|
19
|
+
navCommandLabel: ((item: FlatNavItem) => string) | undefined
|
|
20
|
+
openDevTool: (tool: BasaltDevDockTool) => void
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type SlotName = 'hotkeys' | 'spotlight' | 'modals'
|
|
24
|
+
export type Slots = { readonly [K in SlotName]?: ComponentType<SlotProps> }
|
|
25
|
+
|
|
26
|
+
let slots: Slots = {}
|
|
27
|
+
const store = createNotifier()
|
|
28
|
+
|
|
29
|
+
/** @internal — idempotent: re-registering the same component is a no-op (no notify, no new snapshot). */
|
|
30
|
+
export function registerSlot(name: SlotName, component: ComponentType<SlotProps>): void {
|
|
31
|
+
if (slots[name] === component) return
|
|
32
|
+
slots = { ...slots, [name]: component }
|
|
33
|
+
store.notify()
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** @internal — test isolation only. */
|
|
37
|
+
export function resetSlots(): void {
|
|
38
|
+
slots = {}
|
|
39
|
+
store.notify()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const getSlots = (): Slots => slots
|
|
43
|
+
|
|
44
|
+
/** @internal — the registered mounts, read by `BasaltProvider`. */
|
|
45
|
+
export function useSlots(): Slots {
|
|
46
|
+
return useSyncExternalStore(store.subscribe, getSlots, getSlots)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// ── Mounted providers ─────────────────────────────────────────────────────────
|
|
50
|
+
|
|
51
|
+
const providers = createLiveCounter()
|
|
52
|
+
|
|
53
|
+
/** @internal — `BasaltProvider` declares itself mounted; returns the retraction. */
|
|
54
|
+
export const trackProvider = providers.acquire
|
|
55
|
+
|
|
56
|
+
/** @internal — whether a `BasaltProvider` is mounted to commit a slot registered right now. */
|
|
57
|
+
export function hasMountedProvider(): boolean {
|
|
58
|
+
return providers.count() > 0
|
|
59
|
+
}
|
|
@@ -5,12 +5,11 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import type { SpotlightActionData } from '@mantine/spotlight'
|
|
7
7
|
import type { AnyRouter } from '@tanstack/react-router'
|
|
8
|
-
import { getActiveCommands } from '../commands/
|
|
8
|
+
import { getActiveCommands, runCommandSafely } from '../commands/command-stash'
|
|
9
9
|
import { detectMac, formatShortcutDisplay } from '../commands/shortcut-format'
|
|
10
|
-
import { flattenNav, getActiveNav } from '../router-tanstack/nav'
|
|
11
|
-
import type { FlatNavItem } from '../router-tanstack/nav'
|
|
10
|
+
import { flattenNav, getActiveNav } from '../router-tanstack/nav-stash'
|
|
11
|
+
import type { FlatNavItem } from '../router-tanstack/nav-stash'
|
|
12
12
|
import type { BasaltDevDockTool } from '../dev-dock'
|
|
13
|
-
import { runCommandSafely } from './hotkeys'
|
|
14
13
|
|
|
15
14
|
const DEV_TOOLS: ReadonlyArray<{ tool: BasaltDevDockTool; label: string }> = [
|
|
16
15
|
{ tool: 'router', label: 'Router devtools' },
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Spotlight mount: `@mantine/spotlight` plus the generated nav + command actions. Registered by
|
|
3
|
+
* `defineNav` and `defineCommands` (never imported by the provider), so an app defining neither ships
|
|
4
|
+
* none of it.
|
|
5
|
+
*/
|
|
6
|
+
import { Spotlight } from '@mantine/spotlight'
|
|
7
|
+
import type { SpotlightActionData } from '@mantine/spotlight'
|
|
8
|
+
import { useCallback, useEffect, useState } from 'react'
|
|
9
|
+
import { buildSpotlightActions } from './spotlight-actions'
|
|
10
|
+
import type { SlotProps } from './slots'
|
|
11
|
+
|
|
12
|
+
export function SpotlightMount({ router, navCommandLabel, openDevTool }: SlotProps) {
|
|
13
|
+
// Rebuilt on mount and again at every open, so a command's `when()` is read when the palette
|
|
14
|
+
// appears, not frozen at mount — and `detectMac` (inside the builder) never runs during render.
|
|
15
|
+
const [actions, setActions] = useState<SpotlightActionData[]>([])
|
|
16
|
+
const refreshActions = useCallback(
|
|
17
|
+
() => setActions(buildSpotlightActions(router, openDevTool, navCommandLabel)),
|
|
18
|
+
[router, openDevTool, navCommandLabel],
|
|
19
|
+
)
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
// oxlint-disable-next-line react/set-state-in-effect -- reads `navigator` and `when()`: never during render
|
|
22
|
+
refreshActions()
|
|
23
|
+
}, [refreshActions])
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<Spotlight
|
|
27
|
+
actions={actions}
|
|
28
|
+
onSpotlightOpen={refreshActions}
|
|
29
|
+
shortcut="mod + K"
|
|
30
|
+
maxHeight={420}
|
|
31
|
+
/>
|
|
32
|
+
)
|
|
33
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The nav definition's runtime stash and its flatten reader, as a leaf (types only from `./nav`):
|
|
3
|
+
* `defineNav` fills it; Spotlight's action builder, the route transitions and the provider read it.
|
|
4
|
+
* Kept apart from `nav.ts` so no reader imports the module that registers the Spotlight mount.
|
|
5
|
+
*/
|
|
6
|
+
import type { AnyNavGroup, AnyNavItem } from './nav'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The runtime stash `defineNav` fills (the last call wins, like `defineCommands`), read by
|
|
10
|
+
* `BasaltProvider` to project the nav into Spotlight. Type augmentation is erased, so this is the
|
|
11
|
+
* live source.
|
|
12
|
+
*
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
let activeNav: { groups: ReadonlyArray<AnyNavGroup> } | undefined
|
|
16
|
+
|
|
17
|
+
/** @internal */
|
|
18
|
+
export function getActiveNav(): { groups: ReadonlyArray<AnyNavGroup> } | undefined {
|
|
19
|
+
return activeNav
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** @internal — `defineNav` only. The last call wins. */
|
|
23
|
+
export function setActiveNav(nav: { groups: ReadonlyArray<AnyNavGroup> }): void {
|
|
24
|
+
activeNav = nav
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A flattened destination, tagged with the group it was declared in. */
|
|
28
|
+
export type FlatNavItem = AnyNavItem & { groupId: string; groupLabel: string }
|
|
29
|
+
|
|
30
|
+
/** Depth-first flatten, parent then children — the leaf every other surface reads. */
|
|
31
|
+
export function flattenNav(config: { groups: ReadonlyArray<AnyNavGroup> }): FlatNavItem[] {
|
|
32
|
+
const out: FlatNavItem[] = []
|
|
33
|
+
const walk = (items: ReadonlyArray<AnyNavItem>, groupId: string, groupLabel: string): void => {
|
|
34
|
+
for (const item of items) {
|
|
35
|
+
out.push({ ...item, groupId, groupLabel })
|
|
36
|
+
if (item.children) walk(item.children, groupId, groupLabel)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
for (const g of config.groups) walk(g.items, g.id, g.label)
|
|
40
|
+
return out
|
|
41
|
+
}
|
|
@@ -9,10 +9,15 @@
|
|
|
9
9
|
* silently. Compile-verified. A config whose selling point is "typos are compile errors" cannot
|
|
10
10
|
* have a hole exactly where metadata typos live.
|
|
11
11
|
*
|
|
12
|
-
* No JSX and no Mantine in this module — `./router-tanstack` is a headless barrel by contract.
|
|
12
|
+
* No JSX and no Mantine in this module — `./router-tanstack` is a headless barrel by contract. The one
|
|
13
|
+
* exception is `defineNav`'s registration of the Spotlight mount (`../provider/slots`): nav entries
|
|
14
|
+
* feed the palette, and the mount is only reachable through this call.
|
|
13
15
|
*/
|
|
14
16
|
import type { RegisteredRouter } from '@tanstack/react-router'
|
|
15
17
|
import type { ReactNode } from 'react'
|
|
18
|
+
import { registerSlot } from '../provider/slots'
|
|
19
|
+
import { setActiveNav } from './nav-stash'
|
|
20
|
+
import { SpotlightMount } from '../provider/spotlight-mount'
|
|
16
21
|
import type { NavMobilePlacement, NavSectionMobile } from '../shell/nav-types'
|
|
17
22
|
|
|
18
23
|
// ── authoring shapes ─────────────────────────────────────────────────────────────────────────────
|
|
@@ -157,7 +162,9 @@ export function defineNav<
|
|
|
157
162
|
const T extends ReadonlyArray<NavTabId<G>>,
|
|
158
163
|
>(config: RegisteredOr<NavConfig<G, T>>): NavConfig<G, T> {
|
|
159
164
|
const nav = config as unknown as NavConfig<G, T>
|
|
160
|
-
|
|
165
|
+
setActiveNav(nav)
|
|
166
|
+
// Nav entries feed the palette, so defining the nav is what puts Spotlight in the bundle.
|
|
167
|
+
registerSlot('spotlight', SpotlightMount)
|
|
161
168
|
return nav
|
|
162
169
|
}
|
|
163
170
|
|
|
@@ -170,20 +177,6 @@ type RegisteredOr<T> = 0 extends 1 & RegisteredRouter['routeTree']
|
|
|
170
177
|
? 'basalt: declare Register { router }'
|
|
171
178
|
: T
|
|
172
179
|
|
|
173
|
-
/**
|
|
174
|
-
* The runtime stash `defineNav` fills (the last call wins, like `defineCommands`), read by
|
|
175
|
-
* `BasaltProvider` to project the nav into Spotlight. Type augmentation is erased, so this is the
|
|
176
|
-
* live source.
|
|
177
|
-
*
|
|
178
|
-
* @internal
|
|
179
|
-
*/
|
|
180
|
-
let activeNav: { groups: ReadonlyArray<AnyNavGroup> } | undefined
|
|
181
|
-
|
|
182
|
-
/** @internal */
|
|
183
|
-
export function getActiveNav(): { groups: ReadonlyArray<AnyNavGroup> } | undefined {
|
|
184
|
-
return activeNav
|
|
185
|
-
}
|
|
186
|
-
|
|
187
180
|
// ── readers ──────────────────────────────────────────────────────────────────────────────────────
|
|
188
181
|
|
|
189
182
|
function findById(items: ReadonlyArray<AnyNavItem>, id: string): AnyNavItem | undefined {
|
|
@@ -214,18 +207,5 @@ export function navTarget<
|
|
|
214
207
|
throw new Error(`navTarget: no nav item with id "${id}"`)
|
|
215
208
|
}
|
|
216
209
|
|
|
217
|
-
|
|
218
|
-
export type
|
|
219
|
-
|
|
220
|
-
/** Depth-first flatten, parent then children — the leaf every other surface reads. */
|
|
221
|
-
export function flattenNav(config: { groups: ReadonlyArray<AnyNavGroup> }): FlatNavItem[] {
|
|
222
|
-
const out: FlatNavItem[] = []
|
|
223
|
-
const walk = (items: ReadonlyArray<AnyNavItem>, groupId: string, groupLabel: string): void => {
|
|
224
|
-
for (const item of items) {
|
|
225
|
-
out.push({ ...item, groupId, groupLabel })
|
|
226
|
-
if (item.children) walk(item.children, groupId, groupLabel)
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
for (const g of config.groups) walk(g.items, g.id, g.label)
|
|
230
|
-
return out
|
|
231
|
-
}
|
|
210
|
+
export { flattenNav, getActiveNav } from './nav-stash'
|
|
211
|
+
export type { FlatNavItem } from './nav-stash'
|
|
@@ -13,7 +13,7 @@ import { createElement, forwardRef, useMemo } from 'react'
|
|
|
13
13
|
import type { AnchorHTMLAttributes, ElementType, ReactNode } from 'react'
|
|
14
14
|
import { Link, useMatchRoute, useRouterState } from '@tanstack/react-router'
|
|
15
15
|
import type { MobileNavConfig, NavAnchor, SidebarItem, SidebarSection } from '../shell/nav-types'
|
|
16
|
-
import { flattenNav } from './nav'
|
|
16
|
+
import { flattenNav } from './nav-stash'
|
|
17
17
|
import type { AnyNavGroup, AnyNavItem, NavConfig, NavItemId, NavTabId } from './nav'
|
|
18
18
|
|
|
19
19
|
export type UseNavOptions<G extends ReadonlyArray<AnyNavGroup>> = {
|
package/src/surfaces.ts
CHANGED
|
@@ -407,7 +407,7 @@ export const SURFACES = {
|
|
|
407
407
|
guardKinds: [],
|
|
408
408
|
pluginRules: [],
|
|
409
409
|
description:
|
|
410
|
-
'defineCommands (typed command bus), defineOverlays/overlays (a form dialog is a registered overlay opened with overlays.open), setColorScheme, toggleSidebar, ShortcutsHelp, defineNotifications/emit/NotificationBell (emit only accepts kinds registered in defineNotifications; folded in from ./notifications); BasaltProvider mounts Spotlight, Modals
|
|
410
|
+
'defineCommands (typed command bus), defineOverlays/overlays (a form dialog is a registered overlay opened with overlays.open), setColorScheme, toggleSidebar, ShortcutsHelp, defineNotifications/emit/NotificationBell (emit only accepts kinds registered in defineNotifications; folded in from ./notifications); BasaltProvider mounts Notifications; Spotlight mounts once defineNav/defineCommands ran, Modals once defineOverlays/overlays.confirm ran',
|
|
411
411
|
optionalPeers: ['@tanstack/react-hotkeys'],
|
|
412
412
|
forbiddenImports: [],
|
|
413
413
|
},
|