@zayne-labs/ui-react 0.7.5 → 0.8.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.
@@ -1,140 +1,279 @@
1
+ import { dataAttr } from '../../chunk-DNYM6XGW.js';
1
2
  import { cnMerge } from '../../chunk-OHG7GB7O.js';
2
- import '../../chunk-PZ5AY32C.js';
3
+ import { __export } from '../../chunk-PZ5AY32C.js';
3
4
  import * as React from 'react';
4
- import { useRef, useState } from 'react';
5
- import { handleFileValidation } from '@zayne-labs/toolkit-core';
6
- import { useToggle, useCallbackRef } from '@zayne-labs/toolkit-react';
7
- import { mergeTwoProps, composeRefs } from '@zayne-labs/toolkit-react/utils';
8
- import { isFunction } from '@zayne-labs/toolkit-type-helpers';
5
+ import { isValidElement, Fragment, useRef, useState, useCallback } from 'react';
6
+ import { withSlotNameAndSymbol, getSlotMap, mergeTwoProps, composeRefs } from '@zayne-labs/toolkit-react/utils';
7
+ import { isArray, isFile, isFunction } from '@zayne-labs/toolkit-type-helpers';
8
+ import { toArray, handleFileValidation, createImagePreview } from '@zayne-labs/toolkit-core';
9
+ import { useCallbackRef } from '@zayne-labs/toolkit-react';
9
10
 
11
+ var generateUniqueId = (file) => {
12
+ if (!isFile(file)) {
13
+ return file.id;
14
+ }
15
+ return `${file.name}-(${Math.round(performance.now())})-${crypto.randomUUID().slice(0, 8)}`;
16
+ };
17
+ var clearObjectURL = (fileObject) => {
18
+ const shouldClearObjectURL = isFile(fileObject?.file) && fileObject.file.type.startsWith("image/");
19
+ if (fileObject?.preview && shouldClearObjectURL) {
20
+ URL.revokeObjectURL(fileObject.preview);
21
+ }
22
+ };
23
+
24
+ // src/components/ui/drop-zone/use-drop-zone.ts
10
25
  var useDropZone = (props) => {
11
- const inputRef = useRef(null);
12
26
  const {
13
27
  allowedFileTypes,
14
28
  children,
15
29
  classNames,
16
30
  disallowDuplicates = true,
17
- existingFiles,
18
31
  extraInputProps,
19
32
  extraRootProps,
20
- fileLimit,
33
+ initialFiles,
34
+ maxFileCount,
21
35
  maxFileSize,
36
+ multiple,
37
+ onFilesChange,
22
38
  onUpload,
23
39
  onUploadError,
40
+ onUploadErrors,
24
41
  onUploadSuccess,
25
42
  render,
26
43
  validator
27
- // eslint-disable-next-line ts-eslint/no-unnecessary-condition -- Can be undefined
28
44
  } = props ?? {};
29
- const [isDragging, toggleIsDragging] = useToggle(false);
30
- const [acceptedFiles, setAcceptedFiles] = useState([]);
31
- const handleFileUpload = useCallbackRef(
32
- (event) => {
33
- if (event.defaultPrevented) return;
34
- if (event.type === "drop") {
35
- event.preventDefault();
36
- toggleIsDragging(false);
37
- }
38
- const fileList = event.type === "drop" ? event.dataTransfer.files : event.target.files;
39
- if (fileList === null) {
40
- console.warn("No file selected");
41
- return;
42
- }
43
- const validFilesArray = handleFileValidation({
44
- existingFileArray: existingFiles,
45
- newFileList: fileList,
46
- onError: onUploadError,
47
- onSuccess: onUploadSuccess,
48
- validationSettings: {
49
- allowedFileTypes,
50
- disallowDuplicates,
51
- fileLimit,
52
- maxFileSize
53
- },
54
- validator
55
- });
56
- if (validFilesArray.length === 0) return;
57
- setAcceptedFiles(validFilesArray);
58
- onUpload?.({ acceptedFiles: validFilesArray, event });
45
+ const inputRef = useRef(null);
46
+ const initialFileArray = toArray(initialFiles).filter(Boolean);
47
+ const [dropZoneState, setDropZoneState] = useState({
48
+ errors: [],
49
+ filesWithPreview: initialFileArray.map((fileMeta) => ({
50
+ file: fileMeta,
51
+ id: fileMeta.id,
52
+ preview: fileMeta.url
53
+ })),
54
+ isDragging: false
55
+ });
56
+ const toggleIsDragging = (value) => {
57
+ setDropZoneState((prevState) => ({ ...prevState, isDragging: value }));
58
+ };
59
+ const addFiles = useCallbackRef((fileList, event) => {
60
+ if (!fileList || fileList.length === 0) {
61
+ console.warn("No file selected!");
62
+ return;
59
63
  }
60
- );
61
- const handleDragOver = (event) => {
64
+ clearErrors();
65
+ if (!multiple) {
66
+ clearFiles();
67
+ }
68
+ const { errors, validFiles } = handleFileValidation({
69
+ existingFiles: dropZoneState.filesWithPreview.map((fileWithPreview) => fileWithPreview.file),
70
+ newFiles: fileList,
71
+ onError: onUploadError,
72
+ onErrors: onUploadErrors,
73
+ onSuccess: onUploadSuccess,
74
+ validationSettings: { allowedFileTypes, disallowDuplicates, maxFileCount, maxFileSize },
75
+ validator
76
+ });
77
+ if (validFiles.length === 0) {
78
+ setDropZoneState((prevState) => ({ ...prevState, errors }));
79
+ return;
80
+ }
81
+ const filesWithPreview = validFiles.map((file) => ({
82
+ file,
83
+ id: generateUniqueId(file),
84
+ preview: createImagePreview({ file })
85
+ }));
86
+ if (event) {
87
+ onUpload?.({ event, filesWithPreview });
88
+ }
89
+ const newFileUploadState = {
90
+ ...dropZoneState,
91
+ errors,
92
+ ...event?.type === "drop" && { isDragging: false },
93
+ filesWithPreview: multiple ? [...dropZoneState.filesWithPreview, ...filesWithPreview] : filesWithPreview
94
+ };
95
+ onFilesChange?.({ filesWithPreview: newFileUploadState.filesWithPreview });
96
+ setDropZoneState(newFileUploadState);
97
+ inputRef.current && (inputRef.current.value = "");
98
+ });
99
+ const clearFiles = useCallbackRef(() => {
100
+ dropZoneState.filesWithPreview.forEach((fileObject) => clearObjectURL(fileObject));
101
+ const newFileUploadState = {
102
+ ...dropZoneState,
103
+ errors: [],
104
+ filesWithPreview: []
105
+ };
106
+ onFilesChange?.({ filesWithPreview: newFileUploadState.filesWithPreview });
107
+ setDropZoneState(newFileUploadState);
108
+ inputRef.current && (inputRef.current.value = "");
109
+ });
110
+ const removeFile = useCallbackRef((id) => {
111
+ const fileToRemove = dropZoneState.filesWithPreview.find((fileObject) => fileObject.id === id);
112
+ clearObjectURL(fileToRemove);
113
+ const newFilesWithPreview = dropZoneState.filesWithPreview.filter((file) => file.id !== id);
114
+ onFilesChange?.({ filesWithPreview: newFilesWithPreview });
115
+ setDropZoneState({
116
+ ...dropZoneState,
117
+ errors: [],
118
+ filesWithPreview: newFilesWithPreview
119
+ });
120
+ });
121
+ const clearErrors = useCallbackRef(() => {
122
+ setDropZoneState((prevState) => ({ ...prevState, errors: [] }));
123
+ });
124
+ const handleFileUpload = useCallbackRef((event) => {
125
+ if (event.defaultPrevented) return;
126
+ if (inputRef.current?.disabled) return;
127
+ if (event.type === "drop") {
128
+ event.preventDefault();
129
+ event.stopPropagation();
130
+ }
131
+ const fileList = event.type === "drop" ? event.dataTransfer.files : event.target.files;
132
+ if (!multiple) {
133
+ const firstFile = fileList?.[0];
134
+ firstFile && addFiles([firstFile], event);
135
+ return;
136
+ }
137
+ addFiles(fileList, event);
138
+ });
139
+ const handleDragEnter = useCallbackRef((event) => {
62
140
  event.preventDefault();
141
+ event.stopPropagation();
63
142
  toggleIsDragging(true);
64
- };
65
- const handleDragLeave = (event) => {
143
+ });
144
+ const handleDragOver = useCallbackRef((event) => {
145
+ event.preventDefault();
146
+ event.stopPropagation();
147
+ toggleIsDragging(true);
148
+ });
149
+ const handleDragLeave = useCallbackRef((event) => {
66
150
  event.preventDefault();
151
+ event.stopPropagation();
67
152
  toggleIsDragging(false);
68
- };
69
- const getRenderProps = () => ({
70
- acceptedFiles,
71
- inputRef,
72
- isDragging,
73
- openFilePicker: () => inputRef.current?.click()
74
153
  });
75
- const computedChildren = children ?? render;
76
- const getChildren = () => isFunction(computedChildren) ? computedChildren(getRenderProps()) : computedChildren;
77
- const getRootProps = (rootProps) => {
78
- const mergedRootProps = mergeTwoProps(extraRootProps, rootProps);
79
- return {
80
- ...mergedRootProps,
81
- className: cnMerge(
82
- "relative isolate flex flex-col",
83
- mergedRootProps.className,
84
- classNames?.base,
85
- isDragging && ["opacity-60", classNames?.activeDrag, rootProps?.classNames?.activeDrag]
86
- ),
87
- "data-active-drag": isDragging,
88
- "data-part": "root",
89
- "data-scope": "dropzone",
90
- onDragEnter: handleDragOver,
91
- onDragLeave: handleDragLeave,
92
- onDragOver: handleDragOver,
93
- onDrop: handleFileUpload
94
- };
95
- };
96
- const getInputProps = (inputProps) => {
97
- const mergedInputProps = mergeTwoProps(extraInputProps, inputProps);
98
- return {
99
- ...mergedInputProps,
100
- accept: allowedFileTypes ? allowedFileTypes.join(", ") : mergedInputProps.accept,
101
- className: cnMerge(
102
- "absolute inset-0 z-[100] cursor-pointer opacity-0",
103
- classNames?.input,
104
- mergedInputProps.className
105
- ),
106
- "data-active-drag": isDragging,
107
- "data-part": "input",
108
- "data-scope": "dropzone",
109
- onChange: (event) => {
110
- mergedInputProps.onChange?.(event);
111
- handleFileUpload(event);
112
- },
113
- ref: composeRefs([inputRef, mergedInputProps.ref]),
114
- type: "file"
115
- };
116
- };
117
- return {
118
- acceptedFiles,
119
- computedChildren,
120
- getChildren,
154
+ const openFilePicker = useCallbackRef(() => {
155
+ inputRef.current?.click();
156
+ });
157
+ const getRootProps = useCallback(
158
+ (rootProps) => {
159
+ const mergedRootProps = mergeTwoProps(extraRootProps, rootProps);
160
+ return {
161
+ ...mergedRootProps,
162
+ className: cnMerge(
163
+ "relative isolate flex flex-col",
164
+ mergedRootProps.className,
165
+ classNames?.base,
166
+ dropZoneState.isDragging && [
167
+ "opacity-60",
168
+ classNames?.isDragging,
169
+ rootProps?.classNames?.isDragging
170
+ ]
171
+ ),
172
+ "data-dragging": dataAttr(dropZoneState.isDragging),
173
+ "data-scope": "dropzone",
174
+ // eslint-disable-next-line perfectionist/sort-objects -- I need data-scope to be first
175
+ "data-part": "root",
176
+ "data-slot": "dropzone-root",
177
+ onDragEnter: handleDragEnter,
178
+ onDragLeave: handleDragLeave,
179
+ onDragOver: handleDragOver,
180
+ onDrop: handleFileUpload
181
+ };
182
+ },
183
+ [
184
+ classNames?.base,
185
+ classNames?.isDragging,
186
+ extraRootProps,
187
+ dropZoneState.isDragging,
188
+ handleDragEnter,
189
+ handleDragLeave,
190
+ handleDragOver,
191
+ handleFileUpload
192
+ ]
193
+ );
194
+ const getInputProps = useCallback(
195
+ (inputProps) => {
196
+ const mergedInputProps = mergeTwoProps(extraInputProps, inputProps);
197
+ return {
198
+ ...mergedInputProps,
199
+ accept: allowedFileTypes ? allowedFileTypes.join(", ") : mergedInputProps.accept,
200
+ className: cnMerge(
201
+ "absolute inset-0 z-[100] cursor-pointer opacity-0",
202
+ classNames?.input,
203
+ mergedInputProps.className
204
+ ),
205
+ "data-dragging": dataAttr(dropZoneState.isDragging),
206
+ "data-scope": "dropzone",
207
+ // eslint-disable-next-line perfectionist/sort-objects -- I need data-scope to be first
208
+ "data-part": "input",
209
+ "data-slot": "dropzone-input",
210
+ multiple: multiple ?? mergedInputProps.multiple,
211
+ onChange: (event) => {
212
+ mergedInputProps.onChange?.(event);
213
+ handleFileUpload(event);
214
+ },
215
+ ref: composeRefs([inputRef, mergedInputProps.ref]),
216
+ type: "file"
217
+ };
218
+ },
219
+ [
220
+ allowedFileTypes,
221
+ classNames?.input,
222
+ extraInputProps,
223
+ dropZoneState.isDragging,
224
+ handleFileUpload,
225
+ multiple
226
+ ]
227
+ );
228
+ const renderProps = {
229
+ addFiles,
230
+ clearErrors,
231
+ clearFiles,
232
+ dropZoneState,
121
233
  getInputProps,
122
- getRenderProps,
123
234
  getRootProps,
235
+ handleDragEnter,
124
236
  handleDragLeave,
125
237
  handleDragOver,
126
238
  handleFileUpload,
127
239
  inputRef,
128
- isDragging
240
+ openFilePicker,
241
+ removeFile
242
+ };
243
+ const selectedChildren = children ?? render;
244
+ const getResolvedChildren = () => {
245
+ return isFunction(selectedChildren) ? selectedChildren(renderProps) : selectedChildren;
246
+ };
247
+ return {
248
+ ...renderProps,
249
+ getResolvedChildren
129
250
  };
130
251
  };
131
252
 
132
253
  // src/components/ui/drop-zone/drop-zone.tsx
133
- function DropZone(props) {
134
- const api = useDropZone(props);
135
- return /* @__PURE__ */ React.createElement("div", { ...api.getRootProps() }, /* @__PURE__ */ React.createElement("input", { ...api.getInputProps() }), api.getChildren());
254
+ function DropZoneRoot(props) {
255
+ const { withInternalElements = true, ...restOfProps } = props;
256
+ const api = useDropZone(restOfProps);
257
+ const RootComponent = withInternalElements ? "div" : Fragment;
258
+ const InputComponent = withInternalElements ? "input" : Fragment;
259
+ const rootComponentProps = RootComponent === "div" && api.getRootProps();
260
+ const inputComponentProps = InputComponent === "input" && api.getInputProps();
261
+ const resolvedChildren = api.getResolvedChildren();
262
+ const couldChildrenContainSlots = isArray(resolvedChildren) || isValidElement(resolvedChildren) && resolvedChildren.type === Fragment;
263
+ const slots = getSlotMap(resolvedChildren, {
264
+ condition: withInternalElements && couldChildrenContainSlots
265
+ });
266
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(RootComponent, { ...rootComponentProps }, /* @__PURE__ */ React.createElement(InputComponent, { ...inputComponentProps }), slots.default), slots.preview);
136
267
  }
268
+ var DropZoneImagePreview = withSlotNameAndSymbol("preview");
269
+
270
+ // src/components/ui/drop-zone/drop-zone-parts.ts
271
+ var drop_zone_parts_exports = {};
272
+ __export(drop_zone_parts_exports, {
273
+ ImagePreview: () => DropZoneImagePreview,
274
+ Root: () => DropZoneRoot
275
+ });
137
276
 
138
- export { DropZone, useDropZone };
277
+ export { drop_zone_parts_exports as DropZone, DropZoneImagePreview, DropZoneRoot, useDropZone };
139
278
  //# sourceMappingURL=index.js.map
140
279
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/components/ui/drop-zone/use-drop-zone.ts","../../../../src/components/ui/drop-zone/drop-zone.tsx"],"names":[],"mappings":";;;;;;;;;AAgGa,IAAA,WAAA,GAAc,CAAC,KAA4B,KAAA;AACvD,EAAM,MAAA,QAAA,GAAW,OAAyB,IAAI,CAAA;AAE9C,EAAM,MAAA;AAAA,IACL,gBAAA;AAAA,IACA,QAAA;AAAA,IACA,UAAA;AAAA,IACA,kBAAqB,GAAA,IAAA;AAAA,IACrB,aAAA;AAAA,IACA,eAAA;AAAA,IACA,cAAA;AAAA,IACA,SAAA;AAAA,IACA,WAAA;AAAA,IACA,QAAA;AAAA,IACA,aAAA;AAAA,IACA,eAAA;AAAA,I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AAG,gBAAA;AAAA,MACH,QAAQ,gBAAmB,GAAA,gBAAA,CAAiB,IAAK,CAAA,IAAI,IAAI,gBAAiB,CAAA,MAAA;AAAA,MAC1E,SAAW,EAAA,OAAA;AAAA,QACV,mDAAA;AAAA,QACA,UAAY,EAAA,KAAA;AAAA,QACZ,gBAAiB,CAAA;AAAA,OAClB;AAAA,MACA,kBAAoB,EAAA,UAAA;AAAA,MACpB,WAAa,EAAA,OAAA;AAAA,MACb,YAAc,EAAA,UAAA;AAAA,MACd,QAAA,EAAU,CAAC,KAA+C,KAAA;AACzD,QAAA,gBAAA,CAAiB,WAAW,KAAK,CAAA;AACjC,QAAA,gBAAA,CAAiB,KAAK,CAAA;AAAA,OACvB;AAAA,MACA,KAAK,WAAY,CAAA,CAAC,QAAU,EAAA,gBAAA,CAAiB,GAAG,CAAC,CAAA;AAAA,MACjD,IAAM,EAAA;AAAA,KACP;AAAA,GACD;AAEA,EAAO,OAAA;AAAA,IACN,aAAA;AAAA,IACA,gBAAA;AAAA,IACA,WAAA;AAAA,IACA,aAAA;AAAA,IACA,cAAA;AAAA,IACA,YAAA;AAAA,IACA,eAAA;AAAA,IACA,cAAA;AAAA,IACA,gBAAA;AAAA,IACA,QAAA;AAAA,IACA;AAAA,GACD;AACD;;;AC/OA,SAAS,SAAS,KAAyB,EAAA;AAC1C,EAAM,MAAA,GAAA,GAAM,YAAY,KAAK,CAAA;AAE7B,EAAA,uBACE,KAAA,CAAA,aAAA,CAAA,KAAA,EAAA,EAAK,GAAG,GAAA,CAAI,cACZ,EAAA,kBAAA,KAAA,CAAA,aAAA,CAAC,OAAO,EAAA,EAAA,GAAG,IAAI,aAAc,EAAA,EAAG,CAE/B,EAAA,GAAA,CAAI,aACN,CAAA;AAEF","file":"index.js","sourcesContent":["\"use client\";\n\nimport { cnMerge } from \"@/lib/utils/cn\";\nimport { type FileValidationOptions, handleFileValidation } from \"@zayne-labs/toolkit-core\";\nimport { useCallbackRef, useToggle } from \"@zayne-labs/toolkit-react\";\nimport {\n\ttype DiscriminatedRenderProps,\n\ttype InferProps,\n\tcomposeRefs,\n\tmergeTwoProps,\n} from \"@zayne-labs/toolkit-react/utils\";\nimport { isFunction } from \"@zayne-labs/toolkit-type-helpers\";\nimport { useRef, useState } from \"react\";\n\ntype RenderProps = {\n\tacceptedFiles: File[];\n\tinputRef: React.RefObject<HTMLInputElement | null>;\n\tisDragging: boolean;\n\topenFilePicker: () => void;\n};\n\nexport type RootProps = InferProps<\"div\"> & {\n\tclassNames?: {\n\t\tactiveDrag?: string;\n\t\tbase?: string;\n\t};\n};\n\nexport type InputProps = InferProps<\"input\">;\n\ntype DropZoneRenderProps = DiscriminatedRenderProps<\n\tReact.ReactNode | ((props: RenderProps) => React.ReactNode)\n>;\n\nexport type UseDropZoneProps = DropZoneRenderProps & {\n\t/**\n\t * Allowed file types to be uploaded.\n\t */\n\tallowedFileTypes?: string[];\n\n\tclassNames?: { activeDrag?: string; base?: string; input?: string };\n\t/**\n\t * Whether to disallow duplicate files\n\t * @default true\n\t */\n\tdisallowDuplicates?: boolean;\n\n\t/**\n\t * Existing files to be uploaded\n\t */\n\texistingFiles?: File[];\n\n\t/**\n\t * Extra props to pass to the input element\n\t */\n\textraInputProps?: InputProps;\n\n\t/**\n\t * Extra props to pass to the root element\n\t */\n\textraRootProps?: RootProps;\n\n\t/**\n\t * Maximum number of files that can be uploaded.\n\t */\n\tfileLimit?: number;\n\n\t/**\n\t * Maximum file size in MB\n\t */\n\tmaxFileSize?: number;\n\n\t/**\n\t * Callback function to handle file upload\n\t */\n\tonUpload?: (details: {\n\t\tacceptedFiles: File[];\n\t\tevent: React.ChangeEvent<HTMLInputElement> | React.DragEvent<HTMLDivElement>;\n\t}) => void;\n\n\t/**\n\t * Callback function to handle file upload errors\n\t */\n\tonUploadError?: FileValidationOptions[\"onError\"];\n\n\t/**\n\t * Callback function to handle file upload success\n\t */\n\tonUploadSuccess?: FileValidationOptions[\"onSuccess\"];\n\n\t/**\n\t * Custom validator function to handle file validation\n\t */\n\tvalidator?: (context: { existingFileArray: File[] | undefined; newFileList: FileList }) => File[];\n};\n\nexport const useDropZone = (props: UseDropZoneProps) => {\n\tconst inputRef = useRef<HTMLInputElement>(null);\n\n\tconst {\n\t\tallowedFileTypes,\n\t\tchildren,\n\t\tclassNames,\n\t\tdisallowDuplicates = true,\n\t\texistingFiles,\n\t\textraInputProps,\n\t\textraRootProps,\n\t\tfileLimit,\n\t\tmaxFileSize,\n\t\tonUpload,\n\t\tonUploadError,\n\t\tonUploadSuccess,\n\t\trender,\n\t\tvalidator,\n\t\t// eslint-disable-next-line ts-eslint/no-unnecessary-condition -- Can be undefined\n\t} = props ?? {};\n\n\tconst [isDragging, toggleIsDragging] = useToggle(false);\n\n\tconst [acceptedFiles, setAcceptedFiles] = useState<File[]>([]);\n\n\tconst handleFileUpload = useCallbackRef(\n\t\t(event: React.ChangeEvent<HTMLInputElement> | React.DragEvent<HTMLDivElement>) => {\n\t\t\tif (event.defaultPrevented) return;\n\n\t\t\tif (event.type === \"drop\") {\n\t\t\t\tevent.preventDefault();\n\t\t\t\ttoggleIsDragging(false);\n\t\t\t}\n\n\t\t\tconst fileList =\n\t\t\t\tevent.type === \"drop\"\n\t\t\t\t\t? (event as React.DragEvent).dataTransfer.files\n\t\t\t\t\t: (event as React.ChangeEvent<HTMLInputElement>).target.files;\n\n\t\t\tif (fileList === null) {\n\t\t\t\tconsole.warn(\"No file selected\");\n\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst validFilesArray = handleFileValidation({\n\t\t\t\texistingFileArray: existingFiles,\n\t\t\t\tnewFileList: fileList,\n\t\t\t\tonError: onUploadError,\n\t\t\t\tonSuccess: onUploadSuccess,\n\t\t\t\tvalidationSettings: {\n\t\t\t\t\tallowedFileTypes,\n\t\t\t\t\tdisallowDuplicates,\n\t\t\t\t\tfileLimit,\n\t\t\t\t\tmaxFileSize,\n\t\t\t\t},\n\t\t\t\tvalidator,\n\t\t\t});\n\n\t\t\tif (validFilesArray.length === 0) return;\n\n\t\t\tsetAcceptedFiles(validFilesArray);\n\n\t\t\tonUpload?.({ acceptedFiles: validFilesArray, event });\n\t\t}\n\t);\n\n\tconst handleDragOver = (event: React.DragEvent<HTMLDivElement>) => {\n\t\tevent.preventDefault();\n\t\ttoggleIsDragging(true);\n\t};\n\n\tconst handleDragLeave = (event: React.DragEvent<HTMLDivElement>) => {\n\t\tevent.preventDefault();\n\t\ttoggleIsDragging(false);\n\t};\n\n\tconst getRenderProps = () =>\n\t\t({\n\t\t\tacceptedFiles,\n\t\t\tinputRef,\n\t\t\tisDragging,\n\t\t\topenFilePicker: () => inputRef.current?.click(),\n\t\t}) satisfies RenderProps;\n\n\tconst computedChildren = children ?? render;\n\n\tconst getChildren = () =>\n\t\tisFunction(computedChildren) ? computedChildren(getRenderProps()) : computedChildren;\n\n\tconst getRootProps = (rootProps?: RootProps) => {\n\t\tconst mergedRootProps = mergeTwoProps(extraRootProps, rootProps);\n\n\t\treturn {\n\t\t\t...mergedRootProps,\n\t\t\tclassName: cnMerge(\n\t\t\t\t\"relative isolate flex flex-col\",\n\t\t\t\tmergedRootProps.className,\n\t\t\t\tclassNames?.base,\n\t\t\t\tisDragging && [\"opacity-60\", classNames?.activeDrag, rootProps?.classNames?.activeDrag]\n\t\t\t),\n\t\t\t\"data-active-drag\": isDragging,\n\t\t\t\"data-part\": \"root\",\n\t\t\t\"data-scope\": \"dropzone\",\n\t\t\tonDragEnter: handleDragOver,\n\t\t\tonDragLeave: handleDragLeave,\n\t\t\tonDragOver: handleDragOver,\n\t\t\tonDrop: handleFileUpload,\n\t\t};\n\t};\n\n\tconst getInputProps = (inputProps?: InputProps) => {\n\t\tconst mergedInputProps = mergeTwoProps(extraInputProps, inputProps);\n\n\t\treturn {\n\t\t\t...mergedInputProps,\n\t\t\taccept: allowedFileTypes ? allowedFileTypes.join(\", \") : mergedInputProps.accept,\n\t\t\tclassName: cnMerge(\n\t\t\t\t\"absolute inset-0 z-[100] cursor-pointer opacity-0\",\n\t\t\t\tclassNames?.input,\n\t\t\t\tmergedInputProps.className\n\t\t\t),\n\t\t\t\"data-active-drag\": isDragging,\n\t\t\t\"data-part\": \"input\",\n\t\t\t\"data-scope\": \"dropzone\",\n\t\t\tonChange: (event: React.ChangeEvent<HTMLInputElement>) => {\n\t\t\t\tmergedInputProps.onChange?.(event);\n\t\t\t\thandleFileUpload(event);\n\t\t\t},\n\t\t\tref: composeRefs([inputRef, mergedInputProps.ref]),\n\t\t\ttype: \"file\",\n\t\t};\n\t};\n\n\treturn {\n\t\tacceptedFiles,\n\t\tcomputedChildren,\n\t\tgetChildren,\n\t\tgetInputProps,\n\t\tgetRenderProps,\n\t\tgetRootProps,\n\t\thandleDragLeave,\n\t\thandleDragOver,\n\t\thandleFileUpload,\n\t\tinputRef,\n\t\tisDragging,\n\t};\n};\n","import * as React from \"react\";\n\nimport { type UseDropZoneProps, useDropZone } from \"./use-drop-zone\";\n\nfunction DropZone(props: UseDropZoneProps) {\n\tconst api = useDropZone(props);\n\n\treturn (\n\t\t<div {...api.getRootProps()}>\n\t\t\t<input {...api.getInputProps()} />\n\n\t\t\t{api.getChildren()}\n\t\t</div>\n\t);\n}\n\nexport { DropZone };\n"]}
1
+ {"version":3,"sources":["../../../../src/components/ui/drop-zone/utils.ts","../../../../src/components/ui/drop-zone/use-drop-zone.ts","../../../../src/components/ui/drop-zone/drop-zone.tsx","../../../../src/components/ui/drop-zone/drop-zone-parts.ts"],"names":["ReactFragment"],"mappings":";;;;;;;;;;AAIO,IAAM,gBAAA,GAAmB,CAAC,IAAkC,KAAA;AAClE,EAAI,IAAA,CAAC,MAAO,CAAA,IAAI,CAAG,EAAA;AAClB,IAAA,OAAO,IAAK,CAAA,EAAA;AAAA;AAGb,EAAA,OAAO,GAAG,IAAK,CAAA,IAAI,CAAK,EAAA,EAAA,IAAA,CAAK,MAAM,WAAY,CAAA,GAAA,EAAK,CAAC,KAAK,MAAO,CAAA,UAAA,GAAa,KAAM,CAAA,CA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QAAA,EAAA,IAAA,kBAAA,KAAA,CAAA,aAAA,CAAC,aAAe,EAAA,EAAA,GAAG,sCACjB,KAAA,CAAA,aAAA,CAAA,cAAA,EAAA,EAAgB,GAAG,mBAAA,EAAqB,CAExC,EAAA,KAAA,CAAM,OACR,CAAA,EAEC,MAAM,OACR,CAAA;AAEF;AAIa,IAAA,oBAAA,GAAuB,sBAA0C,SAAS;;;AC3DvF,IAAA,uBAAA,GAAA;AAAA,QAAA,CAAA,uBAAA,EAAA;AAAA,EAAA,YAAA,EAAA,MAAA,oBAAA;AAAA,EAAA,IAAA,EAAA,MAAA;AAAA,CAAA,CAAA","file":"index.js","sourcesContent":["import type { FileMeta } from \"@zayne-labs/toolkit-core\";\nimport { isFile } from \"@zayne-labs/toolkit-type-helpers\";\nimport type { FileWithPreview } from \"./use-drop-zone\";\n\nexport const generateUniqueId = (file: File | FileMeta): string => {\n\tif (!isFile(file)) {\n\t\treturn file.id;\n\t}\n\n\treturn `${file.name}-(${Math.round(performance.now())})-${crypto.randomUUID().slice(0, 8)}`;\n};\n\nexport const clearObjectURL = (fileObject: FileWithPreview | undefined) => {\n\tconst shouldClearObjectURL = isFile(fileObject?.file) && fileObject.file.type.startsWith(\"image/\");\n\n\tif (fileObject?.preview && shouldClearObjectURL) {\n\t\tURL.revokeObjectURL(fileObject.preview);\n\t}\n};\n","import { cnMerge } from \"@/lib/utils/cn\";\nimport { dataAttr } from \"@/lib/utils/common\";\nimport { toArray } from \"@zayne-labs/toolkit-core\";\nimport {\n\ttype FileMeta,\n\ttype FileValidationErrorContext,\n\ttype FileValidationOptions,\n\tcreateImagePreview,\n\thandleFileValidation,\n} from \"@zayne-labs/toolkit-core\";\nimport { useCallbackRef } from \"@zayne-labs/toolkit-react\";\nimport {\n\ttype DiscriminatedRenderProps,\n\ttype InferProps,\n\tcomposeRefs,\n\tmergeTwoProps,\n} from \"@zayne-labs/toolkit-react/utils\";\nimport { type Prettify, isFunction } from \"@zayne-labs/toolkit-type-helpers\";\nimport { useCallback, useRef, useState } from \"react\";\nimport { clearObjectURL, generateUniqueId } from \"./utils\";\n\nexport type RootProps = InferProps<HTMLElement> & {\n\tclassNames?: {\n\t\tbase?: string;\n\t\tisDragging?: string;\n\t};\n};\n\nexport type InputProps = InferProps<\"input\">;\n\nexport type FileWithPreview = {\n\tfile: File | FileMeta;\n\tid: string;\n\tpreview?: string;\n};\n\nexport type FileUploadState = {\n\terrors: FileValidationErrorContext[];\n\tfilesWithPreview: FileWithPreview[];\n\tisDragging: boolean;\n};\n\ntype ChangeOrDragEvent = React.ChangeEvent<HTMLInputElement> | React.DragEvent<HTMLElement>;\n\ntype UseDropZoneResult = {\n\taddFiles: (fileList: File[] | FileList | null, event?: ChangeOrDragEvent) => void;\n\tclearErrors: () => void;\n\tclearFiles: () => void;\n\tdropZoneState: FileUploadState;\n\tgetInputProps: (inputProps?: InputProps) => InputProps;\n\tgetResolvedChildren: () => React.ReactNode;\n\tgetRootProps: (rootProps?: RootProps) => RootProps;\n\thandleDragEnter: (event: React.DragEvent<HTMLElement>) => void;\n\thandleDragLeave: (event: React.DragEvent<HTMLElement>) => void;\n\thandleDragOver: (event: React.DragEvent<HTMLElement>) => void;\n\thandleFileUpload: (event: React.ChangeEvent<HTMLInputElement> | React.DragEvent<HTMLElement>) => void;\n\tinputRef: React.RefObject<HTMLInputElement | null>;\n\topenFilePicker: () => void;\n\tremoveFile: (id: string) => void;\n};\n\ntype RenderProps = Omit<UseDropZoneResult, \"getResolvedChildren\">;\n\ntype DropZoneRenderProps = DiscriminatedRenderProps<\n\tReact.ReactNode | ((props: RenderProps) => React.ReactNode)\n>;\n\nexport type UseDropZoneProps = DropZoneRenderProps & {\n\t/**\n\t * Allowed file types to be uploaded.\n\t */\n\tallowedFileTypes?: string[];\n\n\t/**\n\t * CSS classes to apply to the various parts of the drop zone\n\t */\n\tclassNames?: Prettify<RootProps[\"classNames\"] & { input?: string }>;\n\n\t/**\n\t * Whether to disallow duplicate files\n\t * @default true\n\t */\n\tdisallowDuplicates?: boolean;\n\n\t/**\n\t * Extra props to pass to the input element\n\t */\n\textraInputProps?: InputProps;\n\n\t/**\n\t * Extra props to pass to the root element\n\t */\n\textraRootProps?: RootProps;\n\n\t/**\n\t * Initial files to populate the drop zone\n\t */\n\tinitialFiles?: FileMeta | FileMeta[] | null;\n\n\t/**\n\t * Maximum number of files that can be uploaded.\n\t */\n\tmaxFileCount?: number;\n\n\t/**\n\t * Maximum file size in MB\n\t */\n\tmaxFileSize?: number;\n\n\t/**\n\t * Whether to allow multiple files to be uploaded\n\t */\n\tmultiple?: boolean;\n\n\t/**\n\t * Callback function to be called when internal files state changes\n\t */\n\tonFilesChange?: (context: { filesWithPreview: FileWithPreview[] }) => void;\n\n\t/**\n\t * Callback function to be called when new files are uploaded\n\t */\n\tonUpload?: (context: { event: ChangeOrDragEvent; filesWithPreview: FileWithPreview[] }) => void;\n\n\t/**\n\t * Callback function to be called on each file upload as they occur\n\t */\n\tonUploadError?: FileValidationOptions[\"onError\"];\n\n\t/**\n\t * Callback function to be called once after all file upload errors have occurred\n\t */\n\tonUploadErrors?: FileValidationOptions[\"onErrors\"];\n\n\t/**\n\t * Callback function to be called on file upload success\n\t */\n\tonUploadSuccess?: FileValidationOptions[\"onSuccess\"];\n\n\t/**\n\t * Custom validator function to handle file validation\n\t */\n\tvalidator?: FileValidationOptions[\"validator\"];\n};\n\nexport const useDropZone = (props?: UseDropZoneProps): UseDropZoneResult => {\n\tconst {\n\t\tallowedFileTypes,\n\t\tchildren,\n\t\tclassNames,\n\t\tdisallowDuplicates = true,\n\t\textraInputProps,\n\t\textraRootProps,\n\t\tinitialFiles,\n\t\tmaxFileCount,\n\t\tmaxFileSize,\n\t\tmultiple,\n\t\tonFilesChange,\n\t\tonUpload,\n\t\tonUploadError,\n\t\tonUploadErrors,\n\t\tonUploadSuccess,\n\t\trender,\n\t\tvalidator,\n\t} = props ?? {};\n\n\tconst inputRef = useRef<HTMLInputElement>(null);\n\n\tconst initialFileArray = toArray(initialFiles).filter(Boolean);\n\n\tconst [dropZoneState, setDropZoneState] = useState<FileUploadState>({\n\t\terrors: [],\n\t\tfilesWithPreview: initialFileArray.map((fileMeta) => ({\n\t\t\tfile: fileMeta,\n\t\t\tid: fileMeta.id,\n\t\t\tpreview: fileMeta.url,\n\t\t})),\n\t\tisDragging: false,\n\t});\n\n\tconst toggleIsDragging = (value: boolean) => {\n\t\tsetDropZoneState((prevState) => ({ ...prevState, isDragging: value }));\n\t};\n\n\tconst addFiles: UseDropZoneResult[\"addFiles\"] = useCallbackRef((fileList, event) => {\n\t\tif (!fileList || fileList.length === 0) {\n\t\t\tconsole.warn(\"No file selected!\");\n\t\t\treturn;\n\t\t}\n\n\t\t// Clear existing errors when new files are uploaded\n\t\tclearErrors();\n\n\t\t// In single file mode, clear existing files first\n\t\tif (!multiple) {\n\t\t\tclearFiles();\n\t\t}\n\n\t\tconst { errors, validFiles } = handleFileValidation({\n\t\t\texistingFiles: dropZoneState.filesWithPreview.map((fileWithPreview) => fileWithPreview.file),\n\t\t\tnewFiles: fileList,\n\t\t\tonError: onUploadError,\n\t\t\tonErrors: onUploadErrors,\n\t\t\tonSuccess: onUploadSuccess,\n\t\t\tvalidationSettings: { allowedFileTypes, disallowDuplicates, maxFileCount, maxFileSize },\n\t\t\tvalidator,\n\t\t});\n\n\t\tif (validFiles.length === 0) {\n\t\t\tsetDropZoneState((prevState) => ({ ...prevState, errors }));\n\t\t\treturn;\n\t\t}\n\n\t\tconst filesWithPreview: FileWithPreview[] = validFiles.map((file) => ({\n\t\t\tfile,\n\t\t\tid: generateUniqueId(file),\n\t\t\tpreview: createImagePreview({ file }),\n\t\t}));\n\n\t\t// == Only call onUpload callback if event is provided, which indicates that new files were uploaded from an event handler\n\n\t\tif (event) {\n\t\t\tonUpload?.({ event, filesWithPreview });\n\t\t}\n\n\t\tconst newFileUploadState = {\n\t\t\t...dropZoneState,\n\t\t\terrors,\n\t\t\t...(event?.type === \"drop\" && { isDragging: false }),\n\t\t\tfilesWithPreview: multiple\n\t\t\t\t? [...dropZoneState.filesWithPreview, ...filesWithPreview]\n\t\t\t\t: filesWithPreview,\n\t\t} satisfies FileUploadState;\n\n\t\tonFilesChange?.({ filesWithPreview: newFileUploadState.filesWithPreview });\n\n\t\tsetDropZoneState(newFileUploadState);\n\n\t\t// == Reset input value after adding files\n\t\tinputRef.current && (inputRef.current.value = \"\");\n\t});\n\n\tconst clearFiles: UseDropZoneResult[\"clearFiles\"] = useCallbackRef(() => {\n\t\t// == Clean up object URLs if any\n\t\tdropZoneState.filesWithPreview.forEach((fileObject) => clearObjectURL(fileObject));\n\n\t\tconst newFileUploadState = {\n\t\t\t...dropZoneState,\n\t\t\terrors: [],\n\t\t\tfilesWithPreview: [],\n\t\t} satisfies FileUploadState;\n\n\t\tonFilesChange?.({ filesWithPreview: newFileUploadState.filesWithPreview });\n\n\t\tsetDropZoneState(newFileUploadState);\n\n\t\t// == Reset input value after clearing files\n\t\tinputRef.current && (inputRef.current.value = \"\");\n\t});\n\n\tconst removeFile: UseDropZoneResult[\"removeFile\"] = useCallbackRef((id) => {\n\t\tconst fileToRemove = dropZoneState.filesWithPreview.find((fileObject) => fileObject.id === id);\n\n\t\tclearObjectURL(fileToRemove);\n\n\t\tconst newFilesWithPreview = dropZoneState.filesWithPreview.filter((file) => file.id !== id);\n\n\t\tonFilesChange?.({ filesWithPreview: newFilesWithPreview });\n\n\t\tsetDropZoneState({\n\t\t\t...dropZoneState,\n\t\t\terrors: [],\n\t\t\tfilesWithPreview: newFilesWithPreview,\n\t\t});\n\t});\n\n\tconst clearErrors: UseDropZoneResult[\"clearErrors\"] = useCallbackRef(() => {\n\t\tsetDropZoneState((prevState) => ({ ...prevState, errors: [] }));\n\t});\n\n\tconst handleFileUpload: UseDropZoneResult[\"handleFileUpload\"] = useCallbackRef((event) => {\n\t\tif (event.defaultPrevented) return;\n\n\t\tif (inputRef.current?.disabled) return;\n\n\t\tif (event.type === \"drop\") {\n\t\t\tevent.preventDefault();\n\t\t\tevent.stopPropagation();\n\t\t}\n\n\t\tconst fileList =\n\t\t\tevent.type === \"drop\"\n\t\t\t\t? (event as React.DragEvent).dataTransfer.files\n\t\t\t\t: (event as React.ChangeEvent<HTMLInputElement>).target.files;\n\n\t\t// == In single file mode, only use the first file\n\t\tif (!multiple) {\n\t\t\tconst firstFile = fileList?.[0];\n\n\t\t\tfirstFile && addFiles([firstFile], event);\n\n\t\t\treturn;\n\t\t}\n\n\t\taddFiles(fileList, event);\n\t});\n\n\tconst handleDragEnter: UseDropZoneResult[\"handleDragEnter\"] = useCallbackRef((event) => {\n\t\tevent.preventDefault();\n\t\tevent.stopPropagation();\n\t\ttoggleIsDragging(true);\n\t});\n\n\tconst handleDragOver: UseDropZoneResult[\"handleDragOver\"] = useCallbackRef((event) => {\n\t\tevent.preventDefault();\n\t\tevent.stopPropagation();\n\t\ttoggleIsDragging(true);\n\t});\n\n\tconst handleDragLeave: UseDropZoneResult[\"handleDragLeave\"] = useCallbackRef((event) => {\n\t\tevent.preventDefault();\n\t\tevent.stopPropagation();\n\t\ttoggleIsDragging(false);\n\t});\n\n\tconst openFilePicker: UseDropZoneResult[\"openFilePicker\"] = useCallbackRef(() => {\n\t\tinputRef.current?.click();\n\t});\n\n\tconst getRootProps: UseDropZoneResult[\"getRootProps\"] = useCallback(\n\t\t(rootProps) => {\n\t\t\tconst mergedRootProps = mergeTwoProps(extraRootProps, rootProps);\n\n\t\t\treturn {\n\t\t\t\t...mergedRootProps,\n\t\t\t\tclassName: cnMerge(\n\t\t\t\t\t\"relative isolate flex flex-col\",\n\t\t\t\t\tmergedRootProps.className,\n\t\t\t\t\tclassNames?.base,\n\t\t\t\t\tdropZoneState.isDragging && [\n\t\t\t\t\t\t\"opacity-60\",\n\t\t\t\t\t\tclassNames?.isDragging,\n\t\t\t\t\t\trootProps?.classNames?.isDragging,\n\t\t\t\t\t]\n\t\t\t\t),\n\t\t\t\t\"data-dragging\": dataAttr(dropZoneState.isDragging),\n\t\t\t\t\"data-scope\": \"dropzone\",\n\t\t\t\t// eslint-disable-next-line perfectionist/sort-objects -- I need data-scope to be first\n\t\t\t\t\"data-part\": \"root\",\n\t\t\t\t\"data-slot\": \"dropzone-root\",\n\t\t\t\tonDragEnter: handleDragEnter,\n\t\t\t\tonDragLeave: handleDragLeave,\n\t\t\t\tonDragOver: handleDragOver,\n\t\t\t\tonDrop: handleFileUpload,\n\t\t\t};\n\t\t},\n\t\t[\n\t\t\tclassNames?.base,\n\t\t\tclassNames?.isDragging,\n\t\t\textraRootProps,\n\t\t\tdropZoneState.isDragging,\n\t\t\thandleDragEnter,\n\t\t\thandleDragLeave,\n\t\t\thandleDragOver,\n\t\t\thandleFileUpload,\n\t\t]\n\t);\n\n\tconst getInputProps: UseDropZoneResult[\"getInputProps\"] = useCallback(\n\t\t(inputProps) => {\n\t\t\tconst mergedInputProps = mergeTwoProps(extraInputProps, inputProps);\n\n\t\t\treturn {\n\t\t\t\t...mergedInputProps,\n\t\t\t\taccept: allowedFileTypes ? allowedFileTypes.join(\", \") : mergedInputProps.accept,\n\t\t\t\tclassName: cnMerge(\n\t\t\t\t\t\"absolute inset-0 z-[100] cursor-pointer opacity-0\",\n\t\t\t\t\tclassNames?.input,\n\t\t\t\t\tmergedInputProps.className\n\t\t\t\t),\n\t\t\t\t\"data-dragging\": dataAttr(dropZoneState.isDragging),\n\t\t\t\t\"data-scope\": \"dropzone\",\n\t\t\t\t// eslint-disable-next-line perfectionist/sort-objects -- I need data-scope to be first\n\t\t\t\t\"data-part\": \"input\",\n\t\t\t\t\"data-slot\": \"dropzone-input\",\n\t\t\t\tmultiple: multiple ?? mergedInputProps.multiple,\n\t\t\t\tonChange: (event: React.ChangeEvent<HTMLInputElement>) => {\n\t\t\t\t\tmergedInputProps.onChange?.(event);\n\t\t\t\t\thandleFileUpload(event);\n\t\t\t\t},\n\t\t\t\tref: composeRefs([inputRef, mergedInputProps.ref]),\n\t\t\t\ttype: \"file\",\n\t\t\t};\n\t\t},\n\t\t[\n\t\t\tallowedFileTypes,\n\t\t\tclassNames?.input,\n\t\t\textraInputProps,\n\t\t\tdropZoneState.isDragging,\n\t\t\thandleFileUpload,\n\t\t\tmultiple,\n\t\t]\n\t);\n\n\tconst renderProps = {\n\t\taddFiles,\n\t\tclearErrors,\n\t\tclearFiles,\n\t\tdropZoneState,\n\t\tgetInputProps,\n\t\tgetRootProps,\n\t\thandleDragEnter,\n\t\thandleDragLeave,\n\t\thandleDragOver,\n\t\thandleFileUpload,\n\t\tinputRef,\n\t\topenFilePicker,\n\t\tremoveFile,\n\t} satisfies RenderProps;\n\n\tconst selectedChildren = children ?? render;\n\n\tconst getResolvedChildren = () => {\n\t\treturn isFunction(selectedChildren) ? selectedChildren(renderProps) : selectedChildren;\n\t};\n\n\treturn {\n\t\t...renderProps,\n\t\tgetResolvedChildren,\n\t};\n};\n","\"use client\";\n\nimport * as React from \"react\";\n\nimport {\n\ttype GetSlotComponentProps,\n\tgetSlotMap,\n\twithSlotNameAndSymbol,\n} from \"@zayne-labs/toolkit-react/utils\";\nimport { isArray } from \"@zayne-labs/toolkit-type-helpers\";\nimport { Fragment as ReactFragment, isValidElement } from \"react\";\nimport { type UseDropZoneProps, useDropZone } from \"./use-drop-zone\";\n\ntype DropZoneProps = UseDropZoneProps & {\n\t/**\n\t * Controls whether to include internal elements (root and input) or not.\n\t */\n\twithInternalElements?: boolean;\n};\n\nexport function DropZoneRoot(props: DropZoneProps) {\n\tconst { withInternalElements = true, ...restOfProps } = props;\n\n\tconst api = useDropZone(restOfProps);\n\n\tconst RootComponent = withInternalElements ? \"div\" : ReactFragment;\n\tconst InputComponent = withInternalElements ? \"input\" : ReactFragment;\n\n\tconst rootComponentProps = RootComponent === \"div\" && api.getRootProps();\n\tconst inputComponentProps = InputComponent === \"input\" && api.getInputProps();\n\n\tconst resolvedChildren = api.getResolvedChildren();\n\n\t/**\n\t * Whether the children could contain slots.\n\t */\n\tconst couldChildrenContainSlots =\n\t\tisArray(resolvedChildren)\n\t\t|| (isValidElement(resolvedChildren) && resolvedChildren.type === ReactFragment);\n\n\tconst slots = getSlotMap<SlotComponentProps>(resolvedChildren, {\n\t\tcondition: withInternalElements && couldChildrenContainSlots,\n\t});\n\n\treturn (\n\t\t<>\n\t\t\t<RootComponent {...rootComponentProps}>\n\t\t\t\t<InputComponent {...inputComponentProps} />\n\n\t\t\t\t{slots.default}\n\t\t\t</RootComponent>\n\n\t\t\t{slots.preview}\n\t\t</>\n\t);\n}\n\ntype SlotComponentProps = GetSlotComponentProps<\"preview\">;\n\nexport const DropZoneImagePreview = withSlotNameAndSymbol<SlotComponentProps>(\"preview\");\n","export { DropZoneRoot as Root, DropZoneImagePreview as ImagePreview } from \"./drop-zone\";\n"]}
@@ -127,8 +127,9 @@ declare function FormDescription(props: InferProps<"p">): react.JSX.Element;
127
127
  type ErrorMessageRenderProps = {
128
128
  className: string;
129
129
  "data-index": number;
130
- "data-part": string;
131
- "data-scope": string;
130
+ "data-part": "error-message";
131
+ "data-scope": "form";
132
+ "data-slot": "form-error-message";
132
133
  id: string | undefined;
133
134
  onAnimationEnd?: react.ReactEventHandler<HTMLElement>;
134
135
  };
@@ -149,7 +150,8 @@ type FormErrorMessagePrimitiveProps<TFieldValues extends FieldValues> = Discrimi
149
150
  errorMessageAnimation?: string;
150
151
  };
151
152
  control?: Control<TFieldValues>;
152
- withAnimationOnInvalid?: boolean;
153
+ disableErrorAnimation?: boolean;
154
+ disableScrollToErrorField?: boolean;
153
155
  } & ({
154
156
  errorField: FieldPath<TFieldValues>;
155
157
  type?: "regular";