@xerios/design-system 0.0.0-stage → 0.1.1
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 +185 -0
- package/dist/components/Button.svelte.d.ts +70 -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 +401 -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 +110 -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 +151 -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 +79 -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 +899 -0
- package/dist/css/base.css +334 -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,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a drop carries: its entries (files and folders, readable after the event) and its loose files.
|
|
3
|
+
* @typedef {object} Dropped
|
|
4
|
+
* @property {FileSystemEntry[]} entries
|
|
5
|
+
* @property {File[]} loose
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* A file with its path inside what was picked or dropped ('assets/app.css'), or just its name.
|
|
9
|
+
* @typedef {object} PathedFile
|
|
10
|
+
* @property {string} path
|
|
11
|
+
* @property {File} file
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* What a drop carries. Call it in the drop handler itself: the browser empties the transfer once it returns.
|
|
15
|
+
* @param {DataTransfer} data
|
|
16
|
+
* @returns {Dropped}
|
|
17
|
+
*/
|
|
18
|
+
export function dropped(data: DataTransfer): Dropped;
|
|
19
|
+
/**
|
|
20
|
+
* The loose files of a drop, leaving out folders (a drop lists each folder among its files too, with nothing in it).
|
|
21
|
+
* @param {Dropped} drop
|
|
22
|
+
* @returns {File[]}
|
|
23
|
+
*/
|
|
24
|
+
export function droppedFiles({ entries, loose }: Dropped): File[];
|
|
25
|
+
/**
|
|
26
|
+
* Every file under a dropped entry, with its path from `prefix`.
|
|
27
|
+
* @param {FileSystemEntry} entry
|
|
28
|
+
* @param {string} [prefix]
|
|
29
|
+
* @returns {Promise<PathedFile[]>}
|
|
30
|
+
*/
|
|
31
|
+
export function filesUnder(entry: FileSystemEntry, prefix?: string): Promise<PathedFile[]>;
|
|
32
|
+
/**
|
|
33
|
+
* Every file in a drop, with its path. One folder dropped: the paths start inside it, as a picked folder's would, and
|
|
34
|
+
* `folder` is its name. Several things, or loose files: paths from the top, and `folder` is null. Rejects when the
|
|
35
|
+
* browser can't read what was dropped.
|
|
36
|
+
* @param {Dropped} drop
|
|
37
|
+
* @returns {Promise<{ folder: string | null, files: PathedFile[] }>}
|
|
38
|
+
*/
|
|
39
|
+
export function filesOfDrop({ entries, loose }: Dropped): Promise<{
|
|
40
|
+
folder: string | null;
|
|
41
|
+
files: PathedFile[];
|
|
42
|
+
}>;
|
|
43
|
+
/**
|
|
44
|
+
* Whether a file fits an `accept` list ('image/*,.pdf,application/zip'), as the file picker filters. An empty list
|
|
45
|
+
* takes anything. Dropped files skip the picker's filter, so check them with this.
|
|
46
|
+
* @param {File} file
|
|
47
|
+
* @param {string} accept
|
|
48
|
+
*/
|
|
49
|
+
export function accepts(file: File, accept: string): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* A size in bytes, for display: '512 B', '12 KB', '1.2 MB', '10 MB', '3.4 GB'.
|
|
52
|
+
* @param {number} bytes
|
|
53
|
+
*/
|
|
54
|
+
export function fileSize(bytes: number): string;
|
|
55
|
+
export function preview(file: Blob): (/** @type {HTMLImageElement} */ img: HTMLImageElement) => () => void;
|
|
56
|
+
export function fileDrop({ ondrop, ondragging, disabled }: {
|
|
57
|
+
ondrop: (drop: Dropped) => void;
|
|
58
|
+
ondragging?: (over: boolean) => void;
|
|
59
|
+
disabled?: boolean;
|
|
60
|
+
}): (/** @type {HTMLElement} */ node: HTMLElement) => (() => void) | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* What a drop carries: its entries (files and folders, readable after the event) and its loose files.
|
|
63
|
+
*/
|
|
64
|
+
export type Dropped = {
|
|
65
|
+
entries: FileSystemEntry[];
|
|
66
|
+
loose: File[];
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* A file with its path inside what was picked or dropped ('assets/app.css'), or just its name.
|
|
70
|
+
*/
|
|
71
|
+
export type PathedFile = {
|
|
72
|
+
path: string;
|
|
73
|
+
file: File;
|
|
74
|
+
};
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// Files picked or dropped: what a drop carries (folders included), matching a file against an `accept` list, sizes for
|
|
2
|
+
// display, image previews, and the attachment that makes an element a drop target.
|
|
3
|
+
//
|
|
4
|
+
// <div {@attach fileDrop({ ondrop: (drop) => take(drop) })}>…</div> data-dragging while files are over it
|
|
5
|
+
// const { folder, files } = await filesOfDrop(drop) every file in it, with its path
|
|
6
|
+
// droppedFiles(drop) the loose files, folders left out
|
|
7
|
+
// accepts(file, 'image/*,.pdf') as the file picker would filter it
|
|
8
|
+
// fileSize(1536) '2 KB'
|
|
9
|
+
// <img alt="" {@attach preview(file)} /> an object URL, revoked when it goes
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* What a drop carries: its entries (files and folders, readable after the event) and its loose files.
|
|
13
|
+
* @typedef {object} Dropped
|
|
14
|
+
* @property {FileSystemEntry[]} entries
|
|
15
|
+
* @property {File[]} loose
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A file with its path inside what was picked or dropped ('assets/app.css'), or just its name.
|
|
20
|
+
* @typedef {object} PathedFile
|
|
21
|
+
* @property {string} path
|
|
22
|
+
* @property {File} file
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* What a drop carries. Call it in the drop handler itself: the browser empties the transfer once it returns.
|
|
27
|
+
* @param {DataTransfer} data
|
|
28
|
+
* @returns {Dropped}
|
|
29
|
+
*/
|
|
30
|
+
export function dropped(data) {
|
|
31
|
+
// Entries must be taken before the event ends; the files in them can be read later.
|
|
32
|
+
const entries = [...data.items].map((item) => item.webkitGetAsEntry?.()).filter((entry) => entry != null)
|
|
33
|
+
return { entries, loose: [...data.files] }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The loose files of a drop, leaving out folders (a drop lists each folder among its files too, with nothing in it).
|
|
38
|
+
* @param {Dropped} drop
|
|
39
|
+
* @returns {File[]}
|
|
40
|
+
*/
|
|
41
|
+
export function droppedFiles({ entries, loose }) {
|
|
42
|
+
const folders = new Set(entries.filter((entry) => entry.isDirectory).map((entry) => entry.name))
|
|
43
|
+
return loose.filter((file) => !folders.has(file.name))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @param {FileSystemFileEntry} entry
|
|
48
|
+
* @returns {Promise<File>}
|
|
49
|
+
*/
|
|
50
|
+
function fileOf(entry) {
|
|
51
|
+
return new Promise((resolve, reject) => entry.file(resolve, reject))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Every file under a dropped entry, with its path from `prefix`.
|
|
56
|
+
* @param {FileSystemEntry} entry
|
|
57
|
+
* @param {string} [prefix]
|
|
58
|
+
* @returns {Promise<PathedFile[]>}
|
|
59
|
+
*/
|
|
60
|
+
export async function filesUnder(entry, prefix = '') {
|
|
61
|
+
if (entry.isFile) return [{ path: prefix + entry.name, file: await fileOf(/** @type {FileSystemFileEntry} */ (entry)) }]
|
|
62
|
+
if (!entry.isDirectory) return []
|
|
63
|
+
const children = await readAll(/** @type {FileSystemDirectoryEntry} */ (entry).createReader())
|
|
64
|
+
return (await Promise.all(children.map((child) => filesUnder(child, `${prefix}${entry.name}/`)))).flat()
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* readEntries hands back a batch at a time, and an empty one at the end.
|
|
69
|
+
* @param {FileSystemDirectoryReader} reader
|
|
70
|
+
* @returns {Promise<FileSystemEntry[]>}
|
|
71
|
+
*/
|
|
72
|
+
async function readAll(reader) {
|
|
73
|
+
/** @type {FileSystemEntry[]} */
|
|
74
|
+
const batch = await new Promise((resolve, reject) => reader.readEntries(resolve, reject))
|
|
75
|
+
return batch.length ? [...batch, ...(await readAll(reader))] : []
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Every file in a drop, with its path. One folder dropped: the paths start inside it, as a picked folder's would, and
|
|
80
|
+
* `folder` is its name. Several things, or loose files: paths from the top, and `folder` is null. Rejects when the
|
|
81
|
+
* browser can't read what was dropped.
|
|
82
|
+
* @param {Dropped} drop
|
|
83
|
+
* @returns {Promise<{ folder: string | null, files: PathedFile[] }>}
|
|
84
|
+
*/
|
|
85
|
+
export async function filesOfDrop({ entries, loose }) {
|
|
86
|
+
const [single] = entries
|
|
87
|
+
if (entries.length === 1 && single?.isDirectory) {
|
|
88
|
+
const files = await filesUnder(single)
|
|
89
|
+
return { folder: single.name, files: files.map(({ path, file }) => ({ path: path.slice(single.name.length + 1), file })) }
|
|
90
|
+
}
|
|
91
|
+
if (entries.length) return { folder: null, files: (await Promise.all(entries.map((entry) => filesUnder(entry)))).flat() }
|
|
92
|
+
return { folder: null, files: loose.map((file) => ({ path: file.name, file })) }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Whether a file fits an `accept` list ('image/*,.pdf,application/zip'), as the file picker filters. An empty list
|
|
97
|
+
* takes anything. Dropped files skip the picker's filter, so check them with this.
|
|
98
|
+
* @param {File} file
|
|
99
|
+
* @param {string} accept
|
|
100
|
+
*/
|
|
101
|
+
export function accepts(file, accept) {
|
|
102
|
+
const rules = accept
|
|
103
|
+
.split(',')
|
|
104
|
+
.map((rule) => rule.trim().toLowerCase())
|
|
105
|
+
.filter(Boolean)
|
|
106
|
+
if (!rules.length) return true
|
|
107
|
+
const name = file.name.toLowerCase()
|
|
108
|
+
const type = file.type.toLowerCase()
|
|
109
|
+
return rules.some((rule) => (rule.startsWith('.') ? name.endsWith(rule) : rule.endsWith('/*') ? type.startsWith(rule.slice(0, -1)) : type === rule))
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** @param {number} value */
|
|
113
|
+
const tenths = (value) => value.toFixed(1).replace(/\.0$/, '')
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* A size in bytes, for display: '512 B', '12 KB', '1.2 MB', '10 MB', '3.4 GB'.
|
|
117
|
+
* @param {number} bytes
|
|
118
|
+
*/
|
|
119
|
+
export function fileSize(bytes) {
|
|
120
|
+
if (bytes < 1024) return `${bytes} B`
|
|
121
|
+
if (bytes < 1024 ** 2) return `${Math.round(bytes / 1024)} KB`
|
|
122
|
+
if (bytes < 1024 ** 3) return `${tenths(bytes / 1024 ** 2)} MB`
|
|
123
|
+
return `${tenths(bytes / 1024 ** 3)} GB`
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Attachment for an <img>: shows a File or Blob through an object URL, revoked when the image goes or the file changes.
|
|
128
|
+
* A Content Security Policy must allow it: `blob:` in img-src (Upload.Image and Upload.Images draw with this).
|
|
129
|
+
* @param {Blob} file
|
|
130
|
+
*/
|
|
131
|
+
export const preview = (file) => (/** @type {HTMLImageElement} */ img) => {
|
|
132
|
+
const url = URL.createObjectURL(file)
|
|
133
|
+
img.src = url
|
|
134
|
+
return () => URL.revokeObjectURL(url)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// A drag that starts in the page (an image or a link dragged from it) is marked with this type, so drop targets let it
|
|
138
|
+
// pass: Chrome hands a dragged image over as a file too, and taking it would add a copy of what is already on the page.
|
|
139
|
+
const FROM_PAGE = 'application/x-dragged-from-page'
|
|
140
|
+
let marking = false
|
|
141
|
+
function markPageDrags() {
|
|
142
|
+
if (marking) return
|
|
143
|
+
marking = true
|
|
144
|
+
window.addEventListener('dragstart', (event) => event.dataTransfer?.setData(FROM_PAGE, '1'), true)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Files from outside the page: the types of a drag can be read while it is under way, its data only on the drop.
|
|
148
|
+
* @param {DragEvent} event */
|
|
149
|
+
function carriesFiles(event) {
|
|
150
|
+
const types = event.dataTransfer?.types ?? []
|
|
151
|
+
return types.includes('Files') && !types.includes(FROM_PAGE)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Attachment: makes an element a drop target for files and folders. Drags of anything else (text, links), and of
|
|
156
|
+
* anything dragged from the page itself, pass by.
|
|
157
|
+
* ondrop(drop): what was dropped, as read by dropped(). Its entries can be read later; the event itself can't.
|
|
158
|
+
* ondragging(over): true as files are dragged over the element, false as they leave or drop. The element also has
|
|
159
|
+
* data-dragging meanwhile, for CSS.
|
|
160
|
+
* disabled: not a target.
|
|
161
|
+
*
|
|
162
|
+
* @param {{ ondrop: (drop: Dropped) => void, ondragging?: (over: boolean) => void, disabled?: boolean }} options
|
|
163
|
+
*/
|
|
164
|
+
export const fileDrop =
|
|
165
|
+
({ ondrop, ondragging, disabled = false }) =>
|
|
166
|
+
(/** @type {HTMLElement} */ node) => {
|
|
167
|
+
if (disabled) return
|
|
168
|
+
markPageDrags()
|
|
169
|
+
// dragenter and dragleave fire for every child crossed, so count them to know when the pointer has left.
|
|
170
|
+
let depth = 0
|
|
171
|
+
/** @param {number} next */
|
|
172
|
+
function track(next) {
|
|
173
|
+
const was = depth > 0
|
|
174
|
+
depth = Math.max(0, next)
|
|
175
|
+
if (was === depth > 0) return
|
|
176
|
+
node.toggleAttribute('data-dragging', depth > 0)
|
|
177
|
+
ondragging?.(depth > 0)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** @param {DragEvent} event */
|
|
181
|
+
function enter(event) {
|
|
182
|
+
if (!carriesFiles(event)) return
|
|
183
|
+
event.preventDefault()
|
|
184
|
+
track(depth + 1)
|
|
185
|
+
}
|
|
186
|
+
/** @param {DragEvent} event */
|
|
187
|
+
function over(event) {
|
|
188
|
+
if (!carriesFiles(event) || !event.dataTransfer) return
|
|
189
|
+
event.preventDefault()
|
|
190
|
+
event.dataTransfer.dropEffect = 'copy'
|
|
191
|
+
}
|
|
192
|
+
/** @param {DragEvent} event */
|
|
193
|
+
function leave(event) {
|
|
194
|
+
if (carriesFiles(event)) track(depth - 1)
|
|
195
|
+
}
|
|
196
|
+
/** @param {DragEvent} event */
|
|
197
|
+
function drop(event) {
|
|
198
|
+
track(0)
|
|
199
|
+
if (!carriesFiles(event) || !event.dataTransfer) return
|
|
200
|
+
event.preventDefault()
|
|
201
|
+
ondrop(dropped(event.dataTransfer))
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
node.addEventListener('dragenter', enter)
|
|
205
|
+
node.addEventListener('dragover', over)
|
|
206
|
+
node.addEventListener('dragleave', leave)
|
|
207
|
+
node.addEventListener('drop', drop)
|
|
208
|
+
return () => {
|
|
209
|
+
node.removeEventListener('dragenter', enter)
|
|
210
|
+
node.removeEventListener('dragover', over)
|
|
211
|
+
node.removeEventListener('dragleave', leave)
|
|
212
|
+
node.removeEventListener('drop', drop)
|
|
213
|
+
if (depth > 0) track(0)
|
|
214
|
+
}
|
|
215
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function inlineMd(text: any, flags?: {}): any;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Inline markdown for text written by models: **bold**, *italic* / _italic_, `code`, [text](url) and bare
|
|
2
|
+
// URLs or domains. Returns parts to render with Svelte (no {@html}, so nothing in the text can run).
|
|
3
|
+
// Part: { text, b?, i?, code?, href? }.
|
|
4
|
+
|
|
5
|
+
const TOKEN = /`([^`]+)`|\[([^\]]+)\]\((https?:\/\/[^)\s]+)\)|\*\*([^*]+?)\*\*|__([^_]+?)__|(?<![\w*])\*([^*\s][^*]*?)\*(?!\w)|(?<![\w])_([^_\s][^_]*?)_(?!\w)|\b((?:https?:\/\/)?(?:[a-z0-9-]+\.)+[a-z]{2,}(?:\/[^\s,;)\]]*)?)/gi;
|
|
6
|
+
// Bare domains only count with a common TLD, so "e.g." or "Node.js" stay text.
|
|
7
|
+
const DOMAIN_OK = /^https?:|\.(com|io|ai|xyz|dev|app|co|net|org|eu|be|nl|fr|de|uk|tech|so|sh|gg|jobs|careers)(\/|$)/i;
|
|
8
|
+
|
|
9
|
+
export function inlineMd(text, flags = {}) {
|
|
10
|
+
const out = [];
|
|
11
|
+
const s = String(text ?? '');
|
|
12
|
+
let last = 0;
|
|
13
|
+
for (const m of s.matchAll(TOKEN)) {
|
|
14
|
+
const [whole, code, linkText, linkUrl, b1, b2, i1, i2, url] = m;
|
|
15
|
+
if (url !== undefined) {
|
|
16
|
+
const raw = url.replace(/[.,]+$/, '');
|
|
17
|
+
if (!DOMAIN_OK.test(raw)) continue;
|
|
18
|
+
push(s.slice(last, m.index));
|
|
19
|
+
out.push({ ...flags, text: raw, href: /^https?:/i.test(raw) ? raw : 'https://' + raw });
|
|
20
|
+
last = m.index + raw.length;
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
push(s.slice(last, m.index));
|
|
24
|
+
if (code !== undefined) out.push({ ...flags, text: code, code: true });
|
|
25
|
+
else if (linkText !== undefined) out.push({ ...flags, text: linkText, href: linkUrl });
|
|
26
|
+
else if (b1 !== undefined || b2 !== undefined) out.push(...inlineMd(b1 ?? b2, { ...flags, b: true }));
|
|
27
|
+
else out.push(...inlineMd(i1 ?? i2, { ...flags, i: true }));
|
|
28
|
+
last = m.index + whole.length;
|
|
29
|
+
}
|
|
30
|
+
push(s.slice(last));
|
|
31
|
+
return out;
|
|
32
|
+
|
|
33
|
+
function push(t) {
|
|
34
|
+
if (t) out.push({ ...flags, text: t });
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export function invalid(errorId: string | false | null | undefined): (/** @type {Element} */ node: Element) => (() => void) | undefined;
|
|
2
|
+
/** The selector for the first control to focus after a failed submit: an invalid control, or the first button in an
|
|
3
|
+
* invalid group. */
|
|
4
|
+
export const FIRST_INVALID: ":is(input, textarea, select, button)[aria-invalid=\"true\"], [aria-invalid=\"true\"] button";
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attachment: marks a control (or a group of them) invalid while it has an error message, and points it at that
|
|
3
|
+
* message, so every field states its error the same way.
|
|
4
|
+
*
|
|
5
|
+
* <input {@attach invalid(problem && `${id}-error`)} />
|
|
6
|
+
* <ErrorMessage floating id="{id}-error" message={problem} />
|
|
7
|
+
*
|
|
8
|
+
* errorId: the id of the element holding the message, or a falsy value while there is none.
|
|
9
|
+
* Sets aria-invalid: "true", or "false" while valid (left unset, Firefox and Safari report an empty required field as
|
|
10
|
+
* invalid on load). Used on every control alike, buttons included (Select's trigger), although ARIA 1.2 no longer
|
|
11
|
+
* lists it for every role: one attribute for screen readers, styles (`[aria-invalid='true']`) and scripts that look for
|
|
12
|
+
* the first invalid field. Adds errorId to aria-describedby while there is a message, keeping any other ids there
|
|
13
|
+
* (give the control its own descriptions as a fixed attribute: the attachment edits it after Svelte sets it).
|
|
14
|
+
* Runs again whenever errorId changes.
|
|
15
|
+
*
|
|
16
|
+
* @param {string | false | null | undefined} errorId
|
|
17
|
+
*/
|
|
18
|
+
export const invalid = (errorId) => (/** @type {Element} */ node) => {
|
|
19
|
+
node.setAttribute('aria-invalid', errorId ? 'true' : 'false')
|
|
20
|
+
if (!errorId) return
|
|
21
|
+
const others = describedBy(node).filter((id) => id !== errorId)
|
|
22
|
+
node.setAttribute('aria-describedby', [...others, errorId].join(' '))
|
|
23
|
+
return () => {
|
|
24
|
+
const rest = describedBy(node).filter((id) => id !== errorId)
|
|
25
|
+
if (rest.length) node.setAttribute('aria-describedby', rest.join(' '))
|
|
26
|
+
else node.removeAttribute('aria-describedby')
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** @param {Element} node */
|
|
31
|
+
const describedBy = (node) => (node.getAttribute('aria-describedby') ?? '').split(/\s+/).filter(Boolean)
|
|
32
|
+
|
|
33
|
+
/** The selector for the first control to focus after a failed submit: an invalid control, or the first button in an
|
|
34
|
+
* invalid group. */
|
|
35
|
+
export const FIRST_INVALID = ':is(input, textarea, select, button)[aria-invalid="true"], [aria-invalid="true"] button'
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether an option's text matches what was typed: anywhere in it, ignoring case and accents. Empty text matches all.
|
|
3
|
+
* @param {string} text
|
|
4
|
+
* @param {string} query
|
|
5
|
+
*/
|
|
6
|
+
export function matches(text: string, query: string): boolean;
|
|
7
|
+
/** How many options a list holds, at most, before `search: 'auto'` gives it a search field. */
|
|
8
|
+
export const SEARCH_AFTER: 8;
|
|
9
|
+
export function wantsSearch(search: "auto" | boolean | undefined, count: number): boolean;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Narrowing a list of options by typed text: what Select and OptionList (filters/) share with their ListSearch field.
|
|
2
|
+
//
|
|
3
|
+
// const searchable = $derived(wantsSearch(search, options.length))
|
|
4
|
+
// const shown = $derived(options.filter((o) => matches(o.label, query)))
|
|
5
|
+
|
|
6
|
+
/** How many options a list holds, at most, before `search: 'auto'` gives it a search field. */
|
|
7
|
+
export const SEARCH_AFTER = 8
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Whether a list gets a search field: 'auto' when it has more than SEARCH_AFTER options, else as given.
|
|
11
|
+
* @param {'auto' | boolean | undefined} search
|
|
12
|
+
* @param {number} count
|
|
13
|
+
*/
|
|
14
|
+
export const wantsSearch = (search, count) => (search === 'auto' ? count > SEARCH_AFTER : !!search)
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Whether an option's text matches what was typed: anywhere in it, ignoring case and accents. Empty text matches all.
|
|
18
|
+
* @param {string} text
|
|
19
|
+
* @param {string} query
|
|
20
|
+
*/
|
|
21
|
+
export function matches(text, query) {
|
|
22
|
+
const q = fold(query.trim())
|
|
23
|
+
return !q || fold(text).includes(q)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** @param {string} s */
|
|
27
|
+
const fold = (s) => s.normalize('NFD').replace(/\p{M}/gu, '').toLowerCase()
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export function openMenu(x: any, y: any, items: any, label?: string, origin?: null): void;
|
|
2
|
+
/** Open below a visible trigger (the "More actions" button), aligned to its right edge. */
|
|
3
|
+
export function openMenuFrom(button: any, items: any, label: any): void;
|
|
4
|
+
/** True once the element the menu belongs to has scrolled away from where the menu was opened. */
|
|
5
|
+
export function anchorMoved(): boolean;
|
|
6
|
+
export function closeMenu({ restoreFocus }?: {
|
|
7
|
+
restoreFocus?: boolean | undefined;
|
|
8
|
+
}): void;
|
|
9
|
+
export namespace menu {
|
|
10
|
+
let open: boolean;
|
|
11
|
+
let x: number;
|
|
12
|
+
let y: number;
|
|
13
|
+
let items: never[];
|
|
14
|
+
let label: string;
|
|
15
|
+
let trigger: null;
|
|
16
|
+
}
|
|
17
|
+
export function contextMenu(getItems: any, label: any): (node: any) => () => void;
|
|
18
|
+
export type MenuItem = {
|
|
19
|
+
label?: string | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* a toggle's gets its new state; others get nothing
|
|
22
|
+
*/
|
|
23
|
+
action?: ((on: boolean) => unknown) | undefined;
|
|
24
|
+
href?: string | undefined;
|
|
25
|
+
hint?: string | undefined;
|
|
26
|
+
checked?: boolean | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* a switch on the right showing `checked`; pressing it flips it and keeps the menu open
|
|
29
|
+
*/
|
|
30
|
+
toggle?: boolean | undefined;
|
|
31
|
+
disabled?: boolean | undefined;
|
|
32
|
+
danger?: boolean | undefined;
|
|
33
|
+
children?: MenuItem[] | undefined;
|
|
34
|
+
separator?: boolean | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* an Icon name, in the column on the left (a checked item shows its check there instead)
|
|
37
|
+
*/
|
|
38
|
+
icon?: string | undefined;
|
|
39
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// One context menu for the whole app (mount <ContextMenu /> once, at the root). Elements opt in with the `contextMenu`
|
|
2
|
+
// attachment:
|
|
3
|
+
// <li {@attach contextMenu(() => rowMenu(row), 'Row actions')}>
|
|
4
|
+
// Right-click, Shift+F10 or the keyboard Menu key opens it. Inputs keep the browser's own menu.
|
|
5
|
+
//
|
|
6
|
+
// Item shape: { label, icon?, action?, href?, hint?, checked?, toggle?, disabled?, danger?, children?: Item[] } or { separator: true }
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {object} MenuItem
|
|
10
|
+
* @property {string} [label]
|
|
11
|
+
* @property {(on: boolean) => unknown} [action] a toggle's gets its new state; others get nothing
|
|
12
|
+
* @property {string} [href]
|
|
13
|
+
* @property {string} [hint]
|
|
14
|
+
* @property {boolean} [checked]
|
|
15
|
+
* @property {boolean} [toggle] a switch on the right showing `checked`; pressing it flips it and keeps the menu open
|
|
16
|
+
* @property {boolean} [disabled]
|
|
17
|
+
* @property {boolean} [danger]
|
|
18
|
+
* @property {MenuItem[]} [children]
|
|
19
|
+
* @property {boolean} [separator]
|
|
20
|
+
* @property {string} [icon] an Icon name, in the column on the left (a checked item shows its check there instead)
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export const menu = $state({ open: false, x: 0, y: 0, items: [], label: '', trigger: null })
|
|
24
|
+
let returnFocus = null
|
|
25
|
+
let anchor = null,
|
|
26
|
+
anchorRect = null // the element the menu belongs to, and where it was
|
|
27
|
+
|
|
28
|
+
// The element a right-click / Menu key menu acts on gets [data-menu-target] while it's open (outlined in css/base.css).
|
|
29
|
+
let target = null
|
|
30
|
+
function setTarget(el) {
|
|
31
|
+
target?.removeAttribute('data-menu-target')
|
|
32
|
+
target = el
|
|
33
|
+
target?.setAttribute('data-menu-target', '')
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function openMenu(x, y, items, label = 'Actions', origin = null) {
|
|
37
|
+
if (!items?.length) return
|
|
38
|
+
setTarget(null)
|
|
39
|
+
returnFocus = origin?.matches?.(':focus-within') ? document.activeElement : origin?.querySelector?.('button, a, [tabindex]') || origin
|
|
40
|
+
anchor = origin
|
|
41
|
+
anchorRect = origin?.getBoundingClientRect?.() ?? null
|
|
42
|
+
Object.assign(menu, { open: true, x, y, items, label })
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Open below a visible trigger (the "More actions" button), aligned to its right edge. */
|
|
46
|
+
export function openMenuFrom(button, items, label) {
|
|
47
|
+
const r = button.getBoundingClientRect()
|
|
48
|
+
openMenu(r.right - 220, r.bottom + 4, items, label, button)
|
|
49
|
+
menu.trigger = button
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** True once the element the menu belongs to has scrolled away from where the menu was opened. */
|
|
53
|
+
export function anchorMoved() {
|
|
54
|
+
if (!anchor || !anchorRect || !document.contains(anchor)) return true
|
|
55
|
+
const r = anchor.getBoundingClientRect()
|
|
56
|
+
return Math.abs(r.top - anchorRect.top) > 4 || Math.abs(r.left - anchorRect.left) > 4
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function closeMenu({ restoreFocus = true } = {}) {
|
|
60
|
+
if (!menu.open) return
|
|
61
|
+
menu.open = false
|
|
62
|
+
menu.items = []
|
|
63
|
+
menu.trigger = null
|
|
64
|
+
setTarget(null)
|
|
65
|
+
if (restoreFocus && returnFocus && document.contains(returnFocus)) returnFocus.focus?.()
|
|
66
|
+
returnFocus = null
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const isField = (el) => el?.closest?.('input, textarea, select, [contenteditable="true"]')
|
|
70
|
+
|
|
71
|
+
/** Svelte attachment factory. getItems runs at open time, so the menu always reflects current state. */
|
|
72
|
+
export const contextMenu = (getItems, label) => (node) => {
|
|
73
|
+
const fromPointer = (e) => {
|
|
74
|
+
if (isField(e.target)) return // keep copy/paste on text fields
|
|
75
|
+
const items = getItems()
|
|
76
|
+
if (!items?.length) return
|
|
77
|
+
e.preventDefault()
|
|
78
|
+
e.stopPropagation()
|
|
79
|
+
openMenu(e.clientX, e.clientY, items, label, node)
|
|
80
|
+
setTarget(node)
|
|
81
|
+
}
|
|
82
|
+
const fromKey = (e) => {
|
|
83
|
+
if (!(e.key === 'ContextMenu' || (e.shiftKey && e.key === 'F10')) || isField(e.target)) return
|
|
84
|
+
const items = getItems()
|
|
85
|
+
if (!items?.length) return
|
|
86
|
+
e.preventDefault()
|
|
87
|
+
e.stopPropagation()
|
|
88
|
+
const r = (e.target instanceof Element ? e.target : node).getBoundingClientRect()
|
|
89
|
+
openMenu(r.left + Math.min(24, r.width / 2), r.top + Math.min(r.height, 32), items, label, node)
|
|
90
|
+
setTarget(node)
|
|
91
|
+
}
|
|
92
|
+
node.addEventListener('contextmenu', fromPointer)
|
|
93
|
+
node.addEventListener('keydown', fromKey)
|
|
94
|
+
return () => {
|
|
95
|
+
node.removeEventListener('contextmenu', fromPointer)
|
|
96
|
+
node.removeEventListener('keydown', fromKey)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Animates an element's size when what it shows changes, so it reshapes into its new content instead of jumping (a
|
|
3
|
+
* button becoming the file it picked). Call it while a component initialises, with the element and the value whose
|
|
4
|
+
* change reshapes it:
|
|
5
|
+
*
|
|
6
|
+
* let box = $state()
|
|
7
|
+
* morph(() => box, () => file)
|
|
8
|
+
*
|
|
9
|
+
* The element is measured before the DOM updates and after; its width and height then run from the old size to the
|
|
10
|
+
* new one, the content clipped to the box meanwhile. Everything else (radius, colours) is left to CSS transitions on
|
|
11
|
+
* it. Standard curve, 300ms; nothing under reduced motion. Give the content no outro: while it plays, the old content
|
|
12
|
+
* is still in the box, and the new size would be measured with it.
|
|
13
|
+
*
|
|
14
|
+
* @param {() => HTMLElement | undefined} node
|
|
15
|
+
* @param {() => unknown} key
|
|
16
|
+
*/
|
|
17
|
+
export function morph(node: () => HTMLElement | undefined, key: () => unknown): void;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { durations, motion } from './motion.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Animates an element's size when what it shows changes, so it reshapes into its new content instead of jumping (a
|
|
5
|
+
* button becoming the file it picked). Call it while a component initialises, with the element and the value whose
|
|
6
|
+
* change reshapes it:
|
|
7
|
+
*
|
|
8
|
+
* let box = $state()
|
|
9
|
+
* morph(() => box, () => file)
|
|
10
|
+
*
|
|
11
|
+
* The element is measured before the DOM updates and after; its width and height then run from the old size to the
|
|
12
|
+
* new one, the content clipped to the box meanwhile. Everything else (radius, colours) is left to CSS transitions on
|
|
13
|
+
* it. Standard curve, 300ms; nothing under reduced motion. Give the content no outro: while it plays, the old content
|
|
14
|
+
* is still in the box, and the new size would be measured with it.
|
|
15
|
+
*
|
|
16
|
+
* @param {() => HTMLElement | undefined} node
|
|
17
|
+
* @param {() => unknown} key
|
|
18
|
+
*/
|
|
19
|
+
export function morph(node, key) {
|
|
20
|
+
/** @type {DOMRect | null} */
|
|
21
|
+
let before = null
|
|
22
|
+
|
|
23
|
+
$effect.pre(() => {
|
|
24
|
+
key()
|
|
25
|
+
before = node()?.getBoundingClientRect() ?? null
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
$effect(() => {
|
|
29
|
+
key()
|
|
30
|
+
const element = node()
|
|
31
|
+
const from = before
|
|
32
|
+
before = null
|
|
33
|
+
const duration = motion(durations.medium2)
|
|
34
|
+
if (!element || !from || !duration) return
|
|
35
|
+
const to = element.getBoundingClientRect()
|
|
36
|
+
if (Math.abs(from.width - to.width) < 1 && Math.abs(from.height - to.height) < 1) return
|
|
37
|
+
element.animate(
|
|
38
|
+
[
|
|
39
|
+
{ width: `${from.width}px`, height: `${from.height}px`, overflow: 'clip' },
|
|
40
|
+
{ width: `${to.width}px`, height: `${to.height}px`, overflow: 'clip' },
|
|
41
|
+
],
|
|
42
|
+
// Material 3's standard curve (easings.standard.standard), as CSS: a WAAPI easing can't be a function.
|
|
43
|
+
{ duration, easing: 'cubic-bezier(0.2, 0, 0, 1)' },
|
|
44
|
+
)
|
|
45
|
+
})
|
|
46
|
+
}
|