@milktop/react-ui 0.3.9 → 0.3.10
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.
|
@@ -35,8 +35,9 @@
|
|
|
35
35
|
* Files, so on submit: new = items that are Files, kept = the rest, removed =
|
|
36
36
|
* the saved ones no longer there.
|
|
37
37
|
*
|
|
38
|
-
* Clicking an image opens it large
|
|
39
|
-
*
|
|
38
|
+
* Clicking an image opens it large, with arrows (and the arrow keys) to move
|
|
39
|
+
* through the others; other files open their `url`, if any, in a new tab (see
|
|
40
|
+
* `onOpen`). Files with a `url` get a download button.
|
|
40
41
|
*/
|
|
41
42
|
import { type HTMLAttributes, type ReactNode } from 'react';
|
|
42
43
|
import '../icons.js';
|
|
@@ -81,7 +82,7 @@ export type AttachmentsProps = Omit<HTMLAttributes<HTMLDivElement>, 'onDrop'> &
|
|
|
81
82
|
name?: string;
|
|
82
83
|
keptName?: string;
|
|
83
84
|
disabled?: boolean;
|
|
84
|
-
classes?: Classes<'root' | 'header' | 'label' | 'switch' | 'empty' | 'files' | 'file' | 'open' | 'thumb' | 'icon' | 'ext' | 'meta' | 'name' | 'size' | 'actions' | 'action' | 'addTile' | 'addInput' | 'addIcon' | 'addText' | 'addLabel' | 'limit' | 'rejected' | 'dropHint' | 'preview' | 'downloadLink'>;
|
|
85
|
+
classes?: Classes<'root' | 'header' | 'label' | 'switch' | 'empty' | 'files' | 'file' | 'open' | 'thumb' | 'icon' | 'ext' | 'meta' | 'name' | 'size' | 'actions' | 'action' | 'addTile' | 'addInput' | 'addIcon' | 'addText' | 'addLabel' | 'limit' | 'rejected' | 'dropHint' | 'stage' | 'preview' | 'step' | 'position' | 'downloadLink'>;
|
|
85
86
|
};
|
|
86
87
|
export declare const formatSize: (bytes: number, locale?: string) => string;
|
|
87
88
|
export declare function Attachments({ items: itemsProp, defaultItems, onItemsChange, layout: layoutProp, defaultLayout, onLayoutChange, switchable, persist, columns, removable, addable, accept, maxFiles, maxFileSize, addLabel: addLabelProp, emptyText, onOpen, onRemove, onAdd, onReject, label, name, keptName, disabled: disabledProp, className, classes, style, ...rest }: AttachmentsProps): import("react").JSX.Element;
|
|
@@ -36,8 +36,9 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
|
|
|
36
36
|
* Files, so on submit: new = items that are Files, kept = the rest, removed =
|
|
37
37
|
* the saved ones no longer there.
|
|
38
38
|
*
|
|
39
|
-
* Clicking an image opens it large
|
|
40
|
-
*
|
|
39
|
+
* Clicking an image opens it large, with arrows (and the arrow keys) to move
|
|
40
|
+
* through the others; other files open their `url`, if any, in a new tab (see
|
|
41
|
+
* `onOpen`). Files with a `url` get a download button.
|
|
41
42
|
*/
|
|
42
43
|
import { useEffect, useId, useMemo, useRef, useState } from 'react';
|
|
43
44
|
import { createFileTypeValidator, getAcceptAttrString, getFileKey } from '@zag-js/file-utils';
|
|
@@ -223,17 +224,29 @@ export function Attachments({ items: itemsProp, defaultItems = [], onItemsChange
|
|
|
223
224
|
return () => observer.disconnect();
|
|
224
225
|
}, [layout, columns, list.length]);
|
|
225
226
|
const addWide = layout === 'grid' && gridColumns > 0 && list.length > 0 && list.length % gridColumns === 0;
|
|
226
|
-
const
|
|
227
|
+
const images = list.filter((entry) => entry.image && entry.url);
|
|
228
|
+
const [previewIndex, setPreviewIndex] = useState(0);
|
|
227
229
|
const [previewOpen, setPreviewOpen] = useState(false);
|
|
230
|
+
const previewing = images[previewIndex];
|
|
231
|
+
const several = images.length > 1;
|
|
232
|
+
const step = (by) => setPreviewIndex((index) => (index + by + images.length) % images.length);
|
|
233
|
+
function previewKey(e) {
|
|
234
|
+
if (!several)
|
|
235
|
+
return;
|
|
236
|
+
if (e.key === 'ArrowLeft')
|
|
237
|
+
step(-1);
|
|
238
|
+
if (e.key === 'ArrowRight')
|
|
239
|
+
step(1);
|
|
240
|
+
}
|
|
228
241
|
function open(entry) {
|
|
229
242
|
if (onOpen?.(entry.item))
|
|
230
243
|
return;
|
|
231
244
|
if (entry.image && entry.url) {
|
|
232
|
-
|
|
245
|
+
setPreviewIndex(images.indexOf(entry));
|
|
233
246
|
setPreviewOpen(true);
|
|
234
247
|
}
|
|
235
248
|
else if (entry.url)
|
|
236
249
|
window.open(entry.url, '_blank', 'noopener');
|
|
237
250
|
}
|
|
238
|
-
return (_jsxs("div", { ...rest, ...part('root'), className: cx(className, classes?.root), ref: root, "data-layout": layout, "aria-labelledby": labelId, "aria-describedby": field?.ariaDescribedby, "aria-invalid": field?.invalid || undefined, "data-invalid": field?.invalid || undefined, "data-disabled": disabled || undefined, "data-empty": !list.length || undefined, "data-add-wide": addWide || undefined, "data-fixed": columns ? '' : undefined, style: { ...style, ...(columns && { '--columns': columns }) }, onDragEnter: dragOver, onDragOver: dragOver, onDragLeave: dragLeave, onDrop: dropped, role: "group", children: [((label && !field) || switchable) && (_jsxs("div", { ...part('header'), children: [label && !field && _jsx("span", { ...part('label'), id: labelId, children: label }), switchable && (_jsx(Segmented, { className: classes?.switch, size: "sm", iconOnly: true, "aria-label": t.attachments.layout, items: layouts, value: layout, onValueChange: (next) => next && setLayout(next) }))] })), name && _jsx("input", { ref: posted, type: "file", name: name, multiple: true, hidden: true, "aria-hidden": "true", tabIndex: -1 }), keptName && keptIds.map((kept) => _jsx("input", { type: "hidden", name: keptName, value: String(kept) }, String(kept))), !list.length && !addable && emptyText && _jsx("p", { ...part('empty'), children: emptyText }), _jsxs("ul", { ...part('files'), ref: filesEl, children: [list.map((entry, i) => (_jsxs("li", { ...part('file'), children: [_jsxs("button", { ...part('open'), type: "button", "aria-label": t.attachments.open(entry.name), onClick: () => open(entry), children: [_jsx("span", { ...part('thumb'), children: entry.thumb ? _jsx("img", { src: entry.thumb, alt: "", loading: "lazy" }) : _jsxs(_Fragment, { children: [_jsx("iconify-icon", { ...part('icon'), icon: entry.icon, "aria-hidden": "true" }), entry.ext && _jsx("span", { ...part('ext'), children: entry.ext })] }) }), _jsxs("span", { ...part('meta'), children: [_jsx("span", { ...part('name'), children: entry.name }), entry.size && _jsx("span", { ...part('size'), children: entry.size })] })] }), _jsxs("span", { ...part('actions'), children: [entry.url && (_jsx("a", { ...part('action'), href: entry.url, download: entry.name, "aria-label": t.attachments.downloadFile(entry.name), children: _jsx("iconify-icon", { icon: "lucide:download" }) })), removable && (_jsx("button", { ...part('action'), type: "button", "aria-label": t.common.remove(entry.name), onClick: () => remove(entry), children: _jsx("iconify-icon", { icon: "lucide:x" }) }))] })] }, i))), addable && !atMax && (_jsx("li", { ...part('file'), "data-add": "", children: _jsxs("label", { ...part('add-tile'), children: [_jsx("input", { ...part('add-input'), ...(field && { id: field.ids.control }), type: "file", multiple: maxFiles !== 1, ...(accept && { accept }), onChange: added }), _jsx("span", { ...part('add-icon'), children: _jsx("iconify-icon", { icon: "lucide:plus" }) }), _jsxs("span", { ...part('add-text'), children: [_jsx("span", { ...part('add-label'), children: addLabel }), !list.length && _jsx("span", { ...part('limit'), children: t.attachments.orDrop }), maxFiles && list.length > 0 && _jsx("span", { ...part('limit'), children: t.attachments.count(count, maxFiles) })] })] }) }))] }), rejected.length > 0 && (_jsx("ul", { ...part('rejected'), role: "alert", children: rejected.map((item, i) => _jsx("li", { children: t.attachments.notAdded(item.file.name, item.reason) }, i)) })), dragging && (_jsxs("div", { ...part('drop-hint'), "aria-hidden": "true", children: [_jsx("iconify-icon", { icon: "lucide:upload" }), _jsx("span", { children: atMax && maxFiles ? t.attachments.limit(maxFiles) : t.attachments.dropToAdd })] })), _jsx(Dialog, { size: "lg", heading: previewing?.name, open: previewOpen, onOpenChange: setPreviewOpen, footer: previewing && _jsx("a", { ...part('download-link'), href: previewing.url, download: previewing.name, children: t.attachments.download }), children: previewing && _jsx("img", { ...part('preview'), src: previewing.url, alt: previewing.name }) })] }));
|
|
251
|
+
return (_jsxs("div", { ...rest, ...part('root'), className: cx(className, classes?.root), ref: root, "data-layout": layout, "aria-labelledby": labelId, "aria-describedby": field?.ariaDescribedby, "aria-invalid": field?.invalid || undefined, "data-invalid": field?.invalid || undefined, "data-disabled": disabled || undefined, "data-empty": !list.length || undefined, "data-add-wide": addWide || undefined, "data-fixed": columns ? '' : undefined, style: { ...style, ...(columns && { '--columns': columns }) }, onDragEnter: dragOver, onDragOver: dragOver, onDragLeave: dragLeave, onDrop: dropped, role: "group", children: [((label && !field) || switchable) && (_jsxs("div", { ...part('header'), children: [label && !field && _jsx("span", { ...part('label'), id: labelId, children: label }), switchable && (_jsx(Segmented, { className: classes?.switch, size: "sm", iconOnly: true, "aria-label": t.attachments.layout, items: layouts, value: layout, onValueChange: (next) => next && setLayout(next) }))] })), name && _jsx("input", { ref: posted, type: "file", name: name, multiple: true, hidden: true, "aria-hidden": "true", tabIndex: -1 }), keptName && keptIds.map((kept) => _jsx("input", { type: "hidden", name: keptName, value: String(kept) }, String(kept))), !list.length && !addable && emptyText && _jsx("p", { ...part('empty'), children: emptyText }), _jsxs("ul", { ...part('files'), ref: filesEl, children: [list.map((entry, i) => (_jsxs("li", { ...part('file'), children: [_jsxs("button", { ...part('open'), type: "button", "aria-label": t.attachments.open(entry.name), onClick: () => open(entry), children: [_jsx("span", { ...part('thumb'), children: entry.thumb ? _jsx("img", { src: entry.thumb, alt: "", loading: "lazy" }) : _jsxs(_Fragment, { children: [_jsx("iconify-icon", { ...part('icon'), icon: entry.icon, "aria-hidden": "true" }), entry.ext && _jsx("span", { ...part('ext'), children: entry.ext })] }) }), _jsxs("span", { ...part('meta'), children: [_jsx("span", { ...part('name'), children: entry.name }), entry.size && _jsx("span", { ...part('size'), children: entry.size })] })] }), _jsxs("span", { ...part('actions'), children: [entry.url && (_jsx("a", { ...part('action'), href: entry.url, download: entry.name, "aria-label": t.attachments.downloadFile(entry.name), children: _jsx("iconify-icon", { icon: "lucide:download" }) })), removable && (_jsx("button", { ...part('action'), type: "button", "aria-label": t.common.remove(entry.name), onClick: () => remove(entry), children: _jsx("iconify-icon", { icon: "lucide:x" }) }))] })] }, i))), addable && !atMax && (_jsx("li", { ...part('file'), "data-add": "", children: _jsxs("label", { ...part('add-tile'), children: [_jsx("input", { ...part('add-input'), ...(field && { id: field.ids.control }), type: "file", multiple: maxFiles !== 1, ...(accept && { accept }), onChange: added }), _jsx("span", { ...part('add-icon'), children: _jsx("iconify-icon", { icon: "lucide:plus" }) }), _jsxs("span", { ...part('add-text'), children: [_jsx("span", { ...part('add-label'), children: addLabel }), !list.length && _jsx("span", { ...part('limit'), children: t.attachments.orDrop }), maxFiles && list.length > 0 && _jsx("span", { ...part('limit'), children: t.attachments.count(count, maxFiles) })] })] }) }))] }), rejected.length > 0 && (_jsx("ul", { ...part('rejected'), role: "alert", children: rejected.map((item, i) => _jsx("li", { children: t.attachments.notAdded(item.file.name, item.reason) }, i)) })), dragging && (_jsxs("div", { ...part('drop-hint'), "aria-hidden": "true", children: [_jsx("iconify-icon", { icon: "lucide:upload" }), _jsx("span", { children: atMax && maxFiles ? t.attachments.limit(maxFiles) : t.attachments.dropToAdd })] })), _jsx(Dialog, { size: "lg", heading: previewing?.name, open: previewOpen, onOpenChange: setPreviewOpen, onKeyDown: previewKey, footer: previewing && _jsxs(_Fragment, { children: [several && _jsxs("span", { ...part('position'), children: [previewIndex + 1, " / ", images.length] }), _jsx("a", { ...part('download-link'), href: previewing.url, download: previewing.name, children: t.attachments.download })] }), children: previewing && (_jsxs("div", { ...part('stage'), "data-several": several || undefined, children: [_jsx("img", { ...part('preview'), src: previewing.url, alt: previewing.name }), several && _jsxs(_Fragment, { children: [_jsx("button", { ...part('step'), "data-side": "previous", type: "button", "aria-label": t.attachments.previous, onClick: () => step(-1), children: _jsx("iconify-icon", { icon: "lucide:chevron-left" }) }), _jsx("button", { ...part('step'), "data-side": "next", type: "button", "aria-label": t.attachments.next, onClick: () => step(1), children: _jsx("iconify-icon", { icon: "lucide:chevron-right" }) })] })] })) })] }));
|
|
239
252
|
}
|
|
@@ -79,7 +79,17 @@
|
|
|
79
79
|
justify-content: center; gap: 0.375rem; border-radius: calc(var(--ui-radius) + 4px); border: 2px dashed var(--ui-accent);
|
|
80
80
|
background: color-mix(in srgb, var(--ui-accent-soft) 85%, transparent); color: var(--ui-accent-soft-text); font-weight: 500; pointer-events: none; font-size: 20px;
|
|
81
81
|
& span { font-size: 14px; } }
|
|
82
|
+
/* With several images it keeps one height, so the arrows stay put from image to image. */
|
|
83
|
+
[data-scope=attachments][data-part=stage] { position: relative;
|
|
84
|
+
&[data-several] { display: flex; align-items: center; justify-content: center; height: 70vh; } }
|
|
82
85
|
[data-scope=attachments][data-part=preview] { display: block; max-width: 100%; max-height: 70vh; margin: 0 auto; border-radius: var(--ui-radius); }
|
|
86
|
+
[data-scope=attachments][data-part=step] { position: absolute; top: 50%; translate: 0 -50%; display: grid; place-items: center; width: 2.25rem; height: 2.25rem; padding: 0;
|
|
87
|
+
border: 1px solid var(--ui-border); border-radius: 50%; background: var(--ui-surface); color: var(--ui-text); cursor: pointer; font-size: 18px; box-shadow: var(--ui-card-shadow);
|
|
88
|
+
&:hover { background: var(--ui-hover); }
|
|
89
|
+
&:focus-visible { outline: 2px solid var(--ui-ring-soft); }
|
|
90
|
+
&[data-side=previous] { left: 0.5rem; }
|
|
91
|
+
&[data-side=next] { right: 0.5rem; } }
|
|
92
|
+
[data-scope=attachments][data-part=position] { margin-right: auto; align-self: center; font-size: 14px; color: var(--ui-muted); font-variant-numeric: tabular-nums; }
|
|
83
93
|
[data-scope=attachments][data-part=download-link] { display: inline-flex; align-items: center; height: var(--ui-control-height-sm); padding: 0 0.75rem;
|
|
84
94
|
border-radius: var(--ui-radius); border: 1px solid var(--ui-border); color: var(--ui-text); text-decoration: none; font-size: 14px;
|
|
85
95
|
&:hover { background: var(--ui-hover); } }
|
|
@@ -190,6 +190,8 @@ export type Messages = {
|
|
|
190
190
|
orDrop: string;
|
|
191
191
|
dropToAdd: string;
|
|
192
192
|
download: string;
|
|
193
|
+
previous: string;
|
|
194
|
+
next: string;
|
|
193
195
|
open: (name: string) => string;
|
|
194
196
|
downloadFile: (name: string) => string;
|
|
195
197
|
count: (count: number, max: number) => string;
|
package/dist/format/messages.js
CHANGED
|
@@ -60,7 +60,7 @@ export const en = {
|
|
|
60
60
|
reasons: { FILE_INVALID_TYPE: 'Not an allowed type', FILE_TOO_LARGE: 'Too large', TOO_MANY_FILES: 'Too many files', FILE_TOO_SMALL: 'Too small' },
|
|
61
61
|
},
|
|
62
62
|
attachments: {
|
|
63
|
-
add: 'Add files', layout: 'Layout', grid: 'Grid', list: 'List', orDrop: 'or drop files here', dropToAdd: 'Drop to add', download: 'Download',
|
|
63
|
+
add: 'Add files', layout: 'Layout', grid: 'Grid', list: 'List', orDrop: 'or drop files here', dropToAdd: 'Drop to add', download: 'Download', previous: 'Previous image', next: 'Next image',
|
|
64
64
|
open: (name) => `Open ${name}`, downloadFile: (name) => `Download ${name}`,
|
|
65
65
|
count: (count, max) => `${count} of ${max}`, limit: (max) => `The limit is ${max} files`,
|
|
66
66
|
notType: 'not an accepted type', over: (size) => `over ${size}`, notAdded: (name, reason) => `${name} wasn’t added: ${reason}.`,
|
|
@@ -150,7 +150,7 @@ export const fr = {
|
|
|
150
150
|
},
|
|
151
151
|
attachments: {
|
|
152
152
|
add: 'Ajouter des fichiers', layout: 'Affichage', grid: 'Grille', list: 'Liste', orDrop: 'ou déposez des fichiers ici',
|
|
153
|
-
dropToAdd: 'Déposez pour ajouter', download: 'Télécharger',
|
|
153
|
+
dropToAdd: 'Déposez pour ajouter', download: 'Télécharger', previous: 'Image précédente', next: 'Image suivante',
|
|
154
154
|
open: (name) => `Ouvrir ${name}`, downloadFile: (name) => `Télécharger ${name}`,
|
|
155
155
|
count: (count, max) => `${count} sur ${max}`, limit: (max) => `La limite est de ${max} fichiers`,
|
|
156
156
|
notType: 'type non accepté', over: (size) => `plus de ${size}`, notAdded: (name, reason) => `${name} n’a pas été ajouté : ${reason}.`,
|