brustjs 0.1.25-alpha → 0.1.26-alpha

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.
@@ -1685,3 +1685,29 @@ code, pre { font-family: var(--font-mono); }
1685
1685
  .dex-type--fairy, .dex-tc__colhead--fairy, .dex-tc__rowhead--fairy { background: var(--magenta-300); }
1686
1686
 
1687
1687
  .dex-hide { display: none !important; }
1688
+
1689
+ /* ----------------------------------------------------------------------------
1690
+ * SPA navigation preloader — a top progress bar rendered by the NavPreloader
1691
+ * React ISLAND (components/NavPreloader.tsx) while brustjs/navigation reports
1692
+ * phase === 'loading'. The island reads the nav store via useNav() and mounts
1693
+ * this bar only during a navigation; the bar is fixed so it escapes the .aa-app
1694
+ * grid and sits above the sticky topbar (z-index:10).
1695
+ * ------------------------------------------------------------------------- */
1696
+ .nav-preloader {
1697
+ position: fixed;
1698
+ inset: 0 0 auto 0;
1699
+ height: 3px;
1700
+ z-index: 9999;
1701
+ overflow: hidden;
1702
+ pointer-events: none;
1703
+ }
1704
+ .nav-preloader__bar {
1705
+ height: 100%;
1706
+ width: 40%;
1707
+ background: var(--aa-gradient, #1B75BB);
1708
+ animation: nav-preloader-slide 0.9s ease-in-out infinite;
1709
+ }
1710
+ @keyframes nav-preloader-slide {
1711
+ from { transform: translateX(-110%); }
1712
+ to { transform: translateX(360%); }
1713
+ }
@@ -10,24 +10,25 @@
10
10
  //
11
11
  // MUST stay single-return with no local bindings above it — a local `const`
12
12
  // would make the compiler soft-fall-back to an SSR component (no <html> shell).
13
- // active-nav uses conditional ELEMENTS (S11), not a className ternary
14
- // (unsupported). AppLayout owns the single <main> (leaves are fragments in the
15
- // <Outlet/> slot) a leaf adding its own <main> breaks SPA-nav extraction.
16
- // See ../FRAMEWORK-GAPS.md.
13
+ // Active-nav is now CLIENT-driven: each <NavLink> is a native directive component
14
+ // whose behavior watches brustjs/navigation's `nav.path` and sets is-active itself
15
+ // (no SSR `active` conditional, no data-brust-active-nav magic). AppLayout owns the
16
+ // single <main> (leaves are fragments in the <Outlet/> slot) — a leaf adding its
17
+ // own <main> breaks SPA-nav extraction. See ../FRAMEWORK-GAPS.md.
17
18
  import { BrustPage, Island, Outlet } from 'brustjs'
18
19
  import type { TeamMember } from '../lib/types'
20
+ import NavLink from './NavLink'
21
+ import NavPreloader from './NavPreloader'
19
22
  import TeamBuilder from './TeamBuilder'
20
23
  import ThemeToggle from './ThemeToggle'
21
24
 
22
25
  export default function AppLayout({
23
26
  title,
24
- active,
25
27
  crumb,
26
28
  teamProps,
27
29
  mode,
28
30
  }: {
29
31
  title: string
30
- active: 'list' | 'typechart'
31
32
  crumb: string
32
33
  teamProps: { teamInitial: TeamMember[] }
33
34
  mode: 'dark' | 'light'
@@ -51,26 +52,8 @@ export default function AppLayout({
51
52
  </div>
52
53
  <nav className="aa-sidebar__nav">
53
54
  <div className="aa-sidebar__group-title">Pokédex</div>
54
- {active === 'list' ? (
55
- <a className="aa-nav-item is-active" href="/">
56
- <span>All Pokémon</span>
57
- </a>
58
- ) : (
59
- <a className="aa-nav-item" href="/">
60
- <span>All Pokémon</span>
61
- </a>
62
- )}
63
- {active === 'typechart' ? (
64
- <a className="aa-nav-item is-active" href="/type-chart">
65
- <span>Type chart</span>
66
- <span className="aa-nav-item__count">native</span>
67
- </a>
68
- ) : (
69
- <a className="aa-nav-item" href="/type-chart">
70
- <span>Type chart</span>
71
- <span className="aa-nav-item__count">native</span>
72
- </a>
73
- )}
55
+ <NavLink native href="/" label="All Pokémon" />
56
+ <NavLink native href="/type-chart" label="Type chart" count="native" />
74
57
  </nav>
75
58
  <div className="aa-sidebar__user">
76
59
  <span className="aa-avatar aa-avatar--sm dex-brand-avatar">B</span>
@@ -99,6 +82,7 @@ export default function AppLayout({
99
82
  </main>
100
83
 
101
84
  <Island component={TeamBuilder} props={teamProps} ssr hydrate="load" />
85
+ <Island component={NavPreloader} hydrate="load" />
102
86
  </div>
103
87
  </BrustPage>
104
88
  )
@@ -0,0 +1,50 @@
1
+ // NATIVE INTERACTIVE COMPONENT — a sidebar nav link whose active state is driven
2
+ // by the navigation store, WATCHED in the behavior and applied by the author (no
3
+ // data-brust-active-nav magic attribute). This is the "consume the nav store
4
+ // yourself" pattern: `nav` from brustjs/navigation is a plain reactive source
5
+ // (signals shared cross-chunk via brustjs/store's Symbol.for tracker), so a
6
+ // `computed` over `nav.path()` re-runs on every SPA navigation — the same store
7
+ // the React island reads via useNav(). The bootstrap-owned navigator commits
8
+ // nav.path; this behavior reacts.
9
+ //
10
+ // Single-file component: `export const behavior` → _directives.js (react-free);
11
+ // the JSX default → the native template the compiler lowers to minijinja.
12
+ import { nav } from 'brustjs/navigation'
13
+ import { computed } from 'brustjs/store'
14
+
15
+ // behavior → registered as "navLink". Reads the link's own href off the element
16
+ // (no x-props needed), watches nav.path, and returns the active className +
17
+ // aria-current. x-bind-class sets the full className; x-bind-aria-current with a
18
+ // null value removes the attribute (see runtime setBound).
19
+ export const behavior = ({ el }: { el: HTMLElement }) => {
20
+ const linkPath = new URL((el as HTMLAnchorElement).href, location.href).pathname
21
+ const cls = computed(() => (nav.path() === linkPath ? 'aa-nav-item is-active' : 'aa-nav-item'))
22
+ const current = computed(() => (nav.path() === linkPath ? 'page' : null))
23
+ return { cls, current }
24
+ }
25
+
26
+ // default → jinja. The SSR className is the inactive base; the behavior sets
27
+ // is-active on bind and on every SPA nav. `count` is an optional native inline
28
+ // conditional (the "native" chip on the type-chart link).
29
+ export default function NavLink({
30
+ href,
31
+ label,
32
+ count,
33
+ }: {
34
+ href: string
35
+ label: string
36
+ count?: string
37
+ }) {
38
+ return (
39
+ <a
40
+ x-data="navLink"
41
+ x-bind-class="cls"
42
+ x-bind-aria-current="current"
43
+ className="aa-nav-item"
44
+ href={href}
45
+ >
46
+ <span>{label}</span>
47
+ {count ? <span className="aa-nav-item__count">{count}</span> : null}
48
+ </a>
49
+ )
50
+ }
@@ -0,0 +1,21 @@
1
+ // REACT ISLAND demonstrating useNav() — a top "preloader" progress bar shown
2
+ // while an SPA navigation is in flight. This is the React-island counterpart to
3
+ // NavLink's native-behavior consumption of the SAME navigation store: the native
4
+ // behavior sets a class via x-bind, this island re-renders via React. Both read
5
+ // `brustjs/navigation` through brustjs/store's cross-chunk signal tracker.
6
+ //
7
+ // Placed OUTSIDE <main> in AppLayout so the bootstrap navigator (which only
8
+ // unmounts/​swaps islands inside <main>) never tears it down during the very
9
+ // navigation it is indicating. It renders nothing when idle, and an indeterminate
10
+ // bar while `phase === 'loading'`.
11
+ import { useNav } from 'brustjs/client'
12
+
13
+ export default function NavPreloader() {
14
+ const { phase } = useNav()
15
+ if (phase !== 'loading') return null
16
+ return (
17
+ <div className="nav-preloader" role="progressbar" aria-label="กำลังโหลดหน้า">
18
+ <div className="nav-preloader__bar" />
19
+ </div>
20
+ )
21
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brustjs",
3
- "version": "0.1.25-alpha",
3
+ "version": "0.1.26-alpha",
4
4
  "description": "Bun + Rust SSR framework — React on the server, Rust everywhere else (napi cdylib + per-worker SharedArrayBuffer).",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -40,12 +40,12 @@
40
40
  "typescript": "^6.0.3"
41
41
  },
42
42
  "optionalDependencies": {
43
- "brustjs-darwin-x64": "0.1.25-alpha",
44
- "brustjs-darwin-arm64": "0.1.25-alpha",
45
- "brustjs-linux-x64-gnu": "0.1.25-alpha",
46
- "brustjs-linux-arm64-gnu": "0.1.25-alpha",
47
- "brustjs-linux-x64-musl": "0.1.25-alpha",
48
- "brustjs-linux-arm64-musl": "0.1.25-alpha"
43
+ "brustjs-darwin-x64": "0.1.26-alpha",
44
+ "brustjs-darwin-arm64": "0.1.26-alpha",
45
+ "brustjs-linux-x64-gnu": "0.1.26-alpha",
46
+ "brustjs-linux-arm64-gnu": "0.1.26-alpha",
47
+ "brustjs-linux-x64-musl": "0.1.26-alpha",
48
+ "brustjs-linux-arm64-musl": "0.1.26-alpha"
49
49
  },
50
50
  "peerDependencies": {
51
51
  "react": "^19.2.6",
@@ -69,7 +69,8 @@
69
69
  "./client": "./runtime/client/index.ts",
70
70
  "./create": "./runtime/create.ts",
71
71
  "./store": "./runtime/store/index.ts",
72
- "./native": "./runtime/native/index.ts"
72
+ "./native": "./runtime/native/index.ts",
73
+ "./navigation": "./runtime/navigation/index.ts"
73
74
  },
74
75
  "files": [
75
76
  "runtime",
@@ -26,3 +26,7 @@ export type { TreatyResponse, ClientOptions } from '../treaty.ts'
26
26
  // surface and cannot be bundled for the browser. react.ts → define-store.ts is
27
27
  // browser-safe (no node:async_hooks; the server resolver is injected separately).
28
28
  export { useStore } from '../store/react.ts'
29
+
30
+ // Navigation-state view adapter (same rationale as useStore — browser/island
31
+ // entry only). brustjs/navigation itself stays React-free; the hook lives here.
32
+ export { useNav } from '../navigation/react.ts'
package/runtime/index.js CHANGED
@@ -77,8 +77,8 @@ function requireNative() {
77
77
  try {
78
78
  const binding = require('brustjs-android-arm64')
79
79
  const bindingPackageVersion = require('brustjs-android-arm64/package.json').version
80
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
81
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
80
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
81
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
82
82
  }
83
83
  return binding
84
84
  } catch (e) {
@@ -93,8 +93,8 @@ function requireNative() {
93
93
  try {
94
94
  const binding = require('brustjs-android-arm-eabi')
95
95
  const bindingPackageVersion = require('brustjs-android-arm-eabi/package.json').version
96
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
97
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
96
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
97
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
98
98
  }
99
99
  return binding
100
100
  } catch (e) {
@@ -114,8 +114,8 @@ function requireNative() {
114
114
  try {
115
115
  const binding = require('brustjs-win32-x64-gnu')
116
116
  const bindingPackageVersion = require('brustjs-win32-x64-gnu/package.json').version
117
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
118
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
117
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
118
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
119
119
  }
120
120
  return binding
121
121
  } catch (e) {
@@ -130,8 +130,8 @@ function requireNative() {
130
130
  try {
131
131
  const binding = require('brustjs-win32-x64-msvc')
132
132
  const bindingPackageVersion = require('brustjs-win32-x64-msvc/package.json').version
133
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
134
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
133
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
134
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
135
135
  }
136
136
  return binding
137
137
  } catch (e) {
@@ -147,8 +147,8 @@ function requireNative() {
147
147
  try {
148
148
  const binding = require('brustjs-win32-ia32-msvc')
149
149
  const bindingPackageVersion = require('brustjs-win32-ia32-msvc/package.json').version
150
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
151
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
150
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
151
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
152
152
  }
153
153
  return binding
154
154
  } catch (e) {
@@ -163,8 +163,8 @@ function requireNative() {
163
163
  try {
164
164
  const binding = require('brustjs-win32-arm64-msvc')
165
165
  const bindingPackageVersion = require('brustjs-win32-arm64-msvc/package.json').version
166
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
167
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
166
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
167
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
168
168
  }
169
169
  return binding
170
170
  } catch (e) {
@@ -182,8 +182,8 @@ function requireNative() {
182
182
  try {
183
183
  const binding = require('brustjs-darwin-universal')
184
184
  const bindingPackageVersion = require('brustjs-darwin-universal/package.json').version
185
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
186
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
185
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
186
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
187
187
  }
188
188
  return binding
189
189
  } catch (e) {
@@ -198,8 +198,8 @@ function requireNative() {
198
198
  try {
199
199
  const binding = require('brustjs-darwin-x64')
200
200
  const bindingPackageVersion = require('brustjs-darwin-x64/package.json').version
201
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
202
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
201
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
202
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
203
203
  }
204
204
  return binding
205
205
  } catch (e) {
@@ -214,8 +214,8 @@ function requireNative() {
214
214
  try {
215
215
  const binding = require('brustjs-darwin-arm64')
216
216
  const bindingPackageVersion = require('brustjs-darwin-arm64/package.json').version
217
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
218
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
217
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
218
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
219
219
  }
220
220
  return binding
221
221
  } catch (e) {
@@ -234,8 +234,8 @@ function requireNative() {
234
234
  try {
235
235
  const binding = require('brustjs-freebsd-x64')
236
236
  const bindingPackageVersion = require('brustjs-freebsd-x64/package.json').version
237
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
238
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
237
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
238
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
239
239
  }
240
240
  return binding
241
241
  } catch (e) {
@@ -250,8 +250,8 @@ function requireNative() {
250
250
  try {
251
251
  const binding = require('brustjs-freebsd-arm64')
252
252
  const bindingPackageVersion = require('brustjs-freebsd-arm64/package.json').version
253
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
254
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
253
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
254
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
255
255
  }
256
256
  return binding
257
257
  } catch (e) {
@@ -271,8 +271,8 @@ function requireNative() {
271
271
  try {
272
272
  const binding = require('brustjs-linux-x64-musl')
273
273
  const bindingPackageVersion = require('brustjs-linux-x64-musl/package.json').version
274
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
275
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
274
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
275
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
276
276
  }
277
277
  return binding
278
278
  } catch (e) {
@@ -287,8 +287,8 @@ function requireNative() {
287
287
  try {
288
288
  const binding = require('brustjs-linux-x64-gnu')
289
289
  const bindingPackageVersion = require('brustjs-linux-x64-gnu/package.json').version
290
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
291
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
290
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
291
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
292
292
  }
293
293
  return binding
294
294
  } catch (e) {
@@ -305,8 +305,8 @@ function requireNative() {
305
305
  try {
306
306
  const binding = require('brustjs-linux-arm64-musl')
307
307
  const bindingPackageVersion = require('brustjs-linux-arm64-musl/package.json').version
308
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
309
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
308
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
309
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
310
310
  }
311
311
  return binding
312
312
  } catch (e) {
@@ -321,8 +321,8 @@ function requireNative() {
321
321
  try {
322
322
  const binding = require('brustjs-linux-arm64-gnu')
323
323
  const bindingPackageVersion = require('brustjs-linux-arm64-gnu/package.json').version
324
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
325
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
324
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
325
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
326
326
  }
327
327
  return binding
328
328
  } catch (e) {
@@ -339,8 +339,8 @@ function requireNative() {
339
339
  try {
340
340
  const binding = require('brustjs-linux-arm-musleabihf')
341
341
  const bindingPackageVersion = require('brustjs-linux-arm-musleabihf/package.json').version
342
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
343
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
342
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
343
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
344
344
  }
345
345
  return binding
346
346
  } catch (e) {
@@ -355,8 +355,8 @@ function requireNative() {
355
355
  try {
356
356
  const binding = require('brustjs-linux-arm-gnueabihf')
357
357
  const bindingPackageVersion = require('brustjs-linux-arm-gnueabihf/package.json').version
358
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
359
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
358
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
359
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
360
360
  }
361
361
  return binding
362
362
  } catch (e) {
@@ -373,8 +373,8 @@ function requireNative() {
373
373
  try {
374
374
  const binding = require('brustjs-linux-loong64-musl')
375
375
  const bindingPackageVersion = require('brustjs-linux-loong64-musl/package.json').version
376
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
377
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
376
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
377
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
378
378
  }
379
379
  return binding
380
380
  } catch (e) {
@@ -389,8 +389,8 @@ function requireNative() {
389
389
  try {
390
390
  const binding = require('brustjs-linux-loong64-gnu')
391
391
  const bindingPackageVersion = require('brustjs-linux-loong64-gnu/package.json').version
392
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
393
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
392
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
393
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
394
394
  }
395
395
  return binding
396
396
  } catch (e) {
@@ -407,8 +407,8 @@ function requireNative() {
407
407
  try {
408
408
  const binding = require('brustjs-linux-riscv64-musl')
409
409
  const bindingPackageVersion = require('brustjs-linux-riscv64-musl/package.json').version
410
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
411
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
410
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
411
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
412
412
  }
413
413
  return binding
414
414
  } catch (e) {
@@ -423,8 +423,8 @@ function requireNative() {
423
423
  try {
424
424
  const binding = require('brustjs-linux-riscv64-gnu')
425
425
  const bindingPackageVersion = require('brustjs-linux-riscv64-gnu/package.json').version
426
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
427
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
426
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
427
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
428
428
  }
429
429
  return binding
430
430
  } catch (e) {
@@ -440,8 +440,8 @@ function requireNative() {
440
440
  try {
441
441
  const binding = require('brustjs-linux-ppc64-gnu')
442
442
  const bindingPackageVersion = require('brustjs-linux-ppc64-gnu/package.json').version
443
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
444
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
443
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
444
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
445
445
  }
446
446
  return binding
447
447
  } catch (e) {
@@ -456,8 +456,8 @@ function requireNative() {
456
456
  try {
457
457
  const binding = require('brustjs-linux-s390x-gnu')
458
458
  const bindingPackageVersion = require('brustjs-linux-s390x-gnu/package.json').version
459
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
460
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
459
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
460
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
461
461
  }
462
462
  return binding
463
463
  } catch (e) {
@@ -476,8 +476,8 @@ function requireNative() {
476
476
  try {
477
477
  const binding = require('brustjs-openharmony-arm64')
478
478
  const bindingPackageVersion = require('brustjs-openharmony-arm64/package.json').version
479
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
480
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
479
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
480
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
481
481
  }
482
482
  return binding
483
483
  } catch (e) {
@@ -492,8 +492,8 @@ function requireNative() {
492
492
  try {
493
493
  const binding = require('brustjs-openharmony-x64')
494
494
  const bindingPackageVersion = require('brustjs-openharmony-x64/package.json').version
495
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
496
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
495
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
496
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
497
497
  }
498
498
  return binding
499
499
  } catch (e) {
@@ -508,8 +508,8 @@ function requireNative() {
508
508
  try {
509
509
  const binding = require('brustjs-openharmony-arm')
510
510
  const bindingPackageVersion = require('brustjs-openharmony-arm/package.json').version
511
- if (bindingPackageVersion !== '0.1.25-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
512
- throw new Error(`Native binding package version mismatch, expected 0.1.25-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
511
+ if (bindingPackageVersion !== '0.1.26-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
512
+ throw new Error(`Native binding package version mismatch, expected 0.1.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
513
513
  }
514
514
  return binding
515
515
  } catch (e) {
@@ -16,6 +16,8 @@
16
16
  import { createRoot, hydrateRoot, type Root } from 'react-dom/client'
17
17
  import { createElement } from 'react'
18
18
  import { applyStoreSnapshot } from '../store/client-hydrate.ts'
19
+ import { __navStart, __navCommit, __navError, __navInit } from '../navigation/store.ts'
20
+ import { installActiveNav } from '../navigation/active-nav.ts'
19
21
 
20
22
  // Track React roots created by hydrateOne so we can unmount them before
21
23
  // removing their DOM in swapMainContent. Without this, removing the DOM
@@ -205,11 +207,12 @@ export function isInternalLink(a: HTMLAnchorElement, event: MouseEvent): boolean
205
207
 
206
208
  let inFlight: AbortController | null = null
207
209
 
208
- async function navigate(url: URL, push: boolean): Promise<void> {
210
+ export async function navigate(url: URL, push: boolean): Promise<void> {
209
211
  inFlight?.abort()
210
212
  const ac = new AbortController()
211
213
  inFlight = ac
212
214
  try {
215
+ __navStart(url.pathname, url.search)
213
216
  const resp = await fetch(`/_brust/page${url.pathname}${url.search}`, {
214
217
  signal: ac.signal,
215
218
  headers: { Accept: 'application/json' },
@@ -229,8 +232,10 @@ async function navigate(url: URL, push: boolean): Promise<void> {
229
232
  if (push) history.pushState({}, '', url.href)
230
233
  window.scrollTo(0, 0)
231
234
  hydrateMarkersIn(main as HTMLElement)
235
+ __navCommit(url.pathname, url.search)
232
236
  } catch (err) {
233
237
  if ((err as Error).name === 'AbortError') return
238
+ __navError(url.pathname, err)
234
239
  console.warn('[brust] SPA navigation failed, falling back to full reload:', err)
235
240
  location.href = url.href
236
241
  } finally {
@@ -260,10 +265,14 @@ function installInterceptor(): void {
260
265
  if (typeof document !== 'undefined') {
261
266
  if (document.readyState === 'loading') {
262
267
  document.addEventListener('DOMContentLoaded', () => {
268
+ __navInit(location.pathname, location.search)
269
+ installActiveNav()
263
270
  hydrateMarkersIn(document.body)
264
271
  installInterceptor()
265
272
  })
266
273
  } else {
274
+ __navInit(location.pathname, location.search)
275
+ installActiveNav()
267
276
  hydrateMarkersIn(document.body)
268
277
  installInterceptor()
269
278
  }
@@ -13,8 +13,11 @@ export interface IslandProps<P> {
13
13
  * component (no anonymous default export). */
14
14
  component: ComponentType<P>
15
15
  /** Props passed to the component on both server and client. Must be
16
- * JSON-serializable (no functions, classes, DOM nodes, etc.). */
17
- props: P
16
+ * JSON-serializable (no functions, classes, DOM nodes, etc.). Optional — a
17
+ * propless island (e.g. one that reads only global/client state) may omit it;
18
+ * it defaults to `{}`. On native routes, omitting `props` lowers to an empty
19
+ * props_path the renderer fills with `{}`. */
20
+ props?: P
18
21
  /** When to hydrate. Default 'load'. */
19
22
  hydrate?: HydrateTrigger
20
23
  /** Native routes only: render this island server-side (renderToString during
@@ -74,7 +77,8 @@ export function Island<P extends object>({
74
77
  'export or a minified/inlined function.',
75
78
  )
76
79
  }
77
- const propsJson = JSON.stringify(props)
80
+ const resolvedProps = (props ?? {}) as P
81
+ const propsJson = JSON.stringify(resolvedProps)
78
82
  return createElement(
79
83
  'div',
80
84
  {
@@ -82,6 +86,6 @@ export function Island<P extends object>({
82
86
  'data-brust-props': propsJson,
83
87
  'data-brust-hydrate': hydrate,
84
88
  },
85
- createElement(Component, props),
89
+ createElement(Component, resolvedProps),
86
90
  )
87
91
  }
@@ -149,7 +149,9 @@ export async function resolveIslandContext(
149
149
  ): Promise<Record<string, string>> {
150
150
  const out: Record<string, string> = {}
151
151
  for (const entry of manifest) {
152
- const props = pathInto(data, entry.propsPath)
152
+ // Empty propsPath = a propless island → `{}`. (pathInto('') returns the whole
153
+ // context, which is NOT what an absent `props` attr means.)
154
+ const props = entry.propsPath === '' ? {} : pathInto(data, entry.propsPath)
153
155
  // `?? null` handles undefined props; the `?? 'null'` belt-and-braces covers
154
156
  // the case where JSON.stringify itself returns undefined (e.g. a function
155
157
  // value), so entityEncode never receives undefined. Hoisted ABOVE the ssr
@@ -0,0 +1,75 @@
1
+ // runtime/navigation/active-nav.ts
2
+ // The DOM consumer of the navigation store. Installs two effects:
3
+ // - nav.path → reconcile is-active/aria-current on links in [data-brust-active-nav]
4
+ // - nav.phase → mirror to <html data-brust-nav> for CSS-driven indicators
5
+ // Attribute name [data-brust-active-nav] is deliberately distinct from the
6
+ // <html data-brust-nav> phase attr so the reconciler never treats <html> as a
7
+ // container (which would toggle is-active on every <a> in the document).
8
+ import { effect } from '../store/signal.ts'
9
+ import { nav, type NavPhase } from './store.ts'
10
+
11
+ // The install guard lives on globalThis (like __BRUST_NAV__), NOT as a module
12
+ // `let`: every Bun.build island chunk inlines its own copy of this module, so a
13
+ // module-local flag would not stop a second chunk from installing a duplicate
14
+ // pair of effects (both resolve the same singleton signals → double reconcile).
15
+ interface ActiveNavState {
16
+ installed: boolean
17
+ disposers: Array<() => void>
18
+ }
19
+ const GA = globalThis as { __BRUST_ACTIVE_NAV__?: ActiveNavState }
20
+ function activeNavState(): ActiveNavState {
21
+ if (!GA.__BRUST_ACTIVE_NAV__) GA.__BRUST_ACTIVE_NAV__ = { installed: false, disposers: [] }
22
+ return GA.__BRUST_ACTIVE_NAV__
23
+ }
24
+
25
+ export function installActiveNav(): void {
26
+ const a = activeNavState()
27
+ if (a.installed) return
28
+ a.installed = true
29
+ // effect() runs eagerly once and re-runs whenever a signal it read changes;
30
+ // it returns a disposer we keep so __resetActiveNavForTest can unsubscribe.
31
+ a.disposers = [
32
+ effect(() => {
33
+ const path = nav.path()
34
+ reconcileActiveLinks(path)
35
+ }),
36
+ effect(() => {
37
+ const phase = nav.phase()
38
+ setHtmlNav(phase)
39
+ }),
40
+ ]
41
+ }
42
+
43
+ function setHtmlNav(phase: NavPhase): void {
44
+ if (typeof document === 'undefined' || !document.documentElement) return
45
+ // 'success' is a transient logical phase; the DOM attr returns to idle so
46
+ // loading indicators clear once committed.
47
+ document.documentElement.setAttribute('data-brust-nav', phase === 'success' ? 'idle' : phase)
48
+ }
49
+
50
+ function reconcileActiveLinks(currentPath: string): void {
51
+ if (typeof document === 'undefined') return
52
+ const containers = document.querySelectorAll<HTMLElement>('[data-brust-active-nav]')
53
+ for (const el of Array.from(containers)) {
54
+ const activeClass = el.dataset.brustActiveClass || 'is-active'
55
+ const prefix = el.dataset.brustActiveMatch === 'prefix'
56
+ const links = el.querySelectorAll<HTMLAnchorElement>('a[href]')
57
+ for (const a of Array.from(links)) {
58
+ const linkPath = new URL(a.href, location.href).pathname
59
+ const isActive = prefix
60
+ ? currentPath === linkPath || currentPath.startsWith(`${linkPath.replace(/\/$/, '')}/`)
61
+ : currentPath === linkPath
62
+ a.classList.toggle(activeClass, isActive)
63
+ if (isActive) a.setAttribute('aria-current', 'page')
64
+ else a.removeAttribute('aria-current')
65
+ }
66
+ }
67
+ }
68
+
69
+ // Test-only: dispose the installed effects and clear the guard so a subsequent
70
+ // installActiveNav() rebinds to the fresh singleton (after __resetNavForTest).
71
+ export function __resetActiveNavForTest(): void {
72
+ const a = activeNavState()
73
+ for (const d of a.disposers) d()
74
+ GA.__BRUST_ACTIVE_NAV__ = undefined
75
+ }
@@ -0,0 +1,13 @@
1
+ // runtime/navigation/index.ts — brustjs/navigation. React-free, DOM-free entry.
2
+ // (active-nav.ts uses DOM globals but imports no DOM module; it's re-exported for
3
+ // authors who add nav containers dynamically. bootstrap wires it automatically.)
4
+ export type { NavPhase, NavState, NavStore } from './store.ts'
5
+ export {
6
+ nav,
7
+ getNavState,
8
+ subscribe,
9
+ onBeforeNavigate,
10
+ onNavigate,
11
+ onNavigateError,
12
+ } from './store.ts'
13
+ export { installActiveNav } from './active-nav.ts'
@@ -0,0 +1,24 @@
1
+ // React view-layer adapter for the navigation store. Lives OUTSIDE navigation/
2
+ // index.ts (which stays React-free + in the typecheck:treaty gate) and is reached
3
+ // only via brustjs/client, the browser/island entry. Uses useSyncExternalStore so
4
+ // a React island re-renders on every navigation transition; getNavState is
5
+ // version-memoized (store.ts) so the snapshot is referentially stable.
6
+ import { useSyncExternalStore } from 'react'
7
+ import { getNavState, subscribe, type NavState } from './store.ts'
8
+
9
+ /** Watch the navigation state from a React island.
10
+ *
11
+ * ```tsx
12
+ * import { useNav } from 'brustjs/client'
13
+ * function Crumb() {
14
+ * const { path, phase } = useNav()
15
+ * return <span>{phase === 'loading' ? 'Loading…' : path}</span>
16
+ * }
17
+ * ```
18
+ *
19
+ * The third arg (server snapshot) is the same getter — on the server the store
20
+ * holds its idle defaults (no `location`), so SSR renders the idle state and the
21
+ * client hydrates the real one on mount. */
22
+ export function useNav(): NavState {
23
+ return useSyncExternalStore(subscribe, getNavState, getNavState)
24
+ }
@@ -0,0 +1,208 @@
1
+ // runtime/navigation/store.ts
2
+ // Client-side navigation state for brust, driven by the SPA navigator in
3
+ // runtime/islands/bootstrap.ts. Fully React-free AND DOM-free: __navInit takes
4
+ // the initial path/search as arguments (bootstrap reads location), so this module
5
+ // imports nothing web-API and unit-tests without a DOM. The DOM consumer (active
6
+ // links + html[data-brust-nav]) lives in ./active-nav.ts.
7
+ //
8
+ // The `nav` bag of signals is a singleton stashed on globalThis.__BRUST_NAV__:
9
+ // signals share their reactive tracker cross-chunk via Symbol.for (see
10
+ // store/signal.ts), but the bag OBJECT must also be shared or each Bun.build
11
+ // chunk would build its own — the stash gives one object identity for all chunks.
12
+ import { batch, signal, type Signal } from '../store/signal.ts'
13
+
14
+ export type NavPhase = 'idle' | 'loading' | 'success' | 'error'
15
+
16
+ export interface NavState {
17
+ path: string
18
+ search: string
19
+ phase: NavPhase
20
+ error: Error | null
21
+ from: string | null
22
+ to: string | null
23
+ }
24
+
25
+ export interface NavStore {
26
+ path: Signal<string>
27
+ search: Signal<string>
28
+ phase: Signal<NavPhase>
29
+ error: Signal<Error | null>
30
+ from: Signal<string | null>
31
+ to: Signal<string | null>
32
+ }
33
+
34
+ type BeforeCb = (e: { from: string; to: string }) => void
35
+ type NavCb = (e: NavState) => void
36
+ type ErrorCb = (e: { to: string; error: Error }) => void
37
+
38
+ interface NavInternal extends NavStore {
39
+ _subs: Set<NavCb>
40
+ _before: Set<BeforeCb>
41
+ _success: Set<NavCb>
42
+ _error: Set<ErrorCb>
43
+ // version bumps once per committed transition (in emit); `_snap` memoizes the
44
+ // NavState for that version so getNavState() returns a referentially-stable
45
+ // object — required by React's useSyncExternalStore (a fresh object each call
46
+ // would infinite-loop "getSnapshot should be cached"). Mirrors defineStore.
47
+ _version: number
48
+ _snap: { value: NavState; version: number } | null
49
+ }
50
+
51
+ function createNav(): NavInternal {
52
+ return {
53
+ path: signal(''),
54
+ search: signal(''),
55
+ phase: signal<NavPhase>('idle'),
56
+ error: signal<Error | null>(null),
57
+ from: signal<string | null>(null),
58
+ to: signal<string | null>(null),
59
+ _subs: new Set(),
60
+ _before: new Set(),
61
+ _success: new Set(),
62
+ _error: new Set(),
63
+ _version: 0,
64
+ _snap: null,
65
+ }
66
+ }
67
+
68
+ const G = globalThis as { __BRUST_NAV__?: NavInternal }
69
+ function store(): NavInternal {
70
+ if (!G.__BRUST_NAV__) G.__BRUST_NAV__ = createNav()
71
+ return G.__BRUST_NAV__
72
+ }
73
+
74
+ // Public reactive handle. A Proxy resolves the singleton on each property access
75
+ // so (a) every chunk's `nav` points at the one globalThis bag, and (b) tests that
76
+ // reset the singleton see the fresh signals.
77
+ export const nav: NavStore = new Proxy({} as NavStore, {
78
+ get(_t, prop: string | symbol) {
79
+ // Symbol keys (Symbol.toPrimitive, Symbol.for('brust.signal') brand probes,
80
+ // React DevTools, etc.) are never store fields — return undefined without
81
+ // forcing a lazy singleton init.
82
+ if (typeof prop !== 'string') return undefined
83
+ return (store() as unknown as Record<string, unknown>)[prop]
84
+ },
85
+ })
86
+
87
+ export function getNavState(): NavState {
88
+ const s = store()
89
+ // Return the memoized snapshot if no transition has happened since it was
90
+ // built (referential stability for useSyncExternalStore).
91
+ if (s._snap && s._snap.version === s._version) return s._snap.value
92
+ const value: NavState = {
93
+ path: s.path(),
94
+ search: s.search(),
95
+ phase: s.phase(),
96
+ error: s.error(),
97
+ from: s.from(),
98
+ to: s.to(),
99
+ }
100
+ s._snap = { value, version: s._version }
101
+ return value
102
+ }
103
+
104
+ // Bump the snapshot version immediately after a batch of signal writes so the
105
+ // NEXT getNavState() rebuilds (the lifecycle callbacks + emit must see committed
106
+ // state, not the stale memoized snapshot). Called once per transition, right
107
+ // after the batch and before any callback/getNavState read.
108
+ function bumpVersion(s: NavInternal): void {
109
+ s._version += 1
110
+ }
111
+
112
+ function emit(s: NavInternal): void {
113
+ const snap = getNavState()
114
+ for (const cb of [...s._subs]) cb(snap)
115
+ }
116
+
117
+ export function subscribe(cb: NavCb): () => void {
118
+ const s = store()
119
+ s._subs.add(cb)
120
+ return () => s._subs.delete(cb)
121
+ }
122
+ export function onBeforeNavigate(cb: BeforeCb): () => void {
123
+ const s = store()
124
+ s._before.add(cb)
125
+ return () => s._before.delete(cb)
126
+ }
127
+ export function onNavigate(cb: NavCb): () => void {
128
+ const s = store()
129
+ s._success.add(cb)
130
+ return () => s._success.delete(cb)
131
+ }
132
+ export function onNavigateError(cb: ErrorCb): () => void {
133
+ const s = store()
134
+ s._error.add(cb)
135
+ return () => s._error.delete(cb)
136
+ }
137
+
138
+ // Each lifecycle mutator writes several signals. We wrap the writes in batch()
139
+ // so a consumer effect reading multiple fields (e.g. nav.path() + nav.phase())
140
+ // re-runs ONCE on the settled state instead of firing on each torn intermediate
141
+ // write. The lifecycle callbacks (_before/_success/_error) and emit() run AFTER
142
+ // the batch so they observe fully-committed state.
143
+ export function __navInit(path: string, search: string): void {
144
+ const s = store()
145
+ batch(() => {
146
+ s.path.set(path)
147
+ s.search.set(search)
148
+ s.phase.set('idle')
149
+ s.from.set(null)
150
+ s.to.set(null)
151
+ s.error.set(null)
152
+ })
153
+ bumpVersion(s)
154
+ emit(s)
155
+ }
156
+
157
+ // `toSearch` is intentionally unused during 'loading': search is part of the
158
+ // COMMITTED state and is written by __navCommit, not while the fetch is pending.
159
+ // Kept in the signature for call-site symmetry with __navCommit.
160
+ export function __navStart(toPath: string, _toSearch: string): void {
161
+ const s = store()
162
+ const from = s.path()
163
+ batch(() => {
164
+ s.from.set(from)
165
+ s.to.set(toPath)
166
+ s.error.set(null)
167
+ s.phase.set('loading')
168
+ })
169
+ bumpVersion(s)
170
+ for (const cb of [...s._before]) cb({ from, to: toPath })
171
+ emit(s)
172
+ }
173
+
174
+ export function __navCommit(toPath: string, toSearch: string): void {
175
+ const s = store()
176
+ batch(() => {
177
+ s.path.set(toPath)
178
+ s.search.set(toSearch)
179
+ s.to.set(null)
180
+ s.error.set(null)
181
+ s.phase.set('success')
182
+ })
183
+ bumpVersion(s)
184
+ const snap = getNavState()
185
+ for (const cb of [...s._success]) cb(snap)
186
+ emit(s)
187
+ }
188
+
189
+ // Accepts `unknown` because the only caller is bootstrap's catch block (err is
190
+ // `unknown` under strict TS); coerce non-Error throws (strings, DOMException)
191
+ // into an Error so consumers always get a stable shape.
192
+ export function __navError(toPath: string, error: unknown): void {
193
+ const s = store()
194
+ const err = error instanceof Error ? error : new Error(String(error))
195
+ batch(() => {
196
+ s.to.set(null)
197
+ s.error.set(err)
198
+ s.phase.set('error')
199
+ })
200
+ bumpVersion(s)
201
+ for (const cb of [...s._error]) cb({ to: toPath, error: err })
202
+ emit(s)
203
+ }
204
+
205
+ // Test-only: drop the singleton so the next access rebuilds fresh signals.
206
+ export function __resetNavForTest(): void {
207
+ G.__BRUST_NAV__ = undefined
208
+ }
@@ -11,5 +11,13 @@
11
11
  "jsx": "react-jsx",
12
12
  "types": []
13
13
  },
14
- "files": ["treaty.ts", "define-actions.ts", "standard-schema.ts", "treaty.type-test.ts"]
14
+ "files": [
15
+ "treaty.ts",
16
+ "define-actions.ts",
17
+ "standard-schema.ts",
18
+ "treaty.type-test.ts",
19
+ "navigation/index.ts",
20
+ "navigation/store.ts",
21
+ "navigation/active-nav.ts"
22
+ ]
15
23
  }