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,7 +1,8 @@
1
1
  import { type Slot, type Attributes } from "../core.js";
2
2
  import { type MenuOptions } from "./menu.js";
3
+ import { type RouteHandler, type RouteTable, type Routes } from "./panels.js";
3
4
  /** Options for {@link main}. */
4
- export interface MainOptions {
5
+ export interface MainOptions<R = Routes> {
5
6
  /** Aberdeen attr/style string applied to the outermost shell element. */
6
7
  attrs?: Attributes;
7
8
  /** App/page title shown in the top bar. */
@@ -12,8 +13,80 @@ export interface MainOptions {
12
13
  icon?: Slot;
13
14
  /** Action area on the right of the top bar (buttons, menu, ...). */
14
15
  menu?: Slot;
15
- /** The scrollable page content. A string is rendered as rich text. */
16
+ /**
17
+ * The scrollable page content. A string is rendered as rich text.
18
+ * Mutually exclusive with {@link MainOptions.routes}.
19
+ */
16
20
  content?: Slot;
21
+ /**
22
+ * Paths mapped to the functions that draw them, which hands navigation over
23
+ * to the shell. Each route draws one screen of your app, called a panel, and
24
+ * as many panels as fit are shown at a time: one at a time on a phone,
25
+ * several side by side on a wider screen. Mutually exclusive with
26
+ * {@link MainOptions.content}.
27
+ *
28
+ * A segment wrapped in brackets is a param: `[name]` matches one segment as
29
+ * a string, `[name=integer]` matches one segment as a number, and a trailing
30
+ * `[...name]` matches the rest of the path as one raw (still percent-encoded)
31
+ * string, so it has to come last and needs at least one segment to match.
32
+ * The first key that matches wins, a segment a param refuses falls through
33
+ * to a later route (or to {@link MainOptions.notFound}), and each handler's
34
+ * `$page.params` is typed from its own key.
35
+ *
36
+ * `integer` accepts only spellings that survive a round trip back to the
37
+ * same URL, so `/tasks/0042` is not a second path for `/tasks/42`. Ids that
38
+ * aren't safe integers, such as snowflakes, want a plain `[id]`.
39
+ *
40
+ * Navigating is just links: the shell handles the clicks itself, so do *not*
41
+ * also call Aberdeen's `interceptLinks()`. A link opens its target on top of
42
+ * the panel it sits in, closing anything that was above it first, unless it
43
+ * carries `data-panel=replace`, which replaces its own panel instead. A link
44
+ * to something already open goes back to it rather than opening it twice.
45
+ * From code, use {@link panels} (`S.panels.push()` and friends): navigating
46
+ * with `aberdeen/route`'s own `go()` works and still asks the panels'
47
+ * {@link Page.requestClose}, but builds the whole stack from the path. A
48
+ * navigation guard the app registered before mounting (an auth redirect,
49
+ * say) keeps working: the shell asks it first, and puts it back when the
50
+ * shell goes away.
51
+ *
52
+ * The shell draws no back arrows and no ✕ of its own: **every panel provides
53
+ * its own way out**, with `S.box`'s `close` option for a ✕, or
54
+ * {@link Page.close} behind a Cancel button. Escape and the browser's back
55
+ * button are the shell's contribution.
56
+ *
57
+ * Only one routed shell can be mounted at a time (a second one throws),
58
+ * which is what lets {@link panels} be a plain module-level object. Each
59
+ * handler still gets its own `$page` rather than there being one global
60
+ * "current page", since several panels are alive at once. It's that argument
61
+ * that carries the per-route typing of `params`.
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * S.main({
66
+ * title: "Trackle",
67
+ * nav: { items: [{ label: "Projects", href: "/projects" }] },
68
+ * routes: {
69
+ * "/projects": ($page) => { $page.title = "Projects"; drawProjects(); },
70
+ * "/projects/[id]": ($page) => drawProject($page.params.id), // typed string
71
+ * },
72
+ * notFound: ($page) => S.box({ header: "Not found", content: $page.path }),
73
+ * });
74
+ * ```
75
+ */
76
+ routes?: R;
77
+ /**
78
+ * Draws the panel for a path none of the routes match. There are no params
79
+ * to go with it, so `$page.params` is empty; the path itself is in
80
+ * `$page.path`.
81
+ */
82
+ notFound?: RouteHandler<{}>;
83
+ /**
84
+ * Set `false` to show only the top panel, however wide the screen (the nav
85
+ * sidebar still sits beside it). Everything else behaves the same: the URL,
86
+ * the back button, `requestClose`, and the panels' own close buttons. This
87
+ * only changes how many you see. Defaults to `true`.
88
+ */
89
+ stacking?: boolean;
17
90
  /** Footer content, pinned below the scroll area. */
18
91
  footer?: Slot;
19
92
  /**
@@ -23,6 +96,10 @@ export interface MainOptions {
23
96
  * sidebar) — cap to this width and centre horizontally. When unset, everything
24
97
  * fills the available width. Either way the content shares the page surface —
25
98
  * it is not boxed.
99
+ *
100
+ * Ignored when you pass {@link MainOptions.routes}: there the open panels
101
+ * decide the width (see {@link Page.layout}), and the header and footer line
102
+ * themselves up with them.
26
103
  */
27
104
  maxWidth?: string;
28
105
  /** Aberdeen attr/style string applied to the content area. */
@@ -32,18 +109,30 @@ export interface MainOptions {
32
109
  /**
33
110
  * Navigation menu. When provided, renders a sidebar (in `"left"` / `"right"`
34
111
  * mode) or a button+dropdown (in `"button"` mode). The sidebar automatically
35
- * collapses to button mode when the shell is too narrow.
112
+ * collapses to a button when the shell is too narrow — which there opens the
113
+ * nav as a full page sliding in from the left, not as a dropdown.
114
+ *
115
+ * `items` may be a reactive array: the shell reads it inside the sidebar's own
116
+ * scope, so an item arriving or leaving redraws the sidebar and nothing else.
117
+ * The content beside it — in routed mode, the whole panel stack — is left
118
+ * alone.
36
119
  */
37
120
  nav?: MenuOptions;
38
121
  /**
39
122
  * Where to render the nav. Defaults to `"left"`.
40
123
  * - `"left"` / `"right"`: sidebar next to the content area; collapses to a
41
- * button+dropdown in the top bar when the shell width drops below 640 px.
42
- * - `"button"`: always a button+dropdown, never a sidebar.
124
+ * button in the top bar when the shell width drops below 640 px.
125
+ * - `"button"`: always a button, never a sidebar.
126
+ *
127
+ * The button opens a dropdown on a wide shell, and — below 640 px — a
128
+ * full-page nav that slides in from the left, handing over to the chosen
129
+ * screen with a matching slide in from the right.
43
130
  */
44
131
  navPosition?: "left" | "right" | "button";
45
132
  /** Aberdeen attr/style string applied to the sidebar nav panel. */
46
133
  navAttrs?: Attributes;
134
+ /** Aberdeen attr/style string applied to the narrow-screen full-page nav. */
135
+ navPageAttrs?: Attributes;
47
136
  }
48
137
  /**
49
138
  * An application shell that wires up the things almost every app needs: a sticky
@@ -51,6 +140,14 @@ export interface MainOptions {
51
140
  * footer. With {@link MainOptions.maxWidth} the content area is centred and its
52
141
  * width capped. Add a `nav` to get a responsive sidebar (auto-collapses to a
53
142
  * menu button below 640 px, or always a button with `navPosition: "button"`).
143
+ * Below 640 px that button opens the nav as a full page sliding in from the
144
+ * left; picking an item slides it away as the chosen screen enters from the
145
+ * right.
146
+ *
147
+ * Instead of a single `content` slot, pass {@link MainOptions.routes} and the
148
+ * shell takes over navigation: each route draws one screen, called a panel,
149
+ * and as many panels as fit are shown at a time, side by side on a wide screen
150
+ * and one at a time on a phone. See {@link MainOptions.routes} and {@link Page}.
54
151
  *
55
152
  * @example
56
153
  * ```ts
@@ -75,4 +172,4 @@ export interface MainOptions {
75
172
  * }
76
173
  * ```
77
174
  */
78
- export declare function main(opts?: MainOptions): void;
175
+ export declare function main<R extends RouteTable<R>>(opts?: MainOptions<R>): void;
@@ -1,7 +1,9 @@
1
1
  import A from "aberdeen";
2
- import { drawSlot } from "../core.js";
3
- import { menuButton, drawMenu, isFloatingMenuOpen } from "./menu.js";
2
+ import { drawSlot, focusFirst, NARROW_PX } from "../core.js";
3
+ import { 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 } from "./panels.js";
5
7
  A.insertGlobalCss({
6
8
  ".s-main": {
7
9
  // container-type so @container queries below can respond to shell width.
@@ -25,7 +27,9 @@ A.insertGlobalCss({
25
27
  // Body always wraps <main> (with or without a sidebar) so max-width centering
26
28
  // and scrollbar alignment work identically in both cases.
27
29
  // .s-body centres .s-body-inner; .s-body-inner caps the content to maxWidth.
28
- ".s-body": "flex:1 overflow:hidden display:flex flex-direction:row min-height:0 justify-content:center",
30
+ // It's also the positioning + clipping context for the narrow-screen nav page,
31
+ // which slides in and out across its left edge.
32
+ ".s-body": "flex:1 overflow:hidden display:flex flex-direction:row min-height:0 justify-content:center position:relative",
29
33
  ".s-body-inner": "flex:1 min-width:0 display:flex flex-direction:row min-height:0",
30
34
  // Put the sidebar on the right (content fills the left) for right-hand navs.
31
35
  "&.s-nav-right .s-body-inner": "flex-direction:row-reverse",
@@ -36,7 +40,13 @@ A.insertGlobalCss({
36
40
  // can shrink to fit the bounded container (rather than letting wide content push
37
41
  // the whole body — and any sidebar — past the viewport edge). overflow-x:hidden
38
42
  // clips overlong content on the right; vertically it scrolls.
39
- ".s-body main": "flex:1 min-width:0 min-height:0 overflow-x:hidden overflow-y:auto display:flex flex-direction:column",
43
+ // The transition is dormant (nothing else moves <main>); it's there for the
44
+ // incoming half of the nav-page hand-off — see `slideContentIn`.
45
+ ".s-body main": "flex:1 min-width:0 min-height:0 overflow-x:hidden overflow-y:auto display:flex flex-direction:column " +
46
+ "transition: transform 0.3s ease;",
47
+ // A one-shot starting position: parked one screen to the right, with the
48
+ // transition off so it snaps there. Removing the class animates it home.
49
+ ".s-body main.s-slide-in": "transform: translateX(100%); transition:none",
40
50
  // The content area fills the scroll region with comfortable padding.
41
51
  // It is deliberately NOT a boxed "sheet" — content brings its own boxes.
42
52
  ".s-body main > .s-content": "width:100% flex:1 p:$3",
@@ -47,18 +57,56 @@ A.insertGlobalCss({
47
57
  // and the bar already comes from `.s-content`'s padding. Without a scrollbar
48
58
  // there's no margin, so the content keeps its single $3 edge — not 2×$3.
49
59
  ".s-body main.s-scroll-y": "margin-right:$3",
60
+ // Routed mode takes its width from the panel stack instead of from
61
+ // `maxWidth`: the layout engine publishes the ensemble width (sidebar +
62
+ // separator + content area) as --s-shell-w — the standard 1280px page
63
+ // normally, the window's edges while a "large" panel is up — and the body
64
+ // row and the bars cap themselves to it. So the chrome lines up with the
65
+ // columns and the lot stays centred in the shell.
66
+ "&.s-routed > .s-body > .s-body-inner": "max-width: var(--s-shell-w, 100%);",
67
+ "&.s-routed > header > .s-bar": "max-width: var(--s-shell-w, 100%);",
68
+ "&.s-routed > footer > .s-bar": "max-width: var(--s-shell-w, 100%);",
69
+ // Changing the custom property animates the max-widths consuming it, with no
70
+ // JS in the loop: the chrome recentres in step with the panel whose arrival
71
+ // or departure moved it, over the same --s-panel-ms (see panels.ts). During
72
+ // a window resize (and the very first pass) the layout engine raises
73
+ // `.s-shell-snap` so the new width is adopted instantly instead of chasing
74
+ // the window through a transition.
75
+ "&.s-routed > .s-body > .s-body-inner, &.s-routed > header > .s-bar, &.s-routed > footer > .s-bar": "transition: max-width var(--s-panel-ms) ease;",
76
+ "&.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": "transition:none",
50
77
  },
51
78
  // Sidebar nav panel. Items reuse the shared `.s-menu-item` /
52
79
  // `.s-menu-sep` styles from menu.ts, so the sidebar and the floating
53
80
  // dropdown stay visually identical.
54
- // Borderless and transparent so the page's aurora shows through — an airy,
55
- // floating sidebar whose only chrome is the active item's gradient pill.
81
+ // Borderless and transparent so the page's own surface shows through — an airy,
82
+ // floating sidebar whose only chrome is the active item's accent colouring.
56
83
  ".s-nav-panel": {
57
- // Extra horizontal padding leaves room for the active pill's glow, which the
58
- // vertical scroll (overflow-y:auto, which also clips overflow-x) would
59
- // otherwise cut off at the panel edges.
84
+ // The generous horizontal padding is what keeps the rows clear of the content
85
+ // separator on one side and the shell edge on the other; the vertical scroll
86
+ // (overflow-y:auto, which also clips overflow-x) leaves no room to bleed past it.
60
87
  "&": "display:flex flex-direction:column overflow-y:auto flex-shrink:0 max-width:228px padding:$3 gap:$1",
61
88
  },
89
+ // The narrow-screen nav: a full "page" that slides in over the content from the
90
+ // left, rather than a dropdown — on a phone a nav is a screenful of UI, not a
91
+ // popup. Picking an item slides it back out while the chosen screen comes in
92
+ // from the right (see `slideContentIn`), so the two tile across the viewport
93
+ // and navigation reads as a lateral move between screens.
94
+ ".s-nav-page": {
95
+ // It covers the body area only, so the top bar (whose trigger has become an
96
+ // ✕) and the footer stay put — the shell itself never blinks.
97
+ "&":
98
+ // z-index sits under the sticky header's 10: the two never overlap (the
99
+ // body starts below the bar), but the bar should still win if they ever do.
100
+ "position:absolute inset:0 z-index:5 display:flex flex-direction:column " +
101
+ "overflow-y:auto overscroll-behavior:contain border:0 r:0 padding:$2 gap:$1 " +
102
+ "transition: transform 0.3s ease;",
103
+ // Parked one screen to the left: the state the `create=`/`destroy=` hooks
104
+ // transition out of and back into.
105
+ "&.s-nav-page-off": "transform:translateX(-100%) pointer-events:none",
106
+ // Roomier rows than the dropdown's: this is the whole screen, and every row
107
+ // is a thumb target.
108
+ ".s-menu-item": "padding: $2 $3; min-height:3rem font-size:1.05em gap:$3",
109
+ },
62
110
  // In button-only mode (or always-button navPosition), hide the sidebar and
63
111
  // show the trigger. In sidebar mode, show the panel and hide the trigger.
64
112
  // CSS @container queries handle the responsive collapse automatically.
@@ -66,7 +114,7 @@ A.insertGlobalCss({
66
114
  ".s-main.s-nav-btn-only .s-nav-panel": "display:none",
67
115
  ".s-main.s-nav-btn-only .s-nav-trigger": "display:flex",
68
116
  // Collapse sidebar → button when shell is narrow.
69
- "@container (max-width: 640px)": {
117
+ [`@container (max-width: ${NARROW_PX}px)`]: {
70
118
  ".s-main.s-nav-left .s-nav-panel, .s-main.s-nav-right .s-nav-panel, .s-main .s-nav-sep": "display:none",
71
119
  ".s-main.s-nav-left .s-nav-trigger, .s-main.s-nav-right .s-nav-trigger": "display:flex",
72
120
  // On phones a top-level content box becomes a full-bleed block: pull it out
@@ -83,6 +131,14 @@ A.insertGlobalCss({
83
131
  * footer. With {@link MainOptions.maxWidth} the content area is centred and its
84
132
  * width capped. Add a `nav` to get a responsive sidebar (auto-collapses to a
85
133
  * menu button below 640 px, or always a button with `navPosition: "button"`).
134
+ * Below 640 px that button opens the nav as a full page sliding in from the
135
+ * left; picking an item slides it away as the chosen screen enters from the
136
+ * right.
137
+ *
138
+ * Instead of a single `content` slot, pass {@link MainOptions.routes} and the
139
+ * shell takes over navigation: each route draws one screen, called a panel,
140
+ * and as many panels as fit are shown at a time, side by side on a wide screen
141
+ * and one at a time on a phone. See {@link MainOptions.routes} and {@link Page}.
86
142
  *
87
143
  * @example
88
144
  * ```ts
@@ -107,44 +163,70 @@ A.insertGlobalCss({
107
163
  * }
108
164
  * ```
109
165
  */
166
+ // The self-referential constraint is what types each handler's `$page.params`
167
+ // from its own route key. It deliberately has no default: giving `R` one makes
168
+ // TypeScript fall back to it for contextual typing, and every `$page.params`
169
+ // silently degrades to `any`. Callers that pass no `routes` are unaffected —
170
+ // `MainOptions`'s own default kicks in there.
110
171
  export function main(opts = {}) {
172
+ // Whether there is a nav to show is deliberately NOT worked out here: `items`
173
+ // may well be a reactive array, and reading it in the shell's own scope would
174
+ // subscribe *the whole shell* to it — an item arriving later would redraw the
175
+ // lot, and in routed mode that means tearing the panel stack down and building
176
+ // it again from the URL. So every use below reads `nav.items` inside its own
177
+ // scope, and only that scope redraws.
111
178
  const nav = opts.nav;
112
179
  const navPos = opts.navPosition ?? "left";
113
- const hasNav = nav != null && nav.items.length > 0;
114
- const navCls = hasNav ? (navPos === "button" ? ".s-nav-btn-only" : `.s-nav-${navPos}`) : "";
115
- const root = A(`div.s-main${navCls}`, opts.attrs, () => {
180
+ // Whether the narrow-screen full-page nav is showing. Per shell, so nested or
181
+ // sibling `main()`s can't fight over it.
182
+ const $nav = A.proxy({ open: false });
183
+ const routes = opts.routes;
184
+ if (routes != null && opts.content != null) {
185
+ throw new Error("Staffa: S.main() takes either `content` or `routes`, not both");
186
+ }
187
+ // The panel stack owns the routing, so it starts observing (and building its
188
+ // stack from) the URL before any of the shell is drawn — the top bar's back
189
+ // button already needs to know how deep we are. Its options are listed one by
190
+ // one rather than spread from `opts`: a spread reads every key, which on a
191
+ // proxied options object subscribes this scope to all of them.
192
+ const ctl = routes
193
+ ? new PanelController({ routes, notFound: opts.notFound, stacking: opts.stacking, title: opts.title })
194
+ : null;
195
+ // Routed mode caps the shell to the ensemble width the layout engine publishes,
196
+ // rather than to `maxWidth`.
197
+ const capWidth = ctl ? null : opts.maxWidth;
198
+ const root = A(`div.s-main${ctl ? ".s-routed" : ""}`, opts.attrs, () => {
199
+ // Which nav mode the shell is in — sidebar or button — as a class on the
200
+ // shell, for the CSS below to hang the responsive collapse off. Its own
201
+ // scope (see `nav` above), so a nav appearing or emptying out only retags
202
+ // the shell rather than redrawing it.
203
+ A(() => {
204
+ if (nav == null || !nav.items.length)
205
+ return;
206
+ A(navPos === "button" ? ".s-nav-btn-only" : `.s-nav-${navPos}`);
207
+ });
116
208
  // Top bar.
117
209
  A(() => {
118
210
  const hasBar = opts.title != null ||
119
211
  opts.subtitle != null ||
120
212
  opts.icon != null ||
121
213
  opts.menu != null ||
122
- hasNav;
214
+ (nav != null && nav.items.length > 0);
123
215
  if (!hasBar)
124
216
  return;
125
217
  A("header.s-s.neutral", opts.topbarAttrs, () => {
126
218
  A("div.s-bar", () => {
127
219
  // Cap the bar's content to maxWidth and centre it within the full-width header.
128
220
  A(() => {
129
- if (opts.maxWidth != null)
130
- A("max-width:", opts.maxWidth);
221
+ if (capWidth != null)
222
+ A("max-width:", capWidth);
131
223
  });
132
224
  // Nav trigger button — visible when sidebar is hidden (button mode or narrow viewport).
133
225
  A(() => {
134
- if (!hasNav)
226
+ if (nav == null || !nav.items.length)
135
227
  return;
136
228
  // .s-nav-trigger: CSS toggles display based on sidebar visibility.
137
- A("div.s-nav-trigger", () => {
138
- menuButton({
139
- ...nav,
140
- button: {
141
- icon: () => A("span aria-hidden=true #☰"),
142
- ariaLabel: "Open navigation",
143
- attrs: ".neutral .small",
144
- ...nav.button,
145
- },
146
- });
147
- });
229
+ A("div.s-nav-trigger", () => drawNavTrigger(nav, $nav));
148
230
  });
149
231
  A(() => {
150
232
  if (opts.icon != null)
@@ -172,16 +254,25 @@ export function main(opts = {}) {
172
254
  A("div.s-body", () => {
173
255
  A("div.s-body-inner", () => {
174
256
  A(() => {
175
- if (opts.maxWidth != null)
176
- A("max-width:", opts.maxWidth);
257
+ if (capWidth != null)
258
+ A("max-width:", capWidth);
177
259
  });
178
- if (hasNav && navPos !== "button") {
260
+ // The sidebar, in its own scope so a changing item list redraws just
261
+ // it — never the content area beside it (see `nav` above).
262
+ A(() => {
263
+ if (nav == null || !nav.items.length || navPos === "button")
264
+ return;
179
265
  A(`nav.s-nav-panel.s-nav-${navPos}`, opts.navAttrs, () => {
180
266
  drawMenu(nav.items);
181
267
  });
182
268
  A("div.s-nav-sep aria-hidden=true");
183
- }
184
- drawMainContent(opts);
269
+ });
270
+ drawMainContent(opts, ctl);
271
+ });
272
+ // The narrow-screen nav page, laid over the body it slides across.
273
+ A(() => {
274
+ if (nav != null && nav.items.length && $nav.open)
275
+ drawNavPage(nav, opts.navPageAttrs, $nav);
185
276
  });
186
277
  });
187
278
  // Footer — full-width background, content centred to maxWidth via .s-bar.
@@ -190,8 +281,8 @@ export function main(opts = {}) {
190
281
  A("footer", () => {
191
282
  A("div.s-bar", () => {
192
283
  A(() => {
193
- if (opts.maxWidth != null)
194
- A("max-width:", opts.maxWidth);
284
+ if (capWidth != null)
285
+ A("max-width:", capWidth);
195
286
  });
196
287
  drawSlot(opts.footer);
197
288
  });
@@ -199,18 +290,38 @@ export function main(opts = {}) {
199
290
  }
200
291
  });
201
292
  });
202
- // Escape jumps to the navigation: into the sidebar's current item when the
203
- // sidebar is showing, or — when collapsed to (or always) a button — open the
204
- // dropdown (which focuses its current item). Listens on `document` so it works
205
- // wherever focus is, but bows out while another overlay (a dialog, or an
206
- // already-open menu) is up those handle Escape themselves.
207
- if (hasNav) {
293
+ // Escape peels back a panel of UI, and finally jumps to the navigation: into
294
+ // the sidebar's current item when the sidebar is showing, or — when collapsed
295
+ // to (or always) a button open the nav (dropdown or full page, whichever the
296
+ // shell width calls for), which focuses its current item. Listens on
297
+ // `document` so it works wherever focus is, but bows out while another overlay
298
+ // (a dialog, or an already-open menu) is up — those handle Escape themselves.
299
+ if (nav != null || ctl) {
208
300
  const onKey = (e) => {
209
301
  if (e.key !== "Escape" || e.defaultPrevented)
210
302
  return;
211
303
  // An open dialog or menu owns Escape itself — don't also jump to the nav.
212
304
  if (isDialogOpen() || isFloatingMenuOpen())
213
305
  return;
306
+ const trigger = root.querySelector(".s-nav-trigger button");
307
+ // So does the full-page nav: dismiss it and hand focus back to its trigger.
308
+ if ($nav.open) {
309
+ e.preventDefault();
310
+ $nav.open = false;
311
+ trigger?.focus();
312
+ return;
313
+ }
314
+ // Above the stack root, Escape closes the top panel — the same guarded
315
+ // close as a page's own ✕ or the browser's back button. It is, with
316
+ // browser back, the only way out the shell itself provides.
317
+ if (ctl && ctl.$state.paths.length > 1) {
318
+ e.preventDefault();
319
+ void ctl.closeTop();
320
+ return;
321
+ }
322
+ // Whether there is a nav at all is asked of the DOM, not of `nav.items`:
323
+ // a subscription here would be one on the shell's own scope again, and
324
+ // an empty nav simply has neither of the two elements below.
214
325
  // `offsetParent` is null when the sidebar is hidden (display:none).
215
326
  const panel = root.querySelector(".s-nav-panel");
216
327
  if (panel?.offsetParent != null) {
@@ -222,7 +333,6 @@ export function main(opts = {}) {
222
333
  }
223
334
  return;
224
335
  }
225
- const trigger = root.querySelector(".s-nav-trigger button");
226
336
  if (trigger) {
227
337
  e.preventDefault();
228
338
  trigger.click();
@@ -232,7 +342,104 @@ export function main(opts = {}) {
232
342
  A.clean(() => document.removeEventListener("keydown", onKey));
233
343
  }
234
344
  }
235
- function drawMainContent(opts) {
345
+ /**
346
+ * The hamburger in the top bar, shown whenever the sidebar isn't. What it opens
347
+ * depends on how much room the shell has: a dropdown when there's plenty, and —
348
+ * below {@link NARROW_PX} — the full-page nav, which suits a phone far better
349
+ * than a popup. Either way a second click closes again.
350
+ */
351
+ function drawNavTrigger(nav, $nav) {
352
+ let myEl = null;
353
+ A.clean(() => { if (myEl)
354
+ closeFloatingMenu(myEl); });
355
+ button({
356
+ // The glyph doubles as the state: ☰ to open the page, ✕ to dismiss it. Its
357
+ // own scope, so toggling doesn't rebuild (and re-focus) the button.
358
+ icon: () => A(() => ($nav.open ? closeGlyph : menuGlyph)({ size: "1.5em" })),
359
+ ariaLabel: "Open navigation",
360
+ // Quiet chrome, matching the `menu` slot's own buttons at the other end of the
361
+ // bar: the trigger is a way *in* to the app, not something to be sold on, and a
362
+ // filled brand button here shouts down the title it sits next to.
363
+ attrs: ".neutral .small",
364
+ ...nav.button,
365
+ click: (e) => {
366
+ myEl = e.currentTarget;
367
+ const shell = myEl.closest(".s-main");
368
+ if (shell != null && shell.clientWidth <= NARROW_PX) {
369
+ $nav.open = !$nav.open;
370
+ return;
371
+ }
372
+ // Wide shell: the classic dropdown. A click on the trigger never reaches
373
+ // the menu's own outside-click handler, so toggle it here.
374
+ if (isFloatingMenuOpen(myEl))
375
+ closeFloatingMenu(myEl);
376
+ else
377
+ showFloatingMenu({ items: nav.items, anchor: myEl, dropdownAttrs: nav.dropdownAttrs });
378
+ },
379
+ });
380
+ }
381
+ /**
382
+ * The narrow-screen navigation: a full page sliding in over the content from the
383
+ * left. Picking an item slides it back out while the chosen screen enters from
384
+ * the right, so the two tile across the viewport and the whole thing reads as a
385
+ * lateral move rather than a popup blinking out.
386
+ */
387
+ function drawNavPage(nav, attrs, $nav) {
388
+ // Whether this close is a *navigation* — the only kind that hands over to an
389
+ // incoming screen. Dismissing the page just uncovers the content again.
390
+ let navigated = false;
391
+ const pageEl = A("nav.s-nav-page.s-s.neutral aria-label=Navigation create=s-nav-page-off destroy=s-nav-page-off", attrs, () => drawMenu(nav.items, () => { navigated = true; $nav.open = false; }));
392
+ const shell = pageEl.closest(".s-main");
393
+ const behind = pageEl.parentElement?.querySelector(":scope > .s-body-inner");
394
+ // Content mode's incoming half of the hand-off. In routed mode there is no
395
+ // <main> to slide: the chosen screen is a freshly pushed panel, which plays
396
+ // its own enter animation, so this correctly finds nothing.
397
+ const content = behind?.querySelector(":scope > main");
398
+ // The content is fully covered, but without this it stays tabbable and visible
399
+ // to screen readers underneath the page.
400
+ behind?.setAttribute("inert", "");
401
+ // Widening the shell past the collapse point brings the sidebar back, leaving
402
+ // this page covering the content for no reason — so bow out.
403
+ if (shell != null && typeof ResizeObserver !== "undefined") {
404
+ const ro = new ResizeObserver(() => { if (shell.clientWidth > NARROW_PX)
405
+ $nav.open = false; });
406
+ ro.observe(shell);
407
+ A.clean(() => ro.disconnect());
408
+ }
409
+ A.clean(() => {
410
+ behind?.removeAttribute("inert");
411
+ if (!navigated)
412
+ return;
413
+ // Same tick as the page's own destroy transition, so both halves of the
414
+ // hand-off move in lockstep.
415
+ if (content)
416
+ slideContentIn(content);
417
+ shell?.querySelector(".s-nav-trigger button")?.focus();
418
+ });
419
+ // Land on the current page's entry (or the first one) once we're laid out.
420
+ requestAnimationFrame(() => {
421
+ if (document.body.contains(pageEl))
422
+ focusFirst(pageEl, ".s-menu-item[aria-current=page]");
423
+ });
424
+ }
425
+ /**
426
+ * Play the incoming half of the nav-page hand-off: park `el` one screen to the
427
+ * right, then let its CSS transition carry it home. Reading `offsetWidth` in
428
+ * between forces the browser to adopt the parked position as the "before" state,
429
+ * which is what makes the removal animate instead of doing nothing at all.
430
+ */
431
+ function slideContentIn(el) {
432
+ el.classList.add("s-slide-in");
433
+ void el.offsetWidth;
434
+ el.classList.remove("s-slide-in");
435
+ }
436
+ function drawMainContent(opts, ctl) {
437
+ // Routed mode replaces the single scrollable <main> with the panel viewport,
438
+ // which manages its own columns (and their scrolling) from JS.
439
+ if (ctl) {
440
+ ctl.drawStack();
441
+ return;
442
+ }
236
443
  const mainEl = A("main", () => {
237
444
  A("div.s-content", opts.contentAttrs, () => {
238
445
  drawSlot(opts.content);
@@ -1,5 +1,9 @@
1
1
  import { type Slot, type Attributes } from "../core.js";
2
2
  import { type ButtonOptions } from "./button.js";
3
+ /** `☰` — opens a menu or the nav. */
4
+ export declare const menuGlyph: (opts?: import("../icons-helpers.js").IconOptions) => void;
5
+ /** `✕` — dismisses what the {@link menuGlyph} opened. */
6
+ export declare const closeGlyph: (opts?: import("../icons-helpers.js").IconOptions) => void;
3
7
  /**
4
8
  * A clickable item in a menu or sidebar nav.
5
9
  *
@@ -101,8 +105,17 @@ export declare function drawMenu(items: MenuEntry[], onActivate?: () => void): v
101
105
  * Whether a floating menu is currently open. Reflects live state (cleared the
102
106
  * instant it closes), unlike the DOM — the panel lingers briefly while its
103
107
  * `destroy=` transition plays out.
108
+ *
109
+ * @param anchor When given, only reports `true` for a menu opened from *this*
110
+ * anchor — so a component can ask about its own menu rather than any menu.
111
+ */
112
+ export declare function isFloatingMenuOpen(anchor?: HTMLElement): boolean;
113
+ /**
114
+ * Close the open floating menu (if any), returning focus to its anchor. With an
115
+ * `anchor`, only closes when the open menu belongs to it, so dismissing your own
116
+ * menu can't steal someone else's.
104
117
  */
105
- export declare function isFloatingMenuOpen(): boolean;
118
+ export declare function closeFloatingMenu(anchor?: HTMLElement): void;
106
119
  /**
107
120
  * Open a floating dropdown menu anchored to an element. Portals to
108
121
  * `document.body` (never clipped), positions itself (flipping up when there's