@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.
Files changed (214) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +403 -2
  3. package/dist/components/AnchorHeading.svelte +79 -0
  4. package/dist/components/AnchorHeading.svelte.d.ts +28 -0
  5. package/dist/components/Breadcrumbs.svelte +216 -0
  6. package/dist/components/Breadcrumbs.svelte.d.ts +37 -0
  7. package/dist/components/Button.svelte +178 -0
  8. package/dist/components/Button.svelte.d.ts +58 -0
  9. package/dist/components/Canvas.svelte +248 -0
  10. package/dist/components/Canvas.svelte.d.ts +91 -0
  11. package/dist/components/Collapsible.svelte +139 -0
  12. package/dist/components/Collapsible.svelte.d.ts +52 -0
  13. package/dist/components/CollapsibleAdvanced.svelte +174 -0
  14. package/dist/components/CollapsibleAdvanced.svelte.d.ts +43 -0
  15. package/dist/components/DashOutline.svelte +47 -0
  16. package/dist/components/DashOutline.svelte.d.ts +6 -0
  17. package/dist/components/Dialogs.svelte +123 -0
  18. package/dist/components/Dialogs.svelte.d.ts +6 -0
  19. package/dist/components/Drawer.svelte +231 -0
  20. package/dist/components/Drawer.svelte.d.ts +62 -0
  21. package/dist/components/ErrorMessage.svelte +140 -0
  22. package/dist/components/ErrorMessage.svelte.d.ts +41 -0
  23. package/dist/components/Icon.svelte +94 -0
  24. package/dist/components/Icon.svelte.d.ts +33 -0
  25. package/dist/components/InfoTip.svelte +183 -0
  26. package/dist/components/InfoTip.svelte.d.ts +28 -0
  27. package/dist/components/Md.svelte +28 -0
  28. package/dist/components/Md.svelte.d.ts +11 -0
  29. package/dist/components/Modal.svelte +91 -0
  30. package/dist/components/Modal.svelte.d.ts +41 -0
  31. package/dist/components/Notice.svelte +50 -0
  32. package/dist/components/Notice.svelte.d.ts +30 -0
  33. package/dist/components/Popover.svelte +212 -0
  34. package/dist/components/Popover.svelte.d.ts +70 -0
  35. package/dist/components/Shimmer.svelte +27 -0
  36. package/dist/components/Shimmer.svelte.d.ts +22 -0
  37. package/dist/components/Splitter.svelte +152 -0
  38. package/dist/components/Splitter.svelte.d.ts +41 -0
  39. package/dist/components/Table.svelte +448 -0
  40. package/dist/components/Table.svelte.d.ts +268 -0
  41. package/dist/components/Toast.svelte +273 -0
  42. package/dist/components/Toast.svelte.d.ts +31 -0
  43. package/dist/components/Tooltip.svelte +97 -0
  44. package/dist/components/Tooltip.svelte.d.ts +19 -0
  45. package/dist/components/WorkingBorder.svelte +105 -0
  46. package/dist/components/WorkingBorder.svelte.d.ts +26 -0
  47. package/dist/components/empty/Empty.svelte +55 -0
  48. package/dist/components/empty/Empty.svelte.d.ts +39 -0
  49. package/dist/components/empty/EmptyError.svelte +47 -0
  50. package/dist/components/empty/EmptyError.svelte.d.ts +28 -0
  51. package/dist/components/empty/index.d.ts +18 -0
  52. package/dist/components/empty/index.js +10 -0
  53. package/dist/components/filters/DateFilter.svelte +59 -0
  54. package/dist/components/filters/DateFilter.svelte.d.ts +23 -0
  55. package/dist/components/filters/FilterBar.svelte +342 -0
  56. package/dist/components/filters/FilterBar.svelte.d.ts +283 -0
  57. package/dist/components/filters/FilterChip.svelte +235 -0
  58. package/dist/components/filters/FilterChip.svelte.d.ts +68 -0
  59. package/dist/components/filters/OptionList.svelte +352 -0
  60. package/dist/components/filters/OptionList.svelte.d.ts +91 -0
  61. package/dist/components/filters/Panel.svelte +123 -0
  62. package/dist/components/filters/Panel.svelte.d.ts +25 -0
  63. package/dist/components/filters/index.d.ts +288 -0
  64. package/dist/components/filters/index.js +12 -0
  65. package/dist/components/icons.d.ts +54 -0
  66. package/dist/components/icons.js +58 -0
  67. package/dist/components/inputs/ButtonGroup.svelte +104 -0
  68. package/dist/components/inputs/ButtonGroup.svelte.d.ts +40 -0
  69. package/dist/components/inputs/Calendar.svelte +535 -0
  70. package/dist/components/inputs/Calendar.svelte.d.ts +45 -0
  71. package/dist/components/inputs/DateField.svelte +247 -0
  72. package/dist/components/inputs/DateField.svelte.d.ts +74 -0
  73. package/dist/components/inputs/DateInput.svelte +118 -0
  74. package/dist/components/inputs/DateInput.svelte.d.ts +43 -0
  75. package/dist/components/inputs/DatePicker.svelte +102 -0
  76. package/dist/components/inputs/DatePicker.svelte.d.ts +67 -0
  77. package/dist/components/inputs/DateRangePicker.svelte +208 -0
  78. package/dist/components/inputs/DateRangePicker.svelte.d.ts +76 -0
  79. package/dist/components/inputs/DateRangeSelect.svelte +548 -0
  80. package/dist/components/inputs/DateRangeSelect.svelte.d.ts +70 -0
  81. package/dist/components/inputs/FieldLabel.svelte +43 -0
  82. package/dist/components/inputs/FieldLabel.svelte.d.ts +28 -0
  83. package/dist/components/inputs/ListSearch.svelte +142 -0
  84. package/dist/components/inputs/ListSearch.svelte.d.ts +35 -0
  85. package/dist/components/inputs/RadioGroup.svelte +153 -0
  86. package/dist/components/inputs/RadioGroup.svelte.d.ts +49 -0
  87. package/dist/components/inputs/RangeShortcuts.svelte +90 -0
  88. package/dist/components/inputs/RangeShortcuts.svelte.d.ts +31 -0
  89. package/dist/components/inputs/RangeSlider.svelte +225 -0
  90. package/dist/components/inputs/RangeSlider.svelte.d.ts +37 -0
  91. package/dist/components/inputs/SearchField.svelte +24 -0
  92. package/dist/components/inputs/SearchField.svelte.d.ts +41 -0
  93. package/dist/components/inputs/Segmented.svelte +76 -0
  94. package/dist/components/inputs/Segmented.svelte.d.ts +43 -0
  95. package/dist/components/inputs/Select.svelte +552 -0
  96. package/dist/components/inputs/Select.svelte.d.ts +100 -0
  97. package/dist/components/inputs/Slider.svelte +177 -0
  98. package/dist/components/inputs/Slider.svelte.d.ts +33 -0
  99. package/dist/components/inputs/SliderTicks.svelte +62 -0
  100. package/dist/components/inputs/SliderTicks.svelte.d.ts +23 -0
  101. package/dist/components/inputs/Switch.svelte +120 -0
  102. package/dist/components/inputs/Switch.svelte.d.ts +31 -0
  103. package/dist/components/inputs/TagInput.svelte +296 -0
  104. package/dist/components/inputs/TagInput.svelte.d.ts +61 -0
  105. package/dist/components/inputs/TextArea.svelte +86 -0
  106. package/dist/components/inputs/TextArea.svelte.d.ts +56 -0
  107. package/dist/components/inputs/TextField.svelte +170 -0
  108. package/dist/components/inputs/TextField.svelte.d.ts +80 -0
  109. package/dist/components/menu/ContextMenu.svelte +25 -0
  110. package/dist/components/menu/ContextMenu.svelte.d.ts +26 -0
  111. package/dist/components/menu/MenuButton.svelte +46 -0
  112. package/dist/components/menu/MenuButton.svelte.d.ts +16 -0
  113. package/dist/components/menu/MenuList.svelte +319 -0
  114. package/dist/components/menu/MenuList.svelte.d.ts +30 -0
  115. package/dist/components/metadata/Metadata.svelte +38 -0
  116. package/dist/components/metadata/Metadata.svelte.d.ts +31 -0
  117. package/dist/components/metadata/MetadataRow.svelte +84 -0
  118. package/dist/components/metadata/MetadataRow.svelte.d.ts +26 -0
  119. package/dist/components/metadata/index.d.ts +8 -0
  120. package/dist/components/metadata/index.js +13 -0
  121. package/dist/components/side-menu/SideMenu.svelte +138 -0
  122. package/dist/components/side-menu/SideMenu.svelte.d.ts +25 -0
  123. package/dist/components/side-menu/SideMenuGroup.svelte +108 -0
  124. package/dist/components/side-menu/SideMenuGroup.svelte.d.ts +31 -0
  125. package/dist/components/side-menu/SideMenuItem.svelte +221 -0
  126. package/dist/components/side-menu/SideMenuItem.svelte.d.ts +46 -0
  127. package/dist/components/side-menu/context.d.ts +1 -0
  128. package/dist/components/side-menu/context.js +3 -0
  129. package/dist/components/side-menu/index.d.ts +62 -0
  130. package/dist/components/side-menu/index.js +15 -0
  131. package/dist/components/tiles/Tile.svelte +419 -0
  132. package/dist/components/tiles/Tile.svelte.d.ts +58 -0
  133. package/dist/components/tiles/TileGroup.svelte +135 -0
  134. package/dist/components/tiles/TileGroup.svelte.d.ts +45 -0
  135. package/dist/components/tiles/context.d.ts +35 -0
  136. package/dist/components/tiles/context.js +14 -0
  137. package/dist/components/tiles/index.d.ts +6 -0
  138. package/dist/components/tiles/index.js +13 -0
  139. package/dist/components/upload/FileCard.svelte +301 -0
  140. package/dist/components/upload/FileCard.svelte.d.ts +127 -0
  141. package/dist/components/upload/Thumb.svelte +274 -0
  142. package/dist/components/upload/Thumb.svelte.d.ts +101 -0
  143. package/dist/components/upload/UploadFile.svelte +123 -0
  144. package/dist/components/upload/UploadFile.svelte.d.ts +48 -0
  145. package/dist/components/upload/UploadFiles.svelte +177 -0
  146. package/dist/components/upload/UploadFiles.svelte.d.ts +50 -0
  147. package/dist/components/upload/UploadFolder.svelte +153 -0
  148. package/dist/components/upload/UploadFolder.svelte.d.ts +63 -0
  149. package/dist/components/upload/UploadImage.svelte +137 -0
  150. package/dist/components/upload/UploadImage.svelte.d.ts +52 -0
  151. package/dist/components/upload/UploadImages.svelte +172 -0
  152. package/dist/components/upload/UploadImages.svelte.d.ts +52 -0
  153. package/dist/components/upload/UploadZone.svelte +130 -0
  154. package/dist/components/upload/UploadZone.svelte.d.ts +41 -0
  155. package/dist/components/upload/common.d.ts +31 -0
  156. package/dist/components/upload/common.js +103 -0
  157. package/dist/components/upload/context.d.ts +18 -0
  158. package/dist/components/upload/context.js +10 -0
  159. package/dist/components/upload/index.d.ts +16 -0
  160. package/dist/components/upload/index.js +18 -0
  161. package/dist/css/atoms.css +892 -0
  162. package/dist/css/base.css +333 -0
  163. package/dist/css/effects.css +430 -0
  164. package/dist/css/index.css +10 -0
  165. package/dist/css/tokens.css +296 -0
  166. package/dist/fonts.d.ts +1 -0
  167. package/dist/fonts.js +7 -0
  168. package/dist/index.d.ts +68 -0
  169. package/dist/index.js +77 -0
  170. package/dist/lib/announce.d.ts +8 -0
  171. package/dist/lib/announce.js +48 -0
  172. package/dist/lib/controls.d.ts +10 -0
  173. package/dist/lib/controls.js +42 -0
  174. package/dist/lib/dates.d.ts +104 -0
  175. package/dist/lib/dates.js +235 -0
  176. package/dist/lib/dialogs.svelte.d.ts +8 -0
  177. package/dist/lib/dialogs.svelte.js +45 -0
  178. package/dist/lib/dom.d.ts +34 -0
  179. package/dist/lib/dom.js +90 -0
  180. package/dist/lib/field-errors.svelte.d.ts +40 -0
  181. package/dist/lib/field-errors.svelte.js +89 -0
  182. package/dist/lib/files.d.ts +74 -0
  183. package/dist/lib/files.js +215 -0
  184. package/dist/lib/inline-md.d.ts +1 -0
  185. package/dist/lib/inline-md.js +36 -0
  186. package/dist/lib/invalid.d.ts +4 -0
  187. package/dist/lib/invalid.js +35 -0
  188. package/dist/lib/list-search.d.ts +9 -0
  189. package/dist/lib/list-search.js +27 -0
  190. package/dist/lib/menu.svelte.d.ts +39 -0
  191. package/dist/lib/menu.svelte.js +98 -0
  192. package/dist/lib/morph.svelte.d.ts +17 -0
  193. package/dist/lib/morph.svelte.js +46 -0
  194. package/dist/lib/motion.d.ts +115 -0
  195. package/dist/lib/motion.js +174 -0
  196. package/dist/lib/observe.d.ts +36 -0
  197. package/dist/lib/observe.js +142 -0
  198. package/dist/lib/reveal.d.ts +7 -0
  199. package/dist/lib/reveal.js +28 -0
  200. package/dist/lib/roving.d.ts +16 -0
  201. package/dist/lib/roving.js +35 -0
  202. package/dist/lib/runs.d.ts +18 -0
  203. package/dist/lib/runs.js +25 -0
  204. package/dist/lib/scroll-fade.d.ts +4 -0
  205. package/dist/lib/scroll-fade.js +71 -0
  206. package/dist/lib/spotlight.d.ts +4 -0
  207. package/dist/lib/spotlight.js +109 -0
  208. package/dist/lib/ticks.d.ts +12 -0
  209. package/dist/lib/ticks.js +24 -0
  210. package/dist/lib/tooltip.d.ts +4 -0
  211. package/dist/lib/tooltip.js +110 -0
  212. package/dist/lib/validity.d.ts +16 -0
  213. package/dist/lib/validity.js +36 -0
  214. package/package.json +69 -3
@@ -0,0 +1,177 @@
1
+ <script>
2
+ import { getContext, tick } from 'svelte'
3
+ import FileCard from './FileCard.svelte'
4
+ import ErrorMessage from '../ErrorMessage.svelte'
5
+ import { merge, plural, rejection, fileFormat } from './common.js'
6
+ import { accepts, droppedFiles, fileDrop, fileSize } from '../../lib/files.js'
7
+ import { announce } from '../../lib/announce.js'
8
+ import { UPLOAD_ZONE } from './context.js'
9
+ import { flip, slide } from '../../lib/motion.js'
10
+ /**
11
+ * Any number of files, picked or dropped: a list of cards (name and size, a ✕ to remove each) above the button that
12
+ * adds more. Picking again adds to the list; a file already in it is not added twice. Import it as Upload.Files
13
+ * (components/upload/index.js).
14
+ *
15
+ * <Upload.Files bind:files accept=".csv" />
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 button's text while the list is empty (default "Choose files"); addLabel: once it isn't (default "Add
19
+ * files"). accept: as on a file input; dropped files are held to it too, and a drop with nothing that fits says so
20
+ * under the control. id: on the button. disabled. error: a message from the app, in a bubble under the control.
21
+ * Other attributes (class…) and attachments go to the wrapping <div>, which is also where files can be dropped.
22
+ *
23
+ * Keyboard: Enter or Space on the button opens the picker. Tab goes through each card's ✕; removing one moves the
24
+ * focus to the next ✕ (the previous one after the last, the button once the list is empty).
25
+ *
26
+ * @typedef {object} Props
27
+ * @property {File[]} [files]
28
+ * @property {(files: File[]) => void} [onchange]
29
+ * @property {string} [label]
30
+ * @property {string} [addLabel]
31
+ * @property {string} [accept]
32
+ * @property {string} [id]
33
+ * @property {boolean} [disabled]
34
+ * @property {string} [error]
35
+ */
36
+ /** @type {Props & Omit<import('svelte/elements').HTMLAttributes<HTMLDivElement>, keyof Props>} */
37
+ let {
38
+ files = $bindable([]),
39
+ onchange,
40
+ label = 'Choose files',
41
+ addLabel = 'Add files',
42
+ accept = '',
43
+ id,
44
+ disabled = false,
45
+ error = '',
46
+ class: className,
47
+ ...rest
48
+ } = $props()
49
+
50
+ const uid = $props.id()
51
+ /** @type {HTMLInputElement | undefined} */
52
+ let input = $state()
53
+ /** @type {HTMLButtonElement | undefined} */
54
+ let main = $state()
55
+ /** @type {HTMLUListElement | undefined} */
56
+ let list = $state()
57
+ let dragging = $state(false)
58
+ let rejected = $state('')
59
+ const problem = $derived(error || rejected)
60
+
61
+ // Inside an Upload.Zone, the zone takes the drops and hands them here.
62
+ /** @type {import('./context.js').UploadZoneContext | undefined} */
63
+ const zone = getContext(UPLOAD_ZONE)
64
+ $effect(() => {
65
+ if (zone && !disabled) return zone.register(ondrop, 'Drop files here')
66
+ })
67
+
68
+ /** @param {File[]} more */
69
+ function add(more) {
70
+ rejected = ''
71
+ const next = merge(files, more)
72
+ if (next === files) return
73
+ const count = next.length - files.length
74
+ files = next
75
+ onchange?.(next)
76
+ announce(count === 1 ? `${next.at(-1)?.name} added` : `${plural(count)} added`)
77
+ }
78
+
79
+ /** @param {File} file */
80
+ async function remove(file) {
81
+ const index = files.indexOf(file)
82
+ files = files.filter((other) => other !== file)
83
+ onchange?.(files)
84
+ announce(`${file.name} removed`)
85
+ // The card is on its way out (inert while it slides away): the focus goes to the ✕ now in its place.
86
+ await tick()
87
+ const clears = list ? [...list.querySelectorAll(':scope > li:not([inert]) .clear')] : []
88
+ const next = /** @type {HTMLElement | undefined} */ (clears[index] ?? clears.at(-1))
89
+ ;(next ?? main)?.focus()
90
+ }
91
+
92
+ /** @param {import('../../lib/files.js').Dropped} drop */
93
+ function ondrop(drop) {
94
+ if (disabled) return
95
+ const dropped = droppedFiles(drop)
96
+ const fit = dropped.filter((file) => accepts(file, accept))
97
+ if (fit.length) add(fit)
98
+ else rejected = rejection(accept, dropped.length > 0)
99
+ }
100
+ </script>
101
+
102
+ <div class={['upload', className]} {...rest} {@attach fileDrop({ ondrop, ondragging: (over) => (dragging = over), disabled: disabled || !!zone })}>
103
+ {#if files.length}
104
+ <ul bind:this={list} class="list" aria-label="Chosen files">
105
+ {#each files as file (file)}
106
+ <li animate:flip transition:slide>
107
+ <FileCard
108
+ item={{ name: file.name, detail: fileSize(file.size), icon: 'fileBlank', ...fileFormat(file.name) }}
109
+ onclear={() => remove(file)}
110
+ {disabled}
111
+ />
112
+ </li>
113
+ {/each}
114
+ </ul>
115
+ {/if}
116
+ <FileCard
117
+ bind:main
118
+ item={null}
119
+ label={files.length ? addLabel : label}
120
+ icon={files.length ? 'plus' : 'upload'}
121
+ onpick={() => input?.click()}
122
+ {disabled}
123
+ dragging={dragging || !!zone?.dragging}
124
+ {id}
125
+ errorId={!!problem && `${uid}-error`}
126
+ />
127
+ <!-- Closing the file chooser fires a cancel event that bubbles: inside a <dialog> it would close the dialog too. -->
128
+ <input
129
+ bind:this={input}
130
+ type="file"
131
+ {accept}
132
+ multiple
133
+ hidden
134
+ onchange={(event) => {
135
+ const picked = [...(event.currentTarget.files ?? [])]
136
+ event.currentTarget.value = ''
137
+ if (picked.length) add(picked)
138
+ }}
139
+ oncancel={(event) => event.stopPropagation()}
140
+ />
141
+ <ErrorMessage
142
+ floating
143
+ id="{uid}-error"
144
+ message={problem}
145
+ ondismiss={() => {
146
+ rejected = ''
147
+ main?.focus()
148
+ }}
149
+ />
150
+ </div>
151
+
152
+ <style>
153
+ .upload {
154
+ position: relative;
155
+ display: flex;
156
+ flex-direction: column;
157
+ align-items: flex-start;
158
+ gap: var(--space-2);
159
+ width: fit-content;
160
+ max-width: 100%;
161
+ }
162
+ .list {
163
+ display: grid;
164
+ max-width: 100%;
165
+ margin: 0;
166
+ padding: 0;
167
+ list-style: none;
168
+ }
169
+ /* Spaced by padding, not gap, so a card sliding away takes its space with it. */
170
+ .list > li {
171
+ min-width: 0;
172
+ padding-bottom: var(--space-1);
173
+ }
174
+ .list > li:last-child {
175
+ padding-bottom: 0;
176
+ }
177
+ </style>
@@ -0,0 +1,50 @@
1
+ export default UploadFiles;
2
+ type UploadFiles = {
3
+ $on?(type: string, callback: (e: any) => void): () => void;
4
+ $set?(props: Partial<Props & Omit<HTMLAttributes<HTMLDivElement>, keyof Props>>): void;
5
+ };
6
+ declare const UploadFiles: import("svelte").Component<{
7
+ files?: File[] | undefined;
8
+ onchange?: ((files: File[]) => void) | undefined;
9
+ label?: string | undefined;
10
+ addLabel?: string | undefined;
11
+ accept?: string | 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
+ addLabel?: string | undefined;
20
+ accept?: string | undefined;
21
+ id?: string | undefined;
22
+ disabled?: boolean | undefined;
23
+ error?: string | undefined;
24
+ }>, {}, "files">;
25
+ /**
26
+ * Any number of files, picked or dropped: a list of cards (name and size, a ✕ to remove each) above the button that
27
+ * adds more. Picking again adds to the list; a file already in it is not added twice. Import it as Upload.Files
28
+ * (components/upload/index.js).
29
+ *
30
+ * <Upload.Files bind:files accept=".csv" />
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 button's text while the list is empty (default "Choose files"); addLabel: once it isn't (default "Add
34
+ * files"). accept: as on a file input; dropped files are held to it too, and a drop with nothing that fits says so
35
+ * under the control. id: on the button. disabled. error: a message from the app, in a bubble under the control.
36
+ * Other attributes (class…) and attachments go to the wrapping <div>, which is also where files can be dropped.
37
+ *
38
+ * Keyboard: Enter or Space on the button opens the picker. Tab goes through each card's ✕; removing one moves the
39
+ * focus to the next ✕ (the previous one after the last, the button once the list is empty).
40
+ */
41
+ type Props = {
42
+ files?: File[] | undefined;
43
+ onchange?: ((files: File[]) => void) | undefined;
44
+ label?: string | undefined;
45
+ addLabel?: string | undefined;
46
+ accept?: string | undefined;
47
+ id?: string | undefined;
48
+ disabled?: boolean | undefined;
49
+ error?: string | undefined;
50
+ };
@@ -0,0 +1,153 @@
1
+ <script>
2
+ import { getContext } from 'svelte'
3
+ import FileCard from './FileCard.svelte'
4
+ import ErrorMessage from '../ErrorMessage.svelte'
5
+ import { plural } from './common.js'
6
+ import { fileDrop, fileSize, filesOfDrop } from '../../lib/files.js'
7
+ import { announce } from '../../lib/announce.js'
8
+ import { UPLOAD_ZONE } from './context.js'
9
+ /**
10
+ * A folder and every file in it, picked or dropped. The button becomes the folder: once one is chosen it reshapes into
11
+ * a card with the folder's name, how many files it holds and their size, which opens the picker again to replace it,
12
+ * and a ✕ that clears it. Import it as Upload.Folder (components/upload/index.js).
13
+ *
14
+ * <Upload.Folder bind:folder onchange={(folder) => folder && zip(folder.files)} />
15
+ *
16
+ * folder: `{ name, files }` or null (bindable). Each file is `{ path, file }`, its path inside the folder
17
+ * ('assets/app.css'), so the folder's own name is not part of it. onchange(folder): after a pick, a drop or a clear.
18
+ * label: the button's text while empty (default "Choose folder"). id: on the button. disabled. error: a message from the
19
+ * app, in a bubble under the control. Other attributes (class…) and attachments go to the wrapping <div>, which is
20
+ * also where a folder can be dropped. A dropped folder is read before it is taken, which can take a moment for a big
21
+ * one: the button says so meanwhile.
22
+ *
23
+ * Keyboard: Enter or Space opens the picker (to replace the folder, once there is one); Delete or Backspace clears it.
24
+ * Tab reaches the ✕, which clears it and puts the focus back on the button.
25
+ *
26
+ * @typedef {object} Folder
27
+ * @property {string} name
28
+ * @property {import('../../lib/files.js').PathedFile[]} files
29
+ *
30
+ * @typedef {object} Props
31
+ * @property {Folder | null} [folder]
32
+ * @property {(folder: Folder | null) => void} [onchange]
33
+ * @property {string} [label]
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 { folder = $bindable(null), onchange, label = 'Choose folder', id, disabled = false, error = '', class: className, ...rest } = $props()
40
+
41
+ const uid = $props.id()
42
+ /** @type {HTMLInputElement | undefined} */
43
+ let input = $state()
44
+ /** @type {HTMLButtonElement | undefined} */
45
+ let main = $state()
46
+ let dragging = $state(false)
47
+ let reading = $state(false)
48
+ let rejected = $state('')
49
+ const problem = $derived(error || rejected)
50
+
51
+ // Inside an Upload.Zone, the zone takes the drops and hands them here.
52
+ /** @type {import('./context.js').UploadZoneContext | undefined} */
53
+ const zone = getContext(UPLOAD_ZONE)
54
+ $effect(() => {
55
+ if (zone && !disabled) return zone.register(ondrop, 'Drop a folder here')
56
+ })
57
+
58
+ const item = $derived(
59
+ folder && {
60
+ name: folder.name,
61
+ detail: `${plural(folder.files.length)} · ${fileSize(folder.files.reduce((sum, { file }) => sum + file.size, 0))}`,
62
+ icon: 'folderSimple',
63
+ },
64
+ )
65
+
66
+ /** @param {Folder | null} next */
67
+ function choose(next) {
68
+ rejected = ''
69
+ if (next === folder) return
70
+ const had = folder
71
+ folder = next
72
+ onchange?.(next)
73
+ announce(next ? `${next.name} added, ${plural(next.files.length)}` : `${had?.name} removed`)
74
+ }
75
+
76
+ /** @param {File[]} files */
77
+ function picked(files) {
78
+ if (!files.length) {
79
+ rejected = 'That folder is empty.'
80
+ return
81
+ }
82
+ // Each path starts with the picked folder's name: drop it, so the paths start inside the folder.
83
+ const name = files[0]?.webkitRelativePath.split('/')[0] || 'Folder'
84
+ choose({ name, files: files.map((file) => ({ path: file.webkitRelativePath.split('/').slice(1).join('/') || file.name, file })) })
85
+ }
86
+
87
+ /** @param {import('../../lib/files.js').Dropped} drop */
88
+ async function ondrop(drop) {
89
+ if (disabled || reading) return
90
+ rejected = ''
91
+ reading = true
92
+ try {
93
+ const { folder: name, files } = await filesOfDrop(drop)
94
+ if (name === null) rejected = 'Drop one folder here.'
95
+ else if (!files.length) rejected = 'That folder is empty.'
96
+ else choose({ name, files })
97
+ } catch {
98
+ rejected = 'Could not read what was dropped. Pick the folder with the button instead.'
99
+ } finally {
100
+ reading = false
101
+ }
102
+ }
103
+ </script>
104
+
105
+ <div class={['upload', className]} {...rest} {@attach fileDrop({ ondrop, ondragging: (over) => (dragging = over), disabled: disabled || !!zone })}>
106
+ <FileCard
107
+ bind:main
108
+ {item}
109
+ {label}
110
+ icon="folderSimple"
111
+ onpick={() => input?.click()}
112
+ onclear={() => choose(null)}
113
+ {disabled}
114
+ dragging={dragging || !!zone?.dragging}
115
+ busy={reading}
116
+ busyLabel="Reading the folder…"
117
+ {id}
118
+ errorId={!!problem && `${uid}-error`}
119
+ />
120
+ <!-- Closing the file chooser fires a cancel event that bubbles: inside a <dialog> it would close the dialog too. -->
121
+ <input
122
+ bind:this={input}
123
+ type="file"
124
+ webkitdirectory
125
+ hidden
126
+ onchange={(event) => {
127
+ const files = [...(event.currentTarget.files ?? [])]
128
+ event.currentTarget.value = ''
129
+ picked(files)
130
+ }}
131
+ oncancel={(event) => event.stopPropagation()}
132
+ />
133
+ <ErrorMessage
134
+ floating
135
+ id="{uid}-error"
136
+ message={problem}
137
+ ondismiss={() => {
138
+ rejected = ''
139
+ main?.focus()
140
+ }}
141
+ />
142
+ </div>
143
+
144
+ <style>
145
+ .upload {
146
+ position: relative;
147
+ display: flex;
148
+ flex-direction: column;
149
+ align-items: flex-start;
150
+ width: fit-content;
151
+ max-width: 100%;
152
+ }
153
+ </style>
@@ -0,0 +1,63 @@
1
+ export default UploadFolder;
2
+ type UploadFolder = {
3
+ $on?(type: string, callback: (e: any) => void): () => void;
4
+ $set?(props: Partial<Props & Omit<HTMLAttributes<HTMLDivElement>, keyof Props>>): void;
5
+ };
6
+ declare const UploadFolder: import("svelte").Component<{
7
+ folder?: {
8
+ name: string;
9
+ files: import("../../lib/files.js").PathedFile[];
10
+ } | null | undefined;
11
+ onchange?: ((folder: {
12
+ name: string;
13
+ files: import("../../lib/files.js").PathedFile[];
14
+ } | null) => void) | undefined;
15
+ label?: string | undefined;
16
+ id?: string | undefined;
17
+ disabled?: boolean | undefined;
18
+ error?: string | undefined;
19
+ } & Omit<import("svelte/elements").HTMLAttributes<HTMLDivElement>, keyof {
20
+ folder?: {
21
+ name: string;
22
+ files: import("../../lib/files.js").PathedFile[];
23
+ } | null | undefined;
24
+ onchange?: ((folder: {
25
+ name: string;
26
+ files: import("../../lib/files.js").PathedFile[];
27
+ } | null) => void) | undefined;
28
+ label?: string | undefined;
29
+ id?: string | undefined;
30
+ disabled?: boolean | undefined;
31
+ error?: string | undefined;
32
+ }>, {}, "folder">;
33
+ /**
34
+ * A folder and every file in it, picked or dropped. The button becomes the folder: once one is chosen it reshapes into
35
+ * a card with the folder's name, how many files it holds and their size, which opens the picker again to replace it,
36
+ * and a ✕ that clears it. Import it as Upload.Folder (components/upload/index.js).
37
+ *
38
+ * <Upload.Folder bind:folder onchange={(folder) => folder && zip(folder.files)} />
39
+ *
40
+ * folder: `{ name, files }` or null (bindable). Each file is `{ path, file }`, its path inside the folder
41
+ * ('assets/app.css'), so the folder's own name is not part of it. onchange(folder): after a pick, a drop or a clear.
42
+ * label: the button's text while empty (default "Choose folder"). id: on the button. disabled. error: a message from the
43
+ * app, in a bubble under the control. Other attributes (class…) and attachments go to the wrapping <div>, which is
44
+ * also where a folder can be dropped. A dropped folder is read before it is taken, which can take a moment for a big
45
+ * one: the button says so meanwhile.
46
+ *
47
+ * Keyboard: Enter or Space opens the picker (to replace the folder, once there is one); Delete or Backspace clears it.
48
+ * Tab reaches the ✕, which clears it and puts the focus back on the button.
49
+ */
50
+ type Props = {
51
+ folder?: {
52
+ name: string;
53
+ files: import("../../lib/files.js").PathedFile[];
54
+ } | null | undefined;
55
+ onchange?: ((folder: {
56
+ name: string;
57
+ files: import("../../lib/files.js").PathedFile[];
58
+ } | null) => void) | undefined;
59
+ label?: string | undefined;
60
+ id?: string | undefined;
61
+ disabled?: boolean | undefined;
62
+ error?: string | undefined;
63
+ };
@@ -0,0 +1,137 @@
1
+ <script>
2
+ import { getContext } from 'svelte'
3
+ import Thumb from './Thumb.svelte'
4
+ import ErrorMessage from '../ErrorMessage.svelte'
5
+ import { IMAGE_MAX_SIZE, 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
+ /**
10
+ * One image, picked or dropped, shown as a thumbnail. The button becomes the image: once one is chosen it reshapes into
11
+ * a square thumbnail of it (cropped to fill), with the name and size under it, which opens the picker again to replace
12
+ * it, and a ✕ in its corner that clears it. Import it as Upload.Image (components/upload/index.js).
13
+ *
14
+ * <Upload.Image bind:file label="Choose logo" accept="image/png,image/svg+xml" />
15
+ *
16
+ * file: the chosen File, or null (bindable). onchange(file): after a pick, a drop or a clear.
17
+ * label: the button's text while empty (default "Choose image"). accept: as on a file input (default "image/*").
18
+ * Dropped files are held to it too; a drop with nothing that fits says so under the control. maxSize: the largest
19
+ * image taken, in bytes (default 10 MB; Infinity for none); a bigger one, picked or dropped, is turned away with the
20
+ * reason under the control. id: on the button.
21
+ * disabled. error: a message from the app: the control turns red and shows it in a bubble underneath.
22
+ * Other attributes (class…) and attachments go to the wrapping <div>, which is also where an image can be dropped.
23
+ *
24
+ * Keyboard: Enter or Space opens the picker (to replace the image, once there is one); Delete or Backspace clears it.
25
+ * Tab reaches the ✕, which clears it and puts the focus back on the button.
26
+ *
27
+ * @typedef {object} Props
28
+ * @property {File | null} [file]
29
+ * @property {(file: File | null) => void} [onchange]
30
+ * @property {string} [label]
31
+ * @property {string} [accept]
32
+ * @property {number} [maxSize]
33
+ * @property {string} [id]
34
+ * @property {boolean} [disabled]
35
+ * @property {string} [error]
36
+ */
37
+ /** @type {Props & Omit<import('svelte/elements').HTMLAttributes<HTMLDivElement>, keyof Props>} */
38
+ let {
39
+ file = $bindable(null),
40
+ onchange,
41
+ label = 'Choose image',
42
+ accept = 'image/*',
43
+ maxSize = IMAGE_MAX_SIZE,
44
+ id,
45
+ disabled = false,
46
+ error = '',
47
+ class: className,
48
+ ...rest
49
+ } = $props()
50
+
51
+ const uid = $props.id()
52
+ /** @type {HTMLInputElement | undefined} */
53
+ let input = $state()
54
+ /** @type {HTMLButtonElement | undefined} */
55
+ let main = $state()
56
+ let dragging = $state(false)
57
+ let rejected = $state('')
58
+ const problem = $derived(error || rejected)
59
+
60
+ // Inside an Upload.Zone, the zone takes the drops and hands them here.
61
+ /** @type {import('./context.js').UploadZoneContext | undefined} */
62
+ const zone = getContext(UPLOAD_ZONE)
63
+ $effect(() => {
64
+ if (zone && !disabled) return zone.register(ondrop, 'Drop an image here')
65
+ })
66
+
67
+ /** @param {File | null} next */
68
+ function choose(next) {
69
+ rejected = ''
70
+ if (next === file) return
71
+ const had = file
72
+ file = next
73
+ onchange?.(next)
74
+ announce(next ? `${next.name} added` : `${had?.name} removed`)
75
+ }
76
+
77
+ /** Takes the first that fits its type and size, or says why none does. The picker checks neither: its "All files"
78
+ * option lets any file through, and sizes are never checked. @param {File[]} files */
79
+ function take(files) {
80
+ const typed = files.filter((candidate) => accepts(candidate, accept))
81
+ const fit = typed.find((candidate) => candidate.size <= maxSize)
82
+ if (fit) choose(fit)
83
+ else rejected = typed.length ? oversize(typed.slice(0, 1), maxSize) : rejection(accept, files.length > 0)
84
+ }
85
+
86
+ /** @param {import('../../lib/files.js').Dropped} drop */
87
+ function ondrop(drop) {
88
+ if (!disabled) take(droppedFiles(drop))
89
+ }
90
+ </script>
91
+
92
+ <div class={['upload', className]} {...rest} {@attach fileDrop({ ondrop, ondragging: (over) => (dragging = over), disabled: disabled || !!zone })}>
93
+ <Thumb
94
+ bind:main
95
+ {file}
96
+ {label}
97
+ onpick={() => input?.click()}
98
+ onclear={() => choose(null)}
99
+ {disabled}
100
+ dragging={dragging || !!zone?.dragging}
101
+ {id}
102
+ errorId={!!problem && `${uid}-error`}
103
+ />
104
+ <!-- Closing the file chooser fires a cancel event that bubbles: inside a <dialog> it would close the dialog too. -->
105
+ <input
106
+ bind:this={input}
107
+ type="file"
108
+ {accept}
109
+ hidden
110
+ onchange={(event) => {
111
+ const [picked] = event.currentTarget.files ?? []
112
+ event.currentTarget.value = ''
113
+ if (picked) take([picked])
114
+ }}
115
+ oncancel={(event) => event.stopPropagation()}
116
+ />
117
+ <ErrorMessage
118
+ floating
119
+ id="{uid}-error"
120
+ message={problem}
121
+ ondismiss={() => {
122
+ rejected = ''
123
+ main?.focus()
124
+ }}
125
+ />
126
+ </div>
127
+
128
+ <style>
129
+ .upload {
130
+ position: relative;
131
+ display: flex;
132
+ flex-direction: column;
133
+ align-items: flex-start;
134
+ width: fit-content;
135
+ max-width: 100%;
136
+ }
137
+ </style>
@@ -0,0 +1,52 @@
1
+ export default UploadImage;
2
+ type UploadImage = {
3
+ $on?(type: string, callback: (e: any) => void): () => void;
4
+ $set?(props: Partial<Props & Omit<HTMLAttributes<HTMLDivElement>, keyof Props>>): void;
5
+ };
6
+ declare const UploadImage: import("svelte").Component<{
7
+ file?: File | null | undefined;
8
+ onchange?: ((file: File | null) => 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
+ file?: File | null | undefined;
17
+ onchange?: ((file: File | null) => 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
+ }>, {}, "file">;
25
+ /**
26
+ * One image, picked or dropped, shown as a thumbnail. The button becomes the image: once one is chosen it reshapes into
27
+ * a square thumbnail of it (cropped to fill), with the name and size under it, which opens the picker again to replace
28
+ * it, and a ✕ in its corner that clears it. Import it as Upload.Image (components/upload/index.js).
29
+ *
30
+ * <Upload.Image bind:file label="Choose logo" accept="image/png,image/svg+xml" />
31
+ *
32
+ * file: the chosen File, or null (bindable). onchange(file): after a pick, a drop or a clear.
33
+ * label: the button's text while empty (default "Choose image"). accept: as on a file input (default "image/*").
34
+ * Dropped files are held to it too; a drop with nothing that fits says so under the control. maxSize: the largest
35
+ * image taken, in bytes (default 10 MB; Infinity for none); a bigger one, picked or dropped, is turned away with the
36
+ * reason under the control. id: on the button.
37
+ * disabled. error: a message from the app: the control turns red and shows it in a bubble underneath.
38
+ * Other attributes (class…) and attachments go to the wrapping <div>, which is also where an image can be dropped.
39
+ *
40
+ * Keyboard: Enter or Space opens the picker (to replace the image, once there is one); Delete or Backspace clears it.
41
+ * Tab reaches the ✕, which clears it and puts the focus back on the button.
42
+ */
43
+ type Props = {
44
+ file?: File | null | undefined;
45
+ onchange?: ((file: File | null) => 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
+ };