staffa 0.7.4 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  import A from "aberdeen";
2
2
  import { type ContentOptions, type Slot, type Attributes, drawSlot } from "../core.js";
3
+ import { closeContainingPanel } from "./panels.js";
3
4
 
4
5
  /** Options for {@link box}. */
5
6
  export interface BoxOptions extends ContentOptions {
@@ -7,6 +8,22 @@ export interface BoxOptions extends ContentOptions {
7
8
  header?: Slot;
8
9
  /** Footer content, drawn in a styled bar below the body. */
9
10
  footer?: Slot;
11
+ /**
12
+ * Draws a small ✕ button in the box's top-right corner: in the header row when
13
+ * there is a {@link BoxOptions.header | header}, floating over the body when
14
+ * there isn't.
15
+ *
16
+ * `true` closes the panel the box is drawn in, which is how a screen of a
17
+ * routed `S.main()` gives the user a way back (the shell draws no back
18
+ * arrows or ✕ of its own). Which panel that is gets worked out from the DOM
19
+ * when it's clicked, so the box needs no `$page` handed to it and works from
20
+ * any column, top of the stack or not. A box in a column further left closes
21
+ * just that column and leaves the others alone. Outside a routed shell it
22
+ * does nothing but warn.
23
+ *
24
+ * Pass a function to run that instead, for a dismissal of your own.
25
+ */
26
+ close?: boolean | (() => void);
10
27
  /** Aberdeen attr/style string applied to the body (content-holding) element. */
11
28
  contentAttrs?: Attributes;
12
29
  /** Aberdeen attr/style string applied to the header bar. */
@@ -26,11 +43,23 @@ export interface BoxOptions extends ContentOptions {
26
43
  // single divider.
27
44
  A.insertGlobalCss({
28
45
  ".s-box": {
29
- "&": "display:flex flex-direction:column overflow:hidden r: $s-radius-lg;",
46
+ // position:relative so a headerless box can hang its ✕ in the corner.
47
+ "&": "display:flex flex-direction:column overflow:hidden r: $s-radius-lg; position:relative",
30
48
  "&:not(:first-child)": "margin-top: $3",
31
49
  "> header": "display:flex align-items:center gap:$2 padding: $2 $3; border:0 border-bottom: 1px solid $s-faint; r:0 font-weight:600",
32
50
  "> footer": "display:flex align-items:center justify-content:flex-end gap:$2 padding: $2 $3; border:0 border-top: 1px solid $s-faint; r:0",
33
51
  "> div": "p:$3 gap:$3",
52
+ // The ✕: quiet until you're near it, and drawn in the surface's own tokens
53
+ // so it works on whatever the box was recoloured to. `margin-left:auto`
54
+ // parks it at the far end of the header's flex row.
55
+ ".s-box-close":
56
+ "flex-shrink:0 margin-left:auto display:flex align-items:center justify-content:center " +
57
+ "width:1.6rem height:1.6rem p:0 border:0 background:transparent cursor:pointer " +
58
+ "fg:$s-muted font-size:0.95rem line-height:1 r:$s-radius-sm " +
59
+ "transition: color 0.12s, background 0.12s;",
60
+ ".s-box-close:hover": "fg:$s-text background: color-mix(in srgb, $s-text 8%, transparent);",
61
+ // Without a header there is no row to sit in, so it floats over the body.
62
+ "> .s-box-close": "position:absolute top:$2 right:$2 z-index:1",
34
63
  },
35
64
  });
36
65
 
@@ -44,6 +73,9 @@ A.insertGlobalCss({
44
73
  *
45
74
  * Shortcut: pass a function to use it directly as the body content.
46
75
  *
76
+ * {@link BoxOptions.close | `close: true`} adds a ✕ that closes the panel the box
77
+ * is drawn in: the usual way back out of a screen in a routed `S.main()`.
78
+ *
47
79
  * @example
48
80
  * ```ts
49
81
  * const $user = A.proxy({name: "Kvothe"});
@@ -51,6 +83,7 @@ A.insertGlobalCss({
51
83
  * S.textline({ label: "Name", bind: A.ref($user, "name") });
52
84
  * }});
53
85
  * S.box(() => A("p#Just some content")); // shorthand
86
+ * S.box({ header: "Task 42", close: true, content: drawTask }); // ✕ closes this panel
54
87
  * ```
55
88
  */
56
89
  export function box(opts: BoxOptions | Slot = {}): void {
@@ -60,7 +93,14 @@ export function box(opts: BoxOptions | Slot = {}): void {
60
93
  // Header and footer get their own scopes so toggling them doesn't recreate
61
94
  // the body (which may hold focused inputs / lots of content).
62
95
  A(() => {
63
- if (o.header != null) A("header.s-s.neutral", o.headerAttrs, () => drawSlot(o.header));
96
+ if (o.header != null) {
97
+ A("header.s-s.neutral", o.headerAttrs, () => {
98
+ drawSlot(o.header);
99
+ if (o.close) drawCloseButton(o.close);
100
+ });
101
+ } else if (o.close) {
102
+ drawCloseButton(o.close);
103
+ }
64
104
  });
65
105
 
66
106
  A("div", o.contentAttrs, () => {
@@ -72,3 +112,18 @@ export function box(opts: BoxOptions | Slot = {}): void {
72
112
  });
73
113
  });
74
114
  }
115
+
116
+ /**
117
+ * The box's ✕. With `close: true` the panel to close is resolved from the DOM at
118
+ * click time — so one box can close whichever column it happens to be drawn in,
119
+ * and a box outside a routed shell simply warns.
120
+ */
121
+ function drawCloseButton(close: boolean | (() => void)): void {
122
+ A("button.s-box-close type=button aria-label=Close", () => {
123
+ A("click=", (e: Event) => {
124
+ if (typeof close === "function") close();
125
+ else void closeContainingPanel(e.currentTarget as HTMLElement);
126
+ });
127
+ A("span aria-hidden=true #✕");
128
+ });
129
+ }
@@ -1,10 +1,12 @@
1
1
  import A from "aberdeen";
2
- import { type Slot, type Attributes, drawSlot } from "../core.js";
3
- import { type MenuOptions, menuButton, drawMenu, isFloatingMenuOpen } from "./menu.js";
2
+ import { type Slot, type Attributes, drawSlot, focusFirst, NARROW_PX } from "../core.js";
3
+ import { type MenuOptions, drawMenu, showFloatingMenu, isFloatingMenuOpen, closeFloatingMenu, menuGlyph, closeGlyph } from "./menu.js";
4
+ import { button } from "./button.js";
4
5
  import { isDialogOpen } from "./dialog.js";
6
+ import { PanelController, type Page, type RouteHandler, type RouteTable, type Routes } from "./panels.js";
5
7
 
6
8
  /** Options for {@link main}. */
7
- export interface MainOptions {
9
+ export interface MainOptions<R = Routes> {
8
10
  /** Aberdeen attr/style string applied to the outermost shell element. */
9
11
  attrs?: Attributes;
10
12
  /** App/page title shown in the top bar. */
@@ -15,8 +17,80 @@ export interface MainOptions {
15
17
  icon?: Slot;
16
18
  /** Action area on the right of the top bar (buttons, menu, ...). */
17
19
  menu?: Slot;
18
- /** The scrollable page content. A string is rendered as rich text. */
20
+ /**
21
+ * The scrollable page content. A string is rendered as rich text.
22
+ * Mutually exclusive with {@link MainOptions.routes}.
23
+ */
19
24
  content?: Slot;
25
+ /**
26
+ * Paths mapped to the functions that draw them, which hands navigation over
27
+ * to the shell. Each route draws one screen of your app, called a panel, and
28
+ * as many panels as fit are shown at a time: one at a time on a phone,
29
+ * several side by side on a wider screen. Mutually exclusive with
30
+ * {@link MainOptions.content}.
31
+ *
32
+ * A segment wrapped in brackets is a param: `[name]` matches one segment as
33
+ * a string, `[name=integer]` matches one segment as a number, and a trailing
34
+ * `[...name]` matches the rest of the path as one raw (still percent-encoded)
35
+ * string, so it has to come last and needs at least one segment to match.
36
+ * The first key that matches wins, a segment a param refuses falls through
37
+ * to a later route (or to {@link MainOptions.notFound}), and each handler's
38
+ * `$page.params` is typed from its own key.
39
+ *
40
+ * `integer` accepts only spellings that survive a round trip back to the
41
+ * same URL, so `/tasks/0042` is not a second path for `/tasks/42`. Ids that
42
+ * aren't safe integers, such as snowflakes, want a plain `[id]`.
43
+ *
44
+ * Navigating is just links: the shell handles the clicks itself, so do *not*
45
+ * also call Aberdeen's `interceptLinks()`. A link opens its target on top of
46
+ * the panel it sits in, closing anything that was above it first, unless it
47
+ * carries `data-panel=replace`, which replaces its own panel instead. A link
48
+ * to something already open goes back to it rather than opening it twice.
49
+ * From code, use {@link panels} (`S.panels.push()` and friends): navigating
50
+ * with `aberdeen/route`'s own `go()` works and still asks the panels'
51
+ * {@link Page.requestClose}, but builds the whole stack from the path. A
52
+ * navigation guard the app registered before mounting (an auth redirect,
53
+ * say) keeps working: the shell asks it first, and puts it back when the
54
+ * shell goes away.
55
+ *
56
+ * The shell draws no back arrows and no ✕ of its own: **every panel provides
57
+ * its own way out**, with `S.box`'s `close` option for a ✕, or
58
+ * {@link Page.close} behind a Cancel button. Escape and the browser's back
59
+ * button are the shell's contribution.
60
+ *
61
+ * Only one routed shell can be mounted at a time (a second one throws),
62
+ * which is what lets {@link panels} be a plain module-level object. Each
63
+ * handler still gets its own `$page` rather than there being one global
64
+ * "current page", since several panels are alive at once. It's that argument
65
+ * that carries the per-route typing of `params`.
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * S.main({
70
+ * title: "Trackle",
71
+ * nav: { items: [{ label: "Projects", href: "/projects" }] },
72
+ * routes: {
73
+ * "/projects": ($page) => { $page.title = "Projects"; drawProjects(); },
74
+ * "/projects/[id]": ($page) => drawProject($page.params.id), // typed string
75
+ * },
76
+ * notFound: ($page) => S.box({ header: "Not found", content: $page.path }),
77
+ * });
78
+ * ```
79
+ */
80
+ routes?: R;
81
+ /**
82
+ * Draws the panel for a path none of the routes match. There are no params
83
+ * to go with it, so `$page.params` is empty; the path itself is in
84
+ * `$page.path`.
85
+ */
86
+ notFound?: RouteHandler<{}>;
87
+ /**
88
+ * Set `false` to show only the top panel, however wide the screen (the nav
89
+ * sidebar still sits beside it). Everything else behaves the same: the URL,
90
+ * the back button, `requestClose`, and the panels' own close buttons. This
91
+ * only changes how many you see. Defaults to `true`.
92
+ */
93
+ stacking?: boolean;
20
94
  /** Footer content, pinned below the scroll area. */
21
95
  footer?: Slot;
22
96
  /**
@@ -26,6 +100,10 @@ export interface MainOptions {
26
100
  * sidebar) — cap to this width and centre horizontally. When unset, everything
27
101
  * fills the available width. Either way the content shares the page surface —
28
102
  * it is not boxed.
103
+ *
104
+ * Ignored when you pass {@link MainOptions.routes}: there the open panels
105
+ * decide the width (see {@link Page.layout}), and the header and footer line
106
+ * themselves up with them.
29
107
  */
30
108
  maxWidth?: string;
31
109
  /** Aberdeen attr/style string applied to the content area. */
@@ -35,18 +113,30 @@ export interface MainOptions {
35
113
  /**
36
114
  * Navigation menu. When provided, renders a sidebar (in `"left"` / `"right"`
37
115
  * mode) or a button+dropdown (in `"button"` mode). The sidebar automatically
38
- * collapses to button mode when the shell is too narrow.
116
+ * collapses to a button when the shell is too narrow — which there opens the
117
+ * nav as a full page sliding in from the left, not as a dropdown.
118
+ *
119
+ * `items` may be a reactive array: the shell reads it inside the sidebar's own
120
+ * scope, so an item arriving or leaving redraws the sidebar and nothing else.
121
+ * The content beside it — in routed mode, the whole panel stack — is left
122
+ * alone.
39
123
  */
40
124
  nav?: MenuOptions;
41
125
  /**
42
126
  * Where to render the nav. Defaults to `"left"`.
43
127
  * - `"left"` / `"right"`: sidebar next to the content area; collapses to a
44
- * button+dropdown in the top bar when the shell width drops below 640 px.
45
- * - `"button"`: always a button+dropdown, never a sidebar.
128
+ * button in the top bar when the shell width drops below 640 px.
129
+ * - `"button"`: always a button, never a sidebar.
130
+ *
131
+ * The button opens a dropdown on a wide shell, and — below 640 px — a
132
+ * full-page nav that slides in from the left, handing over to the chosen
133
+ * screen with a matching slide in from the right.
46
134
  */
47
135
  navPosition?: "left" | "right" | "button";
48
136
  /** Aberdeen attr/style string applied to the sidebar nav panel. */
49
137
  navAttrs?: Attributes;
138
+ /** Aberdeen attr/style string applied to the narrow-screen full-page nav. */
139
+ navPageAttrs?: Attributes;
50
140
  }
51
141
 
52
142
  A.insertGlobalCss({
@@ -72,7 +162,9 @@ A.insertGlobalCss({
72
162
  // Body always wraps <main> (with or without a sidebar) so max-width centering
73
163
  // and scrollbar alignment work identically in both cases.
74
164
  // .s-body centres .s-body-inner; .s-body-inner caps the content to maxWidth.
75
- ".s-body": "flex:1 overflow:hidden display:flex flex-direction:row min-height:0 justify-content:center",
165
+ // It's also the positioning + clipping context for the narrow-screen nav page,
166
+ // which slides in and out across its left edge.
167
+ ".s-body": "flex:1 overflow:hidden display:flex flex-direction:row min-height:0 justify-content:center position:relative",
76
168
  ".s-body-inner": "flex:1 min-width:0 display:flex flex-direction:row min-height:0",
77
169
  // Put the sidebar on the right (content fills the left) for right-hand navs.
78
170
  "&.s-nav-right .s-body-inner": "flex-direction:row-reverse",
@@ -83,7 +175,14 @@ A.insertGlobalCss({
83
175
  // can shrink to fit the bounded container (rather than letting wide content push
84
176
  // the whole body — and any sidebar — past the viewport edge). overflow-x:hidden
85
177
  // clips overlong content on the right; vertically it scrolls.
86
- ".s-body main": "flex:1 min-width:0 min-height:0 overflow-x:hidden overflow-y:auto display:flex flex-direction:column",
178
+ // The transition is dormant (nothing else moves <main>); it's there for the
179
+ // incoming half of the nav-page hand-off — see `slideContentIn`.
180
+ ".s-body main":
181
+ "flex:1 min-width:0 min-height:0 overflow-x:hidden overflow-y:auto display:flex flex-direction:column " +
182
+ "transition: transform 0.3s ease;",
183
+ // A one-shot starting position: parked one screen to the right, with the
184
+ // transition off so it snaps there. Removing the class animates it home.
185
+ ".s-body main.s-slide-in": "transform: translateX(100%); transition:none",
87
186
  // The content area fills the scroll region with comfortable padding.
88
187
  // It is deliberately NOT a boxed "sheet" — content brings its own boxes.
89
188
  ".s-body main > .s-content": "width:100% flex:1 p:$3",
@@ -94,18 +193,58 @@ A.insertGlobalCss({
94
193
  // and the bar already comes from `.s-content`'s padding. Without a scrollbar
95
194
  // there's no margin, so the content keeps its single $3 edge — not 2×$3.
96
195
  ".s-body main.s-scroll-y": "margin-right:$3",
196
+ // Routed mode takes its width from the panel stack instead of from
197
+ // `maxWidth`: the layout engine publishes the ensemble width (sidebar +
198
+ // separator + content area) as --s-shell-w — the standard 1280px page
199
+ // normally, the window's edges while a "large" panel is up — and the body
200
+ // row and the bars cap themselves to it. So the chrome lines up with the
201
+ // columns and the lot stays centred in the shell.
202
+ "&.s-routed > .s-body > .s-body-inner": "max-width: var(--s-shell-w, 100%);",
203
+ "&.s-routed > header > .s-bar": "max-width: var(--s-shell-w, 100%);",
204
+ "&.s-routed > footer > .s-bar": "max-width: var(--s-shell-w, 100%);",
205
+ // Changing the custom property animates the max-widths consuming it, with no
206
+ // JS in the loop: the chrome recentres in step with the panel whose arrival
207
+ // or departure moved it, over the same --s-panel-ms (see panels.ts). During
208
+ // a window resize (and the very first pass) the layout engine raises
209
+ // `.s-shell-snap` so the new width is adopted instantly instead of chasing
210
+ // the window through a transition.
211
+ "&.s-routed > .s-body > .s-body-inner, &.s-routed > header > .s-bar, &.s-routed > footer > .s-bar":
212
+ "transition: max-width var(--s-panel-ms) ease;",
213
+ "&.s-routed.s-shell-snap > .s-body > .s-body-inner, &.s-routed.s-shell-snap > header > .s-bar, &.s-routed.s-shell-snap > footer > .s-bar":
214
+ "transition:none",
97
215
  },
98
216
  // Sidebar nav panel. Items reuse the shared `.s-menu-item` /
99
217
  // `.s-menu-sep` styles from menu.ts, so the sidebar and the floating
100
218
  // dropdown stay visually identical.
101
- // Borderless and transparent so the page's aurora shows through — an airy,
102
- // floating sidebar whose only chrome is the active item's gradient pill.
219
+ // Borderless and transparent so the page's own surface shows through — an airy,
220
+ // floating sidebar whose only chrome is the active item's accent colouring.
103
221
  ".s-nav-panel": {
104
- // Extra horizontal padding leaves room for the active pill's glow, which the
105
- // vertical scroll (overflow-y:auto, which also clips overflow-x) would
106
- // otherwise cut off at the panel edges.
222
+ // The generous horizontal padding is what keeps the rows clear of the content
223
+ // separator on one side and the shell edge on the other; the vertical scroll
224
+ // (overflow-y:auto, which also clips overflow-x) leaves no room to bleed past it.
107
225
  "&": "display:flex flex-direction:column overflow-y:auto flex-shrink:0 max-width:228px padding:$3 gap:$1",
108
226
  },
227
+ // The narrow-screen nav: a full "page" that slides in over the content from the
228
+ // left, rather than a dropdown — on a phone a nav is a screenful of UI, not a
229
+ // popup. Picking an item slides it back out while the chosen screen comes in
230
+ // from the right (see `slideContentIn`), so the two tile across the viewport
231
+ // and navigation reads as a lateral move between screens.
232
+ ".s-nav-page": {
233
+ // It covers the body area only, so the top bar (whose trigger has become an
234
+ // ✕) and the footer stay put — the shell itself never blinks.
235
+ "&":
236
+ // z-index sits under the sticky header's 10: the two never overlap (the
237
+ // body starts below the bar), but the bar should still win if they ever do.
238
+ "position:absolute inset:0 z-index:5 display:flex flex-direction:column " +
239
+ "overflow-y:auto overscroll-behavior:contain border:0 r:0 padding:$2 gap:$1 " +
240
+ "transition: transform 0.3s ease;",
241
+ // Parked one screen to the left: the state the `create=`/`destroy=` hooks
242
+ // transition out of and back into.
243
+ "&.s-nav-page-off": "transform:translateX(-100%) pointer-events:none",
244
+ // Roomier rows than the dropdown's: this is the whole screen, and every row
245
+ // is a thumb target.
246
+ ".s-menu-item": "padding: $2 $3; min-height:3rem font-size:1.05em gap:$3",
247
+ },
109
248
  // In button-only mode (or always-button navPosition), hide the sidebar and
110
249
  // show the trigger. In sidebar mode, show the panel and hide the trigger.
111
250
  // CSS @container queries handle the responsive collapse automatically.
@@ -113,7 +252,7 @@ A.insertGlobalCss({
113
252
  ".s-main.s-nav-btn-only .s-nav-panel": "display:none",
114
253
  ".s-main.s-nav-btn-only .s-nav-trigger": "display:flex",
115
254
  // Collapse sidebar → button when shell is narrow.
116
- "@container (max-width: 640px)": {
255
+ [`@container (max-width: ${NARROW_PX}px)`]: {
117
256
  ".s-main.s-nav-left .s-nav-panel, .s-main.s-nav-right .s-nav-panel, .s-main .s-nav-sep": "display:none",
118
257
  ".s-main.s-nav-left .s-nav-trigger, .s-main.s-nav-right .s-nav-trigger": "display:flex",
119
258
  // On phones a top-level content box becomes a full-bleed block: pull it out
@@ -131,6 +270,14 @@ A.insertGlobalCss({
131
270
  * footer. With {@link MainOptions.maxWidth} the content area is centred and its
132
271
  * width capped. Add a `nav` to get a responsive sidebar (auto-collapses to a
133
272
  * menu button below 640 px, or always a button with `navPosition: "button"`).
273
+ * Below 640 px that button opens the nav as a full page sliding in from the
274
+ * left; picking an item slides it away as the chosen screen enters from the
275
+ * right.
276
+ *
277
+ * Instead of a single `content` slot, pass {@link MainOptions.routes} and the
278
+ * shell takes over navigation: each route draws one screen, called a panel,
279
+ * and as many panels as fit are shown at a time, side by side on a wide screen
280
+ * and one at a time on a phone. See {@link MainOptions.routes} and {@link Page}.
134
281
  *
135
282
  * @example
136
283
  * ```ts
@@ -155,13 +302,50 @@ A.insertGlobalCss({
155
302
  * }
156
303
  * ```
157
304
  */
158
- export function main(opts: MainOptions = {}): void {
305
+ // The self-referential constraint is what types each handler's `$page.params`
306
+ // from its own route key. It deliberately has no default: giving `R` one makes
307
+ // TypeScript fall back to it for contextual typing, and every `$page.params`
308
+ // silently degrades to `any`. Callers that pass no `routes` are unaffected —
309
+ // `MainOptions`'s own default kicks in there.
310
+ export function main<R extends RouteTable<R>>(opts: MainOptions<R> = {}): void {
311
+ // Whether there is a nav to show is deliberately NOT worked out here: `items`
312
+ // may well be a reactive array, and reading it in the shell's own scope would
313
+ // subscribe *the whole shell* to it — an item arriving later would redraw the
314
+ // lot, and in routed mode that means tearing the panel stack down and building
315
+ // it again from the URL. So every use below reads `nav.items` inside its own
316
+ // scope, and only that scope redraws.
159
317
  const nav = opts.nav;
160
318
  const navPos = opts.navPosition ?? "left";
161
- const hasNav = nav != null && nav.items.length > 0;
162
- const navCls = hasNav ? (navPos === "button" ? ".s-nav-btn-only" : `.s-nav-${navPos}`) : "";
319
+ // Whether the narrow-screen full-page nav is showing. Per shell, so nested or
320
+ // sibling `main()`s can't fight over it.
321
+ const $nav = A.proxy({ open: false });
322
+
323
+ const routes = opts.routes as Routes | undefined;
324
+ if (routes != null && opts.content != null) {
325
+ throw new Error("Staffa: S.main() takes either `content` or `routes`, not both");
326
+ }
327
+ // The panel stack owns the routing, so it starts observing (and building its
328
+ // stack from) the URL before any of the shell is drawn — the top bar's back
329
+ // button already needs to know how deep we are. Its options are listed one by
330
+ // one rather than spread from `opts`: a spread reads every key, which on a
331
+ // proxied options object subscribes this scope to all of them.
332
+ const ctl = routes
333
+ ? new PanelController({ routes, notFound: opts.notFound, stacking: opts.stacking, title: opts.title })
334
+ : null;
335
+ // Routed mode caps the shell to the ensemble width the layout engine publishes,
336
+ // rather than to `maxWidth`.
337
+ const capWidth = ctl ? null : opts.maxWidth;
338
+
339
+ const root = A(`div.s-main${ctl ? ".s-routed" : ""}`, opts.attrs, () => {
340
+ // Which nav mode the shell is in — sidebar or button — as a class on the
341
+ // shell, for the CSS below to hang the responsive collapse off. Its own
342
+ // scope (see `nav` above), so a nav appearing or emptying out only retags
343
+ // the shell rather than redrawing it.
344
+ A(() => {
345
+ if (nav == null || !nav.items.length) return;
346
+ A(navPos === "button" ? ".s-nav-btn-only" : `.s-nav-${navPos}`);
347
+ });
163
348
 
164
- const root = A(`div.s-main${navCls}`, opts.attrs, () => {
165
349
  // Top bar.
166
350
  A(() => {
167
351
  const hasBar =
@@ -169,29 +353,19 @@ export function main(opts: MainOptions = {}): void {
169
353
  opts.subtitle != null ||
170
354
  opts.icon != null ||
171
355
  opts.menu != null ||
172
- hasNav;
356
+ (nav != null && nav.items.length > 0);
173
357
  if (!hasBar) return;
174
358
  A("header.s-s.neutral", opts.topbarAttrs, () => {
175
359
  A("div.s-bar", () => {
176
360
  // Cap the bar's content to maxWidth and centre it within the full-width header.
177
361
  A(() => {
178
- if (opts.maxWidth != null) A("max-width:", opts.maxWidth);
362
+ if (capWidth != null) A("max-width:", capWidth);
179
363
  });
180
364
  // Nav trigger button — visible when sidebar is hidden (button mode or narrow viewport).
181
365
  A(() => {
182
- if (!hasNav) return;
366
+ if (nav == null || !nav.items.length) return;
183
367
  // .s-nav-trigger: CSS toggles display based on sidebar visibility.
184
- A("div.s-nav-trigger", () => {
185
- menuButton({
186
- ...nav,
187
- button: {
188
- icon: () => A("span aria-hidden=true #☰"),
189
- ariaLabel: "Open navigation",
190
- attrs: ".neutral .small",
191
- ...nav.button,
192
- },
193
- });
194
- });
368
+ A("div.s-nav-trigger", () => drawNavTrigger(nav, $nav));
195
369
  });
196
370
 
197
371
  A(() => {
@@ -217,15 +391,22 @@ export function main(opts: MainOptions = {}): void {
217
391
  A("div.s-body", () => {
218
392
  A("div.s-body-inner", () => {
219
393
  A(() => {
220
- if (opts.maxWidth != null) A("max-width:", opts.maxWidth);
394
+ if (capWidth != null) A("max-width:", capWidth);
221
395
  });
222
- if (hasNav && navPos !== "button") {
396
+ // The sidebar, in its own scope so a changing item list redraws just
397
+ // it — never the content area beside it (see `nav` above).
398
+ A(() => {
399
+ if (nav == null || !nav.items.length || navPos === "button") return;
223
400
  A(`nav.s-nav-panel.s-nav-${navPos}`, opts.navAttrs, () => {
224
401
  drawMenu(nav.items);
225
402
  });
226
403
  A("div.s-nav-sep aria-hidden=true");
227
- }
228
- drawMainContent(opts);
404
+ });
405
+ drawMainContent(opts, ctl);
406
+ });
407
+ // The narrow-screen nav page, laid over the body it slides across.
408
+ A(() => {
409
+ if (nav != null && nav.items.length && $nav.open) drawNavPage(nav, opts.navPageAttrs, $nav);
229
410
  });
230
411
  });
231
412
 
@@ -235,7 +416,7 @@ export function main(opts: MainOptions = {}): void {
235
416
  A("footer", () => {
236
417
  A("div.s-bar", () => {
237
418
  A(() => {
238
- if (opts.maxWidth != null) A("max-width:", opts.maxWidth);
419
+ if (capWidth != null) A("max-width:", capWidth);
239
420
  });
240
421
  drawSlot(opts.footer);
241
422
  });
@@ -244,16 +425,36 @@ export function main(opts: MainOptions = {}): void {
244
425
  });
245
426
  }) as HTMLElement;
246
427
 
247
- // Escape jumps to the navigation: into the sidebar's current item when the
248
- // sidebar is showing, or — when collapsed to (or always) a button — open the
249
- // dropdown (which focuses its current item). Listens on `document` so it works
250
- // wherever focus is, but bows out while another overlay (a dialog, or an
251
- // already-open menu) is up those handle Escape themselves.
252
- if (hasNav) {
428
+ // Escape peels back a panel of UI, and finally jumps to the navigation: into
429
+ // the sidebar's current item when the sidebar is showing, or — when collapsed
430
+ // to (or always) a button open the nav (dropdown or full page, whichever the
431
+ // shell width calls for), which focuses its current item. Listens on
432
+ // `document` so it works wherever focus is, but bows out while another overlay
433
+ // (a dialog, or an already-open menu) is up — those handle Escape themselves.
434
+ if (nav != null || ctl) {
253
435
  const onKey = (e: KeyboardEvent) => {
254
436
  if (e.key !== "Escape" || e.defaultPrevented) return;
255
437
  // An open dialog or menu owns Escape itself — don't also jump to the nav.
256
438
  if (isDialogOpen() || isFloatingMenuOpen()) return;
439
+ const trigger = root.querySelector<HTMLElement>(".s-nav-trigger button");
440
+ // So does the full-page nav: dismiss it and hand focus back to its trigger.
441
+ if ($nav.open) {
442
+ e.preventDefault();
443
+ $nav.open = false;
444
+ trigger?.focus();
445
+ return;
446
+ }
447
+ // Above the stack root, Escape closes the top panel — the same guarded
448
+ // close as a page's own ✕ or the browser's back button. It is, with
449
+ // browser back, the only way out the shell itself provides.
450
+ if (ctl && ctl.$state.paths.length > 1) {
451
+ e.preventDefault();
452
+ void ctl.closeTop();
453
+ return;
454
+ }
455
+ // Whether there is a nav at all is asked of the DOM, not of `nav.items`:
456
+ // a subscription here would be one on the shell's own scope again, and
457
+ // an empty nav simply has neither of the two elements below.
257
458
  // `offsetParent` is null when the sidebar is hidden (display:none).
258
459
  const panel = root.querySelector<HTMLElement>(".s-nav-panel");
259
460
  if (panel?.offsetParent != null) {
@@ -263,7 +464,6 @@ export function main(opts: MainOptions = {}): void {
263
464
  if (item) { e.preventDefault(); item.focus(); }
264
465
  return;
265
466
  }
266
- const trigger = root.querySelector<HTMLElement>(".s-nav-trigger button");
267
467
  if (trigger) { e.preventDefault(); trigger.click(); }
268
468
  };
269
469
  document.addEventListener("keydown", onKey);
@@ -271,7 +471,108 @@ export function main(opts: MainOptions = {}): void {
271
471
  }
272
472
  }
273
473
 
274
- function drawMainContent(opts: MainOptions): void {
474
+ /**
475
+ * The hamburger in the top bar, shown whenever the sidebar isn't. What it opens
476
+ * depends on how much room the shell has: a dropdown when there's plenty, and —
477
+ * below {@link NARROW_PX} — the full-page nav, which suits a phone far better
478
+ * than a popup. Either way a second click closes again.
479
+ */
480
+ function drawNavTrigger(nav: MenuOptions, $nav: { open: boolean }): void {
481
+ let myEl: HTMLElement | null = null;
482
+ A.clean(() => { if (myEl) closeFloatingMenu(myEl); });
483
+
484
+ button({
485
+ // The glyph doubles as the state: ☰ to open the page, ✕ to dismiss it. Its
486
+ // own scope, so toggling doesn't rebuild (and re-focus) the button.
487
+ icon: () => A(() => ($nav.open ? closeGlyph : menuGlyph)({ size: "1.5em" })),
488
+ ariaLabel: "Open navigation",
489
+ // Quiet chrome, matching the `menu` slot's own buttons at the other end of the
490
+ // bar: the trigger is a way *in* to the app, not something to be sold on, and a
491
+ // filled brand button here shouts down the title it sits next to.
492
+ attrs: ".neutral .small",
493
+ ...nav.button,
494
+ click: (e: Event) => {
495
+ myEl = e.currentTarget as HTMLElement;
496
+ const shell = myEl.closest<HTMLElement>(".s-main");
497
+ if (shell != null && shell.clientWidth <= NARROW_PX) { $nav.open = !$nav.open; return; }
498
+ // Wide shell: the classic dropdown. A click on the trigger never reaches
499
+ // the menu's own outside-click handler, so toggle it here.
500
+ if (isFloatingMenuOpen(myEl)) closeFloatingMenu(myEl);
501
+ else showFloatingMenu({ items: nav.items, anchor: myEl, dropdownAttrs: nav.dropdownAttrs });
502
+ },
503
+ });
504
+ }
505
+
506
+ /**
507
+ * The narrow-screen navigation: a full page sliding in over the content from the
508
+ * left. Picking an item slides it back out while the chosen screen enters from
509
+ * the right, so the two tile across the viewport and the whole thing reads as a
510
+ * lateral move rather than a popup blinking out.
511
+ */
512
+ function drawNavPage(nav: MenuOptions, attrs: Attributes | undefined, $nav: { open: boolean }): void {
513
+ // Whether this close is a *navigation* — the only kind that hands over to an
514
+ // incoming screen. Dismissing the page just uncovers the content again.
515
+ let navigated = false;
516
+
517
+ const pageEl = A(
518
+ "nav.s-nav-page.s-s.neutral aria-label=Navigation create=s-nav-page-off destroy=s-nav-page-off",
519
+ attrs,
520
+ () => drawMenu(nav.items, () => { navigated = true; $nav.open = false; }),
521
+ ) as HTMLElement;
522
+
523
+ const shell = pageEl.closest<HTMLElement>(".s-main");
524
+ const behind = pageEl.parentElement?.querySelector<HTMLElement>(":scope > .s-body-inner");
525
+ // Content mode's incoming half of the hand-off. In routed mode there is no
526
+ // <main> to slide: the chosen screen is a freshly pushed panel, which plays
527
+ // its own enter animation, so this correctly finds nothing.
528
+ const content = behind?.querySelector<HTMLElement>(":scope > main");
529
+
530
+ // The content is fully covered, but without this it stays tabbable and visible
531
+ // to screen readers underneath the page.
532
+ behind?.setAttribute("inert", "");
533
+
534
+ // Widening the shell past the collapse point brings the sidebar back, leaving
535
+ // this page covering the content for no reason — so bow out.
536
+ if (shell != null && typeof ResizeObserver !== "undefined") {
537
+ const ro = new ResizeObserver(() => { if (shell.clientWidth > NARROW_PX) $nav.open = false; });
538
+ ro.observe(shell);
539
+ A.clean(() => ro.disconnect());
540
+ }
541
+
542
+ A.clean(() => {
543
+ behind?.removeAttribute("inert");
544
+ if (!navigated) return;
545
+ // Same tick as the page's own destroy transition, so both halves of the
546
+ // hand-off move in lockstep.
547
+ if (content) slideContentIn(content);
548
+ shell?.querySelector<HTMLElement>(".s-nav-trigger button")?.focus();
549
+ });
550
+
551
+ // Land on the current page's entry (or the first one) once we're laid out.
552
+ requestAnimationFrame(() => {
553
+ if (document.body.contains(pageEl)) focusFirst(pageEl, ".s-menu-item[aria-current=page]");
554
+ });
555
+ }
556
+
557
+ /**
558
+ * Play the incoming half of the nav-page hand-off: park `el` one screen to the
559
+ * right, then let its CSS transition carry it home. Reading `offsetWidth` in
560
+ * between forces the browser to adopt the parked position as the "before" state,
561
+ * which is what makes the removal animate instead of doing nothing at all.
562
+ */
563
+ function slideContentIn(el: HTMLElement): void {
564
+ el.classList.add("s-slide-in");
565
+ void el.offsetWidth;
566
+ el.classList.remove("s-slide-in");
567
+ }
568
+
569
+ function drawMainContent(opts: MainOptions<any>, ctl: PanelController | null): void {
570
+ // Routed mode replaces the single scrollable <main> with the panel viewport,
571
+ // which manages its own columns (and their scrolling) from JS.
572
+ if (ctl) {
573
+ ctl.drawStack();
574
+ return;
575
+ }
275
576
  const mainEl = A("main", () => {
276
577
  A("div.s-content", opts.contentAttrs, () => {
277
578
  drawSlot(opts.content);