@zazz-ui/core 0.4.1 → 0.5.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/AUTHORING.md +158 -0
- package/CHANGELOG.md +331 -0
- package/README.md +55 -23
- package/dist/scroll-state-C8mA5gq6.js +1 -0
- package/dist/sri.json +124 -72
- package/dist/zazz.css +1 -1
- package/dist/zazz.js +1 -1
- package/editor/zazz.css-data.json +116794 -0
- package/editor/zazz.html-data.json +1683 -0
- package/editor/zazz.language-data.json +5001 -0
- package/editor/zazz.lint-data.json +152 -0
- package/package.json +12 -5
- package/src/base/_breakpoints.css +12 -0
- package/src/base/_layers.css +8 -9
- package/src/base/_properties-2xl.css +171 -0
- package/src/base/_properties-active.css +107 -0
- package/src/base/_properties-checked.css +107 -0
- package/src/base/_properties-current.css +107 -0
- package/src/base/_properties-disabled.css +107 -0
- package/src/base/_properties-focus-visible.css +107 -0
- package/src/base/_properties-focus-within.css +107 -0
- package/src/base/_properties-hover.css +107 -0
- package/src/base/_properties-lg.css +171 -0
- package/src/base/_properties-md.css +171 -0
- package/src/base/_properties-open.css +107 -0
- package/src/base/_properties-sm.css +171 -0
- package/src/base/_properties-starting.css +55 -0
- package/src/base/_properties-stuck.css +55 -0
- package/src/base/_properties-xl.css +171 -0
- package/src/base/_properties.css +555 -0
- package/src/base/_reset.css +67 -75
- package/src/base/_switches.css +145 -0
- package/src/base/_typography.css +437 -370
- package/src/base/_utilities-box.css +142 -0
- package/src/base/_utilities-color.css +290 -0
- package/src/base/_utilities-effects.css +235 -0
- package/src/base/_utilities-flow.css +312 -0
- package/src/base/_utilities-grid.css +143 -0
- package/src/base/_utilities-margin.css +376 -0
- package/src/base/_utilities-pseudo.css +426 -0
- package/src/base/_utilities-sizing.css +259 -0
- package/src/base/_utilities-spacing.css +190 -0
- package/src/base/_utilities-tier-2xl.css +93 -0
- package/src/base/_utilities-tier-active.css +61 -0
- package/src/base/_utilities-tier-checked.css +61 -0
- package/src/base/_utilities-tier-current.css +61 -0
- package/src/base/_utilities-tier-disabled.css +61 -0
- package/src/base/_utilities-tier-focus-visible.css +61 -0
- package/src/base/_utilities-tier-focus-within.css +61 -0
- package/src/base/_utilities-tier-hover.css +63 -0
- package/src/base/_utilities-tier-lg.css +93 -0
- package/src/base/_utilities-tier-md.css +93 -0
- package/src/base/_utilities-tier-open.css +61 -0
- package/src/base/_utilities-tier-sm.css +93 -0
- package/src/base/_utilities-tier-starting.css +35 -0
- package/src/base/_utilities-tier-stuck.css +114 -0
- package/src/base/_utilities-tier-xl.css +93 -0
- package/src/base/_utilities-typography.css +141 -0
- package/src/base/_variables.css +323 -372
- package/src/base/_view-transitions.css +10 -12
- package/src/base/command-score.d.ts.map +1 -1
- package/src/base/command-score.js +1 -2
- package/src/base/command-score.ts +1 -2
- package/src/base/dialog-lifecycle.js +1 -1
- package/src/base/dialog-lifecycle.ts +1 -1
- package/src/base/embla.d.ts +6 -7
- package/src/base/embla.d.ts.map +1 -1
- package/src/base/embla.js +89 -55
- package/src/base/embla.ts +89 -55
- package/src/base/hotkeys.d.ts +0 -5
- package/src/base/hotkeys.d.ts.map +1 -1
- package/src/base/hotkeys.ts +0 -5
- package/src/base/navigation.d.ts +12 -1
- package/src/base/navigation.d.ts.map +1 -1
- package/src/base/navigation.js +229 -86
- package/src/base/navigation.ts +224 -92
- package/src/base/reveal.js +14 -14
- package/src/base/reveal.ts +14 -14
- package/src/base/scroll-state.d.ts +21 -0
- package/src/base/scroll-state.d.ts.map +1 -0
- package/src/base/scroll-state.js +177 -0
- package/src/base/scroll-state.ts +188 -0
- package/src/base/signals.d.ts +1 -2
- package/src/base/signals.d.ts.map +1 -1
- package/src/base/signals.js +4 -6
- package/src/base/signals.ts +4 -6
- package/src/base/style-format.d.ts +33 -0
- package/src/base/style-format.d.ts.map +1 -0
- package/src/base/style-format.js +160 -0
- package/src/base/style-format.ts +180 -0
- package/src/base/typeahead.d.ts +19 -4
- package/src/base/typeahead.d.ts.map +1 -1
- package/src/base/typeahead.js +68 -21
- package/src/base/typeahead.ts +73 -23
- package/src/base/utilities.d.ts +188 -0
- package/src/base/utilities.d.ts.map +1 -0
- package/src/base/utilities.js +750 -0
- package/src/base/utilities.ts +865 -0
- package/src/base/utils.d.ts +3 -7
- package/src/base/utils.d.ts.map +1 -1
- package/src/base/utils.js +4 -8
- package/src/base/utils.ts +4 -8
- package/src/base/zazz-element.d.ts +14 -8
- package/src/base/zazz-element.d.ts.map +1 -1
- package/src/base/zazz-element.js +14 -8
- package/src/base/zazz-element.ts +15 -8
- package/src/globals.d.ts +1 -1
- package/src/head.d.ts +50 -4
- package/src/head.d.ts.map +1 -1
- package/src/head.js +119 -38
- package/src/head.ts +172 -41
- package/src/index.css +57 -14
- package/src/index.js +5 -0
- package/src/index.ts +6 -0
- package/src/manifest.d.ts +36 -5
- package/src/manifest.d.ts.map +1 -1
- package/src/manifest.js +150 -6
- package/src/manifest.ts +167 -9
- package/src/primitives/accordion/accordion.css +18 -17
- package/src/primitives/accordion/accordion.html +42 -9
- package/src/primitives/alert-dialog/alert-dialog.css +13 -13
- package/src/primitives/alert-dialog/alert-dialog.html +26 -12
- package/src/primitives/autocomplete/autocomplete-groups.html +64 -33
- package/src/primitives/autocomplete/autocomplete.css +43 -39
- package/src/primitives/autocomplete/autocomplete.d.ts +6 -5
- package/src/primitives/autocomplete/autocomplete.d.ts.map +1 -1
- package/src/primitives/autocomplete/autocomplete.html +38 -26
- package/src/primitives/autocomplete/autocomplete.js +6 -5
- package/src/primitives/autocomplete/autocomplete.ts +6 -5
- package/src/primitives/avatar/avatar.html +110 -15
- package/src/primitives/badge/badge-icon.html +22 -8
- package/src/primitives/badge/badge.css +401 -128
- package/src/primitives/badge/badge.html +12 -6
- package/src/primitives/breadcrumbs/breadcrumbs.html +25 -5
- package/src/primitives/button/button-icon-only.html +17 -11
- package/src/primitives/button/button-icon.html +9 -3
- package/src/primitives/button/button.css +408 -164
- package/src/primitives/button/button.html +14 -7
- package/src/primitives/button-group/button-group-vertical.html +5 -5
- package/src/primitives/button-group/button-group.css +53 -25
- package/src/primitives/button-group/button-group.html +18 -12
- package/src/primitives/callout/callout.css +114 -0
- package/src/primitives/callout/callout.html +188 -0
- package/src/primitives/card/card-subgrid.html +249 -34
- package/src/primitives/card/card.css +125 -0
- package/src/primitives/card/card.html +141 -20
- package/src/primitives/carousel/carousel.css +30 -20
- package/src/primitives/carousel/carousel.d.ts +7 -10
- package/src/primitives/carousel/carousel.d.ts.map +1 -1
- package/src/primitives/carousel/carousel.html +420 -84
- package/src/primitives/carousel/carousel.js +1 -1
- package/src/primitives/carousel/carousel.ts +8 -11
- package/src/primitives/checkbox/checkbox-tasklist.html +6 -6
- package/src/primitives/checkbox/checkbox.css +98 -40
- package/src/primitives/checkbox/checkbox.d.ts.map +1 -1
- package/src/primitives/checkbox/checkbox.html +13 -8
- package/src/primitives/checkbox/checkbox.js +7 -9
- package/src/primitives/checkbox/checkbox.ts +7 -15
- package/src/primitives/combobox/combobox-multiselect.html +53 -33
- package/src/primitives/combobox/combobox.css +145 -122
- package/src/primitives/combobox/combobox.d.ts +12 -11
- package/src/primitives/combobox/combobox.d.ts.map +1 -1
- package/src/primitives/combobox/combobox.html +48 -28
- package/src/primitives/combobox/combobox.js +46 -51
- package/src/primitives/combobox/combobox.ts +48 -55
- package/src/primitives/command/command-actions.d.ts +2 -3
- package/src/primitives/command/command-actions.d.ts.map +1 -1
- package/src/primitives/command/command-actions.html +48 -33
- package/src/primitives/command/command-actions.js +12 -10
- package/src/primitives/command/command-actions.ts +12 -13
- package/src/primitives/command/command-dialog.html +49 -25
- package/src/primitives/command/command.css +52 -42
- package/src/primitives/command/command.d.ts +10 -7
- package/src/primitives/command/command.d.ts.map +1 -1
- package/src/primitives/command/command.html +95 -46
- package/src/primitives/command/command.js +30 -13
- package/src/primitives/command/command.ts +30 -12
- package/src/primitives/debug/audit-core.d.ts +64 -0
- package/src/primitives/debug/audit-core.d.ts.map +1 -0
- package/src/primitives/debug/audit-core.js +356 -0
- package/src/primitives/debug/audit-core.ts +535 -0
- package/src/primitives/debug/debug.d.ts +50 -0
- package/src/primitives/debug/debug.d.ts.map +1 -0
- package/src/primitives/debug/debug.js +168 -0
- package/src/primitives/debug/debug.ts +211 -0
- package/src/primitives/dialog/dialog-with-form.html +67 -45
- package/src/primitives/dialog/dialog.css +87 -61
- package/src/primitives/dialog/dialog.html +25 -17
- package/src/primitives/fields/fields.css +108 -134
- package/src/primitives/fields/fields.html +39 -0
- package/src/primitives/input/input-icon-leading.html +3 -3
- package/src/primitives/input/input-icon-trailing.html +3 -3
- package/src/primitives/input/input.css +87 -49
- package/src/primitives/input/input.html +15 -15
- package/src/primitives/input-group/input-group-password-group.html +15 -15
- package/src/primitives/input-group/input-group.css +118 -75
- package/src/primitives/input-group/input-group.html +42 -30
- package/src/primitives/kbd/kbd-group.html +15 -2
- package/src/primitives/kbd/kbd.css +44 -31
- package/src/primitives/kbd/kbd.html +7 -1
- package/src/primitives/layout/layout.css +159 -0
- package/src/primitives/layout/layout.html +35 -0
- package/src/primitives/lightbox/lightbox.css +178 -105
- package/src/primitives/lightbox/lightbox.d.ts.map +1 -1
- package/src/primitives/lightbox/lightbox.html +138 -166
- package/src/primitives/lightbox/lightbox.js +10 -13
- package/src/primitives/lightbox/lightbox.ts +12 -13
- package/src/primitives/menu/menu-interest.html +60 -18
- package/src/primitives/menu/menu.css +22 -17
- package/src/primitives/menu/menu.d.ts +5 -5
- package/src/primitives/menu/menu.html +195 -40
- package/src/primitives/menu/menu.js +8 -8
- package/src/primitives/menu/menu.ts +8 -8
- package/src/primitives/menubar/menubar-help-search.html +78 -32
- package/src/primitives/menubar/menubar.html +166 -38
- package/src/primitives/meter/meter.css +41 -38
- package/src/primitives/meter/meter.html +44 -9
- package/src/primitives/mobile-menu/mobile-menu.css +47 -40
- package/src/primitives/mobile-menu/mobile-menu.html +1460 -179
- package/src/primitives/navigation-menu/navigation-menu-featured.html +155 -33
- package/src/primitives/navigation-menu/navigation-menu-icon-grid.html +222 -47
- package/src/primitives/navigation-menu/navigation-menu-interest.html +130 -38
- package/src/primitives/navigation-menu/navigation-menu-megamenu.html +402 -72
- package/src/primitives/navigation-menu/navigation-menu-simple.html +90 -40
- package/src/primitives/navigation-menu/navigation-menu.css +104 -106
- package/src/primitives/navigation-menu/navigation-menu.html +554 -158
- package/src/primitives/otp/otp.css +43 -44
- package/src/primitives/otp/otp.d.ts +4 -5
- package/src/primitives/otp/otp.d.ts.map +1 -1
- package/src/primitives/otp/otp.html +7 -6
- package/src/primitives/otp/otp.js +13 -18
- package/src/primitives/otp/otp.ts +12 -19
- package/src/primitives/password-group/password-group.css +132 -107
- package/src/primitives/password-group/password-group.d.ts.map +1 -1
- package/src/primitives/password-group/password-group.html +23 -16
- package/src/primitives/password-group/password-group.js +11 -12
- package/src/primitives/password-group/password-group.ts +11 -12
- package/src/primitives/popover/popover.css +88 -95
- package/src/primitives/popover/popover.html +18 -0
- package/src/primitives/progress/progress-indeterminate.html +1 -1
- package/src/primitives/progress/progress.css +34 -35
- package/src/primitives/progress/progress.html +18 -4
- package/src/primitives/prose/prose.html +19 -12
- package/src/primitives/radio/radio.css +83 -46
- package/src/primitives/radio/radio.html +16 -11
- package/src/primitives/reveal/reveal.css +37 -46
- package/src/primitives/reveal/reveal.html +15 -0
- package/src/primitives/scroll-fade/scroll-fade.css +115 -0
- package/src/primitives/scroll-fade/scroll-fade.html +69 -0
- package/src/primitives/select/multiselect.d.ts +8 -10
- package/src/primitives/select/multiselect.d.ts.map +1 -1
- package/src/primitives/select/multiselect.js +23 -27
- package/src/primitives/select/multiselect.ts +23 -29
- package/src/primitives/select/select-align.html +79 -16
- package/src/primitives/select/select-multiple.html +8 -4
- package/src/primitives/select/select-sides.html +104 -21
- package/src/primitives/select/select.css +297 -186
- package/src/primitives/select/select.html +24 -5
- package/src/primitives/separator/separator.css +15 -18
- package/src/primitives/separator/separator.html +37 -10
- package/src/primitives/slider/slider.css +48 -38
- package/src/primitives/slider/slider.html +9 -1
- package/src/primitives/style-guard/style-guard.d.ts +19 -0
- package/src/primitives/style-guard/style-guard.d.ts.map +1 -0
- package/src/primitives/style-guard/style-guard.js +125 -0
- package/src/primitives/style-guard/style-guard.ts +153 -0
- package/src/primitives/switch/switch.css +42 -33
- package/src/primitives/switch/switch.html +11 -6
- package/src/primitives/table/table-alternating.html +1 -1
- package/src/primitives/table/table-grid.html +5 -5
- package/src/primitives/table/table-size-sm.html +1 -1
- package/src/primitives/table/table.css +64 -61
- package/src/primitives/table/table.html +7 -7
- package/src/primitives/tabs/tabs.css +167 -166
- package/src/primitives/tabs/tabs.d.ts +10 -11
- package/src/primitives/tabs/tabs.d.ts.map +1 -1
- package/src/primitives/tabs/tabs.html +192 -36
- package/src/primitives/tabs/tabs.js +14 -15
- package/src/primitives/tabs/tabs.ts +14 -15
- package/src/primitives/textarea/textarea.css +69 -44
- package/src/primitives/textarea/textarea.html +3 -3
- package/src/primitives/toaster/toaster.css +90 -92
- package/src/primitives/toaster/toaster.d.ts +0 -6
- package/src/primitives/toaster/toaster.d.ts.map +1 -1
- package/src/primitives/toaster/toaster.html +22 -18
- package/src/primitives/toaster/toaster.js +64 -72
- package/src/primitives/toaster/toaster.ts +71 -87
- package/src/primitives/toggle/toggle.css +368 -153
- package/src/primitives/toggle/toggle.html +27 -9
- package/src/primitives/toggle-group/toggle-group-multiple.html +3 -3
- package/src/primitives/toggle-group/toggle-group-vertical.html +4 -4
- package/src/primitives/toggle-group/toggle-group.css +71 -23
- package/src/primitives/toggle-group/toggle-group.html +3 -3
- package/src/primitives/toolbar/toolbar.html +35 -13
- package/src/primitives/tooltip/tooltip-disabled.html +3 -3
- package/src/primitives/tooltip/tooltip-sides.html +36 -14
- package/src/primitives/tooltip/tooltip-with-kbd.html +5 -5
- package/src/primitives/tooltip/tooltip.css +111 -80
- package/src/primitives/tooltip/tooltip.html +3 -3
- package/src/primitives/utilities/aspect-ratio.html +51 -7
- package/src/primitives/utilities/basis.html +64 -11
- package/src/primitives/utilities/col-span.html +65 -9
- package/src/primitives/utilities/compose.html +48 -10
- package/src/primitives/utilities/flex-align.html +89 -19
- package/src/primitives/utilities/flex-direction.html +111 -13
- package/src/primitives/utilities/flex-grow.html +41 -7
- package/src/primitives/utilities/gap.html +158 -19
- package/src/primitives/utilities/grid-cols.html +75 -7
- package/src/primitives/utilities/grid-responsive.html +111 -12
- package/src/primitives/utilities/margin-auto.html +38 -7
- package/src/primitives/utilities/object-fit.html +75 -13
- package/src/primitives/utilities/order.html +40 -4
- package/src/primitives/utilities/overflow.html +74 -20
- package/src/primitives/utilities/padding-compose.html +67 -10
- package/src/primitives/utilities/padding.html +84 -13
- package/src/primitives/utilities/pile.html +29 -0
- package/src/primitives/utilities/position.html +65 -8
- package/src/primitives/utilities/responsive-flex.html +54 -7
- package/src/primitives/utilities/responsive-grid.html +115 -12
- package/src/primitives/utilities/size.html +120 -16
- package/src/primitives/utilities/width.html +123 -16
- package/examples/about.html +0 -583
- package/examples/components.html +0 -1028
- package/examples/forms.html +0 -504
- package/examples/index.html +0 -261
- package/examples/layout.html +0 -313
- package/examples/products.html +0 -839
- package/examples/responsive.html +0 -332
- package/src/base/_layout.css +0 -571
- package/src/base/_utilities.css +0 -6758
- package/src/primitives/utilities/grid-pile.html +0 -5
package/src/base/navigation.js
CHANGED
|
@@ -1,45 +1,212 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
import { refreshAll } from "./zazz-element.js";
|
|
3
3
|
/**
|
|
4
|
-
* @fileoverview
|
|
5
|
-
* @description
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
4
|
+
* @fileoverview Opt-in in-page navigation via the Navigation API.
|
|
5
|
+
* @description With `<html data-ui-navigation="swap">` on both the current and
|
|
6
|
+
* the destination page, a same-origin navigation fetches the destination and
|
|
7
|
+
* swaps the whole `<body>` in place instead of loading a new document. The
|
|
8
|
+
* browser still owns the URL and history; one `navigate` listener covers link
|
|
9
|
+
* clicks, back/forward, and programmatic navigations.
|
|
9
10
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
11
|
+
* Nothing persists implicitly. An element survives a swap only when it carries
|
|
12
|
+
* `data-ui-persist="<id>"` and the destination has an element with the same id:
|
|
13
|
+
* the live element takes the new one's place, keeping its DOM and state (a toast
|
|
14
|
+
* stack, a playing video, an open mini-cart). Everything else comes from the new
|
|
15
|
+
* page, so headers, footers and current-page links are never stale. A carried
|
|
16
|
+
* element keeps its scroll offsets, and its links take `aria-current` from the
|
|
17
|
+
* destination's copy, so a kept header still marks the current page.
|
|
16
18
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
19
|
+
* An element whose content depends on the page (a section sidebar, a table of
|
|
20
|
+
* contents) should not persist; `data-ui-persist-scroll="<id>"` renders it from
|
|
21
|
+
* the destination and carries only its scroll offset. The window itself
|
|
22
|
+
* scrolls as the browser decides: to the top or the target for a new entry,
|
|
23
|
+
* restored on back/forward.
|
|
24
|
+
*
|
|
25
|
+
* Without the opt-in the browser navigates normally, animated by the CSS
|
|
26
|
+
* cross-document view transitions in `_view-transitions.css`. Reloads, hash
|
|
27
|
+
* changes, downloads and form posts are never intercepted.
|
|
23
28
|
*
|
|
24
29
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/Navigation_API
|
|
25
30
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/NavigateEvent/scroll
|
|
26
31
|
*/
|
|
32
|
+
/** The opt-in, on `<html>` of both pages. */
|
|
33
|
+
const SWAP = "swap";
|
|
34
|
+
/** True when `doc` opted in to in-page navigation. */
|
|
35
|
+
function optedIn(doc) {
|
|
36
|
+
return doc.documentElement.getAttribute("data-ui-navigation") === SWAP;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* @description Records the scroll offsets of `root` and its scrolled
|
|
40
|
+
* descendants. A plain move (no `moveBefore()`) resets them to zero.
|
|
41
|
+
*
|
|
42
|
+
* @param root - A persisted element, still connected.
|
|
43
|
+
* @returns Each scrolled element with its `scrollTop` and `scrollLeft`.
|
|
44
|
+
* @private
|
|
45
|
+
*/
|
|
46
|
+
function scrollOffsets(root) {
|
|
47
|
+
const offsets = [];
|
|
48
|
+
for (const el of [root, ...root.querySelectorAll("*")]) {
|
|
49
|
+
if (el.scrollTop || el.scrollLeft)
|
|
50
|
+
offsets.push([el, el.scrollTop, el.scrollLeft]);
|
|
51
|
+
}
|
|
52
|
+
return offsets;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* @description Gives the links in a persisted element the `aria-current` of
|
|
56
|
+
* their twins in the destination's copy, so a kept header or sidebar marks the
|
|
57
|
+
* new page. Links pair by `href`, in document order; a link with no twin is not
|
|
58
|
+
* current.
|
|
59
|
+
*
|
|
60
|
+
* @param keep - The live persisted element.
|
|
61
|
+
* @param slot - The destination's element with the same id.
|
|
62
|
+
* @private
|
|
63
|
+
*/
|
|
64
|
+
function adoptCurrent(keep, slot) {
|
|
65
|
+
const twins = new Map();
|
|
66
|
+
for (const link of slot.querySelectorAll("a[href], area[href]")) {
|
|
67
|
+
const href = link.getAttribute("href");
|
|
68
|
+
twins.set(href, [...(twins.get(href) ?? []), link]);
|
|
69
|
+
}
|
|
70
|
+
for (const link of keep.querySelectorAll("a[href], area[href]")) {
|
|
71
|
+
const twin = twins.get(link.getAttribute("href"))?.shift();
|
|
72
|
+
const current = twin?.getAttribute("aria-current");
|
|
73
|
+
if (current == null)
|
|
74
|
+
link.removeAttribute("aria-current");
|
|
75
|
+
else
|
|
76
|
+
link.setAttribute("aria-current", current);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* @description Replaces the current body with `next`, carrying over every
|
|
81
|
+
* `data-ui-persist` element whose id appears in both, scroll offsets included;
|
|
82
|
+
* links in a carried element take `aria-current` from the destination. A
|
|
83
|
+
* `data-ui-persist-scroll` element comes from `next` and takes the scroll
|
|
84
|
+
* offset of the current element with the same id. Returns the new body.
|
|
85
|
+
*
|
|
86
|
+
* @param next - The destination's parsed `<body>`.
|
|
87
|
+
* @private
|
|
88
|
+
*/
|
|
89
|
+
function swapBody(next) {
|
|
90
|
+
const current = document.body;
|
|
91
|
+
// Read every scroll offset now, while the layout holds one body: a read once
|
|
92
|
+
// both bodies are connected would lay out the doubled document.
|
|
93
|
+
const live = new Map();
|
|
94
|
+
const offsets = [];
|
|
95
|
+
for (const el of current.querySelectorAll("[data-ui-persist]")) {
|
|
96
|
+
const id = el.getAttribute("data-ui-persist");
|
|
97
|
+
if (id && !live.has(id))
|
|
98
|
+
live.set(id, el);
|
|
99
|
+
}
|
|
100
|
+
for (const keep of live.values())
|
|
101
|
+
offsets.push(...scrollOffsets(keep));
|
|
102
|
+
const scrolled = new Map();
|
|
103
|
+
for (const el of current.querySelectorAll("[data-ui-persist-scroll]")) {
|
|
104
|
+
const id = el.getAttribute("data-ui-persist-scroll");
|
|
105
|
+
if (id && !scrolled.has(id))
|
|
106
|
+
scrolled.set(id, [el.scrollTop, el.scrollLeft]);
|
|
107
|
+
}
|
|
108
|
+
const body = document.importNode(next, true);
|
|
109
|
+
const placeholders = [];
|
|
110
|
+
for (const slot of body.querySelectorAll("[data-ui-persist]")) {
|
|
111
|
+
const keep = live.get(slot.getAttribute("data-ui-persist") ?? "");
|
|
112
|
+
if (keep)
|
|
113
|
+
placeholders.push([slot, keep]);
|
|
114
|
+
}
|
|
115
|
+
for (const el of body.querySelectorAll("[data-ui-persist-scroll]")) {
|
|
116
|
+
const id = el.getAttribute("data-ui-persist-scroll") ?? "";
|
|
117
|
+
const offset = scrolled.get(id);
|
|
118
|
+
scrolled.delete(id);
|
|
119
|
+
if (offset)
|
|
120
|
+
offsets.push([el, ...offset]);
|
|
121
|
+
}
|
|
122
|
+
// Connect the new body beside the old one, move each live element into its
|
|
123
|
+
// slot while both are connected (moveBefore requires that, and then keeps the
|
|
124
|
+
// element's state: no disconnect, no iframe or media reset), then drop the old
|
|
125
|
+
// body. `document.body` is the first body, so it is the new one throughout.
|
|
126
|
+
// Scroll offsets are restored in the same task, before the view transition
|
|
127
|
+
// captures the new state, so a plain move never shows a jump.
|
|
128
|
+
current.before(body);
|
|
129
|
+
for (const [slot, keep] of placeholders) {
|
|
130
|
+
const parent = slot.parentNode;
|
|
131
|
+
if (!parent)
|
|
132
|
+
continue;
|
|
133
|
+
adoptCurrent(keep, slot);
|
|
134
|
+
if (typeof parent.moveBefore === "function")
|
|
135
|
+
parent.moveBefore(keep, slot);
|
|
136
|
+
else
|
|
137
|
+
parent.insertBefore(keep, slot);
|
|
138
|
+
slot.remove();
|
|
139
|
+
}
|
|
140
|
+
current.remove();
|
|
141
|
+
for (const [el, top, left] of offsets)
|
|
142
|
+
el.scrollTo({ top, left, behavior: "instant" });
|
|
143
|
+
return body;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* @description Moves focus to the new page's primary heading so screen readers
|
|
147
|
+
* announce the navigation.
|
|
148
|
+
*
|
|
149
|
+
* @param body - The swapped-in body.
|
|
150
|
+
* @private
|
|
151
|
+
*/
|
|
152
|
+
function routeFocus(body) {
|
|
153
|
+
const main = body.querySelector("main");
|
|
154
|
+
const target = main?.querySelector("h1") ?? main ?? body;
|
|
155
|
+
if (!(target instanceof HTMLElement))
|
|
156
|
+
return;
|
|
157
|
+
if (!target.hasAttribute("tabindex"))
|
|
158
|
+
target.tabIndex = -1;
|
|
159
|
+
target.focus({ preventScroll: true });
|
|
160
|
+
}
|
|
161
|
+
/** Navigations holding the instant-scroll override, and the value it replaced. */
|
|
162
|
+
let instantHolds = 0;
|
|
163
|
+
let savedScrollBehavior = "";
|
|
164
|
+
/**
|
|
165
|
+
* @description Makes window scrolls instant until the returned release runs.
|
|
166
|
+
* Overlapping navigations share one override: the first saves `<html>`'s
|
|
167
|
+
* inline `scroll-behavior`, the last to release restores it.
|
|
168
|
+
*
|
|
169
|
+
* @returns The release; calling it again does nothing.
|
|
170
|
+
* @private
|
|
171
|
+
*/
|
|
172
|
+
function holdInstantScroll() {
|
|
173
|
+
const root = document.documentElement.style;
|
|
174
|
+
if (instantHolds++ === 0) {
|
|
175
|
+
savedScrollBehavior = root.scrollBehavior;
|
|
176
|
+
root.scrollBehavior = "auto";
|
|
177
|
+
}
|
|
178
|
+
let held = true;
|
|
179
|
+
return () => {
|
|
180
|
+
if (!held)
|
|
181
|
+
return;
|
|
182
|
+
held = false;
|
|
183
|
+
if (--instantHolds === 0)
|
|
184
|
+
root.scrollBehavior = savedScrollBehavior;
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
function isSameOrigin(url) {
|
|
188
|
+
return new URL(url, location.href).origin === location.origin;
|
|
189
|
+
}
|
|
27
190
|
if ("navigation" in window) {
|
|
28
191
|
/**
|
|
29
|
-
* URL
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
* inside the handler would re-enter this same listener, re-intercept, fail to
|
|
33
|
-
* find a usable `<main>` again, and loop forever. Cleared as soon as it's seen.
|
|
192
|
+
* URL handed back to the browser for a full load. The `navigate` listener
|
|
193
|
+
* skips it once, so `location.assign()` from inside the handler does not
|
|
194
|
+
* re-enter the interception and loop.
|
|
34
195
|
*/
|
|
35
196
|
let bypassUrl = null;
|
|
197
|
+
function fullLoad(url) {
|
|
198
|
+
bypassUrl = url;
|
|
199
|
+
location.assign(url);
|
|
200
|
+
}
|
|
36
201
|
window.navigation.addEventListener("navigate", (e) => {
|
|
37
|
-
// A navigation we already chose to full-load: let the browser handle it.
|
|
38
202
|
if (bypassUrl !== null && e.destination.url === bypassUrl) {
|
|
39
203
|
bypassUrl = null;
|
|
40
204
|
return;
|
|
41
205
|
}
|
|
42
|
-
|
|
206
|
+
// A reload re-renders the whole document; it is never a swap.
|
|
207
|
+
if (!optedIn(document) ||
|
|
208
|
+
!e.canIntercept ||
|
|
209
|
+
e.navigationType === "reload" ||
|
|
43
210
|
e.hashChange ||
|
|
44
211
|
e.downloadRequest !== null ||
|
|
45
212
|
e.formData !== null ||
|
|
@@ -50,87 +217,63 @@ if ("navigation" in window) {
|
|
|
50
217
|
async handler() {
|
|
51
218
|
let doc;
|
|
52
219
|
try {
|
|
53
|
-
const res = await fetch(e.destination.url);
|
|
220
|
+
const res = await fetch(e.destination.url, { signal: e.signal });
|
|
54
221
|
if (!res.ok)
|
|
55
222
|
throw new Error(res.statusText);
|
|
56
223
|
doc = new DOMParser().parseFromString(await res.text(), "text/html");
|
|
57
224
|
}
|
|
58
225
|
catch {
|
|
59
|
-
|
|
226
|
+
// A newer navigation aborted this one; it must not swap in late.
|
|
227
|
+
if (e.signal.aborted)
|
|
228
|
+
return;
|
|
229
|
+
fullLoad(e.destination.url); // network or HTTP failure
|
|
60
230
|
return;
|
|
61
231
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
//
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
// footer chrome — which lives outside <main> — must change too.
|
|
68
|
-
if (!(newMain instanceof HTMLElement) ||
|
|
69
|
-
!(currentMain instanceof HTMLElement) ||
|
|
70
|
-
newMain.dataset.layout !== currentMain.dataset.layout) {
|
|
71
|
-
fullReload(e.destination.url);
|
|
232
|
+
if (e.signal.aborted)
|
|
233
|
+
return;
|
|
234
|
+
// The destination must opt in too; otherwise it gets a normal load.
|
|
235
|
+
if (!optedIn(doc) || !(doc.body instanceof HTMLElement)) {
|
|
236
|
+
fullLoad(e.destination.url);
|
|
72
237
|
return;
|
|
73
238
|
}
|
|
74
|
-
|
|
75
|
-
|
|
239
|
+
let body = document.body;
|
|
240
|
+
const update = () => {
|
|
241
|
+
body = swapBody(doc.body);
|
|
76
242
|
document.title = doc.title;
|
|
77
243
|
// Restore scroll while the new snapshot is captured so the transition
|
|
78
|
-
// animates from the
|
|
244
|
+
// animates from the right position instead of jumping afterwards.
|
|
79
245
|
e.scroll();
|
|
80
246
|
};
|
|
247
|
+
// The jumps are instant: under the reset's `scroll-behavior: smooth` the
|
|
248
|
+
// window would scroll in view of the live new snapshot. The browser may
|
|
249
|
+
// apply the scroll at the next layout, so the override holds until the
|
|
250
|
+
// transition is done (two frames without one).
|
|
251
|
+
const release = holdInstantScroll();
|
|
81
252
|
if (document.startViewTransition) {
|
|
82
|
-
|
|
253
|
+
try {
|
|
254
|
+
await document.startViewTransition(update).finished;
|
|
255
|
+
}
|
|
256
|
+
finally {
|
|
257
|
+
release();
|
|
258
|
+
}
|
|
83
259
|
}
|
|
84
260
|
else {
|
|
85
|
-
|
|
261
|
+
try {
|
|
262
|
+
update();
|
|
263
|
+
}
|
|
264
|
+
finally {
|
|
265
|
+
requestAnimationFrame(() => requestAnimationFrame(release));
|
|
266
|
+
}
|
|
86
267
|
}
|
|
87
|
-
//
|
|
88
|
-
//
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
refreshAll(newMain);
|
|
92
|
-
routeFocus(newMain);
|
|
268
|
+
// Page-scoped modules (reveal, class-form carousels, …) registered
|
|
269
|
+
// refresh hooks; custom elements ride the swap on their own callbacks.
|
|
270
|
+
refreshAll(body);
|
|
271
|
+
routeFocus(body);
|
|
93
272
|
},
|
|
94
273
|
});
|
|
95
274
|
});
|
|
96
|
-
/**
|
|
97
|
-
* @description Hands a navigation back to the browser for a full page load.
|
|
98
|
-
* Records the URL so the re-entrant `navigate` event skips interception
|
|
99
|
-
* instead of looping.
|
|
100
|
-
*
|
|
101
|
-
* @param url - The destination URL to load normally.
|
|
102
|
-
* @private
|
|
103
|
-
*/
|
|
104
|
-
function fullReload(url) {
|
|
105
|
-
bypassUrl = url;
|
|
106
|
-
location.assign(url);
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* @description Routes focus to the primary heading in swapped main content.
|
|
110
|
-
*
|
|
111
|
-
* @param main - The newly inserted `<main>` element.
|
|
112
|
-
* @private
|
|
113
|
-
*/
|
|
114
|
-
function routeFocus(main) {
|
|
115
|
-
const target = main.querySelector("h1") ?? main;
|
|
116
|
-
if (!(target instanceof HTMLElement))
|
|
117
|
-
return;
|
|
118
|
-
if (!target.hasAttribute("tabindex")) {
|
|
119
|
-
target.tabIndex = -1;
|
|
120
|
-
}
|
|
121
|
-
target.focus({ preventScroll: true });
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* @description Checks whether a URL shares the current document origin.
|
|
125
|
-
*
|
|
126
|
-
* @param url - The URL to compare.
|
|
127
|
-
* @returns True when the URL origin matches `location.origin`.
|
|
128
|
-
* @private
|
|
129
|
-
*/
|
|
130
|
-
function isSameOrigin(url) {
|
|
131
|
-
return new URL(url, location.href).origin === location.origin;
|
|
132
|
-
}
|
|
133
275
|
}
|
|
134
|
-
else {
|
|
135
|
-
console.warn("Navigation API not supported
|
|
276
|
+
else if (optedIn(document)) {
|
|
277
|
+
console.warn("Navigation API not supported: data-ui-navigation falls back to full page loads.");
|
|
136
278
|
}
|
|
279
|
+
export { swapBody };
|