@sdxc/ui 2026.11.1 → 2026.11.4
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 +1 -1
- package/dist/app/app-shell-toggle.d.ts +31 -0
- package/dist/app/app-shell-toggle.js +43 -0
- package/dist/app/app-shell.d.ts +131 -0
- package/dist/app/app-shell.js +542 -0
- package/dist/app/auto-refresh.d.ts +104 -0
- package/dist/app/auto-refresh.js +126 -0
- package/dist/app/behaviors/refresh-schedule.d.ts +53 -0
- package/dist/app/behaviors/refresh-schedule.js +95 -0
- package/dist/app/behaviors/shortcuts.d.ts +175 -0
- package/dist/app/behaviors/shortcuts.js +401 -0
- package/dist/app/behaviors/toaster.d.ts +31 -2
- package/dist/app/behaviors/toaster.js +62 -5
- package/dist/app/chart.d.ts +62 -8
- package/dist/app/chart.js +207 -9
- package/dist/app/command-palette.d.ts +132 -0
- package/dist/app/command-palette.js +181 -0
- package/dist/app/danger-zone.d.ts +66 -0
- package/dist/app/danger-zone.js +66 -0
- package/dist/app/detail-sheet.d.ts +119 -0
- package/dist/app/detail-sheet.js +173 -0
- package/dist/app/drop-zone.d.ts +2 -1
- package/dist/app/drop-zone.js +6 -3
- package/dist/app/editor-layout.d.ts +69 -0
- package/dist/app/editor-layout.js +143 -0
- package/dist/app/empty.d.ts +56 -19
- package/dist/app/empty.js +129 -22
- package/dist/app/filter-bar.d.ts +169 -0
- package/dist/app/filter-bar.js +346 -0
- package/dist/app/index.d.ts +51 -0
- package/dist/app/index.js +51 -0
- package/dist/app/keyboard-help.d.ts +66 -0
- package/dist/app/keyboard-help.js +56 -0
- package/dist/app/mixins/app-shell-controls.d.ts +46 -0
- package/dist/app/mixins/app-shell-controls.js +131 -0
- package/dist/app/mixins/auto-refresh.d.ts +29 -0
- package/dist/app/mixins/auto-refresh.js +66 -0
- package/dist/app/mixins/command-palette.d.ts +56 -0
- package/dist/app/mixins/command-palette.js +297 -0
- package/dist/app/mixins/copy-permalink.d.ts +26 -0
- package/dist/app/mixins/copy-permalink.js +54 -0
- package/dist/app/mixins/detail-sheet-keys.d.ts +28 -0
- package/dist/app/mixins/detail-sheet-keys.js +88 -0
- package/dist/app/mixins/escape-link.d.ts +17 -0
- package/dist/app/mixins/escape-link.js +35 -0
- package/dist/app/mixins/keyboard-help.d.ts +38 -0
- package/dist/app/mixins/keyboard-help.js +51 -0
- package/dist/app/mixins/optimistic-toggle-sync.d.ts +80 -0
- package/dist/app/mixins/optimistic-toggle-sync.js +153 -0
- package/dist/app/mixins/range-picker-keys.d.ts +18 -0
- package/dist/app/mixins/range-picker-keys.js +94 -0
- package/dist/app/mixins/row-navigation.d.ts +85 -0
- package/dist/app/mixins/row-navigation.js +208 -0
- package/dist/app/mixins/save-bar-sync.d.ts +27 -0
- package/dist/app/mixins/save-bar-sync.js +106 -0
- package/dist/app/mixins/save-shortcut.d.ts +47 -0
- package/dist/app/mixins/save-shortcut.js +101 -0
- package/dist/app/mixins/save-status-sync.d.ts +77 -0
- package/dist/app/mixins/save-status-sync.js +159 -0
- package/dist/app/mixins/selection-bar-sync.d.ts +32 -0
- package/dist/app/mixins/selection-bar-sync.js +151 -0
- package/dist/app/mixins/split-view.d.ts +66 -0
- package/dist/app/mixins/split-view.js +319 -0
- package/dist/app/mixins/status-strip-keys.d.ts +17 -0
- package/dist/app/mixins/status-strip-keys.js +79 -0
- package/dist/app/mixins/submit-on-change.d.ts +16 -0
- package/dist/app/mixins/submit-on-change.js +28 -0
- package/dist/app/mixins/table-selection.d.ts +39 -0
- package/dist/app/mixins/table-selection.js +272 -0
- package/dist/app/mixins/toast-queue.d.ts +36 -0
- package/dist/app/mixins/toast-queue.js +77 -0
- package/dist/app/mixins/toast-stack.d.ts +20 -0
- package/dist/app/mixins/toast-stack.js +55 -0
- package/dist/app/optimistic-toggle.d.ts +48 -0
- package/dist/app/optimistic-toggle.js +38 -0
- package/dist/app/page-header.d.ts +71 -0
- package/dist/app/page-header.js +249 -0
- package/dist/app/range-picker.d.ts +102 -0
- package/dist/app/range-picker.js +118 -0
- package/dist/app/save-bar.d.ts +54 -0
- package/dist/app/save-bar.js +64 -0
- package/dist/app/save-status.d.ts +50 -0
- package/dist/app/save-status.js +54 -0
- package/dist/app/selection-bar.d.ts +101 -0
- package/dist/app/selection-bar.js +157 -0
- package/dist/app/settings-layout.d.ts +70 -0
- package/dist/app/settings-layout.js +112 -0
- package/dist/app/settings-section.d.ts +140 -0
- package/dist/app/settings-section.js +182 -0
- package/dist/app/skeleton.d.ts +47 -4
- package/dist/app/skeleton.js +149 -2
- package/dist/app/sparkline.d.ts +61 -0
- package/dist/app/sparkline.js +145 -0
- package/dist/app/split-view.d.ts +154 -0
- package/dist/app/split-view.js +272 -0
- package/dist/app/stat-card.d.ts +109 -0
- package/dist/app/stat-card.js +379 -0
- package/dist/app/status-strip.d.ts +70 -0
- package/dist/app/status-strip.js +155 -0
- package/dist/app/styles/action-row.d.ts +41 -0
- package/dist/app/styles/action-row.js +59 -0
- package/dist/app/styles/stat-delta.d.ts +29 -0
- package/dist/app/styles/stat-delta.js +36 -0
- package/dist/app/styles/toast-stack.d.ts +21 -0
- package/dist/app/styles/toast-stack.js +54 -0
- package/dist/app/table.d.ts +199 -24
- package/dist/app/table.js +640 -36
- package/dist/app/toast.d.ts +105 -5
- package/dist/app/toast.js +89 -17
- package/dist/app/toasts.d.ts +49 -0
- package/dist/app/toasts.js +103 -0
- package/dist/app/utils/app-shell-cookie.d.ts +21 -0
- package/dist/app/utils/app-shell-cookie.js +33 -0
- package/dist/app/utils/chart-marker-indices.d.ts +9 -5
- package/dist/app/utils/chart-marker-indices.js +9 -8
- package/dist/app/utils/chart-path.d.ts +14 -10
- package/dist/app/utils/chart-path.js +46 -18
- package/dist/app/utils/edited-time.d.ts +19 -0
- package/dist/app/utils/edited-time.js +35 -0
- package/dist/app/utils/key-target.d.ts +23 -0
- package/dist/app/utils/key-target.js +48 -0
- package/dist/app/utils/search-params.d.ts +69 -0
- package/dist/app/utils/search-params.js +132 -0
- package/dist/app/utils/shortcut-groups.d.ts +21 -0
- package/dist/app/utils/shortcut-groups.js +53 -0
- package/dist/app/utils/split-view-layout.d.ts +48 -0
- package/dist/app/utils/split-view-layout.js +71 -0
- package/dist/app/view-options.d.ts +99 -0
- package/dist/app/view-options.js +182 -0
- package/dist/components/banner.d.ts +61 -0
- package/dist/components/banner.js +101 -0
- package/dist/components/confirm.d.ts +54 -5
- package/dist/components/confirm.js +44 -5
- package/dist/components/group.d.ts +14 -0
- package/dist/components/group.js +66 -11
- package/dist/components/index.d.ts +5 -0
- package/dist/components/index.js +5 -0
- package/dist/components/input.js +9 -3
- package/dist/components/link-button.js +8 -2
- package/dist/components/property-list.d.ts +76 -0
- package/dist/components/property-list.js +175 -0
- package/dist/components/relative-time.d.ts +72 -0
- package/dist/components/relative-time.js +45 -0
- package/dist/components/status-dot.d.ts +62 -0
- package/dist/components/status-dot.js +99 -0
- package/dist/components/text-field.d.ts +17 -2
- package/dist/components/text-field.js +13 -4
- package/dist/components/timeline.d.ts +67 -0
- package/dist/components/timeline.js +159 -0
- package/dist/components/typeset.d.ts +2 -1
- package/dist/components/typeset.js +4 -1
- package/dist/docs/code-tabs-sync.d.ts +1 -1
- package/dist/docs/code-tabs-sync.js +2 -2
- package/dist/docs/copy-button.d.ts +1 -1
- package/dist/docs/copy-button.js +2 -2
- package/dist/docs/docs-feedback.d.ts +12 -0
- package/dist/docs/docs-feedback.js +4 -2
- package/dist/docs/docs-hub.js +4 -4
- package/dist/docs/docs-layout.d.ts +1 -1
- package/dist/docs/docs-layout.js +1 -1
- package/dist/docs/docs-nav-tools.d.ts +11 -5
- package/dist/docs/docs-nav-tools.js +5 -4
- package/dist/docs/docs-nav.d.ts +12 -5
- package/dist/docs/docs-nav.js +20 -8
- package/dist/docs/docs-pager.d.ts +5 -1
- package/dist/docs/docs-pager.js +7 -6
- package/dist/docs/mixins/docs-nav-tools.d.ts +3 -3
- package/dist/docs/mixins/docs-nav-tools.js +42 -17
- package/dist/docs/next-steps.js +3 -2
- package/dist/docs/page-actions.d.ts +1 -1
- package/dist/docs/page-actions.js +2 -2
- package/dist/docs/table-of-contents.d.ts +1 -1
- package/dist/docs/table-of-contents.js +2 -2
- package/dist/docs/utils/docs-filter.d.ts +26 -0
- package/dist/docs/utils/docs-filter.js +53 -0
- package/dist/mixins/confirm-phrase.d.ts +19 -0
- package/dist/mixins/confirm-phrase.js +56 -0
- package/dist/mixins/index.d.ts +2 -0
- package/dist/mixins/index.js +2 -0
- package/dist/mixins/viewer-time.d.ts +20 -0
- package/dist/mixins/viewer-time.js +40 -0
- package/dist/styles/index.d.ts +1 -0
- package/dist/styles/index.js +1 -0
- package/dist/styles/segmented-control.d.ts +49 -0
- package/dist/styles/segmented-control.js +71 -0
- package/dist/test/table.d.ts +65 -0
- package/dist/test/table.js +108 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/relative-time.d.ts +44 -0
- package/dist/utils/relative-time.js +69 -0
- package/package.json +6 -6
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Why JS: a button cannot disable itself until a field matches a phrase, so script keeps
|
|
3
|
+
* a type-to-confirm dialog's confirming control disabled until the text matches exactly.
|
|
4
|
+
* No-JS baseline: the field's `pattern` and `required` make the browser refuse a mismatch.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { MixinFactory } from "remix/component";
|
|
10
|
+
/** Attribute marking a dialog's confirming control, which {@link confirmPhrase} enables on a match. */
|
|
11
|
+
export declare const CONFIRM_ACTION_ATTRIBUTE = "data-confirm-action";
|
|
12
|
+
/**
|
|
13
|
+
* Applied to a type-to-confirm field: enables the enclosing dialog's
|
|
14
|
+
* `[data-confirm-action]` control exactly while the field holds `phrase`, and clears the
|
|
15
|
+
* field when the dialog closes so every opening starts locked.
|
|
16
|
+
*
|
|
17
|
+
* @example <Input pattern={escaped} required mix={[confirmPhrase("acme-ops")]} />
|
|
18
|
+
*/
|
|
19
|
+
export declare const confirmPhrase: MixinFactory<HTMLInputElement, [phrase: string]>;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Why JS: a button cannot disable itself until a field matches a phrase, so script keeps
|
|
3
|
+
* a type-to-confirm dialog's confirming control disabled until the text matches exactly.
|
|
4
|
+
* No-JS baseline: the field's `pattern` and `required` make the browser refuse a mismatch.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { createMixin } from "remix/component";
|
|
10
|
+
/** Attribute marking a dialog's confirming control, which {@link confirmPhrase} enables on a match. */
|
|
11
|
+
export const CONFIRM_ACTION_ATTRIBUTE = "data-confirm-action";
|
|
12
|
+
/**
|
|
13
|
+
* Applied to a type-to-confirm field: enables the enclosing dialog's
|
|
14
|
+
* `[data-confirm-action]` control exactly while the field holds `phrase`, and clears the
|
|
15
|
+
* field when the dialog closes so every opening starts locked.
|
|
16
|
+
*
|
|
17
|
+
* @example <Input pattern={escaped} required mix={[confirmPhrase("acme-ops")]} />
|
|
18
|
+
*/
|
|
19
|
+
export const confirmPhrase = createMixin((handle) => {
|
|
20
|
+
let phrase = "";
|
|
21
|
+
let controller;
|
|
22
|
+
handle.addEventListener("insert", (event) => {
|
|
23
|
+
controller?.abort();
|
|
24
|
+
controller = new AbortController();
|
|
25
|
+
connect(event.node, () => phrase, controller.signal);
|
|
26
|
+
});
|
|
27
|
+
handle.addEventListener("remove", () => {
|
|
28
|
+
controller?.abort();
|
|
29
|
+
controller = undefined;
|
|
30
|
+
});
|
|
31
|
+
return (next) => {
|
|
32
|
+
phrase = next;
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
/**
|
|
36
|
+
* Locks `input`'s dialog until `signal` aborts.
|
|
37
|
+
*
|
|
38
|
+
* @param input The phrase field.
|
|
39
|
+
* @param phrase Reads the phrase to match, which may change between renders.
|
|
40
|
+
* @param signal Ends every listener once the field leaves the document.
|
|
41
|
+
*/
|
|
42
|
+
function connect(input, phrase, signal) {
|
|
43
|
+
let dialog = input.closest("dialog");
|
|
44
|
+
let action = dialog?.querySelector(`[${CONFIRM_ACTION_ATTRIBUTE}]`);
|
|
45
|
+
/** Enables the confirming control exactly while the field holds the phrase. */
|
|
46
|
+
let sync = () => {
|
|
47
|
+
if (action)
|
|
48
|
+
action.disabled = input.value !== phrase();
|
|
49
|
+
};
|
|
50
|
+
input.addEventListener("input", sync, { signal });
|
|
51
|
+
dialog?.addEventListener("close", () => {
|
|
52
|
+
input.value = "";
|
|
53
|
+
sync();
|
|
54
|
+
}, { signal });
|
|
55
|
+
sync();
|
|
56
|
+
}
|
package/dist/mixins/index.d.ts
CHANGED
package/dist/mixins/index.js
CHANGED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Why JS: a server cannot know the viewer's time zone or clock, so a relative time it renders
|
|
3
|
+
* names the server's zone. No-JS baseline: the server's text and title stand, readable and
|
|
4
|
+
* correct in the zone they name.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { ElementProps, MixinFactory } from "remix/component";
|
|
10
|
+
/** Arguments of {@link viewerTime}: the locale, then the reference moment the text counts from. */
|
|
11
|
+
type ViewerTimeArgs = [locale: string | undefined, now?: string | number];
|
|
12
|
+
/**
|
|
13
|
+
* Rewrites the host `<time>` from its own `datetime` once it is live in a document, and after
|
|
14
|
+
* every update around it: the date and title in the viewer's zone, the text against `now` when
|
|
15
|
+
* given and the viewer's clock otherwise, so a fixed reference reads the same on every machine.
|
|
16
|
+
*
|
|
17
|
+
* @example <time dateTime={iso} mix={[viewerTime(locale, now)]}>{serverText}</time>
|
|
18
|
+
*/
|
|
19
|
+
export declare const viewerTime: MixinFactory<HTMLTimeElement, ViewerTimeArgs, ElementProps>;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Why JS: a server cannot know the viewer's time zone or clock, so a relative time it renders
|
|
3
|
+
* names the server's zone. No-JS baseline: the server's text and title stand, readable and
|
|
4
|
+
* correct in the zone they name.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { createMixin } from "remix/component";
|
|
10
|
+
import { formatRelativeTime } from "../utils/relative-time.js";
|
|
11
|
+
/**
|
|
12
|
+
* Rewrites the host `<time>` from its own `datetime` once it is live in a document, and after
|
|
13
|
+
* every update around it: the date and title in the viewer's zone, the text against `now` when
|
|
14
|
+
* given and the viewer's clock otherwise, so a fixed reference reads the same on every machine.
|
|
15
|
+
*
|
|
16
|
+
* @example <time dateTime={iso} mix={[viewerTime(locale, now)]}>{serverText}</time>
|
|
17
|
+
*/
|
|
18
|
+
export const viewerTime = createMixin((handle) => {
|
|
19
|
+
let locale;
|
|
20
|
+
let now;
|
|
21
|
+
/** Formats the host's own `datetime` in the viewer's zone, against `now` or their clock. */
|
|
22
|
+
function rewrite(node) {
|
|
23
|
+
let formatted = formatRelativeTime(node.dateTime, {
|
|
24
|
+
now,
|
|
25
|
+
locale,
|
|
26
|
+
timeZone: new Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
27
|
+
});
|
|
28
|
+
if (!formatted.dateTime)
|
|
29
|
+
return;
|
|
30
|
+
node.textContent = formatted.text;
|
|
31
|
+
node.title = formatted.title;
|
|
32
|
+
}
|
|
33
|
+
handle.addEventListener("insert", (event) => rewrite(event.node));
|
|
34
|
+
handle.addEventListener("commit", (event) => rewrite(event.node));
|
|
35
|
+
return (nextLocale, nextNow) => {
|
|
36
|
+
locale = nextLocale;
|
|
37
|
+
now = nextNow;
|
|
38
|
+
return handle.element;
|
|
39
|
+
};
|
|
40
|
+
}, { ssr: false });
|
package/dist/styles/index.d.ts
CHANGED
|
@@ -15,5 +15,6 @@ export * from "./landing.js";
|
|
|
15
15
|
export * from "./output-caption-text.js";
|
|
16
16
|
export * from "./panel-chrome.js";
|
|
17
17
|
export * from "./range-thumb-appearance.js";
|
|
18
|
+
export * from "./segmented-control.js";
|
|
18
19
|
export * from "./semantic-color-panel.js";
|
|
19
20
|
export * from "./surface-control.js";
|
package/dist/styles/index.js
CHANGED
|
@@ -15,5 +15,6 @@ export * from "./landing.js";
|
|
|
15
15
|
export * from "./output-caption-text.js";
|
|
16
16
|
export * from "./panel-chrome.js";
|
|
17
17
|
export * from "./range-thumb-appearance.js";
|
|
18
|
+
export * from "./segmented-control.js";
|
|
18
19
|
export * from "./semantic-color-panel.js";
|
|
19
20
|
export * from "./surface-control.js";
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `segmentedTrack()` and `segmentedItem()` style-mixin factories: the recessed track of a
|
|
3
|
+
* segmented control and the segments inside it, with the chosen segment raised on a filled,
|
|
4
|
+
* edged tile in heavier ink, so the choice reads at a glance in light and dark.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { UtilityMixin } from "@sdxc/u";
|
|
10
|
+
/**
|
|
11
|
+
* Options accepted by {@link segmentedItem}.
|
|
12
|
+
*/
|
|
13
|
+
export interface SegmentedItemOptions {
|
|
14
|
+
/**
|
|
15
|
+
* Selector, relative to the segment, that matches while it is the chosen one.
|
|
16
|
+
*
|
|
17
|
+
* @default '&[aria-pressed="true"]'
|
|
18
|
+
*/
|
|
19
|
+
selected?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Selector, relative to the segment, that matches while keyboard focus is on it.
|
|
22
|
+
*
|
|
23
|
+
* @default "&:focus-visible"
|
|
24
|
+
*/
|
|
25
|
+
focused?: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Draws the track the segments sit in: a recessed tint with a hairline edge and an inner
|
|
29
|
+
* padding that leaves room for the chosen segment's edge and the focus ring.
|
|
30
|
+
*
|
|
31
|
+
* @returns A `css()` mixin ready for the track's `mix` prop.
|
|
32
|
+
* @example
|
|
33
|
+
* <div role="group" aria-label={t("range")} mix={[segmentedTrack()]}>{segments}</div>;
|
|
34
|
+
*/
|
|
35
|
+
export declare function segmentedTrack<Node extends Element = Element>(): UtilityMixin<Node>;
|
|
36
|
+
/**
|
|
37
|
+
* Draws one segment. A resting segment is muted ink on the track and takes a translucent
|
|
38
|
+
* wash on hover; the chosen one is a raised tile edged in the strong border role, which
|
|
39
|
+
* keeps at least 3:1 against the track in either scheme, with emphasis ink in semibold.
|
|
40
|
+
* The states compose into one rule, so the chosen state wins whatever order layers land in.
|
|
41
|
+
*
|
|
42
|
+
* @param options Selectors for the chosen and focused states.
|
|
43
|
+
* @returns A `css()` mixin ready for the segment's `mix` prop.
|
|
44
|
+
* @example
|
|
45
|
+
* <button aria-pressed={active ? "true" : "false"} mix={[segmentedItem()]}>{label}</button>;
|
|
46
|
+
* @example
|
|
47
|
+
* <label mix={[segmentedItem({ selected: "&:has(input:checked)", focused: "&:has(input:focus-visible)" })]} />;
|
|
48
|
+
*/
|
|
49
|
+
export declare function segmentedItem<Node extends Element = Element>(options?: SegmentedItemOptions): UtilityMixin<Node>;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `segmentedTrack()` and `segmentedItem()` style-mixin factories: the recessed track of a
|
|
3
|
+
* segmented control and the segments inside it, with the chosen segment raised on a filled,
|
|
4
|
+
* edged tile in heavier ink, so the choice reads at a glance in light and dark.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { combine, raw } from "@sdxc/u/general";
|
|
10
|
+
import { when } from "@sdxc/u/state";
|
|
11
|
+
/** Selected-state selector for a segment that is itself a toggle button. */
|
|
12
|
+
const DEFAULT_SELECTED = '&[aria-pressed="true"]';
|
|
13
|
+
/** Focus selector for a segment that takes focus itself. */
|
|
14
|
+
const DEFAULT_FOCUSED = "&:focus-visible";
|
|
15
|
+
/**
|
|
16
|
+
* Draws the track the segments sit in: a recessed tint with a hairline edge and an inner
|
|
17
|
+
* padding that leaves room for the chosen segment's edge and the focus ring.
|
|
18
|
+
*
|
|
19
|
+
* @returns A `css()` mixin ready for the track's `mix` prop.
|
|
20
|
+
* @example
|
|
21
|
+
* <div role="group" aria-label={t("range")} mix={[segmentedTrack()]}>{segments}</div>;
|
|
22
|
+
*/
|
|
23
|
+
export function segmentedTrack() {
|
|
24
|
+
return raw({
|
|
25
|
+
display: "inline-flex",
|
|
26
|
+
alignItems: "center",
|
|
27
|
+
gap: "0.125rem",
|
|
28
|
+
padding: "0.125rem",
|
|
29
|
+
borderRadius: "var(--ui-radius-md, 0.375rem)",
|
|
30
|
+
backgroundColor: "var(--ui-neutral-bg-tint-hover, Canvas)",
|
|
31
|
+
border: "1px solid var(--ui-neutral-border, GrayText)",
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Draws one segment. A resting segment is muted ink on the track and takes a translucent
|
|
36
|
+
* wash on hover; the chosen one is a raised tile edged in the strong border role, which
|
|
37
|
+
* keeps at least 3:1 against the track in either scheme, with emphasis ink in semibold.
|
|
38
|
+
* The states compose into one rule, so the chosen state wins whatever order layers land in.
|
|
39
|
+
*
|
|
40
|
+
* @param options Selectors for the chosen and focused states.
|
|
41
|
+
* @returns A `css()` mixin ready for the segment's `mix` prop.
|
|
42
|
+
* @example
|
|
43
|
+
* <button aria-pressed={active ? "true" : "false"} mix={[segmentedItem()]}>{label}</button>;
|
|
44
|
+
* @example
|
|
45
|
+
* <label mix={[segmentedItem({ selected: "&:has(input:checked)", focused: "&:has(input:focus-visible)" })]} />;
|
|
46
|
+
*/
|
|
47
|
+
export function segmentedItem(options = {}) {
|
|
48
|
+
let selected = options.selected ?? DEFAULT_SELECTED;
|
|
49
|
+
let focused = options.focused ?? DEFAULT_FOCUSED;
|
|
50
|
+
return combine([
|
|
51
|
+
raw({
|
|
52
|
+
border: "1px solid transparent",
|
|
53
|
+
borderRadius: "var(--ui-radius-sm, 0.25rem)",
|
|
54
|
+
backgroundColor: "transparent",
|
|
55
|
+
color: "var(--ui-neutral-fg-muted, GrayText)",
|
|
56
|
+
fontWeight: "500",
|
|
57
|
+
"&:hover": {
|
|
58
|
+
backgroundColor: "color-mix(in oklch, var(--ui-neutral-fg-emphasis, CanvasText) 7%, transparent)",
|
|
59
|
+
color: "var(--ui-neutral-fg-emphasis, CanvasText)",
|
|
60
|
+
},
|
|
61
|
+
}),
|
|
62
|
+
when(selected, raw({
|
|
63
|
+
backgroundColor: "light-dark(var(--ui-surface-raised, Canvas), var(--ui-neutral-bg-tint-pressed, Canvas))",
|
|
64
|
+
borderColor: "var(--ui-neutral-border-strong, CanvasText)",
|
|
65
|
+
color: "var(--ui-neutral-fg-emphasis, CanvasText)",
|
|
66
|
+
fontWeight: "600",
|
|
67
|
+
boxShadow: "0 1px 2px 0 rgb(0 0 0 / 0.12)",
|
|
68
|
+
})),
|
|
69
|
+
when(focused, raw({ outline: "2px solid var(--ui-brand-ring, Highlight)", outlineOffset: "1px" })),
|
|
70
|
+
]);
|
|
71
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tables the table's browser tests mount, on a hovering pointer and on touch: monitors, long
|
|
3
|
+
* posts and articles with row actions, and the measurements those tests share, so both test
|
|
4
|
+
* files assert against the same rows.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { Table } from "../app/table.js";
|
|
10
|
+
/** One monitor row, enough columns to overflow a phone. */
|
|
11
|
+
export interface Monitor {
|
|
12
|
+
id: string;
|
|
13
|
+
name: string;
|
|
14
|
+
region: string;
|
|
15
|
+
ms: number;
|
|
16
|
+
}
|
|
17
|
+
/** Twelve monitors, enough rows to scroll a container and overflow a phone. */
|
|
18
|
+
export declare const MONITORS: Monitor[];
|
|
19
|
+
/** Mounts `node` in a host `width` pixels wide and returns the host. */
|
|
20
|
+
export declare function mount(node: JSX.Element, width?: number): HTMLElement;
|
|
21
|
+
/** A monitors table with selection, a row link and actions, in a container `height` tall. */
|
|
22
|
+
export declare function monitors(props?: Partial<Table.Props>, height?: number): JSX.Element;
|
|
23
|
+
/** A post with a title long enough to squeeze every other column. */
|
|
24
|
+
export interface Post {
|
|
25
|
+
id: string;
|
|
26
|
+
title: string;
|
|
27
|
+
excerpt: string;
|
|
28
|
+
}
|
|
29
|
+
/** Four posts whose long titles and excerpts squeeze the actions column. */
|
|
30
|
+
export declare const POSTS: Post[];
|
|
31
|
+
/** Labels of the three row actions every post carries. */
|
|
32
|
+
export declare const ACTIONS: string[];
|
|
33
|
+
/**
|
|
34
|
+
* A comfortable posts table whose title and excerpt compete with the actions for width. With
|
|
35
|
+
* `links`, the actions are plain ghost link buttons; otherwise they are `Table.Action`s that
|
|
36
|
+
* fold into a menu named "More actions".
|
|
37
|
+
*/
|
|
38
|
+
export declare function posts(options?: {
|
|
39
|
+
links?: boolean;
|
|
40
|
+
actions?: string[];
|
|
41
|
+
noActions?: boolean;
|
|
42
|
+
date?: boolean;
|
|
43
|
+
}): JSX.Element;
|
|
44
|
+
/** Number of line boxes the text of `element` lays out in. */
|
|
45
|
+
export declare function lineBoxes(element: Element): number;
|
|
46
|
+
/** Asserts every action label in `host` sits on one line and its cell hugs the actions. */
|
|
47
|
+
export declare function expectOneLineActions(host: HTMLElement, labels?: string[]): void;
|
|
48
|
+
/** Four row actions, enough to crowd the visible columns if their cell reserved their width. */
|
|
49
|
+
export declare const MANY_ACTIONS: string[];
|
|
50
|
+
/** Labels of the overlay's three actions: two ghost links and a danger `Table.Action`. */
|
|
51
|
+
export declare const OVERLAY_ACTIONS: string[];
|
|
52
|
+
/**
|
|
53
|
+
* A four-column articles table whose last data cell is a "Published" status, with the three
|
|
54
|
+
* actions an articles list carries: ghost link buttons and a danger `Table.Action`, or with
|
|
55
|
+
* `actions`, three `Table.Action`s under an `overflowLabel`.
|
|
56
|
+
*/
|
|
57
|
+
export declare function articles(options?: {
|
|
58
|
+
actions?: boolean;
|
|
59
|
+
}): JSX.Element;
|
|
60
|
+
/** The controls inside `root` that lay out a box. */
|
|
61
|
+
export declare function shown(root: Element): HTMLElement[];
|
|
62
|
+
/** Accessible name `control` takes from `aria-label`, else from the elements `aria-labelledby` names. */
|
|
63
|
+
export declare function accessibleName(control: HTMLElement): string;
|
|
64
|
+
/** Whether two boxes share any area. */
|
|
65
|
+
export declare function intersects(a: DOMRect, b: DOMRect): boolean;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "remix/component/jsx-runtime";
|
|
2
|
+
import { render } from "remix/component/test";
|
|
3
|
+
import { expect } from "vitest";
|
|
4
|
+
import { Table } from "../app/table.js";
|
|
5
|
+
import { LinkButton } from "../components/link-button.js";
|
|
6
|
+
/** Twelve monitors, enough rows to scroll a container and overflow a phone. */
|
|
7
|
+
export const MONITORS = Array.from({ length: 12 }, (_, index) => ({
|
|
8
|
+
id: `mon_${index}`,
|
|
9
|
+
name: `api-${index}.example.com`,
|
|
10
|
+
region: "Frankfurt, Germany",
|
|
11
|
+
ms: 100 + index * 7,
|
|
12
|
+
}));
|
|
13
|
+
/** Mounts `node` in a host `width` pixels wide and returns the host. */
|
|
14
|
+
export function mount(node, width = 720) {
|
|
15
|
+
let host = document.createElement("div");
|
|
16
|
+
host.style.width = `${width}px`;
|
|
17
|
+
document.body.appendChild(host);
|
|
18
|
+
render(node, { container: host });
|
|
19
|
+
return host;
|
|
20
|
+
}
|
|
21
|
+
/** A monitors table with selection, a row link and actions, in a container `height` tall. */
|
|
22
|
+
export function monitors(props = {}, height) {
|
|
23
|
+
return (_jsx(Table.Container, { mix: [], style: height ? { maxHeight: `${height}px` } : undefined, children: _jsxs(Table, { "aria-label": "Monitors", ...props, children: [_jsx(Table.Header, { children: _jsxs(Table.Row, { children: [_jsx(Table.SelectAll, { label: "Select all monitors" }), _jsx(Table.Column, { children: "Name" }), _jsx(Table.Column, { children: "Region" }), _jsx(Table.Column, { numeric: true, children: "Response" }), _jsx(Table.ActionsColumn, { label: "Actions" })] }) }), _jsx(Table.Body, { children: MONITORS.map((monitor) => (_jsxs(Table.Row, { children: [_jsx(Table.Select, { name: "monitor", value: monitor.id, label: `Select ${monitor.name}` }), _jsx(Table.Cell, { children: _jsx(Table.RowLink, { href: `#${monitor.id}`, children: monitor.name }) }), _jsx(Table.Cell, { children: monitor.region }), _jsx(Table.NumberCell, { value: monitor.ms, locale: "en", format: { style: "unit", unit: "millisecond" } }), _jsxs(Table.Actions, { children: [_jsx("button", { type: "button", children: "Pause" }), _jsx("button", { type: "button", children: "Delete" })] })] }, monitor.id))) })] }) }));
|
|
24
|
+
}
|
|
25
|
+
/** Four posts whose long titles and excerpts squeeze the actions column. */
|
|
26
|
+
export const POSTS = Array.from({ length: 4 }, (_, index) => ({
|
|
27
|
+
id: `post_${index}`,
|
|
28
|
+
title: `Designing a publishing workflow that survives a newsroom deadline, part ${index + 1}`,
|
|
29
|
+
excerpt: "How a small editorial team drafts, reviews, schedules and corrects stories without stepping on each other.",
|
|
30
|
+
}));
|
|
31
|
+
/** Labels of the three row actions every post carries. */
|
|
32
|
+
export const ACTIONS = ["Edit", "Quick edit", "View"];
|
|
33
|
+
/**
|
|
34
|
+
* One post's actions cell: plain ghost link buttons with `links`, else `Table.Action`s that fold
|
|
35
|
+
* into a menu named "More actions".
|
|
36
|
+
*/
|
|
37
|
+
function PostActions(handle) {
|
|
38
|
+
return () => {
|
|
39
|
+
let { id, labels, links } = handle.props;
|
|
40
|
+
if (links) {
|
|
41
|
+
return (_jsx(Table.Actions, { children: labels.map((label) => (_jsx(LinkButton, { href: `#${id}/${label}`, variant: "ghost", color: "neutral", size: "sm", children: label }, label))) }));
|
|
42
|
+
}
|
|
43
|
+
return (_jsx(Table.Actions, { overflowLabel: "More actions", children: labels.map((label) => (_jsx(Table.Action, { href: `#${id}/${label}`, children: label }, label))) }));
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* A comfortable posts table whose title and excerpt compete with the actions for width. With
|
|
48
|
+
* `links`, the actions are plain ghost link buttons; otherwise they are `Table.Action`s that
|
|
49
|
+
* fold into a menu named "More actions".
|
|
50
|
+
*/
|
|
51
|
+
export function posts(options = {}) {
|
|
52
|
+
let actions = options.actions ?? ACTIONS;
|
|
53
|
+
return (_jsx(Table.Container, { children: _jsxs(Table, { "aria-label": "Posts", density: "comfortable", children: [_jsx(Table.Header, { children: _jsxs(Table.Row, { children: [_jsx(Table.SelectAll, { label: "Select all posts" }), _jsx(Table.Column, { children: "Title" }), _jsx(Table.Column, { children: "Excerpt" }), _jsx(Table.Column, { children: "Status" }), options.date ? _jsx(Table.Column, { children: "Published" }) : null, options.noActions ? null : _jsx(Table.ActionsColumn, { label: "Actions" })] }) }), _jsx(Table.Body, { children: POSTS.map((post) => (_jsxs(Table.Row, { children: [_jsx(Table.Select, { name: "post", value: post.id, label: `Select ${post.title}` }), _jsx(Table.Cell, { children: _jsx(Table.RowLink, { href: `#${post.id}`, children: post.title }) }), _jsx(Table.Cell, { children: post.excerpt }), _jsx(Table.StatusCell, { color: "success", children: "Published" }), options.date ? (_jsx(Table.DateCell, { date: "2026-12-01T12:00:00Z", now: "2026-12-20T12:00:00Z", locale: "en-US", local: true })) : null, options.noActions ? null : (_jsx(PostActions, { id: post.id, labels: actions, links: options.links }))] }, post.id))) })] }) }));
|
|
54
|
+
}
|
|
55
|
+
/** Number of line boxes the text of `element` lays out in. */
|
|
56
|
+
export function lineBoxes(element) {
|
|
57
|
+
let range = document.createRange();
|
|
58
|
+
range.selectNodeContents(element);
|
|
59
|
+
let tops = new Set([...range.getClientRects()].map((rect) => Math.round(rect.top)));
|
|
60
|
+
return tops.size;
|
|
61
|
+
}
|
|
62
|
+
/** Asserts every action label in `host` sits on one line and its cell hugs the actions. */
|
|
63
|
+
export function expectOneLineActions(host, labels = ACTIONS) {
|
|
64
|
+
let cells = [...host.querySelectorAll('tbody [data-slot="row-actions"]')];
|
|
65
|
+
expect(cells).toHaveLength(POSTS.length);
|
|
66
|
+
for (let cell of cells) {
|
|
67
|
+
let actions = [...cell.querySelectorAll("a")].filter((action) => action.getClientRects().length > 0);
|
|
68
|
+
expect(actions.map((action) => action.textContent)).toEqual(labels);
|
|
69
|
+
for (let action of actions)
|
|
70
|
+
expect(lineBoxes(action)).toBe(1);
|
|
71
|
+
let first = actions[0].getBoundingClientRect();
|
|
72
|
+
let last = actions.at(-1).getBoundingClientRect();
|
|
73
|
+
let style = getComputedStyle(cell);
|
|
74
|
+
let padding = parseFloat(style.paddingInlineStart) + parseFloat(style.paddingInlineEnd);
|
|
75
|
+
expect(cell.getBoundingClientRect().width).toBeLessThanOrEqual(last.right - first.left + padding + 1);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/** Four row actions, enough to crowd the visible columns if their cell reserved their width. */
|
|
79
|
+
export const MANY_ACTIONS = ["Edit", "Quick edit", "View", "Duplicate"];
|
|
80
|
+
/** Labels of the overlay's three actions: two ghost links and a danger `Table.Action`. */
|
|
81
|
+
export const OVERLAY_ACTIONS = ["View", "Post on X", "Delete"];
|
|
82
|
+
/**
|
|
83
|
+
* A four-column articles table whose last data cell is a "Published" status, with the three
|
|
84
|
+
* actions an articles list carries: ghost link buttons and a danger `Table.Action`, or with
|
|
85
|
+
* `actions`, three `Table.Action`s under an `overflowLabel`.
|
|
86
|
+
*/
|
|
87
|
+
export function articles(options = {}) {
|
|
88
|
+
return (_jsx(Table.Container, { children: _jsxs(Table, { "aria-label": "Articles", density: "comfortable", children: [_jsx(Table.Header, { children: _jsxs(Table.Row, { children: [_jsx(Table.Column, { children: "Title" }), _jsx(Table.Column, { children: "Author" }), _jsx(Table.Column, { children: "Status" }), _jsx(Table.ActionsColumn, { label: "Actions" })] }) }), _jsx(Table.Body, { children: POSTS.map((post) => (_jsxs(Table.Row, { children: [_jsx(Table.Cell, { children: _jsx(Table.RowLink, { href: `#${post.id}`, children: post.title }) }), _jsx(Table.Cell, { children: "Sergio" }), _jsx(Table.StatusCell, { color: "success", children: "Published" }), options.actions ? (_jsxs(Table.Actions, { overflowLabel: `More actions for ${post.title}`, children: [_jsx(Table.Action, { href: `#${post.id}/view`, children: OVERLAY_ACTIONS[0] }), _jsx(Table.Action, { href: `#${post.id}/share`, children: OVERLAY_ACTIONS[1] }), _jsx(Table.Action, { danger: true, children: OVERLAY_ACTIONS[2] })] })) : (_jsxs(Table.Actions, { children: [_jsx(LinkButton, { href: `#${post.id}/view`, variant: "ghost", color: "neutral", size: "sm", children: OVERLAY_ACTIONS[0] }), _jsx(LinkButton, { href: `#${post.id}/share`, variant: "ghost", color: "neutral", size: "sm", children: OVERLAY_ACTIONS[1] }), _jsx(Table.Action, { danger: true, children: OVERLAY_ACTIONS[2] })] }))] }, post.id))) })] }) }));
|
|
89
|
+
}
|
|
90
|
+
/** The controls inside `root` that lay out a box. */
|
|
91
|
+
export function shown(root) {
|
|
92
|
+
return [...root.querySelectorAll("a, button")].filter((control) => control.getClientRects().length > 0);
|
|
93
|
+
}
|
|
94
|
+
/** Accessible name `control` takes from `aria-label`, else from the elements `aria-labelledby` names. */
|
|
95
|
+
export function accessibleName(control) {
|
|
96
|
+
let label = control.getAttribute("aria-label");
|
|
97
|
+
if (label)
|
|
98
|
+
return label;
|
|
99
|
+
let ids = control.getAttribute("aria-labelledby")?.split(/\s+/) ?? [];
|
|
100
|
+
return ids
|
|
101
|
+
.map((id) => document.getElementById(id)?.textContent ?? "")
|
|
102
|
+
.join(" ")
|
|
103
|
+
.trim();
|
|
104
|
+
}
|
|
105
|
+
/** Whether two boxes share any area. */
|
|
106
|
+
export function intersects(a, b) {
|
|
107
|
+
return a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom;
|
|
108
|
+
}
|
package/dist/utils/index.d.ts
CHANGED
package/dist/utils/index.js
CHANGED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a moment as the relative text, machine instant and absolute title a relative time
|
|
3
|
+
* shows, through `Intl` alone. Kept framework-free so the server component and the browser
|
|
4
|
+
* mixin that rewrites it in the viewer's zone produce the same strings from the same input.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Types for {@link formatRelativeTime}.
|
|
11
|
+
*/
|
|
12
|
+
export declare namespace RelativeTimeFormat {
|
|
13
|
+
/** A moment as a `Date`, an ISO 8601 string or epoch milliseconds. */
|
|
14
|
+
type DateInput = Date | string | number;
|
|
15
|
+
/** What a moment renders as, given a reference time, locale and zone. */
|
|
16
|
+
interface Formatted {
|
|
17
|
+
/** The machine-readable instant for `<time datetime>`, empty when the input does not parse. */
|
|
18
|
+
dateTime: string;
|
|
19
|
+
/** The visible text: relative within a week, a medium date after it. */
|
|
20
|
+
text: string;
|
|
21
|
+
/** The absolute time in the formatting zone, then in UTC when the two differ, one per line. */
|
|
22
|
+
title: string;
|
|
23
|
+
}
|
|
24
|
+
/** Options for {@link formatRelativeTime}. */
|
|
25
|
+
interface Options {
|
|
26
|
+
/** The reference moment "ago" counts from. Defaults to the current time. */
|
|
27
|
+
now?: DateInput;
|
|
28
|
+
/** BCP 47 locale for every string. Defaults to the runtime's locale. */
|
|
29
|
+
locale?: string;
|
|
30
|
+
/** IANA zone for the date and the title's first line. Defaults to {@link DEFAULT_TIME_ZONE}. */
|
|
31
|
+
timeZone?: string;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Formats `date` as a relative time shows it, for any part that needs the text without the
|
|
36
|
+
* `<time>` element. Under a minute reads in seconds and "now" at zero, and future moments read "in 5m";
|
|
37
|
+
* anything a week or more away reads as a medium date in `timeZone`.
|
|
38
|
+
*
|
|
39
|
+
* @param date The moment to format.
|
|
40
|
+
* @param options Reference time, locale and zone.
|
|
41
|
+
* @returns The `datetime` value, the visible text and the title.
|
|
42
|
+
* @example formatRelativeTime("2026-11-01T08:00:00Z", { now: "2026-11-01T08:02:00Z", locale: "en" }).text // "2m ago"
|
|
43
|
+
*/
|
|
44
|
+
export declare function formatRelativeTime(date: RelativeTimeFormat.DateInput, options?: RelativeTimeFormat.Options): RelativeTimeFormat.Formatted;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a moment as the relative text, machine instant and absolute title a relative time
|
|
3
|
+
* shows, through `Intl` alone. Kept framework-free so the server component and the browser
|
|
4
|
+
* mixin that rewrites it in the viewer's zone produce the same strings from the same input.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
/** How long a time stays relative before it reads as a date: seven days, in milliseconds. */
|
|
10
|
+
const RELATIVE_LIMIT_MS = 7 * 24 * 60 * 60 * 1000;
|
|
11
|
+
/** Zone the server formats in when none is given, so every server renders the same text. */
|
|
12
|
+
const DEFAULT_TIME_ZONE = "UTC";
|
|
13
|
+
/** Text shown for a date that does not parse. */
|
|
14
|
+
const INVALID_TEXT = "—";
|
|
15
|
+
/** Units a relative time steps through, largest last, each with its length in milliseconds. */
|
|
16
|
+
const UNITS = [
|
|
17
|
+
["second", 1000],
|
|
18
|
+
["minute", 60 * 1000],
|
|
19
|
+
["hour", 60 * 60 * 1000],
|
|
20
|
+
["day", 24 * 60 * 60 * 1000],
|
|
21
|
+
];
|
|
22
|
+
/**
|
|
23
|
+
* Formats `date` as a relative time shows it, for any part that needs the text without the
|
|
24
|
+
* `<time>` element. Under a minute reads in seconds and "now" at zero, and future moments read "in 5m";
|
|
25
|
+
* anything a week or more away reads as a medium date in `timeZone`.
|
|
26
|
+
*
|
|
27
|
+
* @param date The moment to format.
|
|
28
|
+
* @param options Reference time, locale and zone.
|
|
29
|
+
* @returns The `datetime` value, the visible text and the title.
|
|
30
|
+
* @example formatRelativeTime("2026-11-01T08:00:00Z", { now: "2026-11-01T08:02:00Z", locale: "en" }).text // "2m ago"
|
|
31
|
+
*/
|
|
32
|
+
export function formatRelativeTime(date, options = {}) {
|
|
33
|
+
let moment = new Date(date);
|
|
34
|
+
if (Number.isNaN(moment.getTime()))
|
|
35
|
+
return { dateTime: "", text: INVALID_TEXT, title: "" };
|
|
36
|
+
let now = options.now === undefined ? new Date() : new Date(options.now);
|
|
37
|
+
let timeZone = options.timeZone ?? DEFAULT_TIME_ZONE;
|
|
38
|
+
let delta = moment.getTime() - now.getTime();
|
|
39
|
+
return {
|
|
40
|
+
dateTime: moment.toISOString(),
|
|
41
|
+
text: Math.abs(delta) < RELATIVE_LIMIT_MS
|
|
42
|
+
? relativeText(delta, options.locale)
|
|
43
|
+
: new Intl.DateTimeFormat(options.locale, { dateStyle: "medium", timeZone }).format(moment),
|
|
44
|
+
title: absoluteTitle(moment, options.locale, timeZone),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Picks the largest unit the distance fills at least once and truncates toward zero, so 59
|
|
49
|
+
* minutes reads "59m" until the hour has actually passed.
|
|
50
|
+
*/
|
|
51
|
+
function relativeText(delta, locale) {
|
|
52
|
+
let format = new Intl.RelativeTimeFormat(locale, { style: "narrow", numeric: "auto" });
|
|
53
|
+
let [unit, size] = UNITS[0];
|
|
54
|
+
for (let [candidate, length] of UNITS) {
|
|
55
|
+
if (Math.abs(delta) >= length)
|
|
56
|
+
[unit, size] = [candidate, length];
|
|
57
|
+
}
|
|
58
|
+
return format.format(Math.trunc(delta / size) || 0, unit);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The absolute time in `timeZone` with its zone named, then in UTC on a second line, which a
|
|
62
|
+
* title shows as a two-line tooltip. A UTC zone gives the one line.
|
|
63
|
+
*/
|
|
64
|
+
function absoluteTitle(moment, locale, timeZone) {
|
|
65
|
+
let options = { dateStyle: "medium", timeStyle: "long" };
|
|
66
|
+
let zoned = new Intl.DateTimeFormat(locale, { ...options, timeZone }).format(moment);
|
|
67
|
+
let utc = new Intl.DateTimeFormat(locale, { ...options, timeZone: "UTC" }).format(moment);
|
|
68
|
+
return zoned === utc ? utc : `${zoned}\n${utc}`;
|
|
69
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sdxc/ui",
|
|
3
|
-
"version": "2026.11.
|
|
3
|
+
"version": "2026.11.4",
|
|
4
4
|
"description": "Styled, accessible remix/component components rendered as server HTML",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -28,16 +28,16 @@
|
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@remix-run/ui": "0.12.1",
|
|
31
|
-
"@sdxc/highlight": "2026.11.
|
|
32
|
-
"@sdxc/icons": "2026.
|
|
31
|
+
"@sdxc/highlight": "2026.11.3",
|
|
32
|
+
"@sdxc/icons": "2026.11.3",
|
|
33
33
|
"@sdxc/iife": "2026.10.23",
|
|
34
|
-
"@sdxc/u": "2026.11.
|
|
35
|
-
"@sdxc/uuid": "2026.
|
|
34
|
+
"@sdxc/u": "2026.11.3",
|
|
35
|
+
"@sdxc/uuid": "2026.11.3"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
38
|
"remix": "3.1.0"
|
|
39
39
|
},
|
|
40
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "f9b4adfc3d85b98a3d628b69cc2e1b9c0b7af2d8",
|
|
41
41
|
"publishConfig": {
|
|
42
42
|
"access": "public"
|
|
43
43
|
},
|