@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,172 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import { getContext, tick } from 'svelte'
|
|
3
|
+
import Thumb from './Thumb.svelte'
|
|
4
|
+
import ErrorMessage from '../ErrorMessage.svelte'
|
|
5
|
+
import { IMAGE_MAX_SIZE, merge, oversize, rejection } from './common.js'
|
|
6
|
+
import { accepts, droppedFiles, fileDrop } from '../../lib/files.js'
|
|
7
|
+
import { announce } from '../../lib/announce.js'
|
|
8
|
+
import { UPLOAD_ZONE } from './context.js'
|
|
9
|
+
import { flip, scale } from '../../lib/motion.js'
|
|
10
|
+
/**
|
|
11
|
+
* Any number of images, picked or dropped: a wrapping grid of square thumbnails (each with its name and size, and a ✕
|
|
12
|
+
* to remove it), ended by a dashed tile that adds more. Picking again adds to the grid; an image already in it is not
|
|
13
|
+
* added twice. Import it as Upload.Images (components/upload/index.js).
|
|
14
|
+
*
|
|
15
|
+
* <Upload.Images bind:files />
|
|
16
|
+
*
|
|
17
|
+
* files: the chosen Files (bindable; a new array on every change). onchange(files): after a pick, a drop or a removal.
|
|
18
|
+
* label: the add tile's text (default "Add images"). accept: as on a file input (default "image/*"); dropped files are
|
|
19
|
+
* held to it too, and a drop with nothing that fits says so under the control. maxSize: the largest image taken, in
|
|
20
|
+
* bytes (default 10 MB; Infinity for none); bigger ones, picked or dropped, are left out and the reason shows under
|
|
21
|
+
* the control, while the rest are added. id: on the add tile. disabled. error: a
|
|
22
|
+
* message from the app, in a bubble under the control. Other attributes (class…) and attachments go to the wrapping
|
|
23
|
+
* <div>, which is also where images can be dropped.
|
|
24
|
+
*
|
|
25
|
+
* Keyboard: Enter or Space on the add tile opens the picker. Tab goes through each thumbnail's ✕; removing one moves
|
|
26
|
+
* the focus to the next ✕ (the previous one after the last, the add tile once the grid is empty).
|
|
27
|
+
*
|
|
28
|
+
* @typedef {object} Props
|
|
29
|
+
* @property {File[]} [files]
|
|
30
|
+
* @property {(files: File[]) => void} [onchange]
|
|
31
|
+
* @property {string} [label]
|
|
32
|
+
* @property {string} [accept]
|
|
33
|
+
* @property {number} [maxSize]
|
|
34
|
+
* @property {string} [id]
|
|
35
|
+
* @property {boolean} [disabled]
|
|
36
|
+
* @property {string} [error]
|
|
37
|
+
*/
|
|
38
|
+
/** @type {Props & Omit<import('svelte/elements').HTMLAttributes<HTMLDivElement>, keyof Props>} */
|
|
39
|
+
let {
|
|
40
|
+
files = $bindable([]),
|
|
41
|
+
onchange,
|
|
42
|
+
label = 'Add images',
|
|
43
|
+
accept = 'image/*',
|
|
44
|
+
maxSize = IMAGE_MAX_SIZE,
|
|
45
|
+
id,
|
|
46
|
+
disabled = false,
|
|
47
|
+
error = '',
|
|
48
|
+
class: className,
|
|
49
|
+
...rest
|
|
50
|
+
} = $props()
|
|
51
|
+
|
|
52
|
+
const uid = $props.id()
|
|
53
|
+
/** @type {HTMLInputElement | undefined} */
|
|
54
|
+
let input = $state()
|
|
55
|
+
/** @type {HTMLButtonElement | undefined} */
|
|
56
|
+
let main = $state()
|
|
57
|
+
/** @type {HTMLUListElement | undefined} */
|
|
58
|
+
let grid = $state()
|
|
59
|
+
let dragging = $state(false)
|
|
60
|
+
let rejected = $state('')
|
|
61
|
+
const problem = $derived(error || rejected)
|
|
62
|
+
|
|
63
|
+
// Inside an Upload.Zone, the zone takes the drops and hands them here.
|
|
64
|
+
/** @type {import('./context.js').UploadZoneContext | undefined} */
|
|
65
|
+
const zone = getContext(UPLOAD_ZONE)
|
|
66
|
+
$effect(() => {
|
|
67
|
+
if (zone && !disabled) return zone.register(ondrop, 'Drop images here')
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
/** @param {File[]} more */
|
|
71
|
+
function add(more) {
|
|
72
|
+
rejected = ''
|
|
73
|
+
const next = merge(files, more)
|
|
74
|
+
if (next === files) return
|
|
75
|
+
const count = next.length - files.length
|
|
76
|
+
files = next
|
|
77
|
+
onchange?.(next)
|
|
78
|
+
announce(count === 1 ? `${next.at(-1)?.name} added` : `${count} images added`)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** @param {File} file */
|
|
82
|
+
async function remove(file) {
|
|
83
|
+
const index = files.indexOf(file)
|
|
84
|
+
files = files.filter((other) => other !== file)
|
|
85
|
+
onchange?.(files)
|
|
86
|
+
announce(`${file.name} removed`)
|
|
87
|
+
// The thumbnail is on its way out (inert while it shrinks away): the focus goes to the ✕ now in its place.
|
|
88
|
+
await tick()
|
|
89
|
+
const clears = grid ? [...grid.querySelectorAll(':scope > li:not([inert]) .clear')] : []
|
|
90
|
+
const next = /** @type {HTMLElement | undefined} */ (clears[index] ?? clears.at(-1))
|
|
91
|
+
;(next ?? main)?.focus()
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Adds those that fit their type and size, and says why any were left out. The picker checks neither: its "All
|
|
95
|
+
* files" option lets any file through, and sizes are never checked. @param {File[]} more */
|
|
96
|
+
function take(more) {
|
|
97
|
+
const typed = more.filter((file) => accepts(file, accept))
|
|
98
|
+
const fit = typed.filter((file) => file.size <= maxSize)
|
|
99
|
+
const big = typed.filter((file) => file.size > maxSize)
|
|
100
|
+
if (fit.length) add(fit)
|
|
101
|
+
if (big.length) rejected = oversize(big, maxSize, 'images')
|
|
102
|
+
else if (!typed.length) rejected = rejection(accept, more.length > 0)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** @param {import('../../lib/files.js').Dropped} drop */
|
|
106
|
+
function ondrop(drop) {
|
|
107
|
+
if (!disabled) take(droppedFiles(drop))
|
|
108
|
+
}
|
|
109
|
+
</script>
|
|
110
|
+
|
|
111
|
+
<div class={['upload', className]} {...rest} {@attach fileDrop({ ondrop, ondragging: (over) => (dragging = over), disabled: disabled || !!zone })}>
|
|
112
|
+
<ul bind:this={grid} class="grid" aria-label="Chosen images">
|
|
113
|
+
{#each files as file (file)}
|
|
114
|
+
<li animate:flip transition:scale={{ start: 0.9 }}>
|
|
115
|
+
<Thumb {file} onclear={() => remove(file)} {disabled} />
|
|
116
|
+
</li>
|
|
117
|
+
{/each}
|
|
118
|
+
<li>
|
|
119
|
+
<Thumb
|
|
120
|
+
bind:main
|
|
121
|
+
file={null}
|
|
122
|
+
{label}
|
|
123
|
+
icon="plus"
|
|
124
|
+
square
|
|
125
|
+
onpick={() => input?.click()}
|
|
126
|
+
{disabled}
|
|
127
|
+
dragging={dragging || !!zone?.dragging}
|
|
128
|
+
{id}
|
|
129
|
+
errorId={!!problem && `${uid}-error`}
|
|
130
|
+
/>
|
|
131
|
+
</li>
|
|
132
|
+
</ul>
|
|
133
|
+
<!-- Closing the file chooser fires a cancel event that bubbles: inside a <dialog> it would close the dialog too. -->
|
|
134
|
+
<input
|
|
135
|
+
bind:this={input}
|
|
136
|
+
type="file"
|
|
137
|
+
{accept}
|
|
138
|
+
multiple
|
|
139
|
+
hidden
|
|
140
|
+
onchange={(event) => {
|
|
141
|
+
const picked = [...(event.currentTarget.files ?? [])]
|
|
142
|
+
event.currentTarget.value = ''
|
|
143
|
+
if (picked.length) take(picked)
|
|
144
|
+
}}
|
|
145
|
+
oncancel={(event) => event.stopPropagation()}
|
|
146
|
+
/>
|
|
147
|
+
<ErrorMessage
|
|
148
|
+
floating
|
|
149
|
+
id="{uid}-error"
|
|
150
|
+
message={problem}
|
|
151
|
+
ondismiss={() => {
|
|
152
|
+
rejected = ''
|
|
153
|
+
main?.focus()
|
|
154
|
+
}}
|
|
155
|
+
/>
|
|
156
|
+
</div>
|
|
157
|
+
|
|
158
|
+
<style>
|
|
159
|
+
.upload {
|
|
160
|
+
position: relative;
|
|
161
|
+
max-width: 100%;
|
|
162
|
+
}
|
|
163
|
+
.grid {
|
|
164
|
+
display: flex;
|
|
165
|
+
flex-wrap: wrap;
|
|
166
|
+
align-items: flex-start;
|
|
167
|
+
gap: var(--space-3) var(--space-2);
|
|
168
|
+
margin: 0;
|
|
169
|
+
padding: 0;
|
|
170
|
+
list-style: none;
|
|
171
|
+
}
|
|
172
|
+
</style>
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export default UploadImages;
|
|
2
|
+
type UploadImages = {
|
|
3
|
+
$on?(type: string, callback: (e: any) => void): () => void;
|
|
4
|
+
$set?(props: Partial<Props & Omit<HTMLAttributes<HTMLDivElement>, keyof Props>>): void;
|
|
5
|
+
};
|
|
6
|
+
declare const UploadImages: import("svelte").Component<{
|
|
7
|
+
files?: File[] | undefined;
|
|
8
|
+
onchange?: ((files: File[]) => void) | undefined;
|
|
9
|
+
label?: string | undefined;
|
|
10
|
+
accept?: string | undefined;
|
|
11
|
+
maxSize?: number | undefined;
|
|
12
|
+
id?: string | undefined;
|
|
13
|
+
disabled?: boolean | undefined;
|
|
14
|
+
error?: string | undefined;
|
|
15
|
+
} & Omit<import("svelte/elements").HTMLAttributes<HTMLDivElement>, keyof {
|
|
16
|
+
files?: File[] | undefined;
|
|
17
|
+
onchange?: ((files: File[]) => void) | undefined;
|
|
18
|
+
label?: string | undefined;
|
|
19
|
+
accept?: string | undefined;
|
|
20
|
+
maxSize?: number | undefined;
|
|
21
|
+
id?: string | undefined;
|
|
22
|
+
disabled?: boolean | undefined;
|
|
23
|
+
error?: string | undefined;
|
|
24
|
+
}>, {}, "files">;
|
|
25
|
+
/**
|
|
26
|
+
* Any number of images, picked or dropped: a wrapping grid of square thumbnails (each with its name and size, and a ✕
|
|
27
|
+
* to remove it), ended by a dashed tile that adds more. Picking again adds to the grid; an image already in it is not
|
|
28
|
+
* added twice. Import it as Upload.Images (components/upload/index.js).
|
|
29
|
+
*
|
|
30
|
+
* <Upload.Images bind:files />
|
|
31
|
+
*
|
|
32
|
+
* files: the chosen Files (bindable; a new array on every change). onchange(files): after a pick, a drop or a removal.
|
|
33
|
+
* label: the add tile's text (default "Add images"). accept: as on a file input (default "image/*"); dropped files are
|
|
34
|
+
* held to it too, and a drop with nothing that fits says so under the control. maxSize: the largest image taken, in
|
|
35
|
+
* bytes (default 10 MB; Infinity for none); bigger ones, picked or dropped, are left out and the reason shows under
|
|
36
|
+
* the control, while the rest are added. id: on the add tile. disabled. error: a
|
|
37
|
+
* message from the app, in a bubble under the control. Other attributes (class…) and attachments go to the wrapping
|
|
38
|
+
* <div>, which is also where images can be dropped.
|
|
39
|
+
*
|
|
40
|
+
* Keyboard: Enter or Space on the add tile opens the picker. Tab goes through each thumbnail's ✕; removing one moves
|
|
41
|
+
* the focus to the next ✕ (the previous one after the last, the add tile once the grid is empty).
|
|
42
|
+
*/
|
|
43
|
+
type Props = {
|
|
44
|
+
files?: File[] | undefined;
|
|
45
|
+
onchange?: ((files: File[]) => void) | undefined;
|
|
46
|
+
label?: string | undefined;
|
|
47
|
+
accept?: string | undefined;
|
|
48
|
+
maxSize?: number | undefined;
|
|
49
|
+
id?: string | undefined;
|
|
50
|
+
disabled?: boolean | undefined;
|
|
51
|
+
error?: string | undefined;
|
|
52
|
+
};
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import { setContext, untrack } from 'svelte'
|
|
3
|
+
import Icon from '../Icon.svelte'
|
|
4
|
+
import { fileDrop } from '../../lib/files.js'
|
|
5
|
+
import { fade, durations } from '../../lib/motion.js'
|
|
6
|
+
import { UPLOAD_ZONE } from './context.js'
|
|
7
|
+
/**
|
|
8
|
+
* A larger place to drop files on, for the Upload inside it: whatever is dropped anywhere on the zone goes to that
|
|
9
|
+
* Upload as if dropped on its button (held to its `accept`, with its error under it). Import it as Upload.Zone
|
|
10
|
+
* (components/upload/index.js).
|
|
11
|
+
*
|
|
12
|
+
* <Upload.Zone>
|
|
13
|
+
* <Upload.Images bind:files />
|
|
14
|
+
* </Upload.Zone>
|
|
15
|
+
*
|
|
16
|
+
* <Upload.Zone bare> any area (a card, a dialog's body), marked only while files are over it
|
|
17
|
+
* …the area's content…
|
|
18
|
+
* <Upload.Folder bind:folder />
|
|
19
|
+
* </Upload.Zone>
|
|
20
|
+
*
|
|
21
|
+
* By default it is a dashed area: an icon and the hint over its content, all centred. bare: no frame of its own (it
|
|
22
|
+
* takes its parent's corner radius); while files are dragged over it, a dashed overlay with the hint covers it.
|
|
23
|
+
* hint: the line saying what to drop (default: the Upload's own, such as "Drop images here"). One Upload per zone;
|
|
24
|
+
* with more, the first takes the drops. While that Upload is disabled the zone takes nothing.
|
|
25
|
+
* Other attributes (class…) and attachments go to the <div>.
|
|
26
|
+
*
|
|
27
|
+
* Keyboard: the zone is not a control. The Upload inside is reached and used as usual.
|
|
28
|
+
*
|
|
29
|
+
* @typedef {object} Props
|
|
30
|
+
* @property {boolean} [bare]
|
|
31
|
+
* @property {string} [hint]
|
|
32
|
+
* @property {import('svelte').Snippet} [children]
|
|
33
|
+
*/
|
|
34
|
+
/** @type {Props & Omit<import('svelte/elements').HTMLAttributes<HTMLDivElement>, keyof Props>} */
|
|
35
|
+
let { bare = false, hint, class: className, children, ...rest } = $props()
|
|
36
|
+
|
|
37
|
+
/** @type {{ take: (drop: import('../../lib/files.js').Dropped) => void, hint: string }[]} */
|
|
38
|
+
let takers = $state.raw([])
|
|
39
|
+
let dragging = $state(false)
|
|
40
|
+
const line = $derived(hint ?? takers[0]?.hint ?? 'Drop files here')
|
|
41
|
+
|
|
42
|
+
/** @type {import('./context.js').UploadZoneContext} */
|
|
43
|
+
const zone = {
|
|
44
|
+
get dragging() {
|
|
45
|
+
return dragging
|
|
46
|
+
},
|
|
47
|
+
// Called from the Upload's effect: untracked, so that effect doesn't rerun as the list changes.
|
|
48
|
+
register: (take, ownHint) =>
|
|
49
|
+
untrack(() => {
|
|
50
|
+
const taker = { take, hint: ownHint }
|
|
51
|
+
takers = [...takers, taker]
|
|
52
|
+
return () => {
|
|
53
|
+
takers = takers.filter((other) => other !== taker)
|
|
54
|
+
}
|
|
55
|
+
}),
|
|
56
|
+
}
|
|
57
|
+
setContext(UPLOAD_ZONE, zone)
|
|
58
|
+
</script>
|
|
59
|
+
|
|
60
|
+
<div
|
|
61
|
+
class={['zone', bare ? 'bare' : 'framed', dragging && 'dragging', className]}
|
|
62
|
+
{...rest}
|
|
63
|
+
{@attach fileDrop({ ondrop: (drop) => takers[0]?.take(drop), ondragging: (over) => (dragging = over), disabled: !takers.length })}
|
|
64
|
+
>
|
|
65
|
+
{#if !bare}
|
|
66
|
+
<p class="hint"><Icon name="upload" size="lg" />{line}</p>
|
|
67
|
+
{/if}
|
|
68
|
+
{@render children?.()}
|
|
69
|
+
{#if bare && dragging}
|
|
70
|
+
<div class="overlay" aria-hidden="true" transition:fade={{ duration: durations.short3 }}><Icon name="upload" />{line}</div>
|
|
71
|
+
{/if}
|
|
72
|
+
</div>
|
|
73
|
+
|
|
74
|
+
<style>
|
|
75
|
+
.zone {
|
|
76
|
+
position: relative;
|
|
77
|
+
min-width: 0;
|
|
78
|
+
}
|
|
79
|
+
.framed {
|
|
80
|
+
display: flex;
|
|
81
|
+
flex-direction: column;
|
|
82
|
+
align-items: center;
|
|
83
|
+
justify-content: center;
|
|
84
|
+
gap: var(--space-3);
|
|
85
|
+
min-height: 140px;
|
|
86
|
+
padding: var(--space-4);
|
|
87
|
+
border: 2px dashed var(--line-strong);
|
|
88
|
+
border-radius: var(--r-lg);
|
|
89
|
+
background: var(--control-1);
|
|
90
|
+
transition:
|
|
91
|
+
border-color var(--dur) var(--ease-out),
|
|
92
|
+
background-color var(--dur) var(--ease-out);
|
|
93
|
+
}
|
|
94
|
+
.framed.dragging {
|
|
95
|
+
border-color: var(--accent);
|
|
96
|
+
background: var(--accent-soft);
|
|
97
|
+
}
|
|
98
|
+
.bare {
|
|
99
|
+
border-radius: inherit;
|
|
100
|
+
}
|
|
101
|
+
.hint {
|
|
102
|
+
display: flex;
|
|
103
|
+
flex-direction: column;
|
|
104
|
+
align-items: center;
|
|
105
|
+
gap: var(--space-1);
|
|
106
|
+
margin: 0;
|
|
107
|
+
color: var(--fg-2);
|
|
108
|
+
font-size: 13px;
|
|
109
|
+
text-align: center;
|
|
110
|
+
}
|
|
111
|
+
.hint > :global(.icon) {
|
|
112
|
+
color: var(--muted);
|
|
113
|
+
}
|
|
114
|
+
/* Over the content, letting the drag events through to it (the zone counts them as they cross its children). On a
|
|
115
|
+
solid surface under the tint, so the content doesn't show through. */
|
|
116
|
+
.overlay {
|
|
117
|
+
position: absolute;
|
|
118
|
+
inset: 0;
|
|
119
|
+
display: flex;
|
|
120
|
+
align-items: center;
|
|
121
|
+
justify-content: center;
|
|
122
|
+
gap: var(--space-2);
|
|
123
|
+
border: 2px dashed var(--accent);
|
|
124
|
+
border-radius: inherit;
|
|
125
|
+
background: linear-gradient(var(--accent-soft), var(--accent-soft)), var(--surface);
|
|
126
|
+
color: var(--fg);
|
|
127
|
+
font-size: 13px;
|
|
128
|
+
pointer-events: none;
|
|
129
|
+
}
|
|
130
|
+
</style>
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export default UploadZone;
|
|
2
|
+
type UploadZone = {
|
|
3
|
+
$on?(type: string, callback: (e: any) => void): () => void;
|
|
4
|
+
$set?(props: Partial<Props & Omit<HTMLAttributes<HTMLDivElement>, keyof Props>>): void;
|
|
5
|
+
};
|
|
6
|
+
declare const UploadZone: import("svelte").Component<{
|
|
7
|
+
bare?: boolean | undefined;
|
|
8
|
+
hint?: string | undefined;
|
|
9
|
+
children?: import("svelte").Snippet<[]> | undefined;
|
|
10
|
+
} & Omit<import("svelte/elements").HTMLAttributes<HTMLDivElement>, keyof {
|
|
11
|
+
bare?: boolean | undefined;
|
|
12
|
+
hint?: string | undefined;
|
|
13
|
+
children?: import("svelte").Snippet<[]> | undefined;
|
|
14
|
+
}>, {}, "">;
|
|
15
|
+
/**
|
|
16
|
+
* A larger place to drop files on, for the Upload inside it: whatever is dropped anywhere on the zone goes to that
|
|
17
|
+
* Upload as if dropped on its button (held to its `accept`, with its error under it). Import it as Upload.Zone
|
|
18
|
+
* (components/upload/index.js).
|
|
19
|
+
*
|
|
20
|
+
* <Upload.Zone>
|
|
21
|
+
* <Upload.Images bind:files />
|
|
22
|
+
* </Upload.Zone>
|
|
23
|
+
*
|
|
24
|
+
* <Upload.Zone bare> any area (a card, a dialog's body), marked only while files are over it
|
|
25
|
+
* …the area's content…
|
|
26
|
+
* <Upload.Folder bind:folder />
|
|
27
|
+
* </Upload.Zone>
|
|
28
|
+
*
|
|
29
|
+
* By default it is a dashed area: an icon and the hint over its content, all centred. bare: no frame of its own (it
|
|
30
|
+
* takes its parent's corner radius); while files are dragged over it, a dashed overlay with the hint covers it.
|
|
31
|
+
* hint: the line saying what to drop (default: the Upload's own, such as "Drop images here"). One Upload per zone;
|
|
32
|
+
* with more, the first takes the drops. While that Upload is disabled the zone takes nothing.
|
|
33
|
+
* Other attributes (class…) and attachments go to the <div>.
|
|
34
|
+
*
|
|
35
|
+
* Keyboard: the zone is not a control. The Upload inside is reached and used as usual.
|
|
36
|
+
*/
|
|
37
|
+
type Props = {
|
|
38
|
+
bare?: boolean | undefined;
|
|
39
|
+
hint?: string | undefined;
|
|
40
|
+
children?: import("svelte").Snippet<[]> | undefined;
|
|
41
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Why nothing in a drop was taken: none of its files fit `accept`, or it held only folders.
|
|
3
|
+
* @param {string} accept
|
|
4
|
+
* @param {boolean} hadFiles
|
|
5
|
+
*/
|
|
6
|
+
export function rejection(accept: string, hadFiles: boolean): string;
|
|
7
|
+
/**
|
|
8
|
+
* Why files were left out for their size: one by name, several by count.
|
|
9
|
+
* @param {File[]} skipped
|
|
10
|
+
* @param {number} maxSize
|
|
11
|
+
* @param {string} [noun] what several are called
|
|
12
|
+
*/
|
|
13
|
+
export function oversize(skipped: File[], maxSize: number, noun?: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* The list with `more` added at the end, leaving out files it already holds.
|
|
16
|
+
* @param {File[]} files
|
|
17
|
+
* @param {File[]} more
|
|
18
|
+
*/
|
|
19
|
+
export function merge(files: File[], more: File[]): File[];
|
|
20
|
+
/**
|
|
21
|
+
* A file's format from its name: the extension to show on its icon ('PDF'; empty when it has none, or one too long to
|
|
22
|
+
* fit, over four letters) and the tone to colour it in (a data-tone name, or 'accent').
|
|
23
|
+
* @param {string} name
|
|
24
|
+
*/
|
|
25
|
+
export function fileFormat(name: string): {
|
|
26
|
+
ext: string;
|
|
27
|
+
tone: string;
|
|
28
|
+
};
|
|
29
|
+
/** The default size limit for images: 10 MB. */
|
|
30
|
+
export const IMAGE_MAX_SIZE: number;
|
|
31
|
+
export function plural(count: number): string;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// What the Upload components share: how they word what they turn away (a type they don't take, a file too big), the
|
|
2
|
+
// default size limit for images, and how a list takes more files.
|
|
3
|
+
import { fileSize } from '../../lib/files.js'
|
|
4
|
+
|
|
5
|
+
const list = new Intl.ListFormat('en-GB', { type: 'disjunction' })
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* An accept list in words: 'image/*,.pdf' → 'image or PDF'.
|
|
9
|
+
* @param {string} accept
|
|
10
|
+
*/
|
|
11
|
+
function kinds(accept) {
|
|
12
|
+
const words = accept
|
|
13
|
+
.split(',')
|
|
14
|
+
.map((rule) => rule.trim())
|
|
15
|
+
.filter(Boolean)
|
|
16
|
+
.map((rule) => (rule.startsWith('.') ? rule.slice(1).toUpperCase() : rule.endsWith('/*') ? rule.slice(0, -2) : (rule.split('/')[1] ?? rule).toUpperCase()))
|
|
17
|
+
return list.format([...new Set(words)])
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Why nothing in a drop was taken: none of its files fit `accept`, or it held only folders.
|
|
22
|
+
* @param {string} accept
|
|
23
|
+
* @param {boolean} hadFiles
|
|
24
|
+
*/
|
|
25
|
+
export function rejection(accept, hadFiles) {
|
|
26
|
+
return hadFiles && accept ? `Only ${kinds(accept)} files can go here.` : 'Only files can go here, not folders.'
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Why files were left out for their size: one by name, several by count.
|
|
31
|
+
* @param {File[]} skipped
|
|
32
|
+
* @param {number} maxSize
|
|
33
|
+
* @param {string} [noun] what several are called
|
|
34
|
+
*/
|
|
35
|
+
export function oversize(skipped, maxSize, noun = 'files') {
|
|
36
|
+
const limit = fileSize(maxSize)
|
|
37
|
+
const [one] = skipped
|
|
38
|
+
return skipped.length === 1 && one ? `${one.name} is over ${limit}.` : `${skipped.length} ${noun} are over ${limit} and were left out.`
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The default size limit for images: 10 MB. */
|
|
42
|
+
export const IMAGE_MAX_SIZE = 10 * 1024 ** 2
|
|
43
|
+
|
|
44
|
+
/** @param {File} a @param {File} b */
|
|
45
|
+
const same = (a, b) => a.name === b.name && a.size === b.size && a.lastModified === b.lastModified
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The list with `more` added at the end, leaving out files it already holds.
|
|
49
|
+
* @param {File[]} files
|
|
50
|
+
* @param {File[]} more
|
|
51
|
+
*/
|
|
52
|
+
export function merge(files, more) {
|
|
53
|
+
const added = more.filter((file, i) => !files.some((had) => same(had, file)) && more.findIndex((other) => same(other, file)) === i)
|
|
54
|
+
return added.length ? [...files, ...added] : files
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// File formats by extension, coloured by family: documents blue, spreadsheets green, slides yellow, PDF red, images and
|
|
58
|
+
// media in the accent; anything else (archives, code, the unknown) grey.
|
|
59
|
+
const TONES = {
|
|
60
|
+
danger: ['pdf'],
|
|
61
|
+
info: ['doc', 'docx', 'odt', 'rtf', 'txt', 'md', 'pages'],
|
|
62
|
+
success: ['xls', 'xlsx', 'csv', 'tsv', 'ods', 'numbers'],
|
|
63
|
+
warning: ['ppt', 'pptx', 'odp', 'key'],
|
|
64
|
+
accent: [
|
|
65
|
+
'png',
|
|
66
|
+
'jpg',
|
|
67
|
+
'jpeg',
|
|
68
|
+
'gif',
|
|
69
|
+
'webp',
|
|
70
|
+
'avif',
|
|
71
|
+
'svg',
|
|
72
|
+
'heic',
|
|
73
|
+
'bmp',
|
|
74
|
+
'ico',
|
|
75
|
+
'tif',
|
|
76
|
+
'tiff',
|
|
77
|
+
'mp3',
|
|
78
|
+
'wav',
|
|
79
|
+
'flac',
|
|
80
|
+
'm4a',
|
|
81
|
+
'ogg',
|
|
82
|
+
'mp4',
|
|
83
|
+
'mov',
|
|
84
|
+
'webm',
|
|
85
|
+
'mkv',
|
|
86
|
+
'avi',
|
|
87
|
+
],
|
|
88
|
+
}
|
|
89
|
+
const TONE_OF = new Map(Object.entries(TONES).flatMap(([tone, extensions]) => extensions.map((extension) => [extension, tone])))
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* A file's format from its name: the extension to show on its icon ('PDF'; empty when it has none, or one too long to
|
|
93
|
+
* fit, over four letters) and the tone to colour it in (a data-tone name, or 'accent').
|
|
94
|
+
* @param {string} name
|
|
95
|
+
*/
|
|
96
|
+
export function fileFormat(name) {
|
|
97
|
+
const dot = name.lastIndexOf('.')
|
|
98
|
+
const extension = dot > 0 ? name.slice(dot + 1).toLowerCase() : ''
|
|
99
|
+
return { ext: extension.length <= 4 ? extension.toUpperCase() : '', tone: TONE_OF.get(extension) ?? 'neutral' }
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** @param {number} count */
|
|
103
|
+
export const plural = (count) => `${count} ${count === 1 ? 'file' : 'files'}`
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {object} UploadZoneContext
|
|
3
|
+
* @property {boolean} dragging files are dragged over the zone
|
|
4
|
+
* @property {(take: (drop: import('../../lib/files.js').Dropped) => void, hint: string) => () => void} register makes an
|
|
5
|
+
* Upload the one that takes the zone's drops, with the line saying what to drop; returns its removal
|
|
6
|
+
*/
|
|
7
|
+
export const UPLOAD_ZONE: unique symbol;
|
|
8
|
+
export type UploadZoneContext = {
|
|
9
|
+
/**
|
|
10
|
+
* files are dragged over the zone
|
|
11
|
+
*/
|
|
12
|
+
dragging: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* makes an
|
|
15
|
+
* Upload the one that takes the zone's drops, with the line saying what to drop; returns its removal
|
|
16
|
+
*/
|
|
17
|
+
register: (take: (drop: import("../../lib/files.js").Dropped) => void, hint: string) => () => void;
|
|
18
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// What an Upload.Zone hands the Upload inside it, so files dropped anywhere on the zone reach that Upload.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {object} UploadZoneContext
|
|
5
|
+
* @property {boolean} dragging files are dragged over the zone
|
|
6
|
+
* @property {(take: (drop: import('../../lib/files.js').Dropped) => void, hint: string) => () => void} register makes an
|
|
7
|
+
* Upload the one that takes the zone's drops, with the line saying what to drop; returns its removal
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export const UPLOAD_ZONE = Symbol('upload-zone')
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
declare namespace _default {
|
|
2
|
+
export { UploadFile as File };
|
|
3
|
+
export { UploadFiles as Files };
|
|
4
|
+
export { UploadFolder as Folder };
|
|
5
|
+
export { UploadImage as Image };
|
|
6
|
+
export { UploadImages as Images };
|
|
7
|
+
export { UploadZone as Zone };
|
|
8
|
+
}
|
|
9
|
+
export default _default;
|
|
10
|
+
import UploadFile from './UploadFile.svelte';
|
|
11
|
+
import UploadFiles from './UploadFiles.svelte';
|
|
12
|
+
import UploadFolder from './UploadFolder.svelte';
|
|
13
|
+
import UploadImage from './UploadImage.svelte';
|
|
14
|
+
import UploadImages from './UploadImages.svelte';
|
|
15
|
+
import UploadZone from './UploadZone.svelte';
|
|
16
|
+
export { UploadFile, UploadFiles, UploadFolder, UploadImage, UploadImages, UploadZone };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// File pickers as one import. Each is a button that becomes what it picked (a card, or a thumbnail for images), with a
|
|
2
|
+
// ✕ to clear it, and each takes files dropped on it:
|
|
3
|
+
// <Upload.File> one file <Upload.Files> any number, as a list of cards
|
|
4
|
+
// <Upload.Folder> a folder's files <Upload.Image> one image <Upload.Images> any number, as a grid
|
|
5
|
+
// <Upload.Zone> around one of them gives it a larger place to drop on: a dashed area, or with `bare` any area at all.
|
|
6
|
+
//
|
|
7
|
+
// import Upload from '@xerios/design-system/components/upload/index.js'
|
|
8
|
+
// <Upload.File bind:file accept=".pdf" />
|
|
9
|
+
// <Upload.Zone><Upload.Images bind:files /></Upload.Zone>
|
|
10
|
+
import UploadFile from './UploadFile.svelte'
|
|
11
|
+
import UploadFiles from './UploadFiles.svelte'
|
|
12
|
+
import UploadFolder from './UploadFolder.svelte'
|
|
13
|
+
import UploadImage from './UploadImage.svelte'
|
|
14
|
+
import UploadImages from './UploadImages.svelte'
|
|
15
|
+
import UploadZone from './UploadZone.svelte'
|
|
16
|
+
|
|
17
|
+
export default { File: UploadFile, Files: UploadFiles, Folder: UploadFolder, Image: UploadImage, Images: UploadImages, Zone: UploadZone }
|
|
18
|
+
export { UploadFile, UploadFiles, UploadFolder, UploadImage, UploadImages, UploadZone }
|