@xerios/design-system 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +403 -2
- package/dist/components/AnchorHeading.svelte +79 -0
- package/dist/components/AnchorHeading.svelte.d.ts +28 -0
- package/dist/components/Breadcrumbs.svelte +216 -0
- package/dist/components/Breadcrumbs.svelte.d.ts +37 -0
- package/dist/components/Button.svelte +178 -0
- package/dist/components/Button.svelte.d.ts +58 -0
- package/dist/components/Canvas.svelte +248 -0
- package/dist/components/Canvas.svelte.d.ts +91 -0
- package/dist/components/Collapsible.svelte +139 -0
- package/dist/components/Collapsible.svelte.d.ts +52 -0
- package/dist/components/CollapsibleAdvanced.svelte +174 -0
- package/dist/components/CollapsibleAdvanced.svelte.d.ts +43 -0
- package/dist/components/DashOutline.svelte +47 -0
- package/dist/components/DashOutline.svelte.d.ts +6 -0
- package/dist/components/Dialogs.svelte +123 -0
- package/dist/components/Dialogs.svelte.d.ts +6 -0
- package/dist/components/Drawer.svelte +231 -0
- package/dist/components/Drawer.svelte.d.ts +62 -0
- package/dist/components/ErrorMessage.svelte +140 -0
- package/dist/components/ErrorMessage.svelte.d.ts +41 -0
- package/dist/components/Icon.svelte +94 -0
- package/dist/components/Icon.svelte.d.ts +33 -0
- package/dist/components/InfoTip.svelte +183 -0
- package/dist/components/InfoTip.svelte.d.ts +28 -0
- package/dist/components/Md.svelte +28 -0
- package/dist/components/Md.svelte.d.ts +11 -0
- package/dist/components/Modal.svelte +91 -0
- package/dist/components/Modal.svelte.d.ts +41 -0
- package/dist/components/Notice.svelte +50 -0
- package/dist/components/Notice.svelte.d.ts +30 -0
- package/dist/components/Popover.svelte +212 -0
- package/dist/components/Popover.svelte.d.ts +70 -0
- package/dist/components/Shimmer.svelte +27 -0
- package/dist/components/Shimmer.svelte.d.ts +22 -0
- package/dist/components/Splitter.svelte +152 -0
- package/dist/components/Splitter.svelte.d.ts +41 -0
- package/dist/components/Table.svelte +448 -0
- package/dist/components/Table.svelte.d.ts +268 -0
- package/dist/components/Toast.svelte +273 -0
- package/dist/components/Toast.svelte.d.ts +31 -0
- package/dist/components/Tooltip.svelte +97 -0
- package/dist/components/Tooltip.svelte.d.ts +19 -0
- package/dist/components/WorkingBorder.svelte +105 -0
- package/dist/components/WorkingBorder.svelte.d.ts +26 -0
- package/dist/components/empty/Empty.svelte +55 -0
- package/dist/components/empty/Empty.svelte.d.ts +39 -0
- package/dist/components/empty/EmptyError.svelte +47 -0
- package/dist/components/empty/EmptyError.svelte.d.ts +28 -0
- package/dist/components/empty/index.d.ts +18 -0
- package/dist/components/empty/index.js +10 -0
- package/dist/components/filters/DateFilter.svelte +59 -0
- package/dist/components/filters/DateFilter.svelte.d.ts +23 -0
- package/dist/components/filters/FilterBar.svelte +342 -0
- package/dist/components/filters/FilterBar.svelte.d.ts +283 -0
- package/dist/components/filters/FilterChip.svelte +235 -0
- package/dist/components/filters/FilterChip.svelte.d.ts +68 -0
- package/dist/components/filters/OptionList.svelte +352 -0
- package/dist/components/filters/OptionList.svelte.d.ts +91 -0
- package/dist/components/filters/Panel.svelte +123 -0
- package/dist/components/filters/Panel.svelte.d.ts +25 -0
- package/dist/components/filters/index.d.ts +288 -0
- package/dist/components/filters/index.js +12 -0
- package/dist/components/icons.d.ts +54 -0
- package/dist/components/icons.js +58 -0
- package/dist/components/inputs/ButtonGroup.svelte +104 -0
- package/dist/components/inputs/ButtonGroup.svelte.d.ts +40 -0
- package/dist/components/inputs/Calendar.svelte +535 -0
- package/dist/components/inputs/Calendar.svelte.d.ts +45 -0
- package/dist/components/inputs/DateField.svelte +247 -0
- package/dist/components/inputs/DateField.svelte.d.ts +74 -0
- package/dist/components/inputs/DateInput.svelte +118 -0
- package/dist/components/inputs/DateInput.svelte.d.ts +43 -0
- package/dist/components/inputs/DatePicker.svelte +102 -0
- package/dist/components/inputs/DatePicker.svelte.d.ts +67 -0
- package/dist/components/inputs/DateRangePicker.svelte +208 -0
- package/dist/components/inputs/DateRangePicker.svelte.d.ts +76 -0
- package/dist/components/inputs/DateRangeSelect.svelte +548 -0
- package/dist/components/inputs/DateRangeSelect.svelte.d.ts +70 -0
- package/dist/components/inputs/FieldLabel.svelte +43 -0
- package/dist/components/inputs/FieldLabel.svelte.d.ts +28 -0
- package/dist/components/inputs/ListSearch.svelte +142 -0
- package/dist/components/inputs/ListSearch.svelte.d.ts +35 -0
- package/dist/components/inputs/RadioGroup.svelte +153 -0
- package/dist/components/inputs/RadioGroup.svelte.d.ts +49 -0
- package/dist/components/inputs/RangeShortcuts.svelte +90 -0
- package/dist/components/inputs/RangeShortcuts.svelte.d.ts +31 -0
- package/dist/components/inputs/RangeSlider.svelte +225 -0
- package/dist/components/inputs/RangeSlider.svelte.d.ts +37 -0
- package/dist/components/inputs/SearchField.svelte +24 -0
- package/dist/components/inputs/SearchField.svelte.d.ts +41 -0
- package/dist/components/inputs/Segmented.svelte +76 -0
- package/dist/components/inputs/Segmented.svelte.d.ts +43 -0
- package/dist/components/inputs/Select.svelte +552 -0
- package/dist/components/inputs/Select.svelte.d.ts +100 -0
- package/dist/components/inputs/Slider.svelte +177 -0
- package/dist/components/inputs/Slider.svelte.d.ts +33 -0
- package/dist/components/inputs/SliderTicks.svelte +62 -0
- package/dist/components/inputs/SliderTicks.svelte.d.ts +23 -0
- package/dist/components/inputs/Switch.svelte +120 -0
- package/dist/components/inputs/Switch.svelte.d.ts +31 -0
- package/dist/components/inputs/TagInput.svelte +296 -0
- package/dist/components/inputs/TagInput.svelte.d.ts +61 -0
- package/dist/components/inputs/TextArea.svelte +86 -0
- package/dist/components/inputs/TextArea.svelte.d.ts +56 -0
- package/dist/components/inputs/TextField.svelte +170 -0
- package/dist/components/inputs/TextField.svelte.d.ts +80 -0
- package/dist/components/menu/ContextMenu.svelte +25 -0
- package/dist/components/menu/ContextMenu.svelte.d.ts +26 -0
- package/dist/components/menu/MenuButton.svelte +46 -0
- package/dist/components/menu/MenuButton.svelte.d.ts +16 -0
- package/dist/components/menu/MenuList.svelte +319 -0
- package/dist/components/menu/MenuList.svelte.d.ts +30 -0
- package/dist/components/metadata/Metadata.svelte +38 -0
- package/dist/components/metadata/Metadata.svelte.d.ts +31 -0
- package/dist/components/metadata/MetadataRow.svelte +84 -0
- package/dist/components/metadata/MetadataRow.svelte.d.ts +26 -0
- package/dist/components/metadata/index.d.ts +8 -0
- package/dist/components/metadata/index.js +13 -0
- package/dist/components/side-menu/SideMenu.svelte +138 -0
- package/dist/components/side-menu/SideMenu.svelte.d.ts +25 -0
- package/dist/components/side-menu/SideMenuGroup.svelte +108 -0
- package/dist/components/side-menu/SideMenuGroup.svelte.d.ts +31 -0
- package/dist/components/side-menu/SideMenuItem.svelte +221 -0
- package/dist/components/side-menu/SideMenuItem.svelte.d.ts +46 -0
- package/dist/components/side-menu/context.d.ts +1 -0
- package/dist/components/side-menu/context.js +3 -0
- package/dist/components/side-menu/index.d.ts +62 -0
- package/dist/components/side-menu/index.js +15 -0
- package/dist/components/tiles/Tile.svelte +419 -0
- package/dist/components/tiles/Tile.svelte.d.ts +58 -0
- package/dist/components/tiles/TileGroup.svelte +135 -0
- package/dist/components/tiles/TileGroup.svelte.d.ts +45 -0
- package/dist/components/tiles/context.d.ts +35 -0
- package/dist/components/tiles/context.js +14 -0
- package/dist/components/tiles/index.d.ts +6 -0
- package/dist/components/tiles/index.js +13 -0
- package/dist/components/upload/FileCard.svelte +301 -0
- package/dist/components/upload/FileCard.svelte.d.ts +127 -0
- package/dist/components/upload/Thumb.svelte +274 -0
- package/dist/components/upload/Thumb.svelte.d.ts +101 -0
- package/dist/components/upload/UploadFile.svelte +123 -0
- package/dist/components/upload/UploadFile.svelte.d.ts +48 -0
- package/dist/components/upload/UploadFiles.svelte +177 -0
- package/dist/components/upload/UploadFiles.svelte.d.ts +50 -0
- package/dist/components/upload/UploadFolder.svelte +153 -0
- package/dist/components/upload/UploadFolder.svelte.d.ts +63 -0
- package/dist/components/upload/UploadImage.svelte +137 -0
- package/dist/components/upload/UploadImage.svelte.d.ts +52 -0
- package/dist/components/upload/UploadImages.svelte +172 -0
- package/dist/components/upload/UploadImages.svelte.d.ts +52 -0
- package/dist/components/upload/UploadZone.svelte +130 -0
- package/dist/components/upload/UploadZone.svelte.d.ts +41 -0
- package/dist/components/upload/common.d.ts +31 -0
- package/dist/components/upload/common.js +103 -0
- package/dist/components/upload/context.d.ts +18 -0
- package/dist/components/upload/context.js +10 -0
- package/dist/components/upload/index.d.ts +16 -0
- package/dist/components/upload/index.js +18 -0
- package/dist/css/atoms.css +892 -0
- package/dist/css/base.css +333 -0
- package/dist/css/effects.css +430 -0
- package/dist/css/index.css +10 -0
- package/dist/css/tokens.css +296 -0
- package/dist/fonts.d.ts +1 -0
- package/dist/fonts.js +7 -0
- package/dist/index.d.ts +68 -0
- package/dist/index.js +77 -0
- package/dist/lib/announce.d.ts +8 -0
- package/dist/lib/announce.js +48 -0
- package/dist/lib/controls.d.ts +10 -0
- package/dist/lib/controls.js +42 -0
- package/dist/lib/dates.d.ts +104 -0
- package/dist/lib/dates.js +235 -0
- package/dist/lib/dialogs.svelte.d.ts +8 -0
- package/dist/lib/dialogs.svelte.js +45 -0
- package/dist/lib/dom.d.ts +34 -0
- package/dist/lib/dom.js +90 -0
- package/dist/lib/field-errors.svelte.d.ts +40 -0
- package/dist/lib/field-errors.svelte.js +89 -0
- package/dist/lib/files.d.ts +74 -0
- package/dist/lib/files.js +215 -0
- package/dist/lib/inline-md.d.ts +1 -0
- package/dist/lib/inline-md.js +36 -0
- package/dist/lib/invalid.d.ts +4 -0
- package/dist/lib/invalid.js +35 -0
- package/dist/lib/list-search.d.ts +9 -0
- package/dist/lib/list-search.js +27 -0
- package/dist/lib/menu.svelte.d.ts +39 -0
- package/dist/lib/menu.svelte.js +98 -0
- package/dist/lib/morph.svelte.d.ts +17 -0
- package/dist/lib/morph.svelte.js +46 -0
- package/dist/lib/motion.d.ts +115 -0
- package/dist/lib/motion.js +174 -0
- package/dist/lib/observe.d.ts +36 -0
- package/dist/lib/observe.js +142 -0
- package/dist/lib/reveal.d.ts +7 -0
- package/dist/lib/reveal.js +28 -0
- package/dist/lib/roving.d.ts +16 -0
- package/dist/lib/roving.js +35 -0
- package/dist/lib/runs.d.ts +18 -0
- package/dist/lib/runs.js +25 -0
- package/dist/lib/scroll-fade.d.ts +4 -0
- package/dist/lib/scroll-fade.js +71 -0
- package/dist/lib/spotlight.d.ts +4 -0
- package/dist/lib/spotlight.js +109 -0
- package/dist/lib/ticks.d.ts +12 -0
- package/dist/lib/ticks.js +24 -0
- package/dist/lib/tooltip.d.ts +4 -0
- package/dist/lib/tooltip.js +110 -0
- package/dist/lib/validity.d.ts +16 -0
- package/dist/lib/validity.js +36 -0
- package/package.json +69 -3
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* For a small floating layer (a dropdown list, a menu, a tip, a date panel) opening out of what it belongs to: it fades
|
|
3
|
+
* while growing from `start` (0–1, default 0.97) out of the point nearest `from`, and while moving `distance` px
|
|
4
|
+
* (default 4) away from it. A small, near move, so quicker than the others: 150ms in on the decelerate curve, 100ms out
|
|
5
|
+
* on the accelerate curve (`duration` sets both).
|
|
6
|
+
* from: the element it opens from, or a point `{ x, y }` in viewport px (where a context menu was opened). Which side
|
|
7
|
+
* of it the layer is on (below, above, beside) is measured as the transition starts, so a layer that CSS anchor
|
|
8
|
+
* positioning flipped still comes out of the right side. Without it, the layer grows from its top-left corner.
|
|
9
|
+
* `<ul transition:pop={{ from: button }}>`
|
|
10
|
+
*/
|
|
11
|
+
export function pop(node: any, { from, start, distance, duration }?: {
|
|
12
|
+
from?: null | undefined;
|
|
13
|
+
start?: number | undefined;
|
|
14
|
+
distance?: number | undefined;
|
|
15
|
+
}): ({ direction }: {
|
|
16
|
+
direction: any;
|
|
17
|
+
}) => {
|
|
18
|
+
duration: any;
|
|
19
|
+
easing: (x: any) => number;
|
|
20
|
+
css: (t: any, u: any) => string;
|
|
21
|
+
};
|
|
22
|
+
/** Moves keyed `{#each}` items to their new place on the standard curve. `animate:flip` */
|
|
23
|
+
export function flip(node: any, rect: any, { emphasized, duration }?: {
|
|
24
|
+
emphasized?: boolean | undefined;
|
|
25
|
+
}): import("svelte/animate").AnimationConfig;
|
|
26
|
+
/**
|
|
27
|
+
* A Tween from svelte/motion on M3's standard curve, for animating a number (or anything Tween can interpolate) that
|
|
28
|
+
* stays on screen, such as a count or a progress value. Jumps straight to the target under reduced motion.
|
|
29
|
+
* const width = tween(0); width.target = 40; style:width="{width.current}%"
|
|
30
|
+
* Options: emphasized, duration (as for the transitions).
|
|
31
|
+
*/
|
|
32
|
+
export function tween(value: any, { emphasized, duration }?: {
|
|
33
|
+
emphasized?: boolean | undefined;
|
|
34
|
+
}): Tween<any>;
|
|
35
|
+
export function motion(ms: any): any;
|
|
36
|
+
export namespace durations {
|
|
37
|
+
let short1: number;
|
|
38
|
+
let short2: number;
|
|
39
|
+
let short3: number;
|
|
40
|
+
let short4: number;
|
|
41
|
+
let medium1: number;
|
|
42
|
+
let medium2: number;
|
|
43
|
+
let medium3: number;
|
|
44
|
+
let medium4: number;
|
|
45
|
+
let long1: number;
|
|
46
|
+
let long2: number;
|
|
47
|
+
let long3: number;
|
|
48
|
+
let long4: number;
|
|
49
|
+
let extraLong1: number;
|
|
50
|
+
let extraLong2: number;
|
|
51
|
+
let extraLong3: number;
|
|
52
|
+
let extraLong4: number;
|
|
53
|
+
}
|
|
54
|
+
export namespace easings {
|
|
55
|
+
namespace standard {
|
|
56
|
+
export function standard_1(x: any): number;
|
|
57
|
+
export { standard_1 as standard };
|
|
58
|
+
export function decelerate(x: any): number;
|
|
59
|
+
export function accelerate(x: any): number;
|
|
60
|
+
}
|
|
61
|
+
namespace emphasized {
|
|
62
|
+
export { emphasizedCurve as standard };
|
|
63
|
+
export function decelerate_1(x: any): number;
|
|
64
|
+
export { decelerate_1 as decelerate };
|
|
65
|
+
export function accelerate_1(x: any): number;
|
|
66
|
+
export { accelerate_1 as accelerate };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export namespace defaults {
|
|
70
|
+
export namespace standard_2 {
|
|
71
|
+
import standard_3 = durations.medium2;
|
|
72
|
+
export { standard_3 as standard };
|
|
73
|
+
import decelerate_2 = durations.medium1;
|
|
74
|
+
export { decelerate_2 as decelerate };
|
|
75
|
+
import accelerate_2 = durations.short4;
|
|
76
|
+
export { accelerate_2 as accelerate };
|
|
77
|
+
}
|
|
78
|
+
export { standard_2 as standard };
|
|
79
|
+
export namespace emphasized_1 {
|
|
80
|
+
import standard_4 = durations.long2;
|
|
81
|
+
export { standard_4 as standard };
|
|
82
|
+
import decelerate_3 = durations.medium4;
|
|
83
|
+
export { decelerate_3 as decelerate };
|
|
84
|
+
import accelerate_3 = durations.short4;
|
|
85
|
+
export { accelerate_3 as accelerate };
|
|
86
|
+
}
|
|
87
|
+
export { emphasized_1 as emphasized };
|
|
88
|
+
}
|
|
89
|
+
export function fade(node: any, { emphasized, duration, curve: kind, ...params }?: {
|
|
90
|
+
emphasized?: boolean | undefined;
|
|
91
|
+
curve?: null | undefined;
|
|
92
|
+
}): ({ direction }: {
|
|
93
|
+
direction: any;
|
|
94
|
+
}) => any;
|
|
95
|
+
export function fly(node: any, { emphasized, duration, curve: kind, ...params }?: {
|
|
96
|
+
emphasized?: boolean | undefined;
|
|
97
|
+
curve?: null | undefined;
|
|
98
|
+
}): ({ direction }: {
|
|
99
|
+
direction: any;
|
|
100
|
+
}) => any;
|
|
101
|
+
export function slide(node: any, { emphasized, duration, curve: kind, ...params }?: {
|
|
102
|
+
emphasized?: boolean | undefined;
|
|
103
|
+
curve?: null | undefined;
|
|
104
|
+
}): ({ direction }: {
|
|
105
|
+
direction: any;
|
|
106
|
+
}) => any;
|
|
107
|
+
export function scale(node: any, { emphasized, duration, curve: kind, ...params }?: {
|
|
108
|
+
emphasized?: boolean | undefined;
|
|
109
|
+
curve?: null | undefined;
|
|
110
|
+
}): ({ direction }: {
|
|
111
|
+
direction: any;
|
|
112
|
+
}) => any;
|
|
113
|
+
import { Tween } from 'svelte/motion';
|
|
114
|
+
declare function emphasizedCurve(x: any): number;
|
|
115
|
+
export {};
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// Motion for Svelte, following Google's Material Design 3 motion rules (m3.material.io/styles/motion):
|
|
2
|
+
// - Two easing sets. Standard (the default) is for small, utility-focused moves: controls, menus, tips, panels.
|
|
3
|
+
// Emphasized is for the transitions that should draw the eye, such as a large surface arriving.
|
|
4
|
+
// - Easing by what the element does: it enters (decelerate), exits (accelerate), or begins and ends on screen
|
|
5
|
+
// (standard). The transitions here pick decelerate or accelerate by direction on their own.
|
|
6
|
+
// - Exits are quicker than entrances, and small or near moves are quicker than large or far ones. The defaults follow
|
|
7
|
+
// M3's pairing of curve and duration (see `defaults`); pass a `duration` from `durations` for smaller things.
|
|
8
|
+
// Reduced motion comes from svelte/motion's prefersReducedMotion: every duration here drops to 0 under it, which Svelte
|
|
9
|
+
// transitions and tweens do not do on their own.
|
|
10
|
+
import { prefersReducedMotion, Tween } from 'svelte/motion'
|
|
11
|
+
import { fade as svelteFade, fly as svelteFly, slide as svelteSlide, scale as svelteScale } from 'svelte/transition'
|
|
12
|
+
import { flip as svelteFlip } from 'svelte/animate'
|
|
13
|
+
|
|
14
|
+
/** Duration in ms, or 0 when the user asked for reduced motion. */
|
|
15
|
+
export const motion = (ms) => (prefersReducedMotion.current ? 0 : ms)
|
|
16
|
+
|
|
17
|
+
/** M3's duration tokens, in ms. Short for small things (selection controls, tips, chips), medium for things that
|
|
18
|
+
* cover more of the screen (menus, panels, expanding sections), long and extra long for full-screen moves. */
|
|
19
|
+
export const durations = {
|
|
20
|
+
short1: 50,
|
|
21
|
+
short2: 100,
|
|
22
|
+
short3: 150,
|
|
23
|
+
short4: 200,
|
|
24
|
+
medium1: 250,
|
|
25
|
+
medium2: 300,
|
|
26
|
+
medium3: 350,
|
|
27
|
+
medium4: 400,
|
|
28
|
+
long1: 450,
|
|
29
|
+
long2: 500,
|
|
30
|
+
long3: 550,
|
|
31
|
+
long4: 600,
|
|
32
|
+
extraLong1: 700,
|
|
33
|
+
extraLong2: 800,
|
|
34
|
+
extraLong3: 900,
|
|
35
|
+
extraLong4: 1000,
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// One axis of a cubic Bézier from (0,0) to (1,1) with control points a and b, and its derivative, at t.
|
|
39
|
+
const bez = (t, a, b) => ((1 - 3 * b + 3 * a) * t + (3 * b - 6 * a)) * t * t + 3 * a * t
|
|
40
|
+
const slope = (t, a, b) => 3 * (1 - 3 * b + 3 * a) * t * t + 2 * (3 * b - 6 * a) * t + 3 * a
|
|
41
|
+
|
|
42
|
+
/** Cubic Bézier timing function, as in CSS: maps progress 0–1 to eased progress. */
|
|
43
|
+
function cubicBezier(x1, y1, x2, y2) {
|
|
44
|
+
return (x) => {
|
|
45
|
+
if (x <= 0) return 0
|
|
46
|
+
if (x >= 1) return 1
|
|
47
|
+
// Newton's method for the t whose x is the input, falling back to bisection where the slope is flat.
|
|
48
|
+
let t = x
|
|
49
|
+
for (let i = 0; i < 8; i++) {
|
|
50
|
+
const d = bez(t, x1, x2) - x,
|
|
51
|
+
s = slope(t, x1, x2)
|
|
52
|
+
if (Math.abs(d) < 1e-6) return bez(t, y1, y2)
|
|
53
|
+
if (Math.abs(s) < 1e-6) break
|
|
54
|
+
t -= d / s
|
|
55
|
+
}
|
|
56
|
+
let lo = 0,
|
|
57
|
+
hi = 1
|
|
58
|
+
t = x
|
|
59
|
+
while (hi - lo > 1e-6) {
|
|
60
|
+
if (bez(t, x1, x2) < x) lo = t
|
|
61
|
+
else hi = t
|
|
62
|
+
t = (lo + hi) / 2
|
|
63
|
+
}
|
|
64
|
+
return bez(t, y1, y2)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// M3's emphasized curve is a path, not one Bézier: an accelerating segment to (1/6, 0.4), then a decelerating one to
|
|
69
|
+
// (1, 1). Its segments, scaled to fill the unit square, are exactly the emphasized accelerate and decelerate curves.
|
|
70
|
+
const SPLIT = 0.166666
|
|
71
|
+
const emphasizedHead = cubicBezier(0.3, 0, 0.8, 0.15)
|
|
72
|
+
const emphasizedTail = cubicBezier(0.05, 0.7, 0.1, 1)
|
|
73
|
+
const emphasizedCurve = (x) => (x < SPLIT ? 0.4 * emphasizedHead(x / SPLIT) : 0.4 + 0.6 * emphasizedTail((x - SPLIT) / (1 - SPLIT)))
|
|
74
|
+
|
|
75
|
+
/** M3's easing curves as functions, by set and kind. The standard set's decelerate and accelerate are also the CSS
|
|
76
|
+
* tokens --ease-enter and --ease-exit. */
|
|
77
|
+
export const easings = {
|
|
78
|
+
standard: {
|
|
79
|
+
standard: cubicBezier(0.2, 0, 0, 1),
|
|
80
|
+
decelerate: cubicBezier(0, 0, 0, 1),
|
|
81
|
+
accelerate: cubicBezier(0.3, 0, 1, 1),
|
|
82
|
+
},
|
|
83
|
+
emphasized: {
|
|
84
|
+
standard: emphasizedCurve,
|
|
85
|
+
decelerate: cubicBezier(0.05, 0.7, 0.1, 1),
|
|
86
|
+
accelerate: cubicBezier(0.3, 0, 0.8, 0.15),
|
|
87
|
+
},
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** M3's default duration for each curve: how long that kind of move usually takes. */
|
|
91
|
+
export const defaults = {
|
|
92
|
+
standard: { standard: durations.medium2, decelerate: durations.medium1, accelerate: durations.short4 },
|
|
93
|
+
emphasized: { standard: durations.long2, decelerate: durations.medium4, accelerate: durations.short4 },
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Wraps a Svelte transition so it follows the rules above. Extra params on top of the wrapped transition's own:
|
|
98
|
+
* emphasized: use the emphasized set (default standard).
|
|
99
|
+
* duration: ms, both ways (default: M3's duration for the curve, so 250 in and 200 out, or 400 and 200 emphasized).
|
|
100
|
+
* Use different in:/out: durations to set each.
|
|
101
|
+
* curve: 'standard' to use the one curve both ways, for something that grows or shrinks in place (default:
|
|
102
|
+
* decelerate going in, accelerate going out).
|
|
103
|
+
* Returned as a deferred transition, so even a bidirectional `transition:` learns which way it runs each time.
|
|
104
|
+
*/
|
|
105
|
+
function material(transition, curve = null) {
|
|
106
|
+
return (node, { emphasized = false, duration, curve: kind = curve, ...params } = {}) =>
|
|
107
|
+
({ direction }) => {
|
|
108
|
+
const set = emphasized ? 'emphasized' : 'standard'
|
|
109
|
+
const k = kind ?? (direction === 'out' ? 'accelerate' : 'decelerate')
|
|
110
|
+
return transition(node, { ...params, duration: motion(duration ?? defaults[set][k]), easing: easings[set][k] })
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Opacity only. `<div transition:fade>` */
|
|
115
|
+
export const fade = material(svelteFade)
|
|
116
|
+
/** Fades while moving from an offset (`x`, `y`, `opacity` as in svelte/transition). `<div in:fly={{ y: -6 }}>` */
|
|
117
|
+
export const fly = material(svelteFly)
|
|
118
|
+
/** Opens and closes the height (or `axis: 'x'`). Standard curve by default, since the block grows in place. */
|
|
119
|
+
export const slide = material(svelteSlide, 'standard')
|
|
120
|
+
/** Fades while scaling from `start` (0–1). Keep scaling to small things such as chips. */
|
|
121
|
+
export const scale = material(svelteScale)
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* For a small floating layer (a dropdown list, a menu, a tip, a date panel) opening out of what it belongs to: it fades
|
|
125
|
+
* while growing from `start` (0–1, default 0.97) out of the point nearest `from`, and while moving `distance` px
|
|
126
|
+
* (default 4) away from it. A small, near move, so quicker than the others: 150ms in on the decelerate curve, 100ms out
|
|
127
|
+
* on the accelerate curve (`duration` sets both).
|
|
128
|
+
* from: the element it opens from, or a point `{ x, y }` in viewport px (where a context menu was opened). Which side
|
|
129
|
+
* of it the layer is on (below, above, beside) is measured as the transition starts, so a layer that CSS anchor
|
|
130
|
+
* positioning flipped still comes out of the right side. Without it, the layer grows from its top-left corner.
|
|
131
|
+
* `<ul transition:pop={{ from: button }}>`
|
|
132
|
+
*/
|
|
133
|
+
export function pop(node, { from = null, start = 0.97, distance = 4, duration } = {}) {
|
|
134
|
+
return ({ direction }) => {
|
|
135
|
+
const out = direction === 'out'
|
|
136
|
+
let dx = 0,
|
|
137
|
+
dy = 0,
|
|
138
|
+
ox = 0,
|
|
139
|
+
oy = 0
|
|
140
|
+
if (from) {
|
|
141
|
+
const a = from instanceof Element ? from.getBoundingClientRect() : { left: from.x, right: from.x, top: from.y, bottom: from.y }
|
|
142
|
+
const r = node.getBoundingClientRect()
|
|
143
|
+
if (r.top >= a.bottom - 1) dy = -distance
|
|
144
|
+
else if (r.bottom <= a.top + 1) dy = distance
|
|
145
|
+
else if (r.left >= a.right - 1) dx = -distance
|
|
146
|
+
else if (r.right <= a.left + 1) dx = distance
|
|
147
|
+
// The centre of `from`, held inside the layer's box: the point on its edge closest to where it came from.
|
|
148
|
+
ox = Math.min(Math.max((a.left + a.right) / 2 - r.left, 0), r.width)
|
|
149
|
+
oy = Math.min(Math.max((a.top + a.bottom) / 2 - r.top, 0), r.height)
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
duration: motion(duration ?? (out ? durations.short2 : durations.short3)),
|
|
153
|
+
easing: easings.standard[out ? 'accelerate' : 'decelerate'],
|
|
154
|
+
css: (t, u) => `opacity: ${t}; scale: ${start + (1 - start) * t}; translate: ${u * dx}px ${u * dy}px; transform-origin: ${ox}px ${oy}px;`,
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Moves keyed `{#each}` items to their new place on the standard curve. `animate:flip` */
|
|
160
|
+
export function flip(node, rect, { emphasized = false, duration } = {}) {
|
|
161
|
+
const set = emphasized ? 'emphasized' : 'standard'
|
|
162
|
+
return svelteFlip(node, rect, { duration: motion(duration ?? defaults[set].standard), easing: easings[set].standard })
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* A Tween from svelte/motion on M3's standard curve, for animating a number (or anything Tween can interpolate) that
|
|
167
|
+
* stays on screen, such as a count or a progress value. Jumps straight to the target under reduced motion.
|
|
168
|
+
* const width = tween(0); width.target = 40; style:width="{width.current}%"
|
|
169
|
+
* Options: emphasized, duration (as for the transitions).
|
|
170
|
+
*/
|
|
171
|
+
export function tween(value, { emphasized = false, duration } = {}) {
|
|
172
|
+
const set = emphasized ? 'emphasized' : 'standard'
|
|
173
|
+
return new Tween(value, { duration: () => motion(duration ?? defaults[set].standard), easing: easings[set].standard })
|
|
174
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calls back when an element's size changes, and once on subscribing.
|
|
3
|
+
* `box` as for ResizeObserver (default 'content-box'); 'device-pixel-content-box' falls back to 'content-box' where the
|
|
4
|
+
* browser doesn't support it, so check `entry.devicePixelContentBoxSize` before using it.
|
|
5
|
+
* @param {Element} el
|
|
6
|
+
* @param {(entry: ResizeObserverEntry) => void} callback
|
|
7
|
+
* @param {{ box?: ResizeObserverBoxOptions }} [options]
|
|
8
|
+
* @returns {() => void} stops observing
|
|
9
|
+
*/
|
|
10
|
+
export function observeSize(el: Element, callback: (entry: ResizeObserverEntry) => void, { box }?: {
|
|
11
|
+
box?: ResizeObserverBoxOptions;
|
|
12
|
+
}): () => void;
|
|
13
|
+
/**
|
|
14
|
+
* Calls back when an element enters or leaves the viewport (or crosses a threshold), and once on subscribing.
|
|
15
|
+
* `rootMargin` and `threshold` as for IntersectionObserver; elements with the same options share an observer. The root
|
|
16
|
+
* is always the viewport: for a scroll container as root, make an IntersectionObserver of its own.
|
|
17
|
+
* @param {Element} el
|
|
18
|
+
* @param {(entry: IntersectionObserverEntry) => void} callback
|
|
19
|
+
* @param {{ rootMargin?: string, threshold?: number | number[] }} [options]
|
|
20
|
+
* @returns {() => void} stops observing
|
|
21
|
+
*/
|
|
22
|
+
export function observeVisibility(el: Element, callback: (entry: IntersectionObserverEntry) => void, { rootMargin, threshold }?: {
|
|
23
|
+
rootMargin?: string;
|
|
24
|
+
threshold?: number | number[];
|
|
25
|
+
}): () => void;
|
|
26
|
+
/**
|
|
27
|
+
* One shared observer and its subscribers.
|
|
28
|
+
*/
|
|
29
|
+
export type Shared<E extends ResizeObserverEntry | IntersectionObserverEntry> = {
|
|
30
|
+
observer: {
|
|
31
|
+
observe: (el: Element) => void;
|
|
32
|
+
unobserve: (el: Element) => void;
|
|
33
|
+
};
|
|
34
|
+
subs: Map<Element, Set<(entry: E) => void>>;
|
|
35
|
+
last: Map<Element, E>;
|
|
36
|
+
};
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// Shared observers: one ResizeObserver per box model and one IntersectionObserver per set of options, for every element
|
|
2
|
+
// that asks, rather than one each. The browser measures every observed element either way; sharing saves the per-
|
|
3
|
+
// observer bookkeeping and turns a batch of changes into one callback instead of one per element, which counts once a
|
|
4
|
+
// page has a lot of them (a long list of rows, each watching whether its label is cut off).
|
|
5
|
+
//
|
|
6
|
+
// {@attach (node) => observeSize(node, (entry) => (cut = node.scrollWidth > node.clientWidth))}
|
|
7
|
+
// const stop = observeVisibility(node, (entry) => (inView = entry.isIntersecting), { rootMargin: '200px' })
|
|
8
|
+
//
|
|
9
|
+
// Each returns a function that stops observing (return it from an attachment). A callback gets the element's latest
|
|
10
|
+
// entry: when one batch holds several for an element, only the last, which is its current state. Like a fresh observer,
|
|
11
|
+
// it is called once soon after subscribing, also when the element was already observed for someone else. One callback
|
|
12
|
+
// throwing doesn't stop the others; the error is reported.
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* One shared observer and its subscribers.
|
|
16
|
+
* @template {ResizeObserverEntry | IntersectionObserverEntry} E
|
|
17
|
+
* @typedef {{ observer: { observe: (el: Element) => void, unobserve: (el: Element) => void }, subs: Map<Element, Set<(entry: E) => void>>, last: Map<Element, E> }} Shared
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Calls each element's subscribers with its last entry in the batch.
|
|
22
|
+
* @template {ResizeObserverEntry | IntersectionObserverEntry} E
|
|
23
|
+
* @param {Shared<E>} shared
|
|
24
|
+
* @param {E[]} entries
|
|
25
|
+
*/
|
|
26
|
+
function deliver(shared, entries) {
|
|
27
|
+
/** @type {Map<Element, E>} */
|
|
28
|
+
const latest = new Map()
|
|
29
|
+
for (const entry of entries) latest.set(entry.target, entry)
|
|
30
|
+
for (const [el, entry] of latest) {
|
|
31
|
+
const subs = shared.subs.get(el)
|
|
32
|
+
if (!subs) continue
|
|
33
|
+
shared.last.set(el, entry)
|
|
34
|
+
for (const callback of [...subs]) {
|
|
35
|
+
try {
|
|
36
|
+
callback(entry)
|
|
37
|
+
} catch (error) {
|
|
38
|
+
reportError(error)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Adds a subscriber; returns its unsubscribe.
|
|
46
|
+
* @template {ResizeObserverEntry | IntersectionObserverEntry} E
|
|
47
|
+
* @param {Shared<E>} shared
|
|
48
|
+
* @param {Element} el
|
|
49
|
+
* @param {(entry: E) => void} callback
|
|
50
|
+
*/
|
|
51
|
+
function subscribe(shared, el, callback) {
|
|
52
|
+
let subs = shared.subs.get(el)
|
|
53
|
+
if (subs) {
|
|
54
|
+
// Already observed: the observer won't report it again until it changes, so hand over what it last reported (a
|
|
55
|
+
// report still pending reaches every subscriber when it lands).
|
|
56
|
+
queueMicrotask(() => {
|
|
57
|
+
const entry = shared.last.get(el)
|
|
58
|
+
if (entry && shared.subs.get(el)?.has(callback)) callback(entry)
|
|
59
|
+
})
|
|
60
|
+
} else {
|
|
61
|
+
subs = new Set()
|
|
62
|
+
shared.subs.set(el, subs)
|
|
63
|
+
shared.observer.observe(el)
|
|
64
|
+
}
|
|
65
|
+
subs.add(callback)
|
|
66
|
+
return () => {
|
|
67
|
+
if (!subs.delete(callback) || subs.size) return
|
|
68
|
+
shared.subs.delete(el)
|
|
69
|
+
shared.last.delete(el)
|
|
70
|
+
shared.observer.unobserve(el)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** @type {Map<string, Shared<ResizeObserverEntry>>} */
|
|
75
|
+
const sizes = new Map()
|
|
76
|
+
|
|
77
|
+
/** @param {ResizeObserverBoxOptions} box */
|
|
78
|
+
function sizeObserver(box) {
|
|
79
|
+
let shared = sizes.get(box)
|
|
80
|
+
if (!shared) {
|
|
81
|
+
/** @type {Shared<ResizeObserverEntry>} */
|
|
82
|
+
const made = { observer: /** @type {any} */ (null), subs: new Map(), last: new Map() }
|
|
83
|
+
const ro = new ResizeObserver((entries) => deliver(made, entries))
|
|
84
|
+
made.observer = { observe: (el) => ro.observe(el, { box }), unobserve: (el) => ro.unobserve(el) }
|
|
85
|
+
sizes.set(box, (shared = made))
|
|
86
|
+
}
|
|
87
|
+
return shared
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Whether the browser reports device pixels (not Safari): observing with that box throws where it doesn't. */
|
|
91
|
+
let devicePixels = /** @type {boolean | undefined} */ (undefined)
|
|
92
|
+
function supportsDevicePixels() {
|
|
93
|
+
if (devicePixels === undefined) {
|
|
94
|
+
const probe = new ResizeObserver(() => {})
|
|
95
|
+
try {
|
|
96
|
+
probe.observe(document.documentElement, { box: 'device-pixel-content-box' })
|
|
97
|
+
devicePixels = true
|
|
98
|
+
} catch {
|
|
99
|
+
devicePixels = false
|
|
100
|
+
}
|
|
101
|
+
probe.disconnect()
|
|
102
|
+
}
|
|
103
|
+
return devicePixels
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Calls back when an element's size changes, and once on subscribing.
|
|
108
|
+
* `box` as for ResizeObserver (default 'content-box'); 'device-pixel-content-box' falls back to 'content-box' where the
|
|
109
|
+
* browser doesn't support it, so check `entry.devicePixelContentBoxSize` before using it.
|
|
110
|
+
* @param {Element} el
|
|
111
|
+
* @param {(entry: ResizeObserverEntry) => void} callback
|
|
112
|
+
* @param {{ box?: ResizeObserverBoxOptions }} [options]
|
|
113
|
+
* @returns {() => void} stops observing
|
|
114
|
+
*/
|
|
115
|
+
export function observeSize(el, callback, { box = 'content-box' } = {}) {
|
|
116
|
+
if (box === 'device-pixel-content-box' && !supportsDevicePixels()) box = 'content-box'
|
|
117
|
+
return subscribe(sizeObserver(box), el, callback)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** @type {Map<string, Shared<IntersectionObserverEntry>>} */
|
|
121
|
+
const views = new Map()
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Calls back when an element enters or leaves the viewport (or crosses a threshold), and once on subscribing.
|
|
125
|
+
* `rootMargin` and `threshold` as for IntersectionObserver; elements with the same options share an observer. The root
|
|
126
|
+
* is always the viewport: for a scroll container as root, make an IntersectionObserver of its own.
|
|
127
|
+
* @param {Element} el
|
|
128
|
+
* @param {(entry: IntersectionObserverEntry) => void} callback
|
|
129
|
+
* @param {{ rootMargin?: string, threshold?: number | number[] }} [options]
|
|
130
|
+
* @returns {() => void} stops observing
|
|
131
|
+
*/
|
|
132
|
+
export function observeVisibility(el, callback, { rootMargin = '0px', threshold = 0 } = {}) {
|
|
133
|
+
const key = `${rootMargin}|${threshold}`
|
|
134
|
+
let shared = views.get(key)
|
|
135
|
+
if (!shared) {
|
|
136
|
+
/** @type {Shared<IntersectionObserverEntry>} */
|
|
137
|
+
const made = { observer: /** @type {any} */ (null), subs: new Map(), last: new Map() }
|
|
138
|
+
made.observer = new IntersectionObserver((entries) => deliver(made, entries), { rootMargin, threshold })
|
|
139
|
+
views.set(key, (shared = made))
|
|
140
|
+
}
|
|
141
|
+
return subscribe(shared, el, callback)
|
|
142
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scrolls `item` the least it takes to show it whole inside the scrolling `list`: clear of the list's padding, and
|
|
3
|
+
* below the sticky heading of its group if it has one (a [data-sticky-head] in the nearest [role="group"] around it).
|
|
4
|
+
* @param {Element | null | undefined} list
|
|
5
|
+
* @param {Element | null | undefined} item
|
|
6
|
+
*/
|
|
7
|
+
export function reveal(list: Element | null | undefined, item: Element | null | undefined): void;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Scrolling a list to the option the keyboard is on, when the list has sticky group headings (Select's groups,
|
|
2
|
+
// DateRangeSelect's months). The browser's scrollIntoView and scroll-padding only know a fixed band at the top, but a
|
|
3
|
+
// heading sticks at the list's content edge (inside its padding) and is as tall as its text makes it, so a fixed band
|
|
4
|
+
// leaves the option scrolled to partly under it. This measures the heading instead.
|
|
5
|
+
//
|
|
6
|
+
// <div role="group"><div class="group-head" data-sticky-head>March</div> …options… </div>
|
|
7
|
+
// reveal(list, list.querySelector(`[data-i="${active}"]`))
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Scrolls `item` the least it takes to show it whole inside the scrolling `list`: clear of the list's padding, and
|
|
11
|
+
* below the sticky heading of its group if it has one (a [data-sticky-head] in the nearest [role="group"] around it).
|
|
12
|
+
* @param {Element | null | undefined} list
|
|
13
|
+
* @param {Element | null | undefined} item
|
|
14
|
+
*/
|
|
15
|
+
export function reveal(list, item) {
|
|
16
|
+
if (!list || !item) return
|
|
17
|
+
const style = getComputedStyle(list)
|
|
18
|
+
const edge = list.getBoundingClientRect().top + list.clientTop
|
|
19
|
+
const group = item.closest('[role="group"]')
|
|
20
|
+
const head = group && list.contains(group) ? group.querySelector('[data-sticky-head]') : null
|
|
21
|
+
// Where a stuck heading ends: the list's top padding, the heading's own `top`, then its height.
|
|
22
|
+
const covered = head ? (parseFloat(getComputedStyle(head).top) || 0) + head.getBoundingClientRect().height : 0
|
|
23
|
+
const from = edge + parseFloat(style.paddingTop) + covered
|
|
24
|
+
const to = edge + list.clientHeight - parseFloat(style.paddingBottom)
|
|
25
|
+
const r = item.getBoundingClientRect()
|
|
26
|
+
if (r.top < from) list.scrollTop -= from - r.top
|
|
27
|
+
else if (r.bottom > to) list.scrollTop += Math.min(r.bottom - to, r.top - from)
|
|
28
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attachment for a radio group with roving focus: put it on the role="radiogroup" element around role="radio" buttons,
|
|
3
|
+
* where only the chosen one is tabbable (tabindex 0, the others -1). Arrows move to the next or previous option and
|
|
4
|
+
* choose it, wrapping round; Home and End go to the first and last. Choosing is a click on that option, so the option's
|
|
5
|
+
* own onclick does the work.
|
|
6
|
+
*
|
|
7
|
+
* <div role="radiogroup" aria-label="View" {@attach radioKeys}>
|
|
8
|
+
* {#each options as o (o.value)}
|
|
9
|
+
* <button type="button" role="radio" aria-checked={o.value === value} tabindex={o.value === value ? 0 : -1}
|
|
10
|
+
* onclick={() => onchange(o.value)}>{o.label}</button>
|
|
11
|
+
* {/each}
|
|
12
|
+
* </div>
|
|
13
|
+
*
|
|
14
|
+
* @param {HTMLElement} node
|
|
15
|
+
*/
|
|
16
|
+
export function radioKeys(node: HTMLElement): () => void;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attachment for a radio group with roving focus: put it on the role="radiogroup" element around role="radio" buttons,
|
|
3
|
+
* where only the chosen one is tabbable (tabindex 0, the others -1). Arrows move to the next or previous option and
|
|
4
|
+
* choose it, wrapping round; Home and End go to the first and last. Choosing is a click on that option, so the option's
|
|
5
|
+
* own onclick does the work.
|
|
6
|
+
*
|
|
7
|
+
* <div role="radiogroup" aria-label="View" {@attach radioKeys}>
|
|
8
|
+
* {#each options as o (o.value)}
|
|
9
|
+
* <button type="button" role="radio" aria-checked={o.value === value} tabindex={o.value === value ? 0 : -1}
|
|
10
|
+
* onclick={() => onchange(o.value)}>{o.label}</button>
|
|
11
|
+
* {/each}
|
|
12
|
+
* </div>
|
|
13
|
+
*
|
|
14
|
+
* @param {HTMLElement} node
|
|
15
|
+
*/
|
|
16
|
+
export function radioKeys(node) {
|
|
17
|
+
/** @param {KeyboardEvent} e */
|
|
18
|
+
const onkeydown = (e) => {
|
|
19
|
+
const radios = /** @type {HTMLButtonElement[]} */ ([...node.querySelectorAll('[role="radio"]')]).filter(
|
|
20
|
+
(radio) => !radio.disabled && radio.closest('[role="radiogroup"]') === node,
|
|
21
|
+
)
|
|
22
|
+
const at = radios.indexOf(/** @type {HTMLButtonElement} */ (/** @type {Element} */ (e.target).closest?.('[role="radio"]')))
|
|
23
|
+
if (at < 0) return
|
|
24
|
+
const n = radios.length
|
|
25
|
+
const next = { ArrowRight: at + 1, ArrowDown: at + 1, ArrowLeft: at - 1 + n, ArrowUp: at - 1 + n, Home: 0, End: n - 1 }[e.key]
|
|
26
|
+
if (next === undefined) return
|
|
27
|
+
e.preventDefault()
|
|
28
|
+
e.stopPropagation()
|
|
29
|
+
const radio = radios[next % n]
|
|
30
|
+
radio.focus()
|
|
31
|
+
radio.click()
|
|
32
|
+
}
|
|
33
|
+
node.addEventListener('keydown', onkeydown)
|
|
34
|
+
return () => node.removeEventListener('keydown', onkeydown)
|
|
35
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Splits a list, in its order, into runs of neighbours sharing a key: a new run starts wherever an item's key differs
|
|
3
|
+
* from the one before it. Each item keeps its index in the list (for keyboard focus and ids).
|
|
4
|
+
*
|
|
5
|
+
* runs(['a1', 'a2', 'b1', 'a3'], (s) => s[0]) // [{ key: 'a', items: [a1, a2] }, { key: 'b', … }, { key: 'a', … }]
|
|
6
|
+
*
|
|
7
|
+
* @template T
|
|
8
|
+
* @param {T[]} items
|
|
9
|
+
* @param {(item: T) => string} keyOf
|
|
10
|
+
* @returns {{ key: string, items: { item: T, index: number }[] }[]}
|
|
11
|
+
*/
|
|
12
|
+
export function runs<T>(items: T[], keyOf: (item: T) => string): {
|
|
13
|
+
key: string;
|
|
14
|
+
items: {
|
|
15
|
+
item: T;
|
|
16
|
+
index: number;
|
|
17
|
+
}[];
|
|
18
|
+
}[];
|
package/dist/lib/runs.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Splitting a list into sections where something about its items changes (a month, a group, a letter), for lists
|
|
2
|
+
// that show a heading wherever an item differs from the one before it. See monthRuns in dates.js.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Splits a list, in its order, into runs of neighbours sharing a key: a new run starts wherever an item's key differs
|
|
6
|
+
* from the one before it. Each item keeps its index in the list (for keyboard focus and ids).
|
|
7
|
+
*
|
|
8
|
+
* runs(['a1', 'a2', 'b1', 'a3'], (s) => s[0]) // [{ key: 'a', items: [a1, a2] }, { key: 'b', … }, { key: 'a', … }]
|
|
9
|
+
*
|
|
10
|
+
* @template T
|
|
11
|
+
* @param {T[]} items
|
|
12
|
+
* @param {(item: T) => string} keyOf
|
|
13
|
+
* @returns {{ key: string, items: { item: T, index: number }[] }[]}
|
|
14
|
+
*/
|
|
15
|
+
export function runs(items, keyOf) {
|
|
16
|
+
/** @type {{ key: string, items: { item: T, index: number }[] }[]} */
|
|
17
|
+
const out = []
|
|
18
|
+
items.forEach((item, index) => {
|
|
19
|
+
const key = keyOf(item)
|
|
20
|
+
const last = out.at(-1)
|
|
21
|
+
if (last && last.key === key) last.items.push({ item, index })
|
|
22
|
+
else out.push({ key, items: [{ item, index }] })
|
|
23
|
+
})
|
|
24
|
+
return out
|
|
25
|
+
}
|