@sdxc/ui 2026.10.23 → 2026.10.26
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 +4 -0
- package/dist/components/breadcrumbs.d.ts +17 -3
- package/dist/components/breadcrumbs.js +5 -3
- package/dist/components/group.js +3 -2
- package/dist/components/link-button.d.ts +14 -1
- package/dist/components/link-button.js +173 -169
- package/dist/components/link.d.ts +14 -1
- package/dist/components/link.js +63 -59
- package/dist/components/nav-link.d.ts +14 -1
- package/dist/components/nav-link.js +61 -57
- package/dist/components/navigation-menu.d.ts +12 -0
- package/dist/components/navigation-menu.js +15 -6
- package/dist/components/pagination.d.ts +17 -4
- package/dist/components/pagination.js +14 -7
- package/dist/components/sidebar.d.ts +18 -0
- package/dist/components/sidebar.js +22 -9
- package/dist/mixins/active-descendant-keys.d.ts +33 -3
- package/dist/mixins/active-descendant-keys.js +101 -13
- package/dist/mixins/frame-refresh.d.ts +30 -0
- package/dist/mixins/frame-refresh.js +85 -0
- package/dist/mixins/frame-submit.d.ts +65 -0
- package/dist/mixins/frame-submit.js +104 -0
- package/dist/mixins/index.d.ts +2 -0
- package/dist/mixins/index.js +2 -0
- package/dist/mixins/view-transition.js +5 -5
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/link-prefetch.d.ts +33 -0
- package/dist/utils/link-prefetch.js +31 -0
- package/package.json +5 -5
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { jsx as _jsx, jsxs as _jsxs } from "remix/component/jsx-runtime";
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "remix/component/jsx-runtime";
|
|
2
2
|
import { PanelLeftIcon } from "@sdxc/icons";
|
|
3
3
|
import { visuallyHidden } from "@sdxc/u/a11y";
|
|
4
4
|
import { bg, border, borderEdge, colorMix, fg, outline, outlineStyle } from "@sdxc/u/color";
|
|
@@ -16,6 +16,7 @@ import { fontSize, tabularNums, textAlign, textDecoration, textTransform, tracki
|
|
|
16
16
|
import { attrs } from "remix/component";
|
|
17
17
|
import { durations, easings } from "../animations/tokens.js";
|
|
18
18
|
import { hitArea } from "../styles/hit-area.js";
|
|
19
|
+
import { prefetchAttribute, rendersPrefetchHint } from "../utils/link-prefetch.js";
|
|
19
20
|
import { warnIfNoAccessibleName } from "../utils/warn-if-no-accessible-name.js";
|
|
20
21
|
import { Dialog } from "./dialog.js";
|
|
21
22
|
import { ScrollArea } from "./scroll-area.js";
|
|
@@ -37,6 +38,8 @@ const DEFAULT_MENU_BUTTON_SIZE = "md";
|
|
|
37
38
|
const DEFAULT_SHOW_ON_HOVER = true;
|
|
38
39
|
/** Whether {@link Sidebar.MenuSkeleton} renders a leading icon placeholder, when `showIcon` is omitted. */
|
|
39
40
|
const DEFAULT_SHOW_ICON = false;
|
|
41
|
+
/** Prefetch mode {@link Sidebar.MenuLink} and {@link Sidebar.MenuSubLink} fall back to when `prefetch` is omitted. */
|
|
42
|
+
const DEFAULT_PREFETCH = "none";
|
|
40
43
|
/**
|
|
41
44
|
* Named container {@link Sidebar.Provider} declares on its own host, so
|
|
42
45
|
* {@link Sidebar} and {@link Sidebar.Rail} can query the shell's overall
|
|
@@ -658,8 +661,8 @@ Sidebar.MenuButton = function SidebarMenuButton(handle) {
|
|
|
658
661
|
};
|
|
659
662
|
/**
|
|
660
663
|
* Renders a native `<a>` row for navigation inside {@link Sidebar.Menu},
|
|
661
|
-
* visually identical to {@link Sidebar.MenuButton}
|
|
662
|
-
* `aria-current="page"` when unset
|
|
664
|
+
* visually identical to {@link Sidebar.MenuButton} and followed by its `<link
|
|
665
|
+
* rel="prefetch">` when `prefetch="render"`. `active` sets `aria-current="page"` when unset.
|
|
663
666
|
*
|
|
664
667
|
* @param handle Runtime handle carrying the host `<a>`'s props.
|
|
665
668
|
* @returns The render function producing the row's markup.
|
|
@@ -668,17 +671,23 @@ Sidebar.MenuButton = function SidebarMenuButton(handle) {
|
|
|
668
671
|
* <HomeIcon aria-hidden />
|
|
669
672
|
* <span data-sidebar-collapsed-hide>{t("nav.dashboard")}</span>
|
|
670
673
|
* </Sidebar.MenuLink>
|
|
674
|
+
* @example
|
|
675
|
+
* <Sidebar.MenuLink href="/inbox" prefetch="intent">{t("nav.inbox")}</Sidebar.MenuLink>
|
|
671
676
|
*/
|
|
672
677
|
Sidebar.MenuLink = function SidebarMenuLink(handle) {
|
|
673
678
|
return () => {
|
|
674
|
-
let { size, active, "aria-current": ariaCurrent, mix, ...rest } = handle.props;
|
|
679
|
+
let { size, active, prefetch, "aria-current": ariaCurrent, mix, ...rest } = handle.props;
|
|
675
680
|
let resolvedSize = size ?? DEFAULT_MENU_BUTTON_SIZE;
|
|
676
681
|
let resolvedAriaCurrent = resolveAriaCurrent(active, ariaCurrent);
|
|
677
|
-
|
|
682
|
+
let resolvedPrefetch = prefetch ?? DEFAULT_PREFETCH;
|
|
683
|
+
let anchor = (_jsx("a", { "data-rmx-prefetch": prefetchAttribute(resolvedPrefetch), ...rest, "aria-current": resolvedAriaCurrent, "data-slot": "menu-link", "data-size": resolvedSize, "data-active": active || undefined, mix: [
|
|
678
684
|
when("&:focus-visible", outline({ color: "brand.ring", offset: 2 })),
|
|
679
685
|
...menuRowMixins(),
|
|
680
686
|
mix,
|
|
681
687
|
] }));
|
|
688
|
+
if (!rendersPrefetchHint(resolvedPrefetch, rest.href))
|
|
689
|
+
return anchor;
|
|
690
|
+
return (_jsxs(_Fragment, { children: [anchor, _jsx("link", { rel: "prefetch", href: rest.href })] }));
|
|
682
691
|
};
|
|
683
692
|
};
|
|
684
693
|
/**
|
|
@@ -913,8 +922,8 @@ Sidebar.MenuSubButton = function SidebarMenuSubButton(handle) {
|
|
|
913
922
|
};
|
|
914
923
|
/**
|
|
915
924
|
* Renders a native `<a>` row for navigation inside {@link Sidebar.MenuSub},
|
|
916
|
-
* visually identical to {@link Sidebar.MenuSubButton}
|
|
917
|
-
* `
|
|
925
|
+
* visually identical to {@link Sidebar.MenuSubButton} and followed by its `<link
|
|
926
|
+
* rel="prefetch">` when `prefetch="render"`. `active` sets `aria-current="page"` when unset.
|
|
918
927
|
*
|
|
919
928
|
* @param handle Runtime handle carrying the host `<a>`'s props.
|
|
920
929
|
* @returns The render function producing the row's markup.
|
|
@@ -923,13 +932,17 @@ Sidebar.MenuSubButton = function SidebarMenuSubButton(handle) {
|
|
|
923
932
|
*/
|
|
924
933
|
Sidebar.MenuSubLink = function SidebarMenuSubLink(handle) {
|
|
925
934
|
return () => {
|
|
926
|
-
let { active, "aria-current": ariaCurrent, mix, ...rest } = handle.props;
|
|
935
|
+
let { active, prefetch, "aria-current": ariaCurrent, mix, ...rest } = handle.props;
|
|
927
936
|
let resolvedAriaCurrent = resolveAriaCurrent(active, ariaCurrent);
|
|
928
|
-
|
|
937
|
+
let resolvedPrefetch = prefetch ?? DEFAULT_PREFETCH;
|
|
938
|
+
let anchor = (_jsx("a", { "data-rmx-prefetch": prefetchAttribute(resolvedPrefetch), ...rest, "aria-current": resolvedAriaCurrent, "data-slot": "menu-sub-link", "data-active": active || undefined, mix: [
|
|
929
939
|
when("&:focus-visible", outline({ color: "brand.ring", offset: 2 })),
|
|
930
940
|
...menuSubRowMixins(),
|
|
931
941
|
mix,
|
|
932
942
|
] }));
|
|
943
|
+
if (!rendersPrefetchHint(resolvedPrefetch, rest.href))
|
|
944
|
+
return anchor;
|
|
945
|
+
return (_jsxs(_Fragment, { children: [anchor, _jsx("link", { rel: "prefetch", href: rest.href })] }));
|
|
933
946
|
};
|
|
934
947
|
};
|
|
935
948
|
/**
|
|
@@ -1,12 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The combobox keyboard pattern over options a server rendered: arrows choose a row of the
|
|
3
|
-
* listbox the host's `aria-controls` names while focus stays in the host,
|
|
4
|
-
*
|
|
3
|
+
* listbox the host's `aria-controls` names while focus stays in the host, Enter follows it, and
|
|
4
|
+
* a resting choice can load its destination ahead. Without JS every option is still a link a
|
|
5
|
+
* reader can tab to.
|
|
5
6
|
*
|
|
6
7
|
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
8
|
* @copyright Sergio Xalambrí 2026
|
|
8
9
|
*/
|
|
9
10
|
import type { MixinFactory } from "remix/component";
|
|
11
|
+
/** Types for {@link activeDescendantKeys}. */
|
|
12
|
+
export declare namespace ActiveDescendantKeys {
|
|
13
|
+
/**
|
|
14
|
+
* How a chosen option's destination loads ahead of Enter. `"frame"` asks the
|
|
15
|
+
* `remix/component` runtime, which warms the HTTP cache a client navigation reads;
|
|
16
|
+
* `"document"` adds a `<link rel="prefetch">`, which a full document load reuses.
|
|
17
|
+
*/
|
|
18
|
+
type Prefetch = "frame" | "document";
|
|
19
|
+
/** Configuration for {@link activeDescendantKeys}. */
|
|
20
|
+
interface Options {
|
|
21
|
+
/** Loads the destination of an option the arrows rest on, once the choice settles. */
|
|
22
|
+
prefetch?: Prefetch;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
10
25
|
/**
|
|
11
26
|
* The index of the option `combobox` has chosen, or `-1` while the combobox itself is the
|
|
12
27
|
* choice. The choice lives in `aria-activedescendant`, so it reads the same to every caller.
|
|
@@ -24,11 +39,26 @@ export declare function chosenOption(combobox: HTMLElement): number;
|
|
|
24
39
|
* @param index The option to choose, or `-1` to choose none.
|
|
25
40
|
*/
|
|
26
41
|
export declare function chooseOption(combobox: HTMLElement, index: number): void;
|
|
42
|
+
/**
|
|
43
|
+
* Starts loading the destination of the option at `index` so following it lands sooner. Skips a
|
|
44
|
+
* constrained connection, an option without a same-origin link that opens in place, the page
|
|
45
|
+
* already showing, and a destination loaded in the last five minutes. Call it from an event
|
|
46
|
+
* handler, such as when new results settle, since it reads the browser's document.
|
|
47
|
+
*
|
|
48
|
+
* @param combobox The element carrying `aria-controls`.
|
|
49
|
+
* @param index The option whose link to load.
|
|
50
|
+
* @param strategy How the destination loads, as {@link ActiveDescendantKeys.Prefetch} describes.
|
|
51
|
+
*/
|
|
52
|
+
export declare function prefetchOption(combobox: HTMLElement, index: number, strategy: ActiveDescendantKeys.Prefetch): void;
|
|
27
53
|
/**
|
|
28
54
|
* Moves the choice through the controlled options: Down from the host to the first and around
|
|
29
55
|
* from the last, Up back toward the host. Enter follows the chosen option, or the only option
|
|
30
56
|
* when none is chosen and the listbox sits in no `aria-busy` region, so stale rows never open.
|
|
57
|
+
* With `prefetch`, a choice resting 100ms loads its link as {@link prefetchOption} does.
|
|
31
58
|
*
|
|
32
59
|
* @example <input role="combobox" aria-controls="search-options" mix={[activeDescendantKeys()]} />
|
|
60
|
+
* @example <input role="combobox" aria-controls="search-options" mix={[activeDescendantKeys({ prefetch: "frame" })]} />
|
|
33
61
|
*/
|
|
34
|
-
export declare const activeDescendantKeys: MixinFactory<HTMLElement
|
|
62
|
+
export declare const activeDescendantKeys: MixinFactory<HTMLElement, [
|
|
63
|
+
options?: ActiveDescendantKeys.Options
|
|
64
|
+
]>;
|
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The combobox keyboard pattern over options a server rendered: arrows choose a row of the
|
|
3
|
-
* listbox the host's `aria-controls` names while focus stays in the host,
|
|
4
|
-
*
|
|
3
|
+
* listbox the host's `aria-controls` names while focus stays in the host, Enter follows it, and
|
|
4
|
+
* a resting choice can load its destination ahead. Without JS every option is still a link a
|
|
5
|
+
* reader can tab to.
|
|
5
6
|
*
|
|
6
7
|
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
8
|
* @copyright Sergio Xalambrí 2026
|
|
8
9
|
*/
|
|
9
|
-
import { createMixin } from "remix/component";
|
|
10
|
+
import { createMixin, prefetch } from "remix/component";
|
|
11
|
+
/** How long the choice rests on an option before its destination starts loading. */
|
|
12
|
+
const PREFETCH_DELAY_MS = 100;
|
|
13
|
+
/** How long a prefetched destination counts as loaded, the span browsers keep one usable. */
|
|
14
|
+
const PREFETCH_FRESH_MS = 5 * 60 * 1000;
|
|
15
|
+
/** When each destination last started loading, keyed by strategy and address. */
|
|
16
|
+
const PREFETCHED = new Map();
|
|
10
17
|
/** Every option of the listbox `combobox` controls, in document order. */
|
|
11
18
|
function optionsOf(combobox) {
|
|
12
19
|
let id = combobox.getAttribute("aria-controls");
|
|
@@ -44,6 +51,66 @@ export function chooseOption(combobox, index) {
|
|
|
44
51
|
combobox.removeAttribute("aria-activedescendant");
|
|
45
52
|
chosen?.scrollIntoView?.({ block: "nearest" });
|
|
46
53
|
}
|
|
54
|
+
/** Whether the reader asked to save data or sits on a 2G connection, where guesses cost too much. */
|
|
55
|
+
function constrainedConnection() {
|
|
56
|
+
let connection = navigator.connection;
|
|
57
|
+
if (!connection)
|
|
58
|
+
return false;
|
|
59
|
+
return (connection.saveData === true ||
|
|
60
|
+
connection.effectiveType === "slow-2g" ||
|
|
61
|
+
connection.effectiveType === "2g");
|
|
62
|
+
}
|
|
63
|
+
/** Records `key` as loading now, answering whether it had already loaded recently. */
|
|
64
|
+
function prefetchedRecently(key) {
|
|
65
|
+
let now = Date.now();
|
|
66
|
+
let last = PREFETCHED.get(key);
|
|
67
|
+
if (last !== undefined && now - last < PREFETCH_FRESH_MS)
|
|
68
|
+
return true;
|
|
69
|
+
PREFETCHED.set(key, now);
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
/** The same-origin address `value` names without its hash, or `null` for another origin. */
|
|
73
|
+
function sameOriginHref(value, base) {
|
|
74
|
+
let url = new URL(value, base);
|
|
75
|
+
if (url.origin !== new URL(base).origin)
|
|
76
|
+
return null;
|
|
77
|
+
url.hash = "";
|
|
78
|
+
return url.href;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Starts loading the destination of the option at `index` so following it lands sooner. Skips a
|
|
82
|
+
* constrained connection, an option without a same-origin link that opens in place, the page
|
|
83
|
+
* already showing, and a destination loaded in the last five minutes. Call it from an event
|
|
84
|
+
* handler, such as when new results settle, since it reads the browser's document.
|
|
85
|
+
*
|
|
86
|
+
* @param combobox The element carrying `aria-controls`.
|
|
87
|
+
* @param index The option whose link to load.
|
|
88
|
+
* @param strategy How the destination loads, as {@link ActiveDescendantKeys.Prefetch} describes.
|
|
89
|
+
*/
|
|
90
|
+
export function prefetchOption(combobox, index, strategy) {
|
|
91
|
+
let link = optionsOf(combobox)[index]?.querySelector("a[href]");
|
|
92
|
+
if (!link || link.hasAttribute("download") || constrainedConnection())
|
|
93
|
+
return;
|
|
94
|
+
if (link.target !== "" && link.target !== "_self")
|
|
95
|
+
return;
|
|
96
|
+
let doc = combobox.ownerDocument;
|
|
97
|
+
let base = doc.baseURI;
|
|
98
|
+
let href = sameOriginHref(link.getAttribute("href") ?? "", base);
|
|
99
|
+
if (!href || href === sameOriginHref(doc.location.href, base))
|
|
100
|
+
return;
|
|
101
|
+
if (prefetchedRecently(`${strategy} ${href}`))
|
|
102
|
+
return;
|
|
103
|
+
if (strategy === "frame") {
|
|
104
|
+
let target = link.getAttribute("data-rmx-target");
|
|
105
|
+
let src = sameOriginHref(link.getAttribute("data-rmx-src") ?? href, base) ?? href;
|
|
106
|
+
prefetch(target ? src : href, target ? { target } : undefined);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
let hint = doc.createElement("link");
|
|
110
|
+
hint.rel = "prefetch";
|
|
111
|
+
hint.href = href;
|
|
112
|
+
doc.head.appendChild(hint);
|
|
113
|
+
}
|
|
47
114
|
/** Activates an option as a click would: its link when it holds one, else the option itself. */
|
|
48
115
|
function follow(option) {
|
|
49
116
|
(option.querySelector("a[href]") ?? option).click();
|
|
@@ -52,37 +119,58 @@ function follow(option) {
|
|
|
52
119
|
* Moves the choice through the controlled options: Down from the host to the first and around
|
|
53
120
|
* from the last, Up back toward the host. Enter follows the chosen option, or the only option
|
|
54
121
|
* when none is chosen and the listbox sits in no `aria-busy` region, so stale rows never open.
|
|
122
|
+
* With `prefetch`, a choice resting 100ms loads its link as {@link prefetchOption} does.
|
|
55
123
|
*
|
|
56
124
|
* @example <input role="combobox" aria-controls="search-options" mix={[activeDescendantKeys()]} />
|
|
125
|
+
* @example <input role="combobox" aria-controls="search-options" mix={[activeDescendantKeys({ prefetch: "frame" })]} />
|
|
57
126
|
*/
|
|
58
127
|
export const activeDescendantKeys = createMixin((handle) => {
|
|
128
|
+
let options = {};
|
|
129
|
+
let pending;
|
|
130
|
+
handle.signal.addEventListener("abort", () => clearTimeout(pending));
|
|
131
|
+
/** Loads the chosen option's destination once the arrows have rested on it a moment. */
|
|
132
|
+
function schedulePrefetch(host) {
|
|
133
|
+
let strategy = options.prefetch;
|
|
134
|
+
if (!strategy)
|
|
135
|
+
return;
|
|
136
|
+
clearTimeout(pending);
|
|
137
|
+
pending = setTimeout(() => {
|
|
138
|
+
let chosen = chosenOption(host);
|
|
139
|
+
if (chosen >= 0)
|
|
140
|
+
prefetchOption(host, chosen, strategy);
|
|
141
|
+
}, PREFETCH_DELAY_MS);
|
|
142
|
+
}
|
|
59
143
|
handle.addEventListener("insert", (event) => {
|
|
60
144
|
let host = event.node;
|
|
61
145
|
host.addEventListener("keydown", (keyEvent) => {
|
|
62
146
|
if (keyEvent.defaultPrevented || keyEvent.isComposing)
|
|
63
147
|
return;
|
|
64
|
-
let
|
|
148
|
+
let rows = optionsOf(host);
|
|
65
149
|
let chosen = chosenOption(host);
|
|
66
|
-
if (keyEvent.key === "ArrowDown" &&
|
|
150
|
+
if (keyEvent.key === "ArrowDown" && rows.length > 0) {
|
|
67
151
|
keyEvent.preventDefault();
|
|
68
|
-
chooseOption(host, chosen + 1 >=
|
|
152
|
+
chooseOption(host, chosen + 1 >= rows.length ? 0 : chosen + 1);
|
|
153
|
+
schedulePrefetch(host);
|
|
69
154
|
}
|
|
70
155
|
else if (keyEvent.key === "ArrowUp" && chosen >= 0) {
|
|
71
156
|
keyEvent.preventDefault();
|
|
72
157
|
chooseOption(host, chosen - 1);
|
|
158
|
+
schedulePrefetch(host);
|
|
73
159
|
}
|
|
74
|
-
else if (keyEvent.key === "Enter" &&
|
|
160
|
+
else if (keyEvent.key === "Enter" && rows[chosen]) {
|
|
75
161
|
keyEvent.preventDefault();
|
|
76
|
-
follow(
|
|
162
|
+
follow(rows[chosen]);
|
|
77
163
|
}
|
|
78
164
|
else if (keyEvent.key === "Enter" &&
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
!
|
|
165
|
+
rows.length === 1 &&
|
|
166
|
+
rows[0] &&
|
|
167
|
+
!rows[0].closest('[aria-busy="true"]')) {
|
|
82
168
|
keyEvent.preventDefault();
|
|
83
|
-
follow(
|
|
169
|
+
follow(rows[0]);
|
|
84
170
|
}
|
|
85
171
|
}, { signal: handle.signal });
|
|
86
172
|
});
|
|
87
|
-
return () => {
|
|
173
|
+
return (next) => {
|
|
174
|
+
options = next ?? {};
|
|
175
|
+
};
|
|
88
176
|
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps the frame that renders the host current: it reloads on a schedule, on an event, or
|
|
3
|
+
* both, and rests while the page is hidden. JS is what triggers the reloads; without it the
|
|
4
|
+
* frame shows what the page loaded with until the reader reloads the page.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { MixinFactory } from "remix/component";
|
|
10
|
+
/** Types for {@link frameRefresh}. */
|
|
11
|
+
export declare namespace FrameRefresh {
|
|
12
|
+
/** Configuration for {@link frameRefresh}; give `every`, `on`, or both. */
|
|
13
|
+
interface Options {
|
|
14
|
+
/** Milliseconds between the end of one reload and the start of the next. */
|
|
15
|
+
every?: number;
|
|
16
|
+
/** DOM event type that reloads the frame when it reaches the host, read once on insert. */
|
|
17
|
+
on?: string;
|
|
18
|
+
/** Read before each reload; once it answers `true` the host stops reloading the frame. */
|
|
19
|
+
until?: () => boolean;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Reloads the host's enclosing frame every `every` milliseconds and whenever the `on` event
|
|
24
|
+
* reaches the host. One reload runs at a time: an event during a reload queues one more. The
|
|
25
|
+
* schedule pauses while the page is hidden and reloads once when it is shown again.
|
|
26
|
+
*
|
|
27
|
+
* @example <section mix={[frameRefresh({ every: 30_000 })]}>…</section>
|
|
28
|
+
* @example <div mix={[frameRefresh({ on: "app:saved", until: () => done })]}>…</div>
|
|
29
|
+
*/
|
|
30
|
+
export declare const frameRefresh: MixinFactory<Element, [options: FrameRefresh.Options]>;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps the frame that renders the host current: it reloads on a schedule, on an event, or
|
|
3
|
+
* both, and rests while the page is hidden. JS is what triggers the reloads; without it the
|
|
4
|
+
* frame shows what the page loaded with until the reader reloads the page.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { createMixin } from "remix/component";
|
|
10
|
+
import { whileLive } from "./while-live.js";
|
|
11
|
+
/**
|
|
12
|
+
* Reloads the host's enclosing frame every `every` milliseconds and whenever the `on` event
|
|
13
|
+
* reaches the host. One reload runs at a time: an event during a reload queues one more. The
|
|
14
|
+
* schedule pauses while the page is hidden and reloads once when it is shown again.
|
|
15
|
+
*
|
|
16
|
+
* @example <section mix={[frameRefresh({ every: 30_000 })]}>…</section>
|
|
17
|
+
* @example <div mix={[frameRefresh({ on: "app:saved", until: () => done })]}>…</div>
|
|
18
|
+
*/
|
|
19
|
+
export const frameRefresh = createMixin((handle) => {
|
|
20
|
+
let options = {};
|
|
21
|
+
let timer;
|
|
22
|
+
let running = false;
|
|
23
|
+
let queued = false;
|
|
24
|
+
let stopped = false;
|
|
25
|
+
/** Aborts once the host leaves the document, ending the schedule with it. */
|
|
26
|
+
let live;
|
|
27
|
+
/** Whether the stop condition has been met, which ends every later reload for good. */
|
|
28
|
+
function finished() {
|
|
29
|
+
if (!stopped && options.until?.() === true)
|
|
30
|
+
stopped = true;
|
|
31
|
+
return stopped;
|
|
32
|
+
}
|
|
33
|
+
/** Waits `every` milliseconds before the next reload, while the page is shown. */
|
|
34
|
+
function schedule() {
|
|
35
|
+
clearTimeout(timer);
|
|
36
|
+
timer = undefined;
|
|
37
|
+
if (options.every === undefined || finished() || document.hidden)
|
|
38
|
+
return;
|
|
39
|
+
timer = setTimeout(() => void refresh(), options.every);
|
|
40
|
+
}
|
|
41
|
+
/** Reloads the frame unless one is running, then starts the wait for the next one. */
|
|
42
|
+
async function refresh() {
|
|
43
|
+
clearTimeout(timer);
|
|
44
|
+
if (!live || live.aborted || finished())
|
|
45
|
+
return;
|
|
46
|
+
if (running) {
|
|
47
|
+
queued = true;
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
running = true;
|
|
51
|
+
try {
|
|
52
|
+
await handle.frame.reload();
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
/** A failed reload leaves the frame as it was; the schedule tries again on its next turn. */
|
|
56
|
+
}
|
|
57
|
+
finally {
|
|
58
|
+
running = false;
|
|
59
|
+
}
|
|
60
|
+
if (live.aborted)
|
|
61
|
+
return;
|
|
62
|
+
if (queued) {
|
|
63
|
+
queued = false;
|
|
64
|
+
return refresh();
|
|
65
|
+
}
|
|
66
|
+
schedule();
|
|
67
|
+
}
|
|
68
|
+
let follow = whileLive(handle, (host, signal) => {
|
|
69
|
+
live = signal;
|
|
70
|
+
signal.addEventListener("abort", () => clearTimeout(timer));
|
|
71
|
+
host.ownerDocument.addEventListener("visibilitychange", () => {
|
|
72
|
+
if (host.ownerDocument.hidden)
|
|
73
|
+
clearTimeout(timer);
|
|
74
|
+
else if (options.every !== undefined)
|
|
75
|
+
void refresh();
|
|
76
|
+
}, { signal });
|
|
77
|
+
if (options.on)
|
|
78
|
+
host.addEventListener(options.on, () => void refresh(), { signal });
|
|
79
|
+
schedule();
|
|
80
|
+
});
|
|
81
|
+
handle.addEventListener("insert", (event) => follow(event.node));
|
|
82
|
+
return (next) => {
|
|
83
|
+
options = next;
|
|
84
|
+
};
|
|
85
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Submits a form into the frame that renders it: the frame posts the form's data and renders
|
|
3
|
+
* the answer in one request, keeping its own address for later reloads. JS is what sends the
|
|
4
|
+
* data through the frame; without it the form submits as a normal document request.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { MixinFactory } from "remix/component";
|
|
10
|
+
/** DOM event type dispatched by {@link frameSubmit} as a submission moves through its states. */
|
|
11
|
+
declare const FRAME_SUBMIT_EVENT: "ui:frame-submit";
|
|
12
|
+
declare global {
|
|
13
|
+
interface HTMLElementEventMap {
|
|
14
|
+
[FRAME_SUBMIT_EVENT]: FrameSubmitEvent;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** Types for {@link frameSubmit}. */
|
|
18
|
+
export declare namespace FrameSubmit {
|
|
19
|
+
/**
|
|
20
|
+
* Where a submission stands: `pending` once it is sent, `settled` once the frame shows the
|
|
21
|
+
* answer, `aborted` when a newer reload of the frame replaced it, and `failed` when the
|
|
22
|
+
* frame could not load the answer.
|
|
23
|
+
*/
|
|
24
|
+
type State = "pending" | "settled" | "aborted" | "failed";
|
|
25
|
+
/** Configuration for {@link frameSubmit}. */
|
|
26
|
+
interface Options {
|
|
27
|
+
/**
|
|
28
|
+
* Keeps the frame's own address for later reloads. Set `false` to have the frame adopt
|
|
29
|
+
* the form's action as its address once the submission settles.
|
|
30
|
+
*
|
|
31
|
+
* @default true
|
|
32
|
+
*/
|
|
33
|
+
keepSrc?: boolean;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Dispatched on a {@link frameSubmit} form as a submission moves through its states. Each
|
|
38
|
+
* submission reports `pending` and then exactly one of the other states.
|
|
39
|
+
*/
|
|
40
|
+
export declare class FrameSubmitEvent extends Event {
|
|
41
|
+
/** Where the submission stands; `pending` always comes first. */
|
|
42
|
+
readonly state: FrameSubmit.State;
|
|
43
|
+
/** The address the form submitted to. */
|
|
44
|
+
readonly action: string;
|
|
45
|
+
/** Why the frame could not load the answer, set only on `failed`. */
|
|
46
|
+
readonly error: unknown;
|
|
47
|
+
/**
|
|
48
|
+
* @param init The submission's state, its address, and the failure when it failed.
|
|
49
|
+
*/
|
|
50
|
+
constructor(init: {
|
|
51
|
+
state: FrameSubmit.State;
|
|
52
|
+
action: string;
|
|
53
|
+
error?: unknown;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Sends the form's submission through its enclosing frame, marking the form `aria-busy` until
|
|
58
|
+
* the answer renders there; the frame then returns to its own address, so the action answers a
|
|
59
|
+
* frame request with the frame's content, a redirect to it included. A newer submission wins.
|
|
60
|
+
*
|
|
61
|
+
* @example <form method="post" action="/actions/rename" mix={[frameSubmit()]}>…</form>
|
|
62
|
+
* @example form.addEventListener("ui:frame-submit", (event) => event.state === "failed" && retry());
|
|
63
|
+
*/
|
|
64
|
+
export declare const frameSubmit: MixinFactory<HTMLFormElement, [options?: FrameSubmit.Options]>;
|
|
65
|
+
export {};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Submits a form into the frame that renders it: the frame posts the form's data and renders
|
|
3
|
+
* the answer in one request, keeping its own address for later reloads. JS is what sends the
|
|
4
|
+
* data through the frame; without it the form submits as a normal document request.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { createMixin } from "remix/component";
|
|
10
|
+
import { whileLive } from "./while-live.js";
|
|
11
|
+
/** DOM event type dispatched by {@link frameSubmit} as a submission moves through its states. */
|
|
12
|
+
const FRAME_SUBMIT_EVENT = "ui:frame-submit";
|
|
13
|
+
/**
|
|
14
|
+
* Dispatched on a {@link frameSubmit} form as a submission moves through its states. Each
|
|
15
|
+
* submission reports `pending` and then exactly one of the other states.
|
|
16
|
+
*/
|
|
17
|
+
export class FrameSubmitEvent extends Event {
|
|
18
|
+
/** Where the submission stands; `pending` always comes first. */
|
|
19
|
+
state;
|
|
20
|
+
/** The address the form submitted to. */
|
|
21
|
+
action;
|
|
22
|
+
/** Why the frame could not load the answer, set only on `failed`. */
|
|
23
|
+
error;
|
|
24
|
+
/**
|
|
25
|
+
* @param init The submission's state, its address, and the failure when it failed.
|
|
26
|
+
*/
|
|
27
|
+
constructor(init) {
|
|
28
|
+
super(FRAME_SUBMIT_EVENT, { bubbles: true });
|
|
29
|
+
this.state = init.state;
|
|
30
|
+
this.action = init.action;
|
|
31
|
+
this.error = init.error;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Reads the submission the browser would make, honoring `formaction`, `formmethod` and
|
|
36
|
+
* `formenctype` on the button that submitted it.
|
|
37
|
+
*/
|
|
38
|
+
function readSubmission(form, submitter) {
|
|
39
|
+
let button = submitter instanceof HTMLButtonElement || submitter instanceof HTMLInputElement
|
|
40
|
+
? submitter
|
|
41
|
+
: null;
|
|
42
|
+
return {
|
|
43
|
+
action: button?.hasAttribute("formaction") ? button.formAction : form.action,
|
|
44
|
+
method: button?.hasAttribute("formmethod") ? button.formMethod : form.method,
|
|
45
|
+
encType: button?.hasAttribute("formenctype") ? button.formEnctype : form.enctype,
|
|
46
|
+
body: new FormData(form, button),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Sends the form's submission through its enclosing frame, marking the form `aria-busy` until
|
|
51
|
+
* the answer renders there; the frame then returns to its own address, so the action answers a
|
|
52
|
+
* frame request with the frame's content, a redirect to it included. A newer submission wins.
|
|
53
|
+
*
|
|
54
|
+
* @example <form method="post" action="/actions/rename" mix={[frameSubmit()]}>…</form>
|
|
55
|
+
* @example form.addEventListener("ui:frame-submit", (event) => event.state === "failed" && retry());
|
|
56
|
+
*/
|
|
57
|
+
export const frameSubmit = createMixin((handle) => {
|
|
58
|
+
let options = {};
|
|
59
|
+
/** Counts submissions, so only the newest one clears the form's busy state. */
|
|
60
|
+
let latest = 0;
|
|
61
|
+
/** Sends one submission through the frame and reports how it ended. */
|
|
62
|
+
async function submit(form, submission) {
|
|
63
|
+
let request = ++latest;
|
|
64
|
+
let frame = handle.frame;
|
|
65
|
+
let previous = frame.src;
|
|
66
|
+
let report = (state, error) => form.dispatchEvent(new FrameSubmitEvent({ state, action: submission.action, error }));
|
|
67
|
+
form.setAttribute("aria-busy", "true");
|
|
68
|
+
report("pending");
|
|
69
|
+
let { action, method, encType, body } = submission;
|
|
70
|
+
let reload = frame.reload({ src: action, method, encType, body });
|
|
71
|
+
/** The address the reload settled on, with a GET submission's fields in its query. */
|
|
72
|
+
let submitted = frame.src;
|
|
73
|
+
try {
|
|
74
|
+
let signal = await reload;
|
|
75
|
+
report(signal.aborted ? "aborted" : "settled");
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
report("failed", error);
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
/** A newer reload owns the address once it has moved off this submission's. */
|
|
82
|
+
if ((options.keepSrc ?? true) && frame.src === submitted)
|
|
83
|
+
frame.src = previous;
|
|
84
|
+
if (request === latest)
|
|
85
|
+
form.removeAttribute("aria-busy");
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
let follow = whileLive(handle, (form, signal) => {
|
|
89
|
+
form.addEventListener("submit", (event) => {
|
|
90
|
+
let submission = readSubmission(form, event.submitter);
|
|
91
|
+
if (submission.method === "dialog")
|
|
92
|
+
return;
|
|
93
|
+
event.preventDefault();
|
|
94
|
+
let skipValidation = form.noValidate || event.submitter?.hasAttribute("formnovalidate") === true;
|
|
95
|
+
if (!skipValidation && !form.reportValidity())
|
|
96
|
+
return;
|
|
97
|
+
void submit(form, submission);
|
|
98
|
+
}, { signal });
|
|
99
|
+
});
|
|
100
|
+
handle.addEventListener("insert", (event) => follow(event.node));
|
|
101
|
+
return (next) => {
|
|
102
|
+
options = next ?? {};
|
|
103
|
+
};
|
|
104
|
+
});
|
package/dist/mixins/index.d.ts
CHANGED
|
@@ -26,6 +26,8 @@ export * from "./dismiss.js";
|
|
|
26
26
|
export * from "./drag-reorder.js";
|
|
27
27
|
export * from "./drop-zone.js";
|
|
28
28
|
export * from "./dual-range.js";
|
|
29
|
+
export * from "./frame-refresh.js";
|
|
30
|
+
export * from "./frame-submit.js";
|
|
29
31
|
export * from "./grid-list-keys.js";
|
|
30
32
|
export * from "./heading-level-fallback.js";
|
|
31
33
|
export * from "./hotkey.js";
|
package/dist/mixins/index.js
CHANGED
|
@@ -26,6 +26,8 @@ export * from "./dismiss.js";
|
|
|
26
26
|
export * from "./drag-reorder.js";
|
|
27
27
|
export * from "./drop-zone.js";
|
|
28
28
|
export * from "./dual-range.js";
|
|
29
|
+
export * from "./frame-refresh.js";
|
|
30
|
+
export * from "./frame-submit.js";
|
|
29
31
|
export * from "./grid-list-keys.js";
|
|
30
32
|
export * from "./heading-level-fallback.js";
|
|
31
33
|
export * from "./hotkey.js";
|
|
@@ -41,7 +41,7 @@ export class ViewTransitionEvent extends Event {
|
|
|
41
41
|
* mixin instance reacting to the same Frame's `reloadStart` reuses the one
|
|
42
42
|
* transition already opened for it instead of opening a second.
|
|
43
43
|
*/
|
|
44
|
-
const
|
|
44
|
+
const FRAME_TRANSITIONS = new WeakMap();
|
|
45
45
|
/**
|
|
46
46
|
* Opens (or reuses) the native transition wrapping `frame`'s current reload,
|
|
47
47
|
* skipping `startViewTransition()` when unsupported or reduced motion is
|
|
@@ -50,7 +50,7 @@ const frameTransitions = new WeakMap();
|
|
|
50
50
|
* @param frame Frame whose reload this transition wraps.
|
|
51
51
|
*/
|
|
52
52
|
function ensureFrameTransition(frame) {
|
|
53
|
-
let existing =
|
|
53
|
+
let existing = FRAME_TRANSITIONS.get(frame);
|
|
54
54
|
if (existing)
|
|
55
55
|
return existing;
|
|
56
56
|
let state = { transition: null, completed: false };
|
|
@@ -63,7 +63,7 @@ function ensureFrameTransition(frame) {
|
|
|
63
63
|
state.resolveUpdate = resolve;
|
|
64
64
|
}));
|
|
65
65
|
}
|
|
66
|
-
|
|
66
|
+
FRAME_TRANSITIONS.set(frame, state);
|
|
67
67
|
return state;
|
|
68
68
|
}
|
|
69
69
|
/**
|
|
@@ -74,12 +74,12 @@ function ensureFrameTransition(frame) {
|
|
|
74
74
|
* @param frame Frame whose reload just finished.
|
|
75
75
|
*/
|
|
76
76
|
function settleFrameTransition(frame) {
|
|
77
|
-
let state =
|
|
77
|
+
let state = FRAME_TRANSITIONS.get(frame);
|
|
78
78
|
if (!state)
|
|
79
79
|
return;
|
|
80
80
|
state.completed = true;
|
|
81
81
|
state.resolveUpdate?.();
|
|
82
|
-
|
|
82
|
+
FRAME_TRANSITIONS.delete(frame);
|
|
83
83
|
}
|
|
84
84
|
/**
|
|
85
85
|
* Wraps a SharedElement's enclosing Frame reload in one native view
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -24,6 +24,7 @@ export * from "./hue-spectrum.js";
|
|
|
24
24
|
export * from "./is-new-primary-press.js";
|
|
25
25
|
export * from "./key-combo.js";
|
|
26
26
|
export * from "./keyboard-nav.js";
|
|
27
|
+
export * from "./link-prefetch.js";
|
|
27
28
|
export * from "./merge-style.js";
|
|
28
29
|
export * from "./paired-range-inputs.js";
|
|
29
30
|
export * from "./placement.js";
|