staffa 0.13.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -16
- package/dist/components/dialog.js +1 -1
- package/dist/components/main.d.ts +37 -48
- package/dist/components/main.js +25 -61
- package/dist/components/menu.d.ts +8 -0
- package/dist/components/menu.js +40 -6
- package/dist/components/panels.d.ts +114 -82
- package/dist/components/panels.js +198 -230
- package/dist/components/select.js +4 -2
- package/dist/components/tabs.js +5 -4
- package/dist/components/tooltip.js +2 -1
- package/dist/index.d.ts +1 -1
- package/dist/staffa.esm.js +1 -1
- package/dist/theme.d.ts +13 -65
- package/dist/theme.js +21 -8
- package/package.json +1 -1
- package/skill/FloatingMenuOptions.md +10 -0
- package/skill/MainOptions.md +35 -49
- package/skill/Panel.md +31 -32
- package/skill/PanelStack.md +4 -4
- package/skill/SKILL.md +29 -16
- package/skill/main.md +2 -1
- package/src/components/dialog.ts +1 -1
- package/src/components/main.ts +62 -108
- package/src/components/menu.ts +50 -7
- package/src/components/panels.ts +269 -290
- package/src/components/select.ts +4 -2
- package/src/components/tabs.ts +3 -2
- package/src/components/tooltip.ts +2 -1
- package/src/index.ts +1 -1
- package/src/theme.ts +24 -9
package/src/components/main.ts
CHANGED
|
@@ -8,7 +8,7 @@ import { type MenuOptions, drawMenu, isFloatingMenuOpen, consumeBranchNav, anyCu
|
|
|
8
8
|
import { menu as menuIcon, x as closeIcon } from "../icons.js";
|
|
9
9
|
import { iconButton } from "./button.js";
|
|
10
10
|
import { isDialogOpen } from "./dialog.js";
|
|
11
|
-
import { PanelStackController, type PanelStack, type AncestorTable, type Panel, type RouteHandler, type RouteTable, type Routes } from "./panels.js";
|
|
11
|
+
import { PanelStackController, SMALL_MAX_PX, type PanelStack, type AncestorTable, type Panel, type RouteHandler, type RouteTable, type Routes } from "./panels.js";
|
|
12
12
|
|
|
13
13
|
/** Options for {@link main}. */
|
|
14
14
|
export interface MainOptions<R = Routes> {
|
|
@@ -94,16 +94,15 @@ export interface MainOptions<R = Routes> {
|
|
|
94
94
|
* Navigating is just links: the shell handles the clicks itself, so do *not*
|
|
95
95
|
* also call Aberdeen's `interceptLinks()`. A link opens its target on top of
|
|
96
96
|
* the panel it sits in, closing everything after that panel first. The
|
|
97
|
-
* `data-panel` attribute
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
* with `aberdeen/route`'s own `go()` works too — a panel with
|
|
97
|
+
* `data-panel` attribute keeps less of that context instead: `replace`
|
|
98
|
+
* drops the link's own panel too, putting the target in its place, and
|
|
99
|
+
* `open` drops it all, giving the target its own stack, the way a nav item
|
|
100
|
+
* does. A plain link to something already open goes back to it rather than
|
|
101
|
+
* opening it twice, closing whatever was stacked on top — pinned panels
|
|
102
|
+
* excepted (see {@link Panel.pinned}), and panels holding unsaved work,
|
|
103
|
+
* which park instead; a `replace` or `open` applies its usual shape, the
|
|
104
|
+
* open panel moving into it alive. From code, use {@link pushPanel} and
|
|
105
|
+
* friends: navigating with `aberdeen/route`'s own `go()` works too — a panel with
|
|
107
106
|
* {@link Panel.unsaved} work still survives it — but builds the whole stack
|
|
108
107
|
* from the path. A navigation guard the app registered with
|
|
109
108
|
* `route.setGuard` (an auth redirect, say) keeps working: the shell
|
|
@@ -113,11 +112,11 @@ export interface MainOptions<R = Routes> {
|
|
|
113
112
|
* is called ({@link Panel.title} — unset, its first line of text stands in)
|
|
114
113
|
* and what it can do ({@link Panel.actions}); everything else in a column is
|
|
115
114
|
* the panel's own content, boxes included. The shell writes the stack of
|
|
116
|
-
* open panels as breadcrumbs in the top bar — click one to go back to it
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
* the
|
|
120
|
-
*
|
|
115
|
+
* open panels as breadcrumbs in the top bar — click one to go back to it —
|
|
116
|
+
* and places each panel's actions where the room is: on its own column while
|
|
117
|
+
* several fit, in the bar once the shell is narrow and the current panel *is*
|
|
118
|
+
* the screen. Nothing in an app measures the viewport to lay its screens out
|
|
119
|
+
* twice.
|
|
121
120
|
*
|
|
122
121
|
* Only one routed shell can be mounted at a time (a second one throws) —
|
|
123
122
|
* the URL is global, so two of them would fight over it. Nothing else is:
|
|
@@ -192,9 +191,10 @@ export interface MainOptions<R = Routes> {
|
|
|
192
191
|
* How many panels are *shown* at a time. `"auto"` (the default) shows as
|
|
193
192
|
* many columns, side by side, as comfortably fit, ending at the current
|
|
194
193
|
* panel; `"single"` shows only the current panel, however wide the screen
|
|
195
|
-
* —
|
|
196
|
-
*
|
|
197
|
-
*
|
|
194
|
+
* — one screen at a time at every size, each still at its asked width,
|
|
195
|
+
* centred (the nav sidebar still sits beside it). Only the display
|
|
196
|
+
* differs: the stack, the breadcrumbs, the URL, Escape and the back
|
|
197
|
+
* button behave identically in both. Routed mode only.
|
|
198
198
|
*
|
|
199
199
|
* Live: pass a proxied options object (or make this field a getter) and a
|
|
200
200
|
* change is adopted in place — one layout pass, every panel keeping its
|
|
@@ -203,12 +203,13 @@ export interface MainOptions<R = Routes> {
|
|
|
203
203
|
columns?: "auto" | "single";
|
|
204
204
|
/**
|
|
205
205
|
* What a link *without* a `data-panel` attribute does — the per-link
|
|
206
|
-
* attribute always wins.
|
|
207
|
-
*
|
|
208
|
-
* `"
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
206
|
+
* attribute always wins. The three keep less and less of the link's own
|
|
207
|
+
* context: `"push"` (the default) builds on the panel the link sits in,
|
|
208
|
+
* `"replace"` swaps that panel out, and `"open"` ignores it and gives the
|
|
209
|
+
* target its own stack, the way a nav item does. So with `"open"` every
|
|
210
|
+
* click replaces the content as a whole — which, with flat routes, is the
|
|
211
|
+
* conventional sidebar-and-content app: one pane, swapped on every click,
|
|
212
|
+
* the crumb line simply naming it. Routed mode only.
|
|
212
213
|
*
|
|
213
214
|
* Live, like {@link MainOptions.columns}: change it and the next click
|
|
214
215
|
* uses the new default.
|
|
@@ -217,32 +218,23 @@ export interface MainOptions<R = Routes> {
|
|
|
217
218
|
/** Footer content, pinned below the scroll area. */
|
|
218
219
|
footer?: Slot;
|
|
219
220
|
/**
|
|
220
|
-
* Max width for the
|
|
221
|
+
* Max width for the shell's *content*, e.g. `"80rem"`. The header and footer
|
|
221
222
|
* backgrounds still span the full shell width, but their contents — and the
|
|
222
223
|
* sidebar + separator + content trio (or just the content when there's no
|
|
223
224
|
* sidebar) — cap to this width and centre horizontally. When unset, everything
|
|
224
225
|
* fills the available width. Either way the content shares the panel surface —
|
|
225
226
|
* it is not boxed.
|
|
226
227
|
*
|
|
227
|
-
*
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
|
|
231
|
-
maxWidth?: string;
|
|
232
|
-
/**
|
|
233
|
-
* How wide a `"full"` panel gets, in pixels — and with it the whole content
|
|
234
|
-
* area, since a `"full"` fills it exactly. A `"half"` gets half of this, and
|
|
235
|
-
* a `"screen"` ignores it and takes the window. Defaults to 1080; the window
|
|
236
|
-
* caps it when there is less room than that. Routed mode only.
|
|
237
|
-
*
|
|
238
|
-
* This plus {@link MainOptions.navWidth} is the app's standard page — see
|
|
239
|
-
* there.
|
|
228
|
+
* In routed mode this is what the columns divide up (see
|
|
229
|
+
* {@link Panel.maxWidth}), which is the reason to set it on a very wide
|
|
230
|
+
* screen: left uncapped, a stack of small panels will happily march right
|
|
231
|
+
* across a 4K display.
|
|
240
232
|
*
|
|
241
233
|
* Live, like {@link MainOptions.columns}: pass a proxied options object (or
|
|
242
|
-
* make this field a getter) and a change is adopted in one layout pass,
|
|
243
|
-
*
|
|
234
|
+
* make this field a getter) and a change is adopted in one layout pass, every
|
|
235
|
+
* open panel keeping its state.
|
|
244
236
|
*/
|
|
245
|
-
|
|
237
|
+
maxWidth?: string;
|
|
246
238
|
/** Aberdeen attr/style string applied to the content area. */
|
|
247
239
|
contentAttrs?: Attributes;
|
|
248
240
|
/** Aberdeen attr/style string applied to the top bar. */
|
|
@@ -271,13 +263,9 @@ export interface MainOptions<R = Routes> {
|
|
|
271
263
|
navPosition?: "left" | "right";
|
|
272
264
|
/**
|
|
273
265
|
* How wide the nav sidebar column is, in pixels — its hairline included.
|
|
274
|
-
* Defaults to 200.
|
|
266
|
+
* Defaults to 200. Whatever it takes comes off the content area beside it.
|
|
275
267
|
*
|
|
276
|
-
*
|
|
277
|
-
* page*: the width the top bar and footer keep to, and the width the
|
|
278
|
-
* columns settle back to. The defaults come to the familiar 1280px.
|
|
279
|
-
*
|
|
280
|
-
* Live, like {@link MainOptions.fullWidth}.
|
|
268
|
+
* Live, like {@link MainOptions.maxWidth}.
|
|
281
269
|
*/
|
|
282
270
|
navWidth?: number;
|
|
283
271
|
/** Aberdeen attr/style string applied to the sidebar nav panel. */
|
|
@@ -286,15 +274,8 @@ export interface MainOptions<R = Routes> {
|
|
|
286
274
|
navPageAttrs?: Attributes;
|
|
287
275
|
}
|
|
288
276
|
|
|
289
|
-
/**
|
|
290
|
-
* The default nav column (hairline included) and the default width of a
|
|
291
|
-
* `"full"` panel — see {@link MainOptions.navWidth} and
|
|
292
|
-
* {@link MainOptions.fullWidth}. Side by side they come to the 1280px page the
|
|
293
|
-
* shell is usually seen as, but that figure lives nowhere: the browser adds
|
|
294
|
-
* these two up, and an app that changes either simply gets a different page.
|
|
295
|
-
*/
|
|
277
|
+
/** The default nav column, hairline included — see {@link MainOptions.navWidth}. */
|
|
296
278
|
const NAV_W = 200;
|
|
297
|
-
const FULL_W = 1080;
|
|
298
279
|
|
|
299
280
|
A.insertGlobalCss({
|
|
300
281
|
".s-main": {
|
|
@@ -375,28 +356,6 @@ A.insertGlobalCss({
|
|
|
375
356
|
// and the bar already comes from `.s-content`'s padding. Without a scrollbar
|
|
376
357
|
// there's no margin, so the content keeps its single $3 edge — not 2×$3.
|
|
377
358
|
".s-body main.s-scroll-y": "margin-right:$3",
|
|
378
|
-
// Routed mode takes its width from the stack instead of from
|
|
379
|
-
// `maxWidth`: the layout engine publishes the ensemble width (sidebar +
|
|
380
|
-
// separator + content area) as --s-shell-w — the standard page
|
|
381
|
-
// normally, wider while the columns outgrow it (a "screen" page, or
|
|
382
|
-
// extra columns fitting a wide window) — and the body row caps itself
|
|
383
|
-
// to it, staying centred around the columns. Changing the custom
|
|
384
|
-
// property animates the max-width consuming it, with no JS in the loop:
|
|
385
|
-
// the body recentres in step with the panel whose arrival or departure
|
|
386
|
-
// moved it, over the same --s-panel-ms (see panels.ts). During a window
|
|
387
|
-
// resize (and the very first pass) the layout engine raises
|
|
388
|
-
// `.s-shell-snap` so the new width is adopted instantly instead of
|
|
389
|
-
// chasing the window through a transition.
|
|
390
|
-
"&.s-routed > .s-body > .s-body-inner":
|
|
391
|
-
"max-width: var(--s-shell-w, 100%); transition: max-width var(--s-panel-ms) ease;",
|
|
392
|
-
"&.s-routed.s-shell-snap > .s-body > .s-body-inner": "transition:none",
|
|
393
|
-
// The bars don't follow the ensemble past the standard page: a header
|
|
394
|
-
// stretching to the window's edges and back with every "screen" panel
|
|
395
|
-
// reads as the whole app flexing, so the chrome holds still and only
|
|
396
|
-
// the columns grow. (Below the standard width the ensemble is simply
|
|
397
|
-
// the window, which only a resize changes — so the bars never animate,
|
|
398
|
-
// and take no part in the transition above.)
|
|
399
|
-
"&.s-routed > header > .s-bar, &.s-routed > footer > .s-bar": "max-width: calc(var(--s-nav-w) + var(--s-full-w))",
|
|
400
359
|
},
|
|
401
360
|
// Sidebar nav panel. Items reuse the shared `.s-menu-item` /
|
|
402
361
|
// `.s-menu-sep` styles from menu.ts, so the sidebar and the floating
|
|
@@ -443,13 +402,20 @@ A.insertGlobalCss({
|
|
|
443
402
|
// A phone's bar holds two lines of chrome in a screen's width, so it buys
|
|
444
403
|
// the stack and the screen's actions room by spending less on air.
|
|
445
404
|
".s-main > header > .s-bar": "gap:$1 padding: $1 $2;",
|
|
446
|
-
//
|
|
447
|
-
//
|
|
448
|
-
".s-content > .s-box": "margin-inline: calc(-1 * $3); r:0 border-inline:0",
|
|
449
|
-
// At narrow widths, content boxes are full-bleed so there's no inset to
|
|
450
|
-
// align the scrollbar with — cancel the right margin.
|
|
405
|
+
// The narrow bar tucks its content in to $2 (above), so the $3 scrollbar
|
|
406
|
+
// inset no longer has a chrome edge to align with — cancel it.
|
|
451
407
|
".s-main .s-body main.s-scroll-y": "margin-right:0",
|
|
452
408
|
},
|
|
409
|
+
// On phones a top-level content box becomes a full-bleed block: pull it out
|
|
410
|
+
// to negate the content padding and drop the rounded corners. Keyed on
|
|
411
|
+
// SMALL_MAX_PX, not the narrow threshold above: at or below it a column can
|
|
412
|
+
// never be narrower than the window (every size caps at the content area,
|
|
413
|
+
// and a lone column's cap is exactly this — see panels.ts), while just above
|
|
414
|
+
// it a small column floats centred, where a bleeding box would shed its card
|
|
415
|
+
// chrome over open ground.
|
|
416
|
+
[`@container (max-width: ${SMALL_MAX_PX}px)`]: {
|
|
417
|
+
".s-content > .s-box": "margin-inline: calc(-1 * $3); r:0 border-inline:0",
|
|
418
|
+
},
|
|
453
419
|
});
|
|
454
420
|
|
|
455
421
|
/**
|
|
@@ -464,7 +430,8 @@ A.insertGlobalCss({
|
|
|
464
430
|
* Instead of a single `content` slot, pass {@link MainOptions.routes} and the
|
|
465
431
|
* shell takes over navigation: each route draws one screen, called a panel,
|
|
466
432
|
* and as many columns as fit are shown at a time, side by side on a wide screen
|
|
467
|
-
* and one at a time on a phone
|
|
433
|
+
* and one at a time on a phone; {@link MainOptions.maxWidth} caps how much room
|
|
434
|
+
* they have between them. Each panel *declares* its chrome — its
|
|
468
435
|
* {@link Panel.title} and its {@link Panel.actions} — and this shell places it:
|
|
469
436
|
* the stack of titles as breadcrumbs in the bar, the actions on the panel's
|
|
470
437
|
* column while several fit and in the bar once the shell is narrow enough
|
|
@@ -539,9 +506,6 @@ export function main<R extends RouteTable<R>>(opts: MainOptions<R> = {}): PanelS
|
|
|
539
506
|
notFound: opts.notFound,
|
|
540
507
|
ancestors: opts.ancestors,
|
|
541
508
|
title: opts.title,
|
|
542
|
-
// Corrected below, and on every change, from the app's own option:
|
|
543
|
-
// read here it would subscribe the whole shell to it.
|
|
544
|
-
fullWidth: FULL_W,
|
|
545
509
|
$shell,
|
|
546
510
|
})
|
|
547
511
|
: null;
|
|
@@ -553,22 +517,18 @@ export function main<R extends RouteTable<R>>(opts: MainOptions<R> = {}): PanelS
|
|
|
553
517
|
// the new link default. Nothing else of the shell is touched.
|
|
554
518
|
A(() => ctl.setColumns(opts.columns));
|
|
555
519
|
A(() => ctl.setLinkNavigation(opts.linkNavigation));
|
|
556
|
-
A(() => ctl.setFullWidth(opts.fullWidth ?? FULL_W));
|
|
557
520
|
}
|
|
558
521
|
// Where the brand mark and the app's name link — or nowhere, when the app
|
|
559
522
|
// said `home: null` (a title slot holding a control of its own, say).
|
|
560
523
|
const homeHref = ctl && opts.home !== null ? opts.home ?? "/" : null;
|
|
561
|
-
//
|
|
562
|
-
//
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
// either on a proxied options object resizes the shell in place, panels
|
|
570
|
-
// and their state untouched.
|
|
571
|
-
A(() => A(`--s-full-w: ${opts.fullWidth ?? FULL_W}px`));
|
|
524
|
+
// The shell's one width cap, applied to the body row and to both bars, so the
|
|
525
|
+
// chrome and the content always line up. Each of the three reads it in a
|
|
526
|
+
// scope of its own — one that draws nothing, so re-running it is a single
|
|
527
|
+
// style write: an app that changes it on a proxied options object resizes the
|
|
528
|
+
// shell in place, panels and their state untouched.
|
|
529
|
+
const capWidth = () => { if (opts.maxWidth != null) A("max-width:", opts.maxWidth); };
|
|
530
|
+
|
|
531
|
+
const root = A("div.s-main", opts.attrs, () => {
|
|
572
532
|
// `--s-nav-w` is the sidebar's whole column, and nothing at all when there
|
|
573
533
|
// is no sidebar to give it to — a shell without one lines its bars up with
|
|
574
534
|
// the content. This scope also tags the shell with the side the sidebar is
|
|
@@ -598,9 +558,7 @@ export function main<R extends RouteTable<R>>(opts: MainOptions<R> = {}): PanelS
|
|
|
598
558
|
A("header.s-s.neutral", opts.topbarAttrs, () => {
|
|
599
559
|
A("div.s-bar", () => {
|
|
600
560
|
// Cap the bar's content to maxWidth and centre it within the full-width header.
|
|
601
|
-
A(
|
|
602
|
-
if (capWidth != null) A("max-width:", capWidth);
|
|
603
|
-
});
|
|
561
|
+
A(capWidth);
|
|
604
562
|
|
|
605
563
|
// Leading: the ☰ once the nav has collapsed, the logo otherwise.
|
|
606
564
|
// Deliberately no back button, at any width: going back is the
|
|
@@ -661,9 +619,7 @@ export function main<R extends RouteTable<R>>(opts: MainOptions<R> = {}): PanelS
|
|
|
661
619
|
// are identical with and without a sidebar nav.
|
|
662
620
|
A("div.s-body", () => {
|
|
663
621
|
A("div.s-body-inner", () => {
|
|
664
|
-
A(
|
|
665
|
-
if (capWidth != null) A("max-width:", capWidth);
|
|
666
|
-
});
|
|
622
|
+
A(capWidth);
|
|
667
623
|
// The sidebar, in its own scope so a changing item list redraws just
|
|
668
624
|
// it — never the content area beside it (see `nav` above).
|
|
669
625
|
A(() => {
|
|
@@ -686,9 +642,7 @@ export function main<R extends RouteTable<R>>(opts: MainOptions<R> = {}): PanelS
|
|
|
686
642
|
if (opts.footer != null) {
|
|
687
643
|
A("footer", () => {
|
|
688
644
|
A("div.s-bar", () => {
|
|
689
|
-
A(
|
|
690
|
-
if (capWidth != null) A("max-width:", capWidth);
|
|
691
|
-
});
|
|
645
|
+
A(capWidth);
|
|
692
646
|
drawSlot(opts.footer);
|
|
693
647
|
});
|
|
694
648
|
});
|
package/src/components/menu.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import A from "aberdeen";
|
|
2
2
|
import { matchCurrent, current as currentRoute, go } from "aberdeen/route";
|
|
3
3
|
import { type Slot, type Attributes, drawSlot, mountPortal, focusFirst } from "../core.js";
|
|
4
|
-
import { menu as menuIcon, chevronRight } from "../icons.js";
|
|
4
|
+
import { menu as menuIcon, chevronRight, externalLink as newTabIcon, link as linkIcon } from "../icons.js";
|
|
5
5
|
import { button, type ButtonOptions } from "./button.js";
|
|
6
|
+
import { toast } from "./toast.js";
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* A clickable item in a menu or sidebar nav.
|
|
@@ -130,6 +131,14 @@ export interface FloatingMenuOptions {
|
|
|
130
131
|
* context menu — whose anchor has no click handler — wants the click to close.
|
|
131
132
|
*/
|
|
132
133
|
closeOnAnchorClick?: boolean;
|
|
134
|
+
/**
|
|
135
|
+
* The link this menu stands on, as a path or URL. A menu that takes over a
|
|
136
|
+
* link's right-click takes the browser's own link menu away, so it owes the
|
|
137
|
+
* two entries anyone actually reaches for there: with this set, **Open in
|
|
138
|
+
* new tab** and **Copy link** are prepended above a separator, where that
|
|
139
|
+
* menu would have had them. The shell's breadcrumbs use it.
|
|
140
|
+
*/
|
|
141
|
+
link?: string;
|
|
133
142
|
/** Aberdeen attr/style string on the floating panel. */
|
|
134
143
|
dropdownAttrs?: Attributes;
|
|
135
144
|
}
|
|
@@ -152,7 +161,11 @@ A.insertGlobalCss({
|
|
|
152
161
|
".s-menu-list":
|
|
153
162
|
"position:fixed z-index:350 min-width:10rem display:flex flex-direction:column p:$1 " +
|
|
154
163
|
"r:$s-radius-lg " +
|
|
155
|
-
|
|
164
|
+
// The 16px off max-width is the 8px gap `positionMenu` leaves at either
|
|
165
|
+
// window edge: in a window too narrow for the menu, it narrows rather
|
|
166
|
+
// than hanging off the screen. (A `min-width` from the caller still wins,
|
|
167
|
+
// as CSS says it must — that ask is the app's to keep sensible.)
|
|
168
|
+
"max-width: calc(100vw - 16px); overflow-y:auto max-height:min(80vh,28rem) " +
|
|
156
169
|
"transition: opacity 0.15s, transform 0.15s, visibility 0.15s;",
|
|
157
170
|
".s-menu-list.hidden": "opacity:0 pointer-events:none transform:translateY(-6px) visibility:hidden",
|
|
158
171
|
// One class for both the `<a>` (link) and `<button>` forms — they look
|
|
@@ -530,7 +543,7 @@ export function closeFloatingMenu(anchor?: HTMLElement): void {
|
|
|
530
543
|
|
|
531
544
|
function positionMenu(menuEl: HTMLElement, rect: { left: number; right: number; top: number; bottom: number }): void {
|
|
532
545
|
const mw = menuEl.offsetWidth, mh = menuEl.offsetHeight;
|
|
533
|
-
const vw = window.innerWidth,
|
|
546
|
+
const vw = window.innerWidth, vh = window.innerHeight;
|
|
534
547
|
const gap = 4;
|
|
535
548
|
let x = rect.left;
|
|
536
549
|
if (x + mw > vw - 8) x = Math.max(8, rect.right - mw);
|
|
@@ -540,12 +553,42 @@ function positionMenu(menuEl: HTMLElement, rect: { left: number; right: number;
|
|
|
540
553
|
menuEl.style.top = Math.max(8, y) + "px";
|
|
541
554
|
}
|
|
542
555
|
|
|
556
|
+
/**
|
|
557
|
+
* The standard entries for the link a menu stands on (see
|
|
558
|
+
* {@link FloatingMenuOptions.link}). "Open in new tab" is a real new tab, so
|
|
559
|
+
* the target arrives cold, exactly as the link middle-clicked would.
|
|
560
|
+
*/
|
|
561
|
+
function linkItems(href: string): MenuEntry[] {
|
|
562
|
+
return [
|
|
563
|
+
{ label: "Open in new tab", icon: newTabIcon, click: () => { window.open(href, "_blank", "noopener"); } },
|
|
564
|
+
{ label: "Copy link", icon: linkIcon, click: () => void copyLink(href) },
|
|
565
|
+
];
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* Put the link's address on the clipboard, as the absolute URL someone can
|
|
570
|
+
* paste anywhere — what the browser's own "Copy link" would have given them.
|
|
571
|
+
* Confirmed with a toast, since a silent copy leaves you wondering; `writeText`
|
|
572
|
+
* needs a secure context, so a failure says so rather than lying.
|
|
573
|
+
*/
|
|
574
|
+
async function copyLink(href: string): Promise<void> {
|
|
575
|
+
const url = new URL(href, location.href).href;
|
|
576
|
+
try {
|
|
577
|
+
await navigator.clipboard.writeText(url);
|
|
578
|
+
toast({ message: "Link copied." });
|
|
579
|
+
} catch {
|
|
580
|
+
toast({ message: "Couldn't copy the link.", type: "danger" });
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
|
|
543
584
|
mountPortal(() => {
|
|
544
585
|
const f = $floating.opts;
|
|
545
586
|
if (!f) return;
|
|
546
587
|
|
|
547
588
|
const menuEl = A("div.s-menu-list.s-s.neutral.shadow create=hidden destroy=hidden", f.dropdownAttrs, () => {
|
|
548
|
-
drawMenu
|
|
589
|
+
// One drawMenu call, not one per section: it owns the container's roving
|
|
590
|
+
// keyboard focus, and two of them would move it twice per keypress.
|
|
591
|
+
drawMenu(f.link != null ? [...linkItems(f.link), { separator: true }, ...f.items] : f.items, closeFloating);
|
|
549
592
|
}) as HTMLElement;
|
|
550
593
|
|
|
551
594
|
// Capture-phase document handlers replace an invisible backdrop element:
|
|
@@ -672,13 +715,13 @@ export function addContextMenu(opts: ContextMenuOptions): void {
|
|
|
672
715
|
e.preventDefault();
|
|
673
716
|
myEl = e.currentTarget as HTMLElement;
|
|
674
717
|
// Anchor at the exact click/tap point, and close on a plain click of the
|
|
675
|
-
// element (it has no toggle handler of its own).
|
|
718
|
+
// element (it has no toggle handler of its own). The rest of the options
|
|
719
|
+
// pass through whole, so a shared option can't be dropped on the way.
|
|
676
720
|
showFloatingMenu({
|
|
677
|
-
|
|
721
|
+
...opts,
|
|
678
722
|
anchor: myEl,
|
|
679
723
|
at: { x: e.clientX, y: e.clientY },
|
|
680
724
|
closeOnAnchorClick: true,
|
|
681
|
-
dropdownAttrs: opts.dropdownAttrs,
|
|
682
725
|
});
|
|
683
726
|
});
|
|
684
727
|
}
|