@lerianstudio/sindarian-ui 2.0.0-beta.1 → 2.0.0-beta.3
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/dist/components/form/file-upload-field/index.d.ts +11 -2
- package/dist/components/form/file-upload-field/index.d.ts.map +1 -1
- package/dist/components/form/file-upload-field/index.js +8 -3
- package/dist/components/form/input-field/index.d.ts +41 -2
- package/dist/components/form/input-field/index.d.ts.map +1 -1
- package/dist/components/form/input-field/index.js +73 -15
- package/dist/components/form/select-field/index.d.ts +9 -1
- package/dist/components/form/select-field/index.d.ts.map +1 -1
- package/dist/components/form/select-field/index.js +2 -2
- package/dist/components/ui/button/styles.css +31 -1
- package/dist/components/ui/calendar/index.d.ts.map +1 -1
- package/dist/components/ui/calendar/index.js +7 -1
- package/dist/components/ui/file-upload/index.d.ts +235 -9
- package/dist/components/ui/file-upload/index.d.ts.map +1 -1
- package/dist/components/ui/file-upload/index.js +328 -13
- package/dist/components/ui/form.d.ts +4 -0
- package/dist/components/ui/form.d.ts.map +1 -1
- package/dist/components/ui/form.js +48 -6
- package/dist/components/ui/toggle-group/index.d.ts +1 -1
- package/dist/components/ui/toggle-group/index.d.ts.map +1 -1
- package/dist/components/ui/toggle-group/index.js +73 -4
- package/dist/esm/components/form/file-upload-field/index.d.ts +11 -2
- package/dist/esm/components/form/file-upload-field/index.d.ts.map +1 -1
- package/dist/esm/components/form/file-upload-field/index.js +8 -3
- package/dist/esm/components/form/input-field/index.d.ts +41 -2
- package/dist/esm/components/form/input-field/index.d.ts.map +1 -1
- package/dist/esm/components/form/input-field/index.js +73 -15
- package/dist/esm/components/form/select-field/index.d.ts +9 -1
- package/dist/esm/components/form/select-field/index.d.ts.map +1 -1
- package/dist/esm/components/form/select-field/index.js +2 -2
- package/dist/esm/components/ui/calendar/index.d.ts.map +1 -1
- package/dist/esm/components/ui/calendar/index.js +8 -2
- package/dist/esm/components/ui/file-upload/index.d.ts +235 -9
- package/dist/esm/components/ui/file-upload/index.d.ts.map +1 -1
- package/dist/esm/components/ui/file-upload/index.js +327 -13
- package/dist/esm/components/ui/form.d.ts +4 -0
- package/dist/esm/components/ui/form.d.ts.map +1 -1
- package/dist/esm/components/ui/form.js +48 -6
- package/dist/esm/components/ui/toggle-group/index.d.ts +1 -1
- package/dist/esm/components/ui/toggle-group/index.d.ts.map +1 -1
- package/dist/esm/components/ui/toggle-group/index.js +73 -4
- package/package.json +1 -1
|
@@ -34,15 +34,23 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
34
34
|
};
|
|
35
35
|
})();
|
|
36
36
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
-
exports.FileUpload = void 0;
|
|
37
|
+
exports.MultipleFileUpload = exports.FileUpload = void 0;
|
|
38
38
|
exports.validateFile = validateFile;
|
|
39
|
+
exports.humanizeSize = humanizeSize;
|
|
39
40
|
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
40
41
|
/**
|
|
41
|
-
* FileUpload — a controlled "pick a file,
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
42
|
+
* FileUpload — a controlled "pick a file, hand it back" primitive. The defining
|
|
43
|
+
* job: select one file (by click, keyboard, or drag-and-drop), validate it
|
|
44
|
+
* against an accept filter + a byte ceiling, and emit `{ file, text }`.
|
|
45
|
+
*
|
|
46
|
+
* `readAs` decides whether the bytes are decoded on the way through. `'text'`
|
|
47
|
+
* (the default) reads UTF-8 via FileReader.readAsText, which is what a PEM/CSV
|
|
48
|
+
* host wants. `'none'` hands the `File` over untouched and is the only mode a
|
|
49
|
+
* BINARY file can use: a PDF, XLSX or PFX put through readAsText decodes into
|
|
50
|
+
* replacement-character garbage, and that garbage string is then retained for
|
|
51
|
+
* as long as the host holds the value — 20 MiB of PDF became a useless 20 MiB
|
|
52
|
+
* string, and the host still had to read the file a second time to get at the
|
|
53
|
+
* real bytes.
|
|
46
54
|
*
|
|
47
55
|
* The real `<input type="file">` IS the accessible control: it is `sr-only`
|
|
48
56
|
* (visually hidden) but focusable and labelable — never `aria-hidden`, never
|
|
@@ -63,6 +71,11 @@ const jsx_runtime_1 = require("react/jsx-runtime");
|
|
|
63
71
|
* a value, so it cannot masquerade as valid. `accept` is validated against
|
|
64
72
|
* BOTH extension and MIME because the native `accept` attribute is only a
|
|
65
73
|
* browser hint and is bypassable via drag-drop.
|
|
74
|
+
*
|
|
75
|
+
* A host that already announces the rejection itself — a toast in its own
|
|
76
|
+
* locale, driven off `onError` — silences this one by returning nothing from
|
|
77
|
+
* `labels.error`. Two announcements for one event, in two languages, is the
|
|
78
|
+
* accessibility defect; the wording is not.
|
|
66
79
|
*/
|
|
67
80
|
const React = __importStar(require("react"));
|
|
68
81
|
const lucide_react_1 = require("lucide-react");
|
|
@@ -105,7 +118,11 @@ function matchesAccept(file, accept) {
|
|
|
105
118
|
return mime === token;
|
|
106
119
|
});
|
|
107
120
|
}
|
|
108
|
-
/**
|
|
121
|
+
/**
|
|
122
|
+
* Humanize a byte count for the selected-file chip. Binary units, 1 decimal.
|
|
123
|
+
* Exported so a `labels.error` override can format `maxSizeBytes` exactly the
|
|
124
|
+
* way the chip and the default message do, instead of re-deriving binary units.
|
|
125
|
+
*/
|
|
109
126
|
function humanizeSize(bytes) {
|
|
110
127
|
if (bytes < 1024)
|
|
111
128
|
return `${bytes} B`;
|
|
@@ -118,8 +135,8 @@ function humanizeSize(bytes) {
|
|
|
118
135
|
}
|
|
119
136
|
return `${size.toFixed(1)} ${units[unit]}`;
|
|
120
137
|
}
|
|
121
|
-
/**
|
|
122
|
-
function
|
|
138
|
+
/** The default English announcement for each rejection kind. */
|
|
139
|
+
function defaultErrorMessage(error) {
|
|
123
140
|
switch (error.kind) {
|
|
124
141
|
case 'too-large':
|
|
125
142
|
return `File is too large (max ${humanizeSize(error.maxSizeBytes)}).`;
|
|
@@ -129,7 +146,7 @@ function errorMessage(error) {
|
|
|
129
146
|
return 'Could not read the file.';
|
|
130
147
|
}
|
|
131
148
|
}
|
|
132
|
-
exports.FileUpload = React.forwardRef(function FileUpload({ accept, maxSizeBytes, value, onSelect, onError, disabled = false, id, className, 'aria-invalid': ariaInvalid, 'aria-required': ariaRequired, 'aria-describedby': ariaDescribedby, 'aria-label': ariaLabel, ...rest }, ref) {
|
|
149
|
+
exports.FileUpload = React.forwardRef(function FileUpload({ accept, maxSizeBytes, value, readAs = 'text', labels, onSelect, onError, disabled = false, id, className, 'aria-invalid': ariaInvalid, 'aria-required': ariaRequired, 'aria-describedby': ariaDescribedby, 'aria-label': ariaLabel, ...rest }, ref) {
|
|
133
150
|
const internalRef = React.useRef(null);
|
|
134
151
|
React.useImperativeHandle(ref, () => internalRef.current);
|
|
135
152
|
const reactId = React.useId();
|
|
@@ -141,12 +158,21 @@ exports.FileUpload = React.forwardRef(function FileUpload({ accept, maxSizeBytes
|
|
|
141
158
|
// component's own validation/read error — a self-detected bad pick must read
|
|
142
159
|
// as invalid even outside a form.
|
|
143
160
|
const invalid = ariaInvalid || error !== null;
|
|
161
|
+
// Resolve the announcement BEFORE deciding whether the alert exists: a
|
|
162
|
+
// consumer that returns nothing is opting out of this surface entirely,
|
|
163
|
+
// and an association pointing at an unrendered node is worse than none.
|
|
164
|
+
const resolved = error
|
|
165
|
+
? labels?.error
|
|
166
|
+
? labels.error(error)
|
|
167
|
+
: defaultErrorMessage(error)
|
|
168
|
+
: null;
|
|
169
|
+
const errorText = resolved ? resolved : null;
|
|
144
170
|
// Radix Slot OVERWRITES aria-describedby (it does not merge), so merge the
|
|
145
171
|
// primitive's own role=alert error id with the FormControl-injected one so
|
|
146
172
|
// both associations coexist on the input. A plain join, never `cn` —
|
|
147
173
|
// tailwind-merge treats these as class names and would drop an id that
|
|
148
174
|
// happens to look like a conflicting utility.
|
|
149
|
-
const describedBy = [ariaDescribedby,
|
|
175
|
+
const describedBy = [ariaDescribedby, errorText ? errorId : undefined]
|
|
150
176
|
.filter(Boolean)
|
|
151
177
|
.join(' ') || undefined;
|
|
152
178
|
// Last-resolved-wins: a slow read for pick A must not overwrite a newer
|
|
@@ -162,6 +188,15 @@ exports.FileUpload = React.forwardRef(function FileUpload({ accept, maxSizeBytes
|
|
|
162
188
|
onError?.(validationError);
|
|
163
189
|
return;
|
|
164
190
|
}
|
|
191
|
+
// Binary path: no decode, no reader, no retained garbage string. The
|
|
192
|
+
// stale-rejection clear still has to happen here — the alert is this
|
|
193
|
+
// component's own state and a good pick must retire it.
|
|
194
|
+
if (readAs === 'none') {
|
|
195
|
+
readerRef.current = null;
|
|
196
|
+
setError(null);
|
|
197
|
+
onSelect({ file, text: '' });
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
165
200
|
const reader = new FileReader();
|
|
166
201
|
readerRef.current = reader;
|
|
167
202
|
reader.onload = () => {
|
|
@@ -228,10 +263,290 @@ exports.FileUpload = React.forwardRef(function FileUpload({ accept, maxSizeBytes
|
|
|
228
263
|
return ((0, jsx_runtime_1.jsxs)("div", { className: (0, utils_1.cn)('space-y-2', className), children: [(0, jsx_runtime_1.jsxs)("div", { onClick: openPicker, onDragOver: onDragOver, onDragLeave: onDragLeave, onDrop: onDrop, className: (0, utils_1.cn)('border-input bg-card focus-within:ring-ring focus-within:ring-offset-background aria-[invalid=true]:border-destructive aria-[invalid=true]:focus-within:ring-destructive flex w-full items-center gap-3 rounded-md border px-3 py-4 text-sm shadow-xs transition-colors focus-within:ring-2 focus-within:ring-offset-1 focus-within:outline-none', dragActive &&
|
|
229
264
|
'border-ring ring-ring ring-offset-background ring-2 ring-offset-1', disabled
|
|
230
265
|
? 'border-muted bg-muted/30 cursor-not-allowed shadow-none'
|
|
231
|
-
: 'cursor-pointer'), "aria-invalid": invalid || undefined, children: [(0, jsx_runtime_1.jsx)("input", { ref: internalRef, id: inputId, type: "file", accept: accept, disabled: disabled, className: "sr-only", "aria-invalid": invalid || undefined, "aria-required": ariaRequired || undefined, "aria-describedby": describedBy, "aria-label": ariaLabel, onChange: onInputChange, ...rest }), (0, jsx_runtime_1.jsx)(lucide_react_1.Upload, { className: "text-muted-foreground size-4 shrink-0", "aria-hidden": "true" }), value ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)("span", { className: "min-w-0 flex-1 truncate", children: [(0, jsx_runtime_1.jsx)("span", { className: "text-foreground font-medium", children: value.file.name }), ' ', (0, jsx_runtime_1.jsx)("span", { className: "text-muted-foreground tabular-nums", children: humanizeSize(value.file.size) })] }), (0, jsx_runtime_1.jsx)(icon_button_1.IconButton, { type: "button", variant: "plain", size: "small", disabled: disabled, "aria-label":
|
|
266
|
+
: 'cursor-pointer'), "aria-invalid": invalid || undefined, children: [(0, jsx_runtime_1.jsx)("input", { ref: internalRef, id: inputId, type: "file", accept: accept, disabled: disabled, className: "sr-only", "aria-invalid": invalid || undefined, "aria-required": ariaRequired || undefined, "aria-describedby": describedBy, "aria-label": ariaLabel, onChange: onInputChange, ...rest }), (0, jsx_runtime_1.jsx)(lucide_react_1.Upload, { className: "text-muted-foreground size-4 shrink-0", "aria-hidden": "true" }), value ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)("span", { className: "min-w-0 flex-1 truncate", children: [(0, jsx_runtime_1.jsx)("span", { className: "text-foreground font-medium", children: value.file.name }), ' ', (0, jsx_runtime_1.jsx)("span", { className: "text-muted-foreground tabular-nums", children: humanizeSize(value.file.size) })] }), (0, jsx_runtime_1.jsx)(icon_button_1.IconButton, { type: "button", variant: "plain", size: "small", disabled: disabled, "aria-label": labels?.remove?.trim() ? labels.remove : 'Remove file', onClick: (event) => {
|
|
232
267
|
// Don't bubble to the zone's openPicker and re-open the dialog.
|
|
233
268
|
event.stopPropagation();
|
|
234
269
|
clear();
|
|
235
|
-
}, children: (0, jsx_runtime_1.jsx)(lucide_react_1.X, { className: "size-4", "aria-hidden": "true" }) })] })) : ((0, jsx_runtime_1.jsxs)("span", { className: "text-muted-foreground", children: [(0, jsx_runtime_1.jsx)("span", { className: "text-foreground font-medium", children:
|
|
270
|
+
}, children: (0, jsx_runtime_1.jsx)(lucide_react_1.X, { className: "size-4", "aria-hidden": "true" }) })] })) : ((0, jsx_runtime_1.jsxs)("span", { className: "text-muted-foreground", children: [(0, jsx_runtime_1.jsx)("span", { className: "text-foreground font-medium", children: labels?.action ?? 'Choose a file' }), ' ', labels?.hint ?? 'or drag and drop'] }))] }), errorText ? ((0, jsx_runtime_1.jsx)("p", { id: errorId, role: "alert", className: "text-system-error-h1a text-xs font-medium", children: errorText })) : null] }));
|
|
236
271
|
});
|
|
237
272
|
exports.FileUpload.displayName = 'FileUpload';
|
|
273
|
+
/** The default English announcement for each rejection kind. */
|
|
274
|
+
function defaultMultipleErrorMessage(error) {
|
|
275
|
+
if (error.kind === 'too-many') {
|
|
276
|
+
return `Too many files (max ${error.maxFiles}). ${error.file.name} was not added.`;
|
|
277
|
+
}
|
|
278
|
+
return defaultErrorMessage(error);
|
|
279
|
+
}
|
|
280
|
+
/** The default zone copy once the cap is reached, in grammatical English. */
|
|
281
|
+
function defaultFullMessage(maxFiles) {
|
|
282
|
+
return maxFiles === 1
|
|
283
|
+
? 'Maximum of 1 file reached.'
|
|
284
|
+
: `Maximum of ${maxFiles} files reached.`;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* MultipleFileUpload: the plural sibling of FileUpload. Pick SEVERAL files,
|
|
288
|
+
* accumulate them across repeated picks, validate each one, cap the total, and
|
|
289
|
+
* hand back `FileUploadResult[]`.
|
|
290
|
+
*
|
|
291
|
+
* A SIBLING COMPONENT, not a `multiple` flag. This library already answers
|
|
292
|
+
* "this one takes many" that way (`Select` / `MultipleSelect`), and FileUpload
|
|
293
|
+
* strips `'multiple'` from its props on purpose: single-file is its contract,
|
|
294
|
+
* not a default it happens to have. A boolean would have forced every prop
|
|
295
|
+
* here into a union that means one thing when the flag is set and another when
|
|
296
|
+
* it is not: `value` as `Result | Result[] | null`, a `maxFiles` that is
|
|
297
|
+
* meaningless in half the configurations, and a remove control whose
|
|
298
|
+
* accessible name is fixed copy in one mode and per-file in the other. The
|
|
299
|
+
* plural props follow the house shape for plural components: `value?: T[]`
|
|
300
|
+
* with `onValueChange?: (values: T[]) => void`.
|
|
301
|
+
*
|
|
302
|
+
* WHERE THE LINE SITS: this component owns SELECTION and nothing after it.
|
|
303
|
+
* Choosing, validating, capping, listing and removing are its job; uploading
|
|
304
|
+
* is not. That split is not squeamishness about scope, it is where the
|
|
305
|
+
* knowledge actually lives. An upload needs an endpoint, an auth scheme, a
|
|
306
|
+
* concurrency policy, a retry policy and, very often, a parent id that does
|
|
307
|
+
* not exist yet when the files are chosen: the motivating host stages evidence
|
|
308
|
+
* files while a form is being filled and can only upload them against the id
|
|
309
|
+
* that its create call returns afterwards. None of that is knowable from
|
|
310
|
+
* inside a library primitive, and a component that guessed would have to be
|
|
311
|
+
* fought rather than used. So the host keeps its own per-file record with
|
|
312
|
+
* status and retry, and this component keeps the part a form can hold and
|
|
313
|
+
* validate: the chosen files. `FileUploadResult[]` is a value; an upload state
|
|
314
|
+
* machine is not.
|
|
315
|
+
*
|
|
316
|
+
* ACCUMULATION is the defining behaviour. A second pick ADDS to the selection
|
|
317
|
+
* rather than replacing it, because a user assembling five documents does it
|
|
318
|
+
* in two or three trips to the file dialog, not one. Everything else follows
|
|
319
|
+
* from that: room is measured against what is already selected, and the batch
|
|
320
|
+
* that overflows the cap still contributes the files that fit.
|
|
321
|
+
*
|
|
322
|
+
* A BATCH SURVIVES ITS OWN CASUALTIES. One file rejected for type, size or a
|
|
323
|
+
* failed read does not discard the rest of the batch, and it does not consume
|
|
324
|
+
* a slot either: validation runs over the WHOLE batch before the cap is
|
|
325
|
+
* applied, so a file that was never eligible cannot cost an eligible one its
|
|
326
|
+
* place, and `'too-many'` always names a file a slot would genuinely have
|
|
327
|
+
* taken. The alternative punishes
|
|
328
|
+
* a user for a mistake in one file by throwing away four good ones, and hands
|
|
329
|
+
* back no way to tell which was which. Every rejection is reported through
|
|
330
|
+
* `onError` and announced together in one `role="alert"`.
|
|
331
|
+
*
|
|
332
|
+
* Accessibility follows the sibling BELOW THE CAP: the real `<input
|
|
333
|
+
* type="file">` is `sr-only` but focusable and labelable, and never
|
|
334
|
+
* `aria-hidden`, so FormControl-injected ARIA and react-hook-form's focus on
|
|
335
|
+
* error both work while the picker is enabled. At the cap that changes, and
|
|
336
|
+
* the paragraph below says how.
|
|
337
|
+
* The file list sits OUTSIDE the click zone, so activating a remove control
|
|
338
|
+
* cannot also reopen the picker, and each remove control is named after its
|
|
339
|
+
* own file: a column of identical "Remove file" buttons tells a screen-reader
|
|
340
|
+
* user nothing about which row they are on.
|
|
341
|
+
*
|
|
342
|
+
* AT THE CAP the picker takes the native `disabled` attribute, and that DOES
|
|
343
|
+
* take it out of the tab order. This is the one state in which the input is
|
|
344
|
+
* not a focus target, and the one state in which focus-on-error cannot land on
|
|
345
|
+
* it, so it is a real cost rather than a free win. The cap has no counterpart
|
|
346
|
+
* in the single-file sibling, so the precedent followed here is
|
|
347
|
+
* `DateRangePicker`'s trigger: a control whose only job is to open a dialog has
|
|
348
|
+
* no state worth keeping focusable, and native `disabled` is what both removes
|
|
349
|
+
* it from the tab order and keeps the dialog shut. The alternative, an enabled
|
|
350
|
+
* picker that opens the file dialog and then refuses every file with
|
|
351
|
+
* `too-many`, is a control that lies about being available. What keeps the cap
|
|
352
|
+
* from being a dead end is the escape hatch: the remove controls answer to
|
|
353
|
+
* `disabled` alone and NEVER to the cap, so they stay focusable and removing
|
|
354
|
+
* one file reopens the picker.
|
|
355
|
+
*/
|
|
356
|
+
exports.MultipleFileUpload = React.forwardRef(function MultipleFileUpload({ accept, maxSizeBytes, maxFiles, value = [], readAs = 'text', labels, onValueChange, onError, disabled = false, id, className, 'aria-invalid': ariaInvalid, 'aria-required': ariaRequired, 'aria-describedby': ariaDescribedby, 'aria-label': ariaLabel, ...rest }, ref) {
|
|
357
|
+
const internalRef = React.useRef(null);
|
|
358
|
+
React.useImperativeHandle(ref, () => internalRef.current);
|
|
359
|
+
const reactId = React.useId();
|
|
360
|
+
const inputId = id ?? reactId;
|
|
361
|
+
const errorId = `${inputId}-file-upload-error`;
|
|
362
|
+
const [dragActive, setDragActive] = React.useState(false);
|
|
363
|
+
const [errors, setErrors] = React.useState([]);
|
|
364
|
+
// The authoritative base for the next append. Props win on every render, so
|
|
365
|
+
// the host stays in control; the commit below also writes through, so a
|
|
366
|
+
// second batch that settles before the host has re-rendered still appends to
|
|
367
|
+
// the first batch's result instead of overwriting it.
|
|
368
|
+
const valueRef = React.useRef(value);
|
|
369
|
+
React.useEffect(() => {
|
|
370
|
+
valueRef.current = value;
|
|
371
|
+
});
|
|
372
|
+
// Superseding is NOT the contract here the way it is in the single-file
|
|
373
|
+
// sibling — batches accumulate, so an in-flight read is never stale. The
|
|
374
|
+
// readers are tracked for the one case that does have to stop them: an
|
|
375
|
+
// unmount. A read that lands afterwards would settle its batch and commit,
|
|
376
|
+
// calling the host's `onValueChange` for a component that no longer exists.
|
|
377
|
+
const readersRef = React.useRef(new Set());
|
|
378
|
+
React.useEffect(() => () => {
|
|
379
|
+
for (const reader of readersRef.current)
|
|
380
|
+
reader.abort();
|
|
381
|
+
readersRef.current.clear();
|
|
382
|
+
}, []);
|
|
383
|
+
const invalid = ariaInvalid || errors.length > 0;
|
|
384
|
+
// Resolve the announcements BEFORE deciding whether the alert exists: a
|
|
385
|
+
// consumer that returns nothing for every rejection is opting out of this
|
|
386
|
+
// surface, and an association pointing at an unrendered node is worse than
|
|
387
|
+
// none.
|
|
388
|
+
const messages = errors
|
|
389
|
+
.map((failure) => labels?.error
|
|
390
|
+
? labels.error(failure)
|
|
391
|
+
: defaultMultipleErrorMessage(failure))
|
|
392
|
+
.filter((message) => Boolean(message));
|
|
393
|
+
// A plain join, never `cn`: tailwind-merge treats these as class names and
|
|
394
|
+
// would drop an id that happens to look like a conflicting utility.
|
|
395
|
+
const describedBy = [ariaDescribedby, messages.length > 0 ? errorId : undefined]
|
|
396
|
+
.filter(Boolean)
|
|
397
|
+
.join(' ') || undefined;
|
|
398
|
+
const full = maxFiles !== undefined && value.length >= maxFiles;
|
|
399
|
+
// Removing must stay possible at the cap, so only the PICKER closes.
|
|
400
|
+
const pickerDisabled = disabled || full;
|
|
401
|
+
const roomFor = (selected) => maxFiles === undefined
|
|
402
|
+
? Number.POSITIVE_INFINITY
|
|
403
|
+
: Math.max(maxFiles - selected, 0);
|
|
404
|
+
// The cap is enforced HERE, at the only place that appends, because only the
|
|
405
|
+
// commit knows the base it lands on. `ingest` measures room too, but for an
|
|
406
|
+
// asynchronous batch it measures it BEFORE any read settles: two overlapping
|
|
407
|
+
// batches both see the pre-commit selection and would each believe they fit.
|
|
408
|
+
const commit = (accepted, rejections) => {
|
|
409
|
+
const room = roomFor(valueRef.current.length);
|
|
410
|
+
const fitting = accepted.slice(0, room);
|
|
411
|
+
const overflow = accepted[room];
|
|
412
|
+
const failures = overflow !== undefined && maxFiles !== undefined
|
|
413
|
+
? [
|
|
414
|
+
...rejections,
|
|
415
|
+
{ kind: 'too-many', file: overflow.file, maxFiles }
|
|
416
|
+
]
|
|
417
|
+
: rejections;
|
|
418
|
+
setErrors(failures);
|
|
419
|
+
for (const rejection of failures)
|
|
420
|
+
onError?.(rejection);
|
|
421
|
+
if (fitting.length === 0)
|
|
422
|
+
return;
|
|
423
|
+
const next = [...valueRef.current, ...fitting];
|
|
424
|
+
valueRef.current = next;
|
|
425
|
+
onValueChange(next);
|
|
426
|
+
};
|
|
427
|
+
const ingest = (incoming) => {
|
|
428
|
+
if (incoming.length === 0)
|
|
429
|
+
return;
|
|
430
|
+
const room = roomFor(valueRef.current.length);
|
|
431
|
+
const rejections = [];
|
|
432
|
+
// Validate EVERY file BEFORE the cap is applied. Slicing to the remaining
|
|
433
|
+
// room first would let a file that was never eligible consume a slot a
|
|
434
|
+
// good file could have used — one bad pick costing a good one, which is
|
|
435
|
+
// the opposite of a batch surviving its own casualties — and it would
|
|
436
|
+
// leave every file past the slice window neither validated nor reported.
|
|
437
|
+
// Validation is pure metadata (size and accept), so running it over files
|
|
438
|
+
// that may not fit costs nothing.
|
|
439
|
+
const eligible = [];
|
|
440
|
+
for (const file of incoming) {
|
|
441
|
+
const rejection = validateFile(file, { accept, maxSizeBytes });
|
|
442
|
+
// Continue rather than abort: one bad file must not cost the good ones.
|
|
443
|
+
if (rejection) {
|
|
444
|
+
rejections.push(rejection);
|
|
445
|
+
continue;
|
|
446
|
+
}
|
|
447
|
+
eligible.push(file);
|
|
448
|
+
}
|
|
449
|
+
// The cap then applies to the SURVIVORS. One rejection for the batch,
|
|
450
|
+
// naming the FIRST eligible file that did not fit: naming one already
|
|
451
|
+
// refused for its size or type would blame the cap for the wrong thing,
|
|
452
|
+
// and repeating it per overflowing file buries the actionable part.
|
|
453
|
+
if (eligible.length > room && maxFiles !== undefined) {
|
|
454
|
+
rejections.push({ kind: 'too-many', file: eligible[room], maxFiles });
|
|
455
|
+
}
|
|
456
|
+
const candidates = eligible.slice(0, room);
|
|
457
|
+
// Binary path: no decode, no reader, no retained garbage string.
|
|
458
|
+
if (readAs === 'none') {
|
|
459
|
+
commit(candidates.map((file) => ({ file, text: '' })), rejections);
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
if (candidates.length === 0) {
|
|
463
|
+
commit([], rejections);
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
// Slot-per-candidate so the emitted batch keeps PICK order regardless of
|
|
467
|
+
// the order the reads settle in. A null slot is a read that failed; the
|
|
468
|
+
// batch commits once every read has settled, one way or the other.
|
|
469
|
+
const slots = new Array(candidates.length).fill(null);
|
|
470
|
+
let remaining = candidates.length;
|
|
471
|
+
const settle = () => {
|
|
472
|
+
remaining -= 1;
|
|
473
|
+
if (remaining > 0)
|
|
474
|
+
return;
|
|
475
|
+
const accepted = [];
|
|
476
|
+
const readFailures = [];
|
|
477
|
+
slots.forEach((slot, index) => {
|
|
478
|
+
if (slot)
|
|
479
|
+
accepted.push(slot);
|
|
480
|
+
else
|
|
481
|
+
readFailures.push({ kind: 'read-failed', file: candidates[index] });
|
|
482
|
+
});
|
|
483
|
+
commit(accepted, [...rejections, ...readFailures]);
|
|
484
|
+
};
|
|
485
|
+
candidates.forEach((file, index) => {
|
|
486
|
+
const reader = new FileReader();
|
|
487
|
+
readersRef.current.add(reader);
|
|
488
|
+
// Deliberately no `onabort` handler: an aborted read must NOT settle,
|
|
489
|
+
// or the batch would commit at exactly the moment we are stopping it.
|
|
490
|
+
reader.onload = () => {
|
|
491
|
+
readersRef.current.delete(reader);
|
|
492
|
+
slots[index] = { file, text: String(reader.result ?? '') };
|
|
493
|
+
settle();
|
|
494
|
+
};
|
|
495
|
+
reader.onerror = () => {
|
|
496
|
+
readersRef.current.delete(reader);
|
|
497
|
+
settle();
|
|
498
|
+
};
|
|
499
|
+
reader.readAsText(file);
|
|
500
|
+
});
|
|
501
|
+
};
|
|
502
|
+
const onInputChange = (event) => {
|
|
503
|
+
const files = Array.from(event.target.files ?? []);
|
|
504
|
+
// Release the FileList the moment it has been read, or re-picking the same
|
|
505
|
+
// file is silently a no-op: the browser fires `change` only when the
|
|
506
|
+
// selection DIFFERS from what the input already holds.
|
|
507
|
+
event.target.value = '';
|
|
508
|
+
ingest(files);
|
|
509
|
+
};
|
|
510
|
+
const onDragOver = (event) => {
|
|
511
|
+
if (disabled)
|
|
512
|
+
return;
|
|
513
|
+
event.preventDefault();
|
|
514
|
+
setDragActive(true);
|
|
515
|
+
};
|
|
516
|
+
const onDragLeave = (event) => {
|
|
517
|
+
event.preventDefault();
|
|
518
|
+
setDragActive(false);
|
|
519
|
+
};
|
|
520
|
+
const onDrop = (event) => {
|
|
521
|
+
if (disabled)
|
|
522
|
+
return;
|
|
523
|
+
event.preventDefault();
|
|
524
|
+
setDragActive(false);
|
|
525
|
+
// Deliberately NOT gated on `full`: a drop onto a full zone is answered
|
|
526
|
+
// with the too-many rejection, which says why, instead of nothing at all.
|
|
527
|
+
ingest(Array.from(event.dataTransfer.files ?? []));
|
|
528
|
+
};
|
|
529
|
+
// Mouse convenience only. A click that ORIGINATED on the input already opens
|
|
530
|
+
// the picker natively and bubbles up here, so ignore it or it opens twice.
|
|
531
|
+
const openPicker = (event) => {
|
|
532
|
+
if (pickerDisabled || event.target === internalRef.current)
|
|
533
|
+
return;
|
|
534
|
+
internalRef.current?.click();
|
|
535
|
+
};
|
|
536
|
+
const removeAt = (index) => {
|
|
537
|
+
// By index, not by name: two files can share a filename and identity is
|
|
538
|
+
// what the row actually stands for.
|
|
539
|
+
const next = valueRef.current.filter((_, position) => position !== index);
|
|
540
|
+
valueRef.current = next;
|
|
541
|
+
onValueChange(next);
|
|
542
|
+
};
|
|
543
|
+
return ((0, jsx_runtime_1.jsxs)("div", { className: (0, utils_1.cn)('space-y-2', className), children: [(0, jsx_runtime_1.jsxs)("div", { onClick: openPicker, onDragOver: onDragOver, onDragLeave: onDragLeave, onDrop: onDrop, className: (0, utils_1.cn)('border-input bg-card focus-within:ring-ring focus-within:ring-offset-background aria-[invalid=true]:border-destructive aria-[invalid=true]:focus-within:ring-destructive flex w-full items-center gap-3 rounded-md border px-3 py-4 text-sm shadow-xs transition-colors focus-within:ring-2 focus-within:ring-offset-1 focus-within:outline-none', dragActive &&
|
|
544
|
+
'border-ring ring-ring ring-offset-background ring-2 ring-offset-1', pickerDisabled
|
|
545
|
+
? 'border-muted bg-muted/30 cursor-not-allowed shadow-none'
|
|
546
|
+
: 'cursor-pointer'), "aria-invalid": invalid || undefined, children: [(0, jsx_runtime_1.jsx)("input", { ref: internalRef, id: inputId, type: "file", multiple: true, accept: accept, disabled: pickerDisabled, className: "sr-only", "aria-invalid": invalid || undefined, "aria-required": ariaRequired || undefined, "aria-describedby": describedBy, "aria-label": ariaLabel, onChange: onInputChange, ...rest }), (0, jsx_runtime_1.jsx)(lucide_react_1.Upload, { className: "text-muted-foreground size-4 shrink-0", "aria-hidden": "true" }), full && maxFiles !== undefined ? ((0, jsx_runtime_1.jsx)("span", { className: "text-muted-foreground", children: labels?.full
|
|
547
|
+
? labels.full(maxFiles)
|
|
548
|
+
: defaultFullMessage(maxFiles) })) : ((0, jsx_runtime_1.jsxs)("span", { className: "text-muted-foreground", children: [(0, jsx_runtime_1.jsx)("span", { className: "text-foreground font-medium", children: labels?.action ?? 'Choose files' }), ' ', labels?.hint ?? 'or drag and drop'] }))] }), value.length > 0 ? ((0, jsx_runtime_1.jsx)("ul", { className: "space-y-1", children: value.map((entry, index) => ((0, jsx_runtime_1.jsxs)("li", { className: "flex items-center gap-3 text-sm", children: [(0, jsx_runtime_1.jsxs)("span", { className: "min-w-0 flex-1 truncate", children: [(0, jsx_runtime_1.jsx)("span", { className: "text-foreground font-medium", children: entry.file.name }), ' ', (0, jsx_runtime_1.jsx)("span", { className: "text-muted-foreground tabular-nums", children: humanizeSize(entry.file.size) })] }), (0, jsx_runtime_1.jsx)(icon_button_1.IconButton, { type: "button", variant: "plain", size: "small", disabled: disabled, "aria-label": labels?.remove
|
|
549
|
+
? labels.remove(entry.file)
|
|
550
|
+
: `Remove ${entry.file.name}`, onClick: () => removeAt(index), children: (0, jsx_runtime_1.jsx)(lucide_react_1.X, { className: "size-4", "aria-hidden": "true" }) })] }, `${entry.file.name}-${index}`))) })) : null, messages.length > 0 ? ((0, jsx_runtime_1.jsx)("div", { id: errorId, role: "alert", className: "space-y-1", children: messages.map((message, index) => ((0, jsx_runtime_1.jsx)("p", { className: "text-system-error-h1a text-xs font-medium", children: message }, index))) })) : null] }));
|
|
551
|
+
});
|
|
552
|
+
exports.MultipleFileUpload.displayName = 'MultipleFileUpload';
|
|
@@ -16,7 +16,11 @@ declare const useFormField: () => {
|
|
|
16
16
|
formDescriptionId: string;
|
|
17
17
|
formMessageId: string;
|
|
18
18
|
required: boolean | undefined;
|
|
19
|
+
described: Record<DescribingSlot, boolean> | undefined;
|
|
20
|
+
onDescribedChange: ((slot: DescribingSlot, rendered: boolean) => void) | undefined;
|
|
19
21
|
};
|
|
22
|
+
/** The two slots that can describe a control, in the order a reader hears them. */
|
|
23
|
+
type DescribingSlot = 'message' | 'description';
|
|
20
24
|
export type FormItemProps = React.HTMLAttributes<HTMLDivElement> & {
|
|
21
25
|
required?: boolean;
|
|
22
26
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"form.d.ts","sourceRoot":"","sources":["../../../src/components/ui/form.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,KAAK,cAAc,MAAM,uBAAuB,CAAA;AAEvD,OAAO,EAEL,eAAe,EACf,SAAS,EACT,WAAW,EAGZ,MAAM,iBAAiB,CAAA;AAKxB,OAAO,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAS9D,QAAA,MAAM,IAAI,4MAAe,CAAA;AAazB,QAAA,MAAM,SAAS,GACb,YAAY,SAAS,WAAW,GAAG,WAAW,EAC9C,KAAK,SAAS,SAAS,CAAC,YAAY,CAAC,GAAG,SAAS,CAAC,YAAY,CAAC,EAC/D,cAEC,eAAe,CAAC,YAAY,EAAE,KAAK,CAAC,sBAMtC,CAAA;AAED,QAAA,MAAM,YAAY
|
|
1
|
+
{"version":3,"file":"form.d.ts","sourceRoot":"","sources":["../../../src/components/ui/form.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,KAAK,cAAc,MAAM,uBAAuB,CAAA;AAEvD,OAAO,EAEL,eAAe,EACf,SAAS,EACT,WAAW,EAGZ,MAAM,iBAAiB,CAAA;AAKxB,OAAO,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAS9D,QAAA,MAAM,IAAI,4MAAe,CAAA;AAazB,QAAA,MAAM,SAAS,GACb,YAAY,SAAS,WAAW,GAAG,WAAW,EAC9C,KAAK,SAAS,SAAS,CAAC,YAAY,CAAC,GAAG,SAAS,CAAC,YAAY,CAAC,EAC/D,cAEC,eAAe,CAAC,YAAY,EAAE,KAAK,CAAC,sBAMtC,CAAA;AAED,QAAA,MAAM,YAAY;;;;;;;;;;;;;+BA4CW,cAAc,YAAY,OAAO,KAAK,IAAI;CAjBtE,CAAA;AAED,mFAAmF;AACnF,KAAK,cAAc,GAAG,SAAS,GAAG,aAAa,CAAA;AAwC/C,MAAM,MAAM,aAAa,GAAG,KAAK,CAAC,cAAc,CAAC,cAAc,CAAC,GAAG;IACjE,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB,CAAA;AAED,QAAA,MAAM,QAAQ;eAHD,OAAO;wCA8BnB,CAAA;AAGD,MAAM,MAAM,cAAc,GAAG,KAAK,CAAC,wBAAwB,CACzD,OAAO,cAAc,CAAC,IAAI,CAC3B,GAAG;IACF,KAAK,CAAC,EAAE,KAAK,CAAC,SAAS,CAAA;CACxB,CAAA;AAED,QAAA,MAAM,SAAS;YAHL,KAAK,CAAC,SAAS;0CA6BvB,CAAA;AAGF,eAAO,MAAM,WAAW,GAAI,yBAAyB,oBAAoB,sBASxE,CAAA;AAED,QAAA,MAAM,WAAW;;gFAgCf,CAAA;AAGF,QAAA,MAAM,eAAe,yHAgBnB,CAAA;AAGF,QAAA,MAAM,WAAW,yHA6Bf,CAAA;AAGF,OAAO,EACL,YAAY,EACZ,IAAI,EACJ,QAAQ,EACR,SAAS,EACT,WAAW,EACX,eAAe,EACf,WAAW,EACX,SAAS,EACV,CAAA"}
|
|
@@ -61,7 +61,7 @@ const useFormField = () => {
|
|
|
61
61
|
const fieldState = form && fieldContext.name
|
|
62
62
|
? form.getFieldState(fieldContext.name, form.formState)
|
|
63
63
|
: undefined;
|
|
64
|
-
const { id, required } = itemContext;
|
|
64
|
+
const { id, required, described, onDescribedChange } = itemContext;
|
|
65
65
|
return {
|
|
66
66
|
id,
|
|
67
67
|
name: fieldContext.name,
|
|
@@ -69,14 +69,38 @@ const useFormField = () => {
|
|
|
69
69
|
formDescriptionId: `${id}-form-item-description`,
|
|
70
70
|
formMessageId: `${id}-form-item-message`,
|
|
71
71
|
required,
|
|
72
|
+
described,
|
|
73
|
+
onDescribedChange,
|
|
72
74
|
...fieldState
|
|
73
75
|
};
|
|
74
76
|
};
|
|
75
77
|
exports.useFormField = useFormField;
|
|
76
78
|
const FormItemContext = React.createContext({});
|
|
79
|
+
/**
|
|
80
|
+
* Announce that this slot is on the page, for as long as it is.
|
|
81
|
+
*
|
|
82
|
+
* `rendered` is a parameter rather than a caller-side condition because
|
|
83
|
+
* `FormMessage` returns null when it has nothing to say, and a hook cannot sit
|
|
84
|
+
* behind that early return.
|
|
85
|
+
*/
|
|
86
|
+
function useDescribingSlot(slot, rendered) {
|
|
87
|
+
const { onDescribedChange } = React.useContext(FormItemContext);
|
|
88
|
+
React.useEffect(() => {
|
|
89
|
+
if (!rendered)
|
|
90
|
+
return;
|
|
91
|
+
onDescribedChange?.(slot, true);
|
|
92
|
+
return () => onDescribedChange?.(slot, false);
|
|
93
|
+
}, [onDescribedChange, slot, rendered]);
|
|
94
|
+
}
|
|
77
95
|
const FormItem = React.forwardRef(({ className, required, ...props }, ref) => {
|
|
78
96
|
const id = React.useId();
|
|
79
|
-
|
|
97
|
+
const [described, setDescribed] = React.useState({ message: false, description: false });
|
|
98
|
+
// Keeps the object identity when nothing moved, so a slot re-registering
|
|
99
|
+
// does not re-render the item for no reason.
|
|
100
|
+
const onDescribedChange = React.useCallback((slot, rendered) => setDescribed((current) => current[slot] === rendered
|
|
101
|
+
? current
|
|
102
|
+
: { ...current, [slot]: rendered }), []);
|
|
103
|
+
return ((0, jsx_runtime_1.jsx)(FormItemContext.Provider, { value: { id, required, described, onDescribedChange }, children: (0, jsx_runtime_1.jsx)("div", { ref: ref, className: (0, utils_1.cn)('space-y-2', className), ...props }) }));
|
|
80
104
|
});
|
|
81
105
|
exports.FormItem = FormItem;
|
|
82
106
|
FormItem.displayName = 'FormItem';
|
|
@@ -92,15 +116,28 @@ FormLabel.displayName = 'FormLabel';
|
|
|
92
116
|
const FormTooltip = ({ children, ...others }) => ((0, jsx_runtime_1.jsx)(tooltip_1.TooltipProvider, { ...others, children: (0, jsx_runtime_1.jsxs)(tooltip_1.Tooltip, { children: [(0, jsx_runtime_1.jsx)(tooltip_1.TooltipTrigger, { asChild: true, children: (0, jsx_runtime_1.jsx)(lucide_react_1.HelpCircle, { className: "text-container-text ml-2 h-4 w-4" }) }), (0, jsx_runtime_1.jsx)(tooltip_1.TooltipContent, { children: children })] }) }));
|
|
93
117
|
exports.FormTooltip = FormTooltip;
|
|
94
118
|
const FormControl = React.forwardRef(({ ...props }, ref) => {
|
|
95
|
-
const { error, formItemId, formDescriptionId, formMessageId } = useFormField();
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
119
|
+
const { error, formItemId, formDescriptionId, formMessageId, described } = useFormField();
|
|
120
|
+
// Only ids that resolve. A screen reader drops a dangling IDREF silently, so
|
|
121
|
+
// the barrier this removes is diagnostic rather than operational: a form with
|
|
122
|
+
// thirteen broken associations is a form where a real one cannot be spotted.
|
|
123
|
+
//
|
|
124
|
+
// Message first: a reader hears the error before the hint. `undefined` when
|
|
125
|
+
// both slots are empty, which drops the attribute — and, because Radix Slot
|
|
126
|
+
// OVERWRITES rather than merges, still lets the wrapped element carry its own
|
|
127
|
+
// (`ui/file-upload` merges the injected id with its own error id that way).
|
|
128
|
+
const describedBy = [
|
|
129
|
+
described?.message ? formMessageId : undefined,
|
|
130
|
+
described?.description ? formDescriptionId : undefined
|
|
131
|
+
]
|
|
132
|
+
.filter(Boolean)
|
|
133
|
+
.join(' ') || undefined;
|
|
134
|
+
return ((0, jsx_runtime_1.jsx)(react_slot_1.Slot, { ref: ref, id: formItemId, "aria-describedby": describedBy, "aria-invalid": !!error, ...props }));
|
|
99
135
|
});
|
|
100
136
|
exports.FormControl = FormControl;
|
|
101
137
|
FormControl.displayName = 'FormControl';
|
|
102
138
|
const FormDescription = React.forwardRef(({ className, ...props }, ref) => {
|
|
103
139
|
const { formDescriptionId } = useFormField();
|
|
140
|
+
useDescribingSlot('description', true);
|
|
104
141
|
return ((0, jsx_runtime_1.jsx)("p", { ref: ref, id: formDescriptionId, className: (0, utils_1.cn)('text-muted-foreground text-xs font-medium', className), ...props }));
|
|
105
142
|
});
|
|
106
143
|
exports.FormDescription = FormDescription;
|
|
@@ -108,6 +145,11 @@ FormDescription.displayName = 'FormDescription';
|
|
|
108
145
|
const FormMessage = React.forwardRef(({ className, children, ...props }, ref) => {
|
|
109
146
|
const { error, formMessageId } = useFormField();
|
|
110
147
|
const body = error ? String(error?.message) : children;
|
|
148
|
+
// Registered on `body`, not on `error`: a message given plain children
|
|
149
|
+
// renders copy that the control has to point at too. The old expression only
|
|
150
|
+
// added the message id when react-hook-form reported an error, so a
|
|
151
|
+
// standalone message was rendered and never associated.
|
|
152
|
+
useDescribingSlot('message', Boolean(body));
|
|
111
153
|
if (!body) {
|
|
112
154
|
return null;
|
|
113
155
|
}
|
|
@@ -13,6 +13,6 @@ declare const toggleVariants: (props?: ({
|
|
|
13
13
|
declare function ToggleGroup({ className, variant, size, spacing, children, style, ...props }: React.ComponentProps<typeof ToggleGroupPrimitive.Root> & VariantProps<typeof toggleVariants> & {
|
|
14
14
|
spacing?: number;
|
|
15
15
|
}): React.JSX.Element;
|
|
16
|
-
declare function ToggleGroupItem({ className, children, variant, size, ...props }: React.ComponentProps<typeof ToggleGroupPrimitive.Item> & VariantProps<typeof toggleVariants>): React.JSX.Element;
|
|
16
|
+
declare function ToggleGroupItem({ className, children, variant, size, onFocus, ...props }: React.ComponentProps<typeof ToggleGroupPrimitive.Item> & VariantProps<typeof toggleVariants>): React.JSX.Element;
|
|
17
17
|
export { ToggleGroup, ToggleGroupItem };
|
|
18
18
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/components/ui/toggle-group/index.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,KAAK,oBAAoB,MAAM,8BAA8B,CAAA;AACpE,OAAO,EAAO,KAAK,YAAY,EAAE,MAAM,0BAA0B,CAAA;AAIjE;;;;GAIG;AACH,QAAA,MAAM,cAAc;;;8EAoBnB,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/components/ui/toggle-group/index.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,KAAK,oBAAoB,MAAM,8BAA8B,CAAA;AACpE,OAAO,EAAO,KAAK,YAAY,EAAE,MAAM,0BAA0B,CAAA;AAIjE;;;;GAIG;AACH,QAAA,MAAM,cAAc;;;8EAoBnB,CAAA;AA4DD,iBAAS,WAAW,CAAC,EACnB,SAAS,EACT,OAAO,EACP,IAAI,EACJ,OAAW,EACX,QAAQ,EACR,KAAK,EACL,GAAG,KAAK,EACT,EAAE,KAAK,CAAC,cAAc,CAAC,OAAO,oBAAoB,CAAC,IAAI,CAAC,GACvD,YAAY,CAAC,OAAO,cAAc,CAAC,GAAG;IACpC,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB,qBA+BF;AAED,iBAAS,eAAe,CAAC,EACvB,SAAS,EACT,QAAQ,EACR,OAAO,EACP,IAAI,EACJ,OAAO,EACP,GAAG,KAAK,EACT,EAAE,KAAK,CAAC,cAAc,CAAC,OAAO,oBAAoB,CAAC,IAAI,CAAC,GACvD,YAAY,CAAC,OAAO,cAAc,CAAC,qBAiDpC;AAED,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,CAAA"}
|
|
@@ -63,12 +63,58 @@ const toggleVariants = (0, class_variance_authority_1.cva)("hover:bg-muted hover
|
|
|
63
63
|
size: 'default'
|
|
64
64
|
}
|
|
65
65
|
});
|
|
66
|
+
const ARROW_KEYS = ['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'];
|
|
67
|
+
/**
|
|
68
|
+
* Is an arrow key down right now?
|
|
69
|
+
*
|
|
70
|
+
* This is how an item tells keyboard navigation apart from a pointer press or a
|
|
71
|
+
* programmatic focus, and it is the seam Radix uses for the same job in its own
|
|
72
|
+
* `RadioGroup`. It works because `RovingFocusGroup` defers the focus move to a
|
|
73
|
+
* `setTimeout`: the keydown has already bubbled to `document` and set the flag
|
|
74
|
+
* by the time the item receives `focus`.
|
|
75
|
+
*
|
|
76
|
+
* The distinction is not cosmetic. A pointer press focuses BEFORE the click
|
|
77
|
+
* lands, so selecting on every focus would turn one click into
|
|
78
|
+
* select-then-deselect and break mouse selection outright.
|
|
79
|
+
*
|
|
80
|
+
* Returns null when disabled, so `type="multiple"` carries no flag to read.
|
|
81
|
+
*/
|
|
82
|
+
function useArrowKeyPressed(enabled) {
|
|
83
|
+
const arrowKeyPressed = React.useRef(false);
|
|
84
|
+
React.useEffect(() => {
|
|
85
|
+
if (!enabled)
|
|
86
|
+
return;
|
|
87
|
+
const onKeyDown = (event) => {
|
|
88
|
+
if (ARROW_KEYS.includes(event.key))
|
|
89
|
+
arrowKeyPressed.current = true;
|
|
90
|
+
};
|
|
91
|
+
const onKeyUp = () => {
|
|
92
|
+
arrowKeyPressed.current = false;
|
|
93
|
+
};
|
|
94
|
+
document.addEventListener('keydown', onKeyDown);
|
|
95
|
+
document.addEventListener('keyup', onKeyUp);
|
|
96
|
+
window.addEventListener('blur', onKeyUp);
|
|
97
|
+
return () => {
|
|
98
|
+
document.removeEventListener('keydown', onKeyDown);
|
|
99
|
+
document.removeEventListener('keyup', onKeyUp);
|
|
100
|
+
window.removeEventListener('blur', onKeyUp);
|
|
101
|
+
// A group unmounted mid-keystroke never sees the keyup.
|
|
102
|
+
arrowKeyPressed.current = false;
|
|
103
|
+
};
|
|
104
|
+
}, [enabled]);
|
|
105
|
+
return enabled ? arrowKeyPressed : null;
|
|
106
|
+
}
|
|
66
107
|
const ToggleGroupContext = React.createContext({
|
|
67
108
|
size: 'default',
|
|
68
109
|
variant: 'default',
|
|
69
|
-
spacing: 0
|
|
110
|
+
spacing: 0,
|
|
111
|
+
arrowKeyPressed: null
|
|
70
112
|
});
|
|
71
113
|
function ToggleGroup({ className, variant, size, spacing = 0, children, style, ...props }) {
|
|
114
|
+
// Read, never destructured: `type` is the discriminant of the Radix props
|
|
115
|
+
// union, and pulling it out then spreading it back widens the union so the
|
|
116
|
+
// single/multiple props stop resolving.
|
|
117
|
+
const arrowKeyPressed = useArrowKeyPressed(props.type === 'single');
|
|
72
118
|
return ((0, jsx_runtime_1.jsx)(ToggleGroupPrimitive.Root, { "data-slot": "toggle-group", "data-variant": variant, "data-size": size, "data-spacing": spacing,
|
|
73
119
|
// Caller style first: `--gap` is ours and must survive, but everything
|
|
74
120
|
// else the caller passes has to reach the element.
|
|
@@ -76,12 +122,35 @@ function ToggleGroup({ className, variant, size, spacing = 0, children, style, .
|
|
|
76
122
|
// Legacy carried a `data-[spacing=default]:...:shadow-xs` rule here that
|
|
77
123
|
// never matched — `data-spacing` is always a number. Removed rather than
|
|
78
124
|
// activated, so the rendered pixels stay identical to sindarian-x.
|
|
79
|
-
'group/toggle-group flex w-fit items-center gap-[--spacing(var(--gap))] rounded-md', className), ...props, children: (0, jsx_runtime_1.jsx)(ToggleGroupContext.Provider, { value: { variant, size, spacing }, children: children }) }));
|
|
125
|
+
'group/toggle-group flex w-fit items-center gap-[--spacing(var(--gap))] rounded-md', className), ...props, children: (0, jsx_runtime_1.jsx)(ToggleGroupContext.Provider, { value: { variant, size, spacing, arrowKeyPressed }, children: children }) }));
|
|
80
126
|
}
|
|
81
|
-
function ToggleGroupItem({ className, children, variant, size, ...props }) {
|
|
127
|
+
function ToggleGroupItem({ className, children, variant, size, onFocus, ...props }) {
|
|
82
128
|
const context = React.useContext(ToggleGroupContext);
|
|
129
|
+
/**
|
|
130
|
+
* Selection follows focus, which is what `role="radio"` promises.
|
|
131
|
+
*
|
|
132
|
+
* With `type="single"` Radix puts the group on the ARIA radio pattern: the
|
|
133
|
+
* root is a `radiogroup`, each item a `radio` with `aria-checked`. Under that
|
|
134
|
+
* pattern an arrow key both moves focus and checks the item it lands on.
|
|
135
|
+
* Radix only moves focus, so a screen-reader operator used to arrow across
|
|
136
|
+
* "radio, not checked, 2 of 3" with nothing ever selected.
|
|
137
|
+
*
|
|
138
|
+
* `type="multiple"` is a `toolbar` of `aria-pressed` buttons and keeps its
|
|
139
|
+
* current behavior, where moving focus without pressing is correct: the flag
|
|
140
|
+
* is null there.
|
|
141
|
+
*
|
|
142
|
+
* Clicking the element rather than writing the value keeps a single
|
|
143
|
+
* activation path, so Radix's own toggle and any caller `onClick` both fire
|
|
144
|
+
* exactly as they do for a real click.
|
|
145
|
+
*/
|
|
146
|
+
const handleFocus = (event) => {
|
|
147
|
+
onFocus?.(event);
|
|
148
|
+
if (context.arrowKeyPressed?.current) {
|
|
149
|
+
event.currentTarget.click();
|
|
150
|
+
}
|
|
151
|
+
};
|
|
83
152
|
return ((0, jsx_runtime_1.jsx)(ToggleGroupPrimitive.Item, { "data-slot": "toggle-group-item", "data-variant": context.variant || variant, "data-size": context.size || size, "data-spacing": context.spacing, className: (0, utils_1.cn)(toggleVariants({
|
|
84
153
|
variant: context.variant || variant,
|
|
85
154
|
size: context.size || size
|
|
86
|
-
}), 'w-auto min-w-0 shrink-0 px-3 focus:z-10 focus-visible:z-10', 'data-[spacing=0]:rounded-none data-[spacing=0]:shadow-none data-[spacing=0]:first:rounded-l-md data-[spacing=0]:last:rounded-r-md data-[spacing=0]:data-[variant=outline]:border-l-0 data-[spacing=0]:data-[variant=outline]:first:border-l', className), ...props, children: children }));
|
|
155
|
+
}), 'w-auto min-w-0 shrink-0 px-3 focus:z-10 focus-visible:z-10', 'data-[spacing=0]:rounded-none data-[spacing=0]:shadow-none data-[spacing=0]:first:rounded-l-md data-[spacing=0]:last:rounded-r-md data-[spacing=0]:data-[variant=outline]:border-l-0 data-[spacing=0]:data-[variant=outline]:first:border-l', className), ...props, onFocus: handleFocus, children: children }));
|
|
87
156
|
}
|