staffa 0.22.2 → 0.23.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.
@@ -7,7 +7,7 @@ import { type MenuOptions, drawMenu, isFloatingMenuOpen, consumeBranchNav, anyCu
7
7
  import { menu as menuIcon, x as closeIcon } from "../icons.js";
8
8
  import { iconButton } from "./button.js";
9
9
  import { isDialogOpen } from "./dialog.js";
10
- import { PAGE_MS, PanelStackController, SMALL_MAX_PX, type PanelStack, type AncestorTable, type Panel, type RouteHandler, type RouteTable, type Routes } from "./panels.js";
10
+ import { PAGE_MS, PanelStackController, SMALL_MAX_PX, type PanelStack, type PanelAlignment, type AncestorTable, type Panel, type RouteHandler, type RouteTable, type Routes } from "./panels.js";
11
11
 
12
12
  /** Options for {@link main}. */
13
13
  export interface MainOptions<R = Routes> {
@@ -201,6 +201,19 @@ export interface MainOptions<R = Routes> {
201
201
  * state.
202
202
  */
203
203
  columns?: "auto" | "single";
204
+ /**
205
+ * Where the open columns sit when they don't fill the content area — on a
206
+ * wide screen with a single small panel open, say. `"center"` (the default)
207
+ * splits the leftover room between both sides, `"left"` and `"right"` put it
208
+ * all on one side, and `"spaced"` shares it out evenly over the gutters
209
+ * beside *and between* the columns. Columns crowded out on the left take
210
+ * precedence: while there are any, the run hugs the left edge whatever this
211
+ * says. Routed mode only.
212
+ *
213
+ * Live, like {@link MainOptions.columns}: a change is adopted in one layout
214
+ * pass, every panel keeping its state.
215
+ */
216
+ alignment?: PanelAlignment;
204
217
  /**
205
218
  * What a link *without* a `data-panel` attribute does — the per-link
206
219
  * attribute always wins. The three keep less and less of the link's own
@@ -268,6 +281,15 @@ export interface MainOptions<R = Routes> {
268
281
  * Live, like {@link MainOptions.maxWidth}.
269
282
  */
270
283
  navWidth?: number;
284
+ /**
285
+ * Lets the user resize the nav sidebar by dragging the hairline between it
286
+ * and the content area, or with the arrow keys once it has focus. Defaults
287
+ * to `false`.
288
+ *
289
+ * A dragged width stands until {@link MainOptions.navWidth} changes, and
290
+ * stays between 120 px and half the shell.
291
+ */
292
+ navResizable?: boolean;
271
293
  /** Aberdeen attr/style string applied to the sidebar nav panel. */
272
294
  navAttrs?: Attributes;
273
295
  /** Aberdeen attr/style string applied to the narrow-screen full-page nav. */
@@ -276,6 +298,8 @@ export interface MainOptions<R = Routes> {
276
298
 
277
299
  /** The default nav column, hairline included — see {@link MainOptions.navWidth}. */
278
300
  const NAV_W = 200;
301
+ /** The narrowest a user can drag the nav column — see {@link MainOptions.navResizable}. */
302
+ const NAV_MIN_W = 120;
279
303
 
280
304
  A.insertGlobalCss({
281
305
  ".s-main": {
@@ -328,6 +352,12 @@ A.insertGlobalCss({
328
352
  // A vertical hairline between sidebar and content, fading out at both ends —
329
353
  // the vertical sibling of the menu's `hr.s-menu-sep`.
330
354
  ".s-nav-sep": "width:1px flex-shrink:0 align-self:stretch margin: 0.6rem 0; border:0 background: linear-gradient(to bottom, transparent, $s-faint 18%, $s-faint 82%, transparent);",
355
+ // A resizable one is a handle, its hit area widened beyond the hairline.
356
+ ".s-nav-sep[role=separator]": {
357
+ "&": "cursor:col-resize position:relative z-index:1 touch-action:none",
358
+ "&::before": "content:'' position:absolute inset: -0.6rem -4px;",
359
+ "&:hover, &:active, &:focus-visible": "background:$s-accent",
360
+ },
331
361
  // min-height/min-width:0 override flex's `auto`, so <main> can shrink to its
332
362
  // container instead of letting wide content push the body — and any sidebar
333
363
  // — past the viewport edge. The transition is dormant except for the
@@ -457,6 +487,9 @@ export function main<R extends RouteTable<R>>(opts: MainOptions<R> = {}): PanelS
457
487
  // Whether the narrow-screen full-page nav is showing. Per shell, so nested or
458
488
  // sibling `main()`s can't fight over it.
459
489
  const $nav = A.proxy({ open: false });
490
+ // The sidebar column's width: `navWidth`, until the user drags it elsewhere.
491
+ const $navW = A.proxy({ px: NAV_W });
492
+ A(() => { $navW.px = opts.navWidth ?? NAV_W; });
460
493
  // Whether the shell is narrow, at the same NARROW_PX the `@container` queries
461
494
  // above use. One boolean for everything that must agree on the regime — the
462
495
  // bar's layout, what the ☰ does, where a panel's chrome goes — so they can't
@@ -486,6 +519,7 @@ export function main<R extends RouteTable<R>>(opts: MainOptions<R> = {}): PanelS
486
519
  // Live: each in its own scope, so a change on a proxied options object
487
520
  // re-runs only that scope — the columns relayout in place, panels intact.
488
521
  A(() => ctl.setColumns(opts.columns));
522
+ A(() => ctl.setAlignment(opts.alignment));
489
523
  A(() => ctl.setLinkNavigation(opts.linkNavigation));
490
524
  }
491
525
  // Where the brand mark and the app's name link; nowhere under `home: null`.
@@ -501,7 +535,7 @@ export function main<R extends RouteTable<R>>(opts: MainOptions<R> = {}): PanelS
501
535
  // here subscribes this scope alone, never the shell entire (see `nav`).
502
536
  A(() => {
503
537
  if (nav == null || !nav.items.length) A("--s-nav-w: 0px");
504
- else A(`.s-nav-${navPos}`, `--s-nav-w: ${opts.navWidth ?? NAV_W}px`);
538
+ else A(`.s-nav-${navPos}`, `--s-nav-w: ${$navW.px}px`);
505
539
  });
506
540
 
507
541
  // Top bar: `[leading] [identity] …spacer… [trailing]`, each slot's contents
@@ -560,8 +594,15 @@ export function main<R extends RouteTable<R>>(opts: MainOptions<R> = {}): PanelS
560
594
  // falling back to the app's chrome. Promoted actions are marked
561
595
  // `.s-panel-origin`, so a link among them still builds on the
562
596
  // current panel — see `interceptLinks` in panels.ts.
597
+ //
598
+ // Which slot wins is derived, so a panel swap that doesn't change
599
+ // the answer — neither screen declaring actions, the app's `menu`
600
+ // standing on both sides — leaves the slot's DOM alone. This is
601
+ // where an app puts its stateful chrome (`menu`'s search box), and
602
+ // rebuilding it would drop focus, caret and IME composition.
603
+ const $promoted = A.derive(() => ($shell.narrow ? ctl?.currentPanel?.actions : undefined));
563
604
  A(() => {
564
- const actions = $shell.narrow ? ctl?.currentPanel?.actions : undefined;
605
+ const actions = $promoted.value;
565
606
  const slot = actions ?? opts.menu;
566
607
  if (slot != null) A(`div.s-menu${actions != null ? ".s-panel-origin" : ""}`, () => drawSlot(slot));
567
608
  });
@@ -581,7 +622,8 @@ export function main<R extends RouteTable<R>>(opts: MainOptions<R> = {}): PanelS
581
622
  A(`nav.s-nav-panel.s-nav-${navPos}`, opts.navAttrs, () => {
582
623
  drawMenu(nav.items);
583
624
  });
584
- A("div.s-nav-sep aria-hidden=true");
625
+ if (opts.navResizable) drawNavHandle(navPos, $navW);
626
+ else A("div.s-nav-sep aria-hidden=true");
585
627
  });
586
628
  drawMainContent(opts, ctl);
587
629
  });
@@ -762,6 +804,39 @@ function drawNavTrigger(nav: MenuOptions, $nav: { open: boolean }): void {
762
804
  });
763
805
  }
764
806
 
807
+ /**
808
+ * The hairline between sidebar and content as a window splitter: dragging it, or
809
+ * pressing ←/→ while it has focus, sets `$navW.px` — the sidebar's whole column.
810
+ */
811
+ function drawNavHandle(navPos: "left" | "right", $navW: { px: number }): void {
812
+ // Which way the sidebar grows: away from the shell edge it sits against.
813
+ const dir = navPos === "right" ? -1 : 1;
814
+ A("div.s-nav-sep role=separator aria-orientation=vertical aria-label='Resize navigation' tabindex=0", () => {
815
+ A(() => A("aria-valuenow=", $navW.px));
816
+ const handle = A() as HTMLElement;
817
+ const resize = (px: number) => {
818
+ const max = handle.parentElement!.clientWidth / 2;
819
+ $navW.px = Math.round(Math.max(NAV_MIN_W, Math.min(max, px)));
820
+ };
821
+ A("pointerdown=", (e: PointerEvent) => {
822
+ if (e.button !== 0) return;
823
+ e.preventDefault();
824
+ const x0 = e.clientX;
825
+ const w0 = $navW.px;
826
+ const move = (e: PointerEvent) => resize(w0 + dir * (e.clientX - x0));
827
+ handle.setPointerCapture(e.pointerId);
828
+ handle.addEventListener("pointermove", move);
829
+ handle.addEventListener("lostpointercapture", () => handle.removeEventListener("pointermove", move), { once: true });
830
+ });
831
+ A("keydown=", (e: KeyboardEvent) => {
832
+ const step = e.key === "ArrowRight" ? 10 : e.key === "ArrowLeft" ? -10 : 0;
833
+ if (!step) return;
834
+ e.preventDefault();
835
+ resize($navW.px + dir * step);
836
+ });
837
+ });
838
+ }
839
+
765
840
  /**
766
841
  * The narrow-screen navigation: a full panel sliding in over the content from
767
842
  * the left. Picking an item slides it back out as the chosen screen enters from
@@ -102,6 +102,12 @@ export type AncestorTable<R> = {
102
102
  */
103
103
  export type PanelSize = "small" | "medium" | "large" | "none";
104
104
 
105
+ /**
106
+ * Where the open columns sit when they don't fill the content area; see
107
+ * {@link MainOptions.alignment}.
108
+ */
109
+ export type PanelAlignment = "left" | "right" | "center" | "spaced";
110
+
105
111
  /**
106
112
  * What a route handler gets: the params from its route, plus everything the
107
113
  * shell needs to know about the panel it is drawing. It's an Aberdeen proxy, so
@@ -153,6 +159,10 @@ export interface Panel<P = Record<string, string | number | string[]>> {
153
159
  * They are drawn in exactly one of those places at a time, so crossing the
154
160
  * threshold redraws them; anything stateful inside (the focus in a search
155
161
  * box) is lost. Buttons and menus are fine.
162
+ *
163
+ * Assign one function and keep it: a fresh closure on every redraw of your
164
+ * handler is a new slot, so the shell redraws the actions — and on a narrow
165
+ * shell, where they sit in the top bar, the app's own chrome with them.
156
166
  */
157
167
  actions?: Slot;
158
168
  /**
@@ -477,7 +487,8 @@ A.insertGlobalCss({
477
487
  `z-index:2 transition: transform ${PAGE_MS}ms ease-out, opacity ${PAGE_MS}ms linear;`,
478
488
  // The hairline between two columns, fading at both ends (like the sidebar's
479
489
  // `.s-nav-sep`). Columns tile with no gutter — each brings its own `$3` of
480
- // padding — so this sits exactly on the boundary.
490
+ // padding — so this sits exactly on the boundary. A `"spaced"` alignment,
491
+ // which does put a gutter between them, leaves it off (see `layout`).
481
492
  "&.s-panel-sep::before":
482
493
  "content:'' position:absolute left:0 top:0.6rem bottom:0.6rem width:1px z-index:1 " +
483
494
  "background: linear-gradient(to bottom, transparent, $s-faint 18%, $s-faint 82%, transparent);",
@@ -672,6 +683,8 @@ export interface PanelStackOptions {
672
683
  ancestors?: Record<string, AncestorsHandler | undefined>;
673
684
  /** How many panels are shown at a time. See {@link MainOptions.columns}. */
674
685
  columns?: "auto" | "single";
686
+ /** Where a short run of columns sits in the area. See {@link MainOptions.alignment}. */
687
+ alignment?: PanelAlignment;
675
688
  /** What a bare link does. See {@link MainOptions.linkNavigation}. */
676
689
  linkNavigation?: "push" | "replace" | "open";
677
690
  /** The shell's own title, used as the suffix of `document.title`. */
@@ -1505,6 +1518,13 @@ export class PanelStackController implements PanelStack {
1505
1518
  this.scheduleLayout();
1506
1519
  }
1507
1520
 
1521
+ /** Adopt a changed `alignment`: one layout pass, nothing redrawn. */
1522
+ setAlignment(alignment: PanelAlignment | undefined): void {
1523
+ if (this.opts.alignment === alignment) return;
1524
+ this.opts.alignment = alignment;
1525
+ this.scheduleLayout();
1526
+ }
1527
+
1508
1528
  /** Adopt a changed `linkNavigation` default; the next click reads it. */
1509
1529
  setLinkNavigation(mode: "push" | "replace" | "open" | undefined): void {
1510
1530
  this.opts.linkNavigation = mode;
@@ -1854,11 +1874,16 @@ export class PanelStackController implements PanelStack {
1854
1874
  }
1855
1875
  }
1856
1876
 
1857
- // A run that doesn't fill the fixed-width area centres in it — unless
1858
- // panels sit crowded out on its left: then it hugs the left edge instead,
1859
- // so the strip crosses that edge with no gap and a reveal is a plain
1860
- // slide back in.
1861
- const left = first > 0 ? 0 : (geom.area - runSum) / 2;
1877
+ // A run that doesn't fill the fixed-width area is placed in it by the
1878
+ // shell's `alignment`, `"spaced"` sharing the slack out over the gutters
1879
+ // around and between the columns. Unless panels sit crowded out on the
1880
+ // run's left: then it hugs the left edge, so the strip crosses that edge
1881
+ // with no gap, a reveal is a plain slide back in, and no gutter lets a
1882
+ // crowded-out panel peek through.
1883
+ const slack = first > 0 ? 0 : geom.area - runSum;
1884
+ const align = this.opts.alignment ?? "center";
1885
+ const gap = align === "spaced" ? slack / (cur - first + 2) : 0;
1886
+ const left = align === "left" ? 0 : align === "right" ? slack : align === "spaced" ? gap : slack / 2;
1862
1887
 
1863
1888
  for (let i = first; i <= cur; i++) live[i].width = width(live[i]);
1864
1889
  // Never-visible panels get their would-be width, so a reveal doesn't start
@@ -1926,12 +1951,12 @@ export class PanelStackController implements PanelStack {
1926
1951
  }
1927
1952
  }
1928
1953
  el.style.width = `${entry.width}px`;
1929
- x += entry.width;
1954
+ x += entry.width + gap;
1930
1955
  // Written only on a change, so per-panel UI hanging off `visible` or
1931
1956
  // `width` isn't rebuilt by every pass.
1932
1957
  if (entry.$panel.visible !== shown) entry.$panel.visible = shown;
1933
1958
  if (entry.$panel.width !== entry.width) entry.$panel.width = entry.width;
1934
- el.classList.toggle("s-panel-sep", shown && i > first);
1959
+ el.classList.toggle("s-panel-sep", shown && i > first && !gap);
1935
1960
  // Off-screen panels stop rendering, keeping their DOM. A revealed
1936
1961
  // panel is visible at once; one sliding offstage stays visible for
1937
1962
  // the whole slide, its visibility class deferred to the slide's
@@ -41,9 +41,9 @@ A.insertGlobalCss({
41
41
  // a prefix/suffix gives it one.
42
42
  "textarea.s-input, .s-inset.s-inset-bottom": "flex: 0 1 100%; min-height:0",
43
43
  "textarea.s-input": "resize:vertical min-height:3em line-height:1.45",
44
- // Sized to its text there is nothing to scroll, so `auto` shows no bar — until
45
- // a caller caps the height (`inputAttrs: "max-height:12em"`), when it should.
46
- "textarea.s-input.s-autoGrow": "resize:none min-height:2.5em overflow-y:auto",
44
+ // Sized to its text there is nothing to scroll; `fitToContent` turns scrolling
45
+ // on once a caller caps the height (`inputAttrs: "max-height:12em"`).
46
+ "textarea.s-input.s-autoGrow": "resize:none min-height:2.5em overflow-y:hidden",
47
47
  });
48
48
 
49
49
  /**
@@ -106,8 +106,14 @@ export function textarea(opts: TextareaOptions = {}): void {
106
106
 
107
107
  function fitToContent(el: HTMLTextAreaElement): void {
108
108
  el.style.height = "auto";
109
+ // Measure without a scrollbar: one would narrow the text and wrap it early.
110
+ el.style.overflowY = "";
109
111
  // `scrollHeight` measures the padding box, while the height we set sizes the
110
112
  // border box: add the borders back, or the text ends up two pixels too tall
111
113
  // for its box and the textarea scrolls by that much.
112
- el.style.height = `${el.scrollHeight + el.offsetHeight - el.clientHeight}px`;
114
+ const height = el.scrollHeight + el.offsetHeight - el.clientHeight;
115
+ el.style.height = `${height}px`;
116
+ // Scroll only once a `max-height` caps the box: `scrollHeight` is rounded, so
117
+ // an uncapped box can fall a fraction of a pixel short and show a bar.
118
+ if (el.offsetHeight < height) el.style.overflowY = "auto";
113
119
  }
package/src/index.ts CHANGED
@@ -42,7 +42,7 @@ export { buttonGroup, type ButtonGroupOptions } from "./components/buttonGroup.j
42
42
  export { checkbox, type CheckboxOptions } from "./components/checkbox.js";
43
43
  export { form, type FormOptions } from "./components/form.js";
44
44
  export { main, closeNav, type MainOptions } from "./components/main.js";
45
- export { type PanelStack, type Panel, type PanelSize, type Routes, type RouteHandler, type RouteTable, type AncestorsHandler, type AncestorTable, type PathParams, type SegParams } from "./components/panels.js";
45
+ export { type PanelStack, type Panel, type PanelSize, type PanelAlignment, type Routes, type RouteHandler, type RouteTable, type AncestorsHandler, type AncestorTable, type PathParams, type SegParams } from "./components/panels.js";
46
46
  export { menu, menuButton, showFloatingMenu, addContextMenu, isFloatingMenuOpen, closeFloatingMenu, type MenuListOptions, type MenuOptions, type MenuEntry, type MenuItem, type MenuSeparator, type FloatingMenuOptions, type ContextMenuOptions } from "./components/menu.js";
47
47
  export { dialog, alert, confirm, prompt, isDialogOpen, type DialogOptions } from "./components/dialog.js";
48
48
  export { select, type SelectOptions, type SelectOptionInput } from "./components/select.js";