@zayne-labs/ui-react 0.9.16 → 0.9.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. package/dist/esm/await-Fe4LFRKd.js +89 -0
  2. package/dist/esm/await-Fe4LFRKd.js.map +1 -0
  3. package/dist/esm/card-DRjs-vtv.js +54 -0
  4. package/dist/esm/card-DRjs-vtv.js.map +1 -0
  5. package/dist/esm/carousel-Dznupnx8.js +270 -0
  6. package/dist/esm/carousel-Dznupnx8.js.map +1 -0
  7. package/dist/esm/chunk-Cl8Af3a2.js +11 -0
  8. package/dist/esm/cn-_FbtIrlZ.js +8 -0
  9. package/dist/esm/cn-_FbtIrlZ.js.map +1 -0
  10. package/dist/esm/common-BYWy8Q78.js +0 -0
  11. package/dist/esm/components/common/await/index.d.ts +6 -14
  12. package/dist/esm/components/common/await/index.js +10 -8
  13. package/dist/esm/components/common/error-boundary/index.d.ts +2 -17
  14. package/dist/esm/components/common/error-boundary/index.js +6 -4
  15. package/dist/esm/components/common/for/index.d.ts +2 -10
  16. package/dist/esm/components/common/for/index.js +3 -4
  17. package/dist/esm/components/common/index.d.ts +11 -13
  18. package/dist/esm/components/common/index.js +13 -14
  19. package/dist/esm/components/common/show/index.d.ts +2 -26
  20. package/dist/esm/components/common/show/index.js +7 -5
  21. package/dist/esm/components/common/slot/index.d.ts +2 -12
  22. package/dist/esm/components/common/slot/index.js +3 -4
  23. package/dist/esm/components/common/suspense-with-boundary/index.d.ts +3 -11
  24. package/dist/esm/components/common/suspense-with-boundary/index.js +4 -5
  25. package/dist/esm/components/common/switch/index.d.ts +2 -25
  26. package/dist/esm/components/common/switch/index.js +7 -5
  27. package/dist/esm/components/common/teleport/index.d.ts +2 -12
  28. package/dist/esm/components/common/teleport/index.js +3 -4
  29. package/dist/esm/components/ui/card/index.d.ts +2 -17
  30. package/dist/esm/components/ui/card/index.js +5 -6
  31. package/dist/esm/components/ui/carousel/index.d.ts +2 -95
  32. package/dist/esm/components/ui/carousel/index.js +18 -16
  33. package/dist/esm/components/ui/drag-scroll/index.d.ts +2 -21
  34. package/dist/esm/components/ui/drag-scroll/index.js +4 -5
  35. package/dist/esm/components/ui/drop-zone/index.d.ts +3 -11
  36. package/dist/esm/components/ui/drop-zone/index.js +18 -16
  37. package/dist/esm/components/ui/form/index.d.ts +2 -228
  38. package/dist/esm/components/ui/form/index.js +10 -8
  39. package/dist/esm/components/ui/index.d.ts +7 -11
  40. package/dist/esm/components/ui/index.js +19 -20
  41. package/dist/esm/drag-scroll-BLjij7aI.js +111 -0
  42. package/dist/esm/drag-scroll-BLjij7aI.js.map +1 -0
  43. package/dist/esm/drop-zone-_YK9C3Xj.js +313 -0
  44. package/dist/esm/drop-zone-_YK9C3Xj.js.map +1 -0
  45. package/dist/esm/error-boundary-B3ycUZ1t.js +126 -0
  46. package/dist/esm/error-boundary-B3ycUZ1t.js.map +1 -0
  47. package/dist/esm/for-aTw1TgJo.js +42 -0
  48. package/dist/esm/for-aTw1TgJo.js.map +1 -0
  49. package/dist/esm/form-DytCfL6R.js +590 -0
  50. package/dist/esm/form-DytCfL6R.js.map +1 -0
  51. package/dist/esm/getSlot-BIcGgBwN.js +85 -0
  52. package/dist/esm/getSlot-BIcGgBwN.js.map +1 -0
  53. package/dist/esm/{getSlotMap-DTkxtGhd.d.ts → getSlotMap-DA_uhXqU.d.ts} +17 -16
  54. package/dist/esm/index--1COprHP.d.ts +32 -0
  55. package/dist/esm/index-1UCPJf7B.d.ts +19 -0
  56. package/dist/esm/index-ASoFjmZ6.d.ts +196 -0
  57. package/dist/esm/index-BC0r1cBF.d.ts +104 -0
  58. package/dist/esm/index-BSJ30pGj.d.ts +68 -0
  59. package/dist/esm/{for-BH5FhFuV.d.ts → index-BhpBx8dH.d.ts} +14 -6
  60. package/dist/esm/index-BuSQsDCX.d.ts +22 -0
  61. package/dist/esm/index-C0XZoIZD.d.ts +35 -0
  62. package/dist/esm/index-CTZr4PTO.d.ts +24 -0
  63. package/dist/esm/index-CqlM1M0j.d.ts +14 -0
  64. package/dist/esm/index-D-j2Cn1J.d.ts +61 -0
  65. package/dist/esm/index-DQ1yFGd2.d.ts +229 -0
  66. package/dist/esm/index-DnaUCh8d.d.ts +15 -0
  67. package/dist/esm/index-TBkPiipk.d.ts +49 -0
  68. package/dist/esm/lib/utils/index.d.ts +3 -61
  69. package/dist/esm/lib/utils/index.js +4 -5
  70. package/dist/esm/show-BgYrfIxJ.js +41 -0
  71. package/dist/esm/show-BgYrfIxJ.js.map +1 -0
  72. package/dist/esm/slot-RXTKo4L7.js +58 -0
  73. package/dist/esm/slot-RXTKo4L7.js.map +1 -0
  74. package/dist/esm/suspense-with-boundary-TrQxYRG6.js +21 -0
  75. package/dist/esm/suspense-with-boundary-TrQxYRG6.js.map +1 -0
  76. package/dist/esm/switch-PF5xTJfr.js +42 -0
  77. package/dist/esm/switch-PF5xTJfr.js.map +1 -0
  78. package/dist/esm/teleport-dlSjWj_t.js +32 -0
  79. package/dist/esm/teleport-dlSjWj_t.js.map +1 -0
  80. package/dist/esm/utils-D4YNWj2X.js +89 -0
  81. package/dist/esm/utils-D4YNWj2X.js.map +1 -0
  82. package/package.json +4 -3
  83. package/dist/esm/await-parts-BJ6C-y1f.d.ts +0 -36
  84. package/dist/esm/chunk-6QJYHPBL.js +0 -45
  85. package/dist/esm/chunk-6QJYHPBL.js.map +0 -1
  86. package/dist/esm/chunk-BJ7WR4XW.js +0 -274
  87. package/dist/esm/chunk-BJ7WR4XW.js.map +0 -1
  88. package/dist/esm/chunk-CWUEUCR5.js +0 -3
  89. package/dist/esm/chunk-CWUEUCR5.js.map +0 -1
  90. package/dist/esm/chunk-D6QZA3UT.js +0 -56
  91. package/dist/esm/chunk-D6QZA3UT.js.map +0 -1
  92. package/dist/esm/chunk-EYWTW54R.js +0 -42
  93. package/dist/esm/chunk-EYWTW54R.js.map +0 -1
  94. package/dist/esm/chunk-G5BNZM66.js +0 -32
  95. package/dist/esm/chunk-G5BNZM66.js.map +0 -1
  96. package/dist/esm/chunk-JC52CA2O.js +0 -113
  97. package/dist/esm/chunk-JC52CA2O.js.map +0 -1
  98. package/dist/esm/chunk-M7YXNGT6.js +0 -48
  99. package/dist/esm/chunk-M7YXNGT6.js.map +0 -1
  100. package/dist/esm/chunk-MRYXZN2P.js +0 -336
  101. package/dist/esm/chunk-MRYXZN2P.js.map +0 -1
  102. package/dist/esm/chunk-MT2MQDK2.js +0 -13
  103. package/dist/esm/chunk-MT2MQDK2.js.map +0 -1
  104. package/dist/esm/chunk-N4274N5K.js +0 -50
  105. package/dist/esm/chunk-N4274N5K.js.map +0 -1
  106. package/dist/esm/chunk-NXZZXJRH.js +0 -41
  107. package/dist/esm/chunk-NXZZXJRH.js.map +0 -1
  108. package/dist/esm/chunk-OHG7GB7O.js +0 -8
  109. package/dist/esm/chunk-OHG7GB7O.js.map +0 -1
  110. package/dist/esm/chunk-P5QP73HG.js +0 -626
  111. package/dist/esm/chunk-P5QP73HG.js.map +0 -1
  112. package/dist/esm/chunk-PZ5AY32C.js +0 -9
  113. package/dist/esm/chunk-PZ5AY32C.js.map +0 -1
  114. package/dist/esm/chunk-V5ZPMMIH.js +0 -121
  115. package/dist/esm/chunk-V5ZPMMIH.js.map +0 -1
  116. package/dist/esm/chunk-YO5LJ7ZJ.js +0 -74
  117. package/dist/esm/chunk-YO5LJ7ZJ.js.map +0 -1
  118. package/dist/esm/chunk-YSDKXBU7.js +0 -68
  119. package/dist/esm/chunk-YSDKXBU7.js.map +0 -1
  120. package/dist/esm/components/common/await/index.js.map +0 -1
  121. package/dist/esm/components/common/error-boundary/index.js.map +0 -1
  122. package/dist/esm/components/common/for/index.js.map +0 -1
  123. package/dist/esm/components/common/index.js.map +0 -1
  124. package/dist/esm/components/common/show/index.js.map +0 -1
  125. package/dist/esm/components/common/slot/index.js.map +0 -1
  126. package/dist/esm/components/common/suspense-with-boundary/index.js.map +0 -1
  127. package/dist/esm/components/common/switch/index.js.map +0 -1
  128. package/dist/esm/components/common/teleport/index.js.map +0 -1
  129. package/dist/esm/components/ui/card/index.js.map +0 -1
  130. package/dist/esm/components/ui/carousel/index.js.map +0 -1
  131. package/dist/esm/components/ui/drag-scroll/index.js.map +0 -1
  132. package/dist/esm/components/ui/drop-zone/index.js.map +0 -1
  133. package/dist/esm/components/ui/form/index.js.map +0 -1
  134. package/dist/esm/components/ui/index.js.map +0 -1
  135. package/dist/esm/drop-zone-parts-CvseSoXA.d.ts +0 -186
  136. package/dist/esm/error-boundary-BD0X61Sg.d.ts +0 -28
  137. package/dist/esm/lib/utils/index.js.map +0 -1
  138. package/dist/esm/types-mdfDDNrr.d.ts +0 -25
@@ -0,0 +1,313 @@
1
+ import { __export } from "./chunk-Cl8Af3a2.js";
2
+ import { getSlotMap, withSlotNameAndSymbol } from "./utils-D4YNWj2X.js";
3
+ import { SlotRoot } from "./slot-RXTKo4L7.js";
4
+ import { ShowRoot } from "./show-BgYrfIxJ.js";
5
+ import { cnMerge } from "./cn-_FbtIrlZ.js";
6
+ import { Fragment, isValidElement, useCallback, useMemo, useRef, useState } from "react";
7
+ import { createImagePreview, dataAttr, handleFileValidation, toArray } from "@zayne-labs/toolkit-core";
8
+ import { isArray, isFile, isFunction, isString } from "@zayne-labs/toolkit-type-helpers";
9
+ import { createCustomContext, useCallbackRef } from "@zayne-labs/toolkit-react";
10
+ import { jsx, jsxs } from "react/jsx-runtime";
11
+ import { composeRefs, composeTwoEventHandlers, mergeTwoProps } from "@zayne-labs/toolkit-react/utils";
12
+
13
+ //#region src/components/ui/drop-zone/drop-context.ts
14
+ const [DropZoneContextProvider, useDropZoneContext] = createCustomContext({
15
+ hookName: "useDropZoneContext",
16
+ name: "DropZoneContext",
17
+ providerName: "DropZoneRoot"
18
+ });
19
+
20
+ //#endregion
21
+ //#region src/components/ui/drop-zone/utils.ts
22
+ const generateUniqueId = (file) => {
23
+ if (!isFile(file)) return file.id;
24
+ return `${file.name}-(${Math.round(performance.now())})-${crypto.randomUUID().slice(0, 8)}`;
25
+ };
26
+ const createObjectURL = (file, disallowPreviewForNonImageFiles) => {
27
+ if (disallowPreviewForNonImageFiles && !file.type.startsWith("image/")) return;
28
+ return createImagePreview(file);
29
+ };
30
+ const clearObjectURL = (fileWithPreview, disallowPreviewForNonImageFiles) => {
31
+ if (!isFile(fileWithPreview?.file)) return;
32
+ if (disallowPreviewForNonImageFiles && !fileWithPreview.file.type.startsWith("image/")) return;
33
+ if (!fileWithPreview.preview) return;
34
+ URL.revokeObjectURL(fileWithPreview.preview);
35
+ };
36
+
37
+ //#endregion
38
+ //#region src/components/ui/drop-zone/use-drop-zone.ts
39
+ const useDropZone = (props) => {
40
+ const { allowedFileTypes, classNames, disallowDuplicates = true, disallowPreviewForNonImageFiles = true, extraContainerProps, extraInputProps, initialFiles, maxFileCount, maxFileSize, multiple, onFilesChange, onRenderPropsChange, onUpload, onUploadError, onUploadErrors, onUploadSuccess, validator, validatorForAllFiles, withDefaultFilePicker = true } = props ?? {};
41
+ const inputRef = useRef(null);
42
+ const initialFileArray = toArray(initialFiles).filter(Boolean);
43
+ const [dropZoneState, setDropZoneState] = useState({
44
+ errors: [],
45
+ filesWithPreview: initialFileArray.map((fileMeta) => ({
46
+ file: fileMeta,
47
+ id: fileMeta.id,
48
+ preview: fileMeta.url
49
+ })),
50
+ isDragging: false
51
+ });
52
+ const toggleIsDragging = (value) => {
53
+ setDropZoneState((prevState) => ({
54
+ ...prevState,
55
+ isDragging: value
56
+ }));
57
+ };
58
+ const addFiles = useCallbackRef((fileList, event) => {
59
+ if (!fileList || fileList.length === 0) {
60
+ console.warn("No file selected!");
61
+ return;
62
+ }
63
+ clearErrors();
64
+ if (!multiple) clearFiles();
65
+ const { errors, validFiles } = handleFileValidation({
66
+ existingFiles: dropZoneState.filesWithPreview.map((fileWithPreview) => fileWithPreview.file),
67
+ newFiles: fileList,
68
+ onError: onUploadError,
69
+ onErrors: onUploadErrors,
70
+ onSuccess: onUploadSuccess,
71
+ validationSettings: {
72
+ allowedFileTypes,
73
+ disallowDuplicates,
74
+ maxFileCount,
75
+ maxFileSize,
76
+ validator
77
+ },
78
+ validatorForAllFiles
79
+ });
80
+ if (validFiles.length === 0) {
81
+ setDropZoneState((prevState) => ({
82
+ ...prevState,
83
+ errors
84
+ }));
85
+ return;
86
+ }
87
+ const filesWithPreview = validFiles.map((file) => ({
88
+ file,
89
+ id: generateUniqueId(file),
90
+ preview: createObjectURL(file, disallowPreviewForNonImageFiles)
91
+ }));
92
+ if (event) onUpload?.({
93
+ event,
94
+ filesWithPreview
95
+ });
96
+ const newFileUploadState = {
97
+ ...dropZoneState,
98
+ errors,
99
+ ...event?.type === "drop" && { isDragging: false },
100
+ filesWithPreview: multiple ? [...dropZoneState.filesWithPreview, ...filesWithPreview] : filesWithPreview
101
+ };
102
+ onFilesChange?.({ filesWithPreview: newFileUploadState.filesWithPreview });
103
+ setDropZoneState(newFileUploadState);
104
+ inputRef.current && (inputRef.current.value = "");
105
+ });
106
+ const clearFiles = useCallbackRef(() => {
107
+ dropZoneState.filesWithPreview.forEach((fileWithPreview) => clearObjectURL(fileWithPreview, disallowPreviewForNonImageFiles));
108
+ const newFileUploadState = {
109
+ ...dropZoneState,
110
+ errors: [],
111
+ filesWithPreview: []
112
+ };
113
+ onFilesChange?.({ filesWithPreview: newFileUploadState.filesWithPreview });
114
+ setDropZoneState(newFileUploadState);
115
+ inputRef.current && (inputRef.current.value = "");
116
+ });
117
+ const removeFile = useCallbackRef((fileToRemoveOrId) => {
118
+ const actualFileToRemove = isString(fileToRemoveOrId) ? dropZoneState.filesWithPreview.find((file) => file.id === fileToRemoveOrId) : fileToRemoveOrId;
119
+ if (!actualFileToRemove) return;
120
+ clearObjectURL(actualFileToRemove, disallowPreviewForNonImageFiles);
121
+ const newFilesWithPreview = dropZoneState.filesWithPreview.filter((file) => file.id !== actualFileToRemove.id);
122
+ onFilesChange?.({ filesWithPreview: newFilesWithPreview });
123
+ setDropZoneState({
124
+ ...dropZoneState,
125
+ errors: [],
126
+ filesWithPreview: newFilesWithPreview
127
+ });
128
+ });
129
+ const clearErrors = useCallbackRef(() => {
130
+ setDropZoneState((prevState) => ({
131
+ ...prevState,
132
+ errors: []
133
+ }));
134
+ });
135
+ const handleFileUpload = useCallbackRef((event) => {
136
+ if (inputRef.current?.disabled) return;
137
+ if (event.type === "drop") {
138
+ event.preventDefault();
139
+ event.stopPropagation();
140
+ }
141
+ const fileList = event.type === "drop" ? event.dataTransfer.files : event.target.files;
142
+ if (!multiple) {
143
+ const firstFile = fileList?.[0];
144
+ firstFile && addFiles([firstFile], event);
145
+ return;
146
+ }
147
+ addFiles(fileList, event);
148
+ });
149
+ const handleDragEnter = useCallbackRef((event) => {
150
+ event.preventDefault();
151
+ event.stopPropagation();
152
+ toggleIsDragging(true);
153
+ });
154
+ const handleDragOver = useCallbackRef((event) => {
155
+ event.preventDefault();
156
+ event.stopPropagation();
157
+ toggleIsDragging(true);
158
+ });
159
+ const handleDragLeave = useCallbackRef((event) => {
160
+ event.preventDefault();
161
+ event.stopPropagation();
162
+ toggleIsDragging(false);
163
+ });
164
+ const openFilePicker = useCallbackRef(() => {
165
+ inputRef.current?.click();
166
+ });
167
+ const getContainerProps = useCallback((containerProps) => {
168
+ const mergedContainerProps = mergeTwoProps(extraContainerProps, containerProps);
169
+ return {
170
+ ...mergedContainerProps,
171
+ className: cnMerge("relative isolate flex flex-col", mergedContainerProps.className, classNames?.base, dropZoneState.isDragging && [
172
+ "opacity-60",
173
+ classNames?.isDragging,
174
+ containerProps?.classNames?.isDragging
175
+ ]),
176
+ "data-dragging": dataAttr(dropZoneState.isDragging),
177
+ "data-scope": "dropzone",
178
+ "data-part": "container",
179
+ "data-slot": "dropzone-container",
180
+ onDragEnter: composeTwoEventHandlers(handleDragEnter, mergedContainerProps.onDragEnter),
181
+ onDragLeave: composeTwoEventHandlers(handleDragLeave, mergedContainerProps.onDragLeave),
182
+ onDragOver: composeTwoEventHandlers(handleDragOver, mergedContainerProps.onDragOver),
183
+ onDrop: composeTwoEventHandlers(handleFileUpload, mergedContainerProps.onDrop)
184
+ };
185
+ }, [
186
+ classNames?.base,
187
+ classNames?.isDragging,
188
+ extraContainerProps,
189
+ dropZoneState.isDragging,
190
+ handleDragEnter,
191
+ handleDragLeave,
192
+ handleDragOver,
193
+ handleFileUpload
194
+ ]);
195
+ const getInputProps = useCallback((inputProps) => {
196
+ const mergedInputProps = mergeTwoProps(extraInputProps, inputProps);
197
+ return {
198
+ ...mergedInputProps,
199
+ accept: allowedFileTypes ? allowedFileTypes.join(", ") : mergedInputProps.accept,
200
+ className: cnMerge(withDefaultFilePicker ? "absolute inset-0 z-[100] cursor-pointer opacity-0" : "hidden", classNames?.input, mergedInputProps.className),
201
+ "data-dragging": dataAttr(dropZoneState.isDragging),
202
+ "data-scope": "dropzone",
203
+ "data-part": "input",
204
+ "data-slot": "dropzone-input",
205
+ multiple: multiple ?? mergedInputProps.multiple,
206
+ onChange: composeTwoEventHandlers(handleFileUpload, mergedInputProps.onChange),
207
+ ref: composeRefs(inputRef, mergedInputProps.ref),
208
+ type: "file"
209
+ };
210
+ }, [
211
+ allowedFileTypes,
212
+ classNames?.input,
213
+ extraInputProps,
214
+ dropZoneState.isDragging,
215
+ handleFileUpload,
216
+ multiple,
217
+ withDefaultFilePicker
218
+ ]);
219
+ const savedOnRenderPropsChange = useCallbackRef(onRenderPropsChange);
220
+ const dropZoneResult = useMemo(() => {
221
+ const propsForRenderFn = {
222
+ dropZoneActions: {
223
+ addFiles,
224
+ clearErrors,
225
+ clearFiles,
226
+ handleDragEnter,
227
+ handleDragLeave,
228
+ handleDragOver,
229
+ handleFileUpload,
230
+ openFilePicker,
231
+ removeFile
232
+ },
233
+ dropZoneState,
234
+ getContainerProps,
235
+ getInputProps,
236
+ inputRef
237
+ };
238
+ savedOnRenderPropsChange(propsForRenderFn);
239
+ return propsForRenderFn;
240
+ }, [
241
+ savedOnRenderPropsChange,
242
+ addFiles,
243
+ clearErrors,
244
+ clearFiles,
245
+ dropZoneState,
246
+ getInputProps,
247
+ getContainerProps,
248
+ handleDragEnter,
249
+ handleDragLeave,
250
+ handleDragOver,
251
+ handleFileUpload,
252
+ openFilePicker,
253
+ removeFile
254
+ ]);
255
+ return dropZoneResult;
256
+ };
257
+
258
+ //#endregion
259
+ //#region src/components/ui/drop-zone/drop-zone.tsx
260
+ function DropZoneRoot(props) {
261
+ const { children, render, withInternalElements = true,...restOfProps } = props;
262
+ const dropZone = useDropZone(restOfProps);
263
+ const ContainerComponent = withInternalElements ? DropZoneContainer : Fragment;
264
+ const InputComponent = withInternalElements ? DropZoneInput : Fragment;
265
+ const selectedChildren = children ?? render;
266
+ const resolvedChildren = isFunction(selectedChildren) ? selectedChildren(dropZone) : selectedChildren;
267
+ const couldChildrenContainSlots = isArray(resolvedChildren) || isValidElement(resolvedChildren) && resolvedChildren.type === Fragment;
268
+ const slots = withInternalElements && couldChildrenContainSlots ? getSlotMap(resolvedChildren) : { default: resolvedChildren };
269
+ return /* @__PURE__ */ jsxs(DropZoneContextProvider, {
270
+ value: dropZone,
271
+ children: [/* @__PURE__ */ jsxs(ContainerComponent, { children: [/* @__PURE__ */ jsx(InputComponent, {}), slots.default] }), slots.preview]
272
+ });
273
+ }
274
+ function DropZoneInput(props) {
275
+ const { asChild,...restOfProps } = props;
276
+ const dropZoneContext = useDropZoneContext();
277
+ const Component$1 = asChild ? SlotRoot : "input";
278
+ return /* @__PURE__ */ jsx(Component$1, { ...dropZoneContext.getInputProps(restOfProps) });
279
+ }
280
+ function DropZoneContainer(props) {
281
+ const { as: Element = "div", asChild,...restOfProps } = props;
282
+ const dropZoneContext = useDropZoneContext();
283
+ const Component$1 = asChild ? SlotRoot : Element;
284
+ return /* @__PURE__ */ jsx(Component$1, { ...dropZoneContext.getContainerProps(restOfProps) });
285
+ }
286
+ function DropZoneContext(props) {
287
+ const { children } = props;
288
+ const dropZoneContext = useDropZoneContext();
289
+ return children(dropZoneContext);
290
+ }
291
+ const DropZoneImagePreview = withSlotNameAndSymbol("preview", (props) => {
292
+ const { children } = props;
293
+ if (isFunction(children)) return /* @__PURE__ */ jsx(DropZoneContext, { children: (dropZoneCtx) => /* @__PURE__ */ jsx(ShowRoot, {
294
+ when: dropZoneCtx.dropZoneState.filesWithPreview.length > 0,
295
+ children: children(dropZoneCtx)
296
+ }) });
297
+ return children;
298
+ });
299
+
300
+ //#endregion
301
+ //#region src/components/ui/drop-zone/drop-zone-parts.ts
302
+ var drop_zone_parts_exports = {};
303
+ __export(drop_zone_parts_exports, {
304
+ Container: () => DropZoneContainer,
305
+ Context: () => DropZoneContext,
306
+ ImagePreview: () => DropZoneImagePreview,
307
+ Input: () => DropZoneInput,
308
+ Root: () => DropZoneRoot
309
+ });
310
+
311
+ //#endregion
312
+ export { DropZoneContainer, DropZoneContext, DropZoneImagePreview, DropZoneInput, DropZoneRoot, drop_zone_parts_exports, useDropZone, useDropZoneContext };
313
+ //# sourceMappingURL=drop-zone-_YK9C3Xj.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"drop-zone-_YK9C3Xj.js","names":["file: File | FileMeta","file: File","disallowPreviewForNonImageFiles: boolean","fileWithPreview: FileWithPreview | undefined","props?: UseDropZoneProps","value: boolean","addFiles: DropZoneActions[\"addFiles\"]","filesWithPreview: FileWithPreview[]","clearFiles: DropZoneActions[\"clearFiles\"]","removeFile: DropZoneActions[\"removeFile\"]","clearErrors: DropZoneActions[\"clearErrors\"]","handleFileUpload: DropZoneActions[\"handleFileUpload\"]","handleDragEnter: DropZoneActions[\"handleDragEnter\"]","handleDragOver: DropZoneActions[\"handleDragOver\"]","handleDragLeave: DropZoneActions[\"handleDragLeave\"]","openFilePicker: DropZoneActions[\"openFilePicker\"]","getContainerProps: UseDropZoneResult[\"getContainerProps\"]","getInputProps: UseDropZoneResult[\"getInputProps\"]","props: DropZoneRootProps","ReactFragment","props: DropZoneInputProps","Component","props: PolymorphicProps<TElement, DropZoneContainerProps>","props: { children: RenderPropFn }"],"sources":["../../src/components/ui/drop-zone/drop-context.ts","../../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"],"sourcesContent":["import { createCustomContext } from \"@zayne-labs/toolkit-react\";\nimport type { UseDropZoneResult } from \"./use-drop-zone\";\n\nconst [DropZoneContextProvider, useDropZoneContext] = createCustomContext<UseDropZoneResult>({\n\thookName: \"useDropZoneContext\",\n\tname: \"DropZoneContext\",\n\tproviderName: \"DropZoneRoot\",\n});\n\nexport { DropZoneContextProvider, useDropZoneContext };\n","import { type FileMeta, createImagePreview } 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 createObjectURL = (file: File, disallowPreviewForNonImageFiles: boolean) => {\n\tif (disallowPreviewForNonImageFiles && !file.type.startsWith(\"image/\")) return;\n\n\treturn createImagePreview(file);\n};\n\nexport const clearObjectURL = (\n\tfileWithPreview: FileWithPreview | undefined,\n\tdisallowPreviewForNonImageFiles: boolean\n) => {\n\tif (!isFile(fileWithPreview?.file)) return;\n\n\tif (disallowPreviewForNonImageFiles && !fileWithPreview.file.type.startsWith(\"image/\")) return;\n\n\tif (!fileWithPreview.preview) return;\n\n\tURL.revokeObjectURL(fileWithPreview.preview);\n};\n","import { cnMerge } from \"@/lib/utils/cn\";\nimport { dataAttr } from \"@zayne-labs/toolkit-core\";\nimport {\n\ttype FileMeta,\n\ttype FileValidationErrorContext,\n\ttype FileValidationOptions,\n\thandleFileValidation,\n\ttoArray,\n} from \"@zayne-labs/toolkit-core\";\nimport { useCallbackRef } from \"@zayne-labs/toolkit-react\";\nimport {\n\ttype InferProps,\n\tcomposeRefs,\n\tcomposeTwoEventHandlers,\n\tmergeTwoProps,\n} from \"@zayne-labs/toolkit-react/utils\";\nimport { type Prettify, isString } from \"@zayne-labs/toolkit-type-helpers\";\nimport { useCallback, useMemo, useRef, useState } from \"react\";\nimport { clearObjectURL, createObjectURL, generateUniqueId } from \"./utils\";\n\nexport type ContainerProps = 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\t/**\n\t * File object or file metadata\n\t */\n\tfile: File | FileMeta;\n\t/**\n\t * Unique ID for the file\n\t */\n\tid: string;\n\t/**\n\t * Preview URL for the file\n\t * - Will be undefined if `disallowPreviewForNonImageFiles` is set to `true` and the file is not an image\n\t * - Can also be undefined if `URL.createObjectURL` fails\n\t */\n\tpreview: string | undefined;\n};\n\nexport type DropZoneState = {\n\t/**\n\t * List of validation errors\n\t */\n\terrors: FileValidationErrorContext[];\n\t/**\n\t * List of files with their preview URLs and unique IDs\n\t */\n\tfilesWithPreview: FileWithPreview[];\n\t/**\n\t * Whether or not a file is currently being dragged over the drop zone\n\t */\n\tisDragging: boolean;\n};\n\ntype ChangeOrDragEvent = React.ChangeEvent<HTMLInputElement> | React.DragEvent<HTMLElement>;\n\nexport type DropZoneActions = {\n\taddFiles: (fileList: File[] | FileList | null, event?: ChangeOrDragEvent) => void;\n\tclearErrors: () => void;\n\tclearFiles: () => void;\n\thandleDragEnter: (event: React.DragEvent<HTMLElement>) => void;\n\thandleDragLeave: (event: React.DragEvent<HTMLElement>) => void;\n\thandleDragOver: (event: React.DragEvent<HTMLElement>) => void;\n\thandleFileUpload: (event: ChangeOrDragEvent) => void;\n\topenFilePicker: () => void;\n\tremoveFile: (fileToRemoveOrId: string | FileWithPreview) => void;\n};\n\nexport type UseDropZoneResult = {\n\tdropZoneActions: DropZoneActions;\n\tdropZoneState: DropZoneState;\n\tgetContainerProps: (containerProps?: ContainerProps) => ContainerProps;\n\tgetInputProps: (inputProps?: InputProps) => InputProps;\n\tinputRef: React.RefObject<HTMLInputElement | null>;\n};\n\nexport type UseDropZoneProps = {\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<ContainerProps[\"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 * Whether to disallow preview for non-image files\n\t * @default true\n\t */\n\tdisallowPreviewForNonImageFiles?: boolean;\n\n\t/**\n\t * Extra props to pass to the container element\n\t */\n\textraContainerProps?: ContainerProps;\n\n\t/**\n\t * Extra props to pass to the input element\n\t */\n\textraInputProps?: InputProps;\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 the render props change\n\t */\n\tonRenderPropsChange?: (props: UseDropZoneResult) => 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 validation function.\n\t *\n\t * If the function returns false, the file will be rejected\n\t */\n\tvalidator?: NonNullable<FileValidationOptions[\"validationSettings\"]>[\"validator\"];\n\n\t/**\n\t * Custom validation function that runs after all file validation has occurred\n\t */\n\tvalidatorForAllFiles?: FileValidationOptions[\"validatorForAllFiles\"];\n\n\t/**\n\t * Whether to allow the default file picker via the file input element\n\t * @default true\n\t */\n\twithDefaultFilePicker?: boolean;\n};\n\nexport const useDropZone = (props?: UseDropZoneProps): UseDropZoneResult => {\n\tconst {\n\t\tallowedFileTypes,\n\t\tclassNames,\n\t\tdisallowDuplicates = true,\n\t\tdisallowPreviewForNonImageFiles = true,\n\t\textraContainerProps,\n\t\textraInputProps,\n\t\tinitialFiles,\n\t\tmaxFileCount,\n\t\tmaxFileSize,\n\t\tmultiple,\n\t\tonFilesChange,\n\t\tonRenderPropsChange,\n\t\tonUpload,\n\t\tonUploadError,\n\t\tonUploadErrors,\n\t\tonUploadSuccess,\n\t\tvalidator,\n\t\tvalidatorForAllFiles,\n\t\twithDefaultFilePicker = true,\n\t} = props ?? {};\n\n\tconst inputRef = useRef<HTMLInputElement>(null);\n\n\tconst initialFileArray = toArray(initialFiles).filter(Boolean);\n\n\tconst [dropZoneState, setDropZoneState] = useState<DropZoneState>({\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: DropZoneActions[\"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: {\n\t\t\t\tallowedFileTypes,\n\t\t\t\tdisallowDuplicates,\n\t\t\t\tmaxFileCount,\n\t\t\t\tmaxFileSize,\n\t\t\t\tvalidator,\n\t\t\t},\n\t\t\tvalidatorForAllFiles,\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: createObjectURL(file, disallowPreviewForNonImageFiles),\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 DropZoneState;\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: DropZoneActions[\"clearFiles\"] = useCallbackRef(() => {\n\t\t// == Clean up object URLs if any\n\t\tdropZoneState.filesWithPreview.forEach((fileWithPreview) =>\n\t\t\tclearObjectURL(fileWithPreview, disallowPreviewForNonImageFiles)\n\t\t);\n\n\t\tconst newFileUploadState = {\n\t\t\t...dropZoneState,\n\t\t\terrors: [],\n\t\t\tfilesWithPreview: [],\n\t\t} satisfies DropZoneState;\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: DropZoneActions[\"removeFile\"] = useCallbackRef((fileToRemoveOrId) => {\n\t\tconst actualFileToRemove = isString(fileToRemoveOrId)\n\t\t\t? dropZoneState.filesWithPreview.find((file) => file.id === fileToRemoveOrId)\n\t\t\t: fileToRemoveOrId;\n\n\t\tif (!actualFileToRemove) return;\n\n\t\tclearObjectURL(actualFileToRemove, disallowPreviewForNonImageFiles);\n\n\t\tconst newFilesWithPreview = dropZoneState.filesWithPreview.filter(\n\t\t\t(file) => file.id !== actualFileToRemove.id\n\t\t);\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: DropZoneActions[\"clearErrors\"] = useCallbackRef(() => {\n\t\tsetDropZoneState((prevState) => ({ ...prevState, errors: [] }));\n\t});\n\n\tconst handleFileUpload: DropZoneActions[\"handleFileUpload\"] = useCallbackRef((event) => {\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: DropZoneActions[\"handleDragEnter\"] = useCallbackRef((event) => {\n\t\tevent.preventDefault();\n\t\tevent.stopPropagation();\n\t\ttoggleIsDragging(true);\n\t});\n\n\tconst handleDragOver: DropZoneActions[\"handleDragOver\"] = useCallbackRef((event) => {\n\t\tevent.preventDefault();\n\t\tevent.stopPropagation();\n\t\ttoggleIsDragging(true);\n\t});\n\n\tconst handleDragLeave: DropZoneActions[\"handleDragLeave\"] = useCallbackRef((event) => {\n\t\tevent.preventDefault();\n\t\tevent.stopPropagation();\n\t\ttoggleIsDragging(false);\n\t});\n\n\tconst openFilePicker: DropZoneActions[\"openFilePicker\"] = useCallbackRef(() => {\n\t\tinputRef.current?.click();\n\t});\n\n\tconst getContainerProps: UseDropZoneResult[\"getContainerProps\"] = useCallback(\n\t\t(containerProps) => {\n\t\t\tconst mergedContainerProps = mergeTwoProps(extraContainerProps, containerProps);\n\n\t\t\treturn {\n\t\t\t\t...mergedContainerProps,\n\t\t\t\tclassName: cnMerge(\n\t\t\t\t\t\"relative isolate flex flex-col\",\n\t\t\t\t\tmergedContainerProps.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\tcontainerProps?.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\": \"container\",\n\t\t\t\t\"data-slot\": \"dropzone-container\",\n\t\t\t\tonDragEnter: composeTwoEventHandlers(handleDragEnter, mergedContainerProps.onDragEnter),\n\t\t\t\tonDragLeave: composeTwoEventHandlers(handleDragLeave, mergedContainerProps.onDragLeave),\n\t\t\t\tonDragOver: composeTwoEventHandlers(handleDragOver, mergedContainerProps.onDragOver),\n\t\t\t\tonDrop: composeTwoEventHandlers(handleFileUpload, mergedContainerProps.onDrop),\n\t\t\t};\n\t\t},\n\t\t[\n\t\t\tclassNames?.base,\n\t\t\tclassNames?.isDragging,\n\t\t\textraContainerProps,\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\twithDefaultFilePicker ? \"absolute inset-0 z-[100] cursor-pointer opacity-0\" : \"hidden\",\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: composeTwoEventHandlers(handleFileUpload, mergedInputProps.onChange),\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\twithDefaultFilePicker,\n\t\t]\n\t);\n\n\tconst savedOnRenderPropsChange = useCallbackRef(onRenderPropsChange);\n\n\tconst dropZoneResult = useMemo(() => {\n\t\tconst propsForRenderFn = {\n\t\t\tdropZoneActions: {\n\t\t\t\taddFiles,\n\t\t\t\tclearErrors,\n\t\t\t\tclearFiles,\n\t\t\t\thandleDragEnter,\n\t\t\t\thandleDragLeave,\n\t\t\t\thandleDragOver,\n\t\t\t\thandleFileUpload,\n\t\t\t\topenFilePicker,\n\t\t\t\tremoveFile,\n\t\t\t},\n\t\t\tdropZoneState,\n\t\t\tgetContainerProps,\n\t\t\tgetInputProps,\n\t\t\tinputRef,\n\t\t} satisfies UseDropZoneResult;\n\n\t\tsavedOnRenderPropsChange(propsForRenderFn);\n\n\t\treturn propsForRenderFn;\n\t}, [\n\t\tsavedOnRenderPropsChange,\n\t\taddFiles,\n\t\tclearErrors,\n\t\tclearFiles,\n\t\tdropZoneState,\n\t\tgetInputProps,\n\t\tgetContainerProps,\n\t\thandleDragEnter,\n\t\thandleDragLeave,\n\t\thandleDragOver,\n\t\thandleFileUpload,\n\t\topenFilePicker,\n\t\tremoveFile,\n\t]);\n\n\treturn dropZoneResult;\n};\n","\"use client\";\n\nimport * as React from \"react\";\n\nimport { Show, Slot } from \"@/components/common\";\nimport { type GetSlotComponentProps, getSlotMap, withSlotNameAndSymbol } from \"@/lib/utils/getSlotMap\";\nimport type { DiscriminatedRenderProps, PolymorphicProps } from \"@zayne-labs/toolkit-react/utils\";\nimport { isArray, isFunction } from \"@zayne-labs/toolkit-type-helpers\";\nimport { Fragment as ReactFragment, isValidElement } from \"react\";\nimport { DropZoneContextProvider, useDropZoneContext } from \"./drop-context\";\nimport {\n\ttype ContainerProps,\n\ttype InputProps,\n\ttype UseDropZoneProps,\n\ttype UseDropZoneResult,\n\tuseDropZone,\n} from \"./use-drop-zone\";\n\nexport type DropZoneRenderPropType = DiscriminatedRenderProps<\n\tReact.ReactNode | ((props: UseDropZoneResult) => React.ReactNode)\n>;\n\nexport type DropZoneRootProps = DropZoneRenderPropType\n\t& UseDropZoneProps & {\n\t\t/**\n\t\t * Controls whether to include internal elements (root and input) or not.\n\t\t */\n\t\twithInternalElements?: boolean;\n\t};\n\nexport function DropZoneRoot(props: DropZoneRootProps) {\n\tconst { children, render, withInternalElements = true, ...restOfProps } = props;\n\n\tconst dropZone = useDropZone(restOfProps);\n\n\tconst ContainerComponent = withInternalElements ? DropZoneContainer : ReactFragment;\n\tconst InputComponent = withInternalElements ? DropZoneInput : ReactFragment;\n\n\tconst selectedChildren = children ?? render;\n\n\tconst resolvedChildren = isFunction(selectedChildren) ? selectedChildren(dropZone) : selectedChildren;\n\n\tconst couldChildrenContainSlots =\n\t\tisArray(resolvedChildren)\n\t\t|| (isValidElement(resolvedChildren) && resolvedChildren.type === ReactFragment);\n\n\tconst slots =\n\t\twithInternalElements && couldChildrenContainSlots\n\t\t\t? getSlotMap<SlotComponentProps>(resolvedChildren)\n\t\t\t: ({ default: resolvedChildren } as ReturnType<typeof getSlotMap<SlotComponentProps>>);\n\n\treturn (\n\t\t<DropZoneContextProvider value={dropZone}>\n\t\t\t<ContainerComponent>\n\t\t\t\t<InputComponent />\n\n\t\t\t\t{slots.default}\n\t\t\t</ContainerComponent>\n\n\t\t\t{slots.preview}\n\t\t</DropZoneContextProvider>\n\t);\n}\n\ntype DropZoneInputProps = InputProps & { asChild?: boolean };\n\nexport function DropZoneInput(props: DropZoneInputProps) {\n\tconst { asChild, ...restOfProps } = props;\n\n\tconst dropZoneContext = useDropZoneContext();\n\n\tconst Component = asChild ? Slot.Root : \"input\";\n\n\treturn <Component {...dropZoneContext.getInputProps(restOfProps)} />;\n}\n\ntype DropZoneContainerProps = ContainerProps & { asChild?: boolean };\n\nexport function DropZoneContainer<TElement extends React.ElementType = \"div\">(\n\tprops: PolymorphicProps<TElement, DropZoneContainerProps>\n) {\n\tconst { as: Element = \"div\", asChild, ...restOfProps } = props;\n\n\tconst dropZoneContext = useDropZoneContext();\n\n\tconst Component = asChild ? Slot.Root : Element;\n\n\treturn <Component {...dropZoneContext.getContainerProps(restOfProps)} />;\n}\n\ntype RenderPropFn = (props: UseDropZoneResult) => React.ReactNode;\n\ntype SlotComponentProps = GetSlotComponentProps<\"preview\", React.ReactNode | RenderPropFn>;\n\nexport function DropZoneContext(props: { children: RenderPropFn }) {\n\tconst { children } = props;\n\n\tconst dropZoneContext = useDropZoneContext();\n\n\treturn children(dropZoneContext);\n}\n\nexport const DropZoneImagePreview = withSlotNameAndSymbol<SlotComponentProps>(\"preview\", (props) => {\n\tconst { children } = props;\n\n\tif (isFunction(children)) {\n\t\treturn (\n\t\t\t<DropZoneContext>\n\t\t\t\t{(dropZoneCtx) => (\n\t\t\t\t\t<Show.Root when={dropZoneCtx.dropZoneState.filesWithPreview.length > 0}>\n\t\t\t\t\t\t{children(dropZoneCtx)}\n\t\t\t\t\t</Show.Root>\n\t\t\t\t)}\n\t\t\t</DropZoneContext>\n\t\t);\n\t}\n\n\treturn children;\n});\n","export {\n\tDropZoneRoot as Root,\n\tDropZoneImagePreview as ImagePreview,\n\tDropZoneContext as Context,\n\tDropZoneInput as Input,\n\tDropZoneContainer as Container,\n} from \"./drop-zone\";\n"],"mappings":";;;;;;;;;;;;;AAGA,MAAM,CAAC,yBAAyB,mBAAmB,GAAG,oBAAuC;CAC5F,UAAU;CACV,MAAM;CACN,cAAc;AACd,EAAC;;;;ACHF,MAAa,mBAAmB,CAACA,SAAkC;AAClE,MAAK,OAAO,KAAK,CAChB,QAAO,KAAK;AAGb,SAAQ,EAAE,KAAK,KAAK,IAAI,KAAK,MAAM,YAAY,KAAK,CAAC,CAAC,IAAI,OAAO,YAAY,CAAC,MAAM,GAAG,EAAE,CAAC;AAC1F;AAED,MAAa,kBAAkB,CAACC,MAAYC,oCAA6C;AACxF,KAAI,oCAAoC,KAAK,KAAK,WAAW,SAAS,CAAE;AAExE,QAAO,mBAAmB,KAAK;AAC/B;AAED,MAAa,iBAAiB,CAC7BC,iBACAD,oCACI;A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e,iBAAiB,IAAI,iBAAiB,SAASA;CAEnE,MAAM,QACL,wBAAwB,4BACrB,WAA+B,iBAAiB,GAC/C,EAAE,SAAS,iBAAkB;AAElC,wBACC,KAAC;EAAwB,OAAO;6BAC/B,KAAC,iDACA,IAAC,mBAAiB,EAEjB,MAAM,WACa,EAEpB,MAAM;GACkB;AAE3B;AAID,SAAgB,cAAcC,OAA2B;CACxD,MAAM,EAAE,QAAS,GAAG,aAAa,GAAG;CAEpC,MAAM,kBAAkB,oBAAoB;CAE5C,MAAMC,cAAY,qBAAsB;AAExC,wBAAO,IAACA,eAAU,GAAI,gBAAgB,cAAc,YAAY,GAAI;AACpE;AAID,SAAgB,kBACfC,OACC;CACD,MAAM,EAAE,IAAI,UAAU,OAAO,QAAS,GAAG,aAAa,GAAG;CAEzD,MAAM,kBAAkB,oBAAoB;CAE5C,MAAMD,cAAY,qBAAsB;AAExC,wBAAO,IAACA,eAAU,GAAI,gBAAgB,kBAAkB,YAAY,GAAI;AACxE;AAMD,SAAgB,gBAAgBE,OAAmC;CAClE,MAAM,EAAE,UAAU,GAAG;CAErB,MAAM,kBAAkB,oBAAoB;AAE5C,QAAO,SAAS,gBAAgB;AAChC;AAED,MAAa,uBAAuB,sBAA0C,WAAW,CAAC,UAAU;CACnG,MAAM,EAAE,UAAU,GAAG;AAErB,KAAI,WAAW,SAAS,CACvB,wBACC,IAAC,6BACC,CAAC,gCACD;EAAW,MAAM,YAAY,cAAc,iBAAiB,SAAS;YACnE,SAAS,YAAY;GACX,GAEI;AAIpB,QAAO;AACP,EAAC"}
@@ -0,0 +1,126 @@
1
+ import { Component, useState } from "react";
2
+ import { isFunction } from "@zayne-labs/toolkit-type-helpers";
3
+ import { createCustomContext, useCallbackRef } from "@zayne-labs/toolkit-react";
4
+ import { jsx } from "react/jsx-runtime";
5
+
6
+ //#region src/components/common/error-boundary/error-boundary-context.ts
7
+ const [ErrorBoundaryContext, useErrorBoundaryContext] = createCustomContext({
8
+ hookName: "useErrorBoundaryContext",
9
+ name: "ErrorBoundaryContext",
10
+ providerName: "ErrorBoundaryContextProvider"
11
+ });
12
+
13
+ //#endregion
14
+ //#region src/components/common/error-boundary/error-boundary.tsx
15
+ const initialState = {
16
+ error: null,
17
+ hasError: false
18
+ };
19
+ const hasArrayChanged = (arrayOne = [], arrayTwo = []) => {
20
+ return arrayOne.length !== arrayTwo.length || arrayOne.some((item, index) => !Object.is(item, arrayTwo[index]));
21
+ };
22
+ /**
23
+ * Copied from react-error-boundary package
24
+ * @see https://github.com/bvaughn/react-error-boundary
25
+ */
26
+ var ErrorBoundary = class extends Component {
27
+ constructor(props) {
28
+ super(props);
29
+ this.state = initialState;
30
+ }
31
+ static getDerivedStateFromError(error) {
32
+ return {
33
+ error,
34
+ hasError: true
35
+ };
36
+ }
37
+ componentDidCatch(error, info) {
38
+ this.props.onError?.({
39
+ error,
40
+ info
41
+ });
42
+ }
43
+ componentDidUpdate(prevProps, prevState) {
44
+ const { hasError } = this.state;
45
+ const { resetKeys } = this.props;
46
+ if (hasError && prevState.error !== null && hasArrayChanged(prevProps.resetKeys, resetKeys)) {
47
+ this.props.onReset?.({
48
+ next: resetKeys,
49
+ prev: prevProps.resetKeys,
50
+ reason: "keys"
51
+ });
52
+ this.setState(initialState);
53
+ }
54
+ }
55
+ render() {
56
+ const { children, fallback } = this.props;
57
+ const { error, hasError } = this.state;
58
+ let childToRender = children;
59
+ if (hasError) switch (true) {
60
+ case isFunction(fallback): {
61
+ const fallbackRenderProps = {
62
+ error,
63
+ resetErrorBoundary: this.#resetErrorBoundary
64
+ };
65
+ childToRender = fallback(fallbackRenderProps);
66
+ break;
67
+ }
68
+ case Boolean(fallback): {
69
+ childToRender = fallback;
70
+ break;
71
+ }
72
+ default: console.warn("No fallback provided to error boundary");
73
+ }
74
+ const contextValue = {
75
+ error,
76
+ hasError,
77
+ resetErrorBoundary: this.#resetErrorBoundary
78
+ };
79
+ return /* @__PURE__ */ jsx(ErrorBoundaryContext, {
80
+ value: contextValue,
81
+ children: childToRender
82
+ });
83
+ }
84
+ #resetErrorBoundary = (...parameters) => {
85
+ const { error } = this.state;
86
+ if (error !== null) {
87
+ this.props.onReset?.({
88
+ args: parameters,
89
+ reason: "imperative-api"
90
+ });
91
+ this.setState(initialState);
92
+ }
93
+ };
94
+ };
95
+
96
+ //#endregion
97
+ //#region src/components/common/error-boundary/useErrorBoundary.ts
98
+ const useErrorBoundary = () => {
99
+ const { resetErrorBoundary } = useErrorBoundaryContext();
100
+ const [state, setState] = useState({
101
+ error: null,
102
+ hasError: false
103
+ });
104
+ if (state.hasError) throw state.error;
105
+ const resetBoundary = useCallbackRef(() => {
106
+ resetErrorBoundary();
107
+ setState({
108
+ error: null,
109
+ hasError: false
110
+ });
111
+ });
112
+ const showBoundary = useCallbackRef((error) => {
113
+ setState({
114
+ error,
115
+ hasError: true
116
+ });
117
+ });
118
+ return {
119
+ resetBoundary,
120
+ showBoundary
121
+ };
122
+ };
123
+
124
+ //#endregion
125
+ export { ErrorBoundary, useErrorBoundary, useErrorBoundaryContext };
126
+ //# sourceMappingURL=error-boundary-B3ycUZ1t.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error-boundary-B3ycUZ1t.js","names":["initialState: ErrorBoundaryState","arrayOne: unknown[]","arrayTwo: unknown[]","props: ErrorBoundaryProps","error: Error","info: React.ErrorInfo","prevProps: ErrorBoundaryProps","prevState: ErrorBoundaryState","#resetErrorBoundary","error: TError"],"sources":["../../src/components/common/error-boundary/error-boundary-context.ts","../../src/components/common/error-boundary/error-boundary.tsx","../../src/components/common/error-boundary/useErrorBoundary.ts"],"sourcesContent":["import { createCustomContext } from \"@zayne-labs/toolkit-react\";\n\nexport type ErrorBoundaryContextType = {\n\terror: unknown;\n\thasError: boolean;\n\tresetErrorBoundary: (...args: unknown[]) => void;\n};\n\nexport const [ErrorBoundaryContext, useErrorBoundaryContext] =\n\tcreateCustomContext<ErrorBoundaryContextType>({\n\t\thookName: \"useErrorBoundaryContext\",\n\t\tname: \"ErrorBoundaryContext\",\n\t\tproviderName: \"ErrorBoundaryContextProvider\",\n\t});\n","import * as React from \"react\";\n\nimport { isFunction } from \"@zayne-labs/toolkit-type-helpers\";\nimport { Component } from \"react\";\nimport { ErrorBoundaryContext, type ErrorBoundaryContextType } from \"./error-boundary-context\";\nimport type { ErrorBoundaryProps, FallbackProps } from \"./types\";\n\ntype ErrorBoundaryState =\n\t| {\n\t\t\terror: Error;\n\t\t\thasError: true;\n\t }\n\t| {\n\t\t\terror: null;\n\t\t\thasError: false;\n\t };\n\nconst initialState: ErrorBoundaryState = {\n\terror: null,\n\thasError: false,\n};\n\nconst hasArrayChanged = (arrayOne: unknown[] = [], arrayTwo: unknown[] = []) => {\n\treturn (\n\t\tarrayOne.length !== arrayTwo.length\n\t\t|| arrayOne.some((item, index) => !Object.is(item, arrayTwo[index]))\n\t);\n};\n\n/**\n * Copied from react-error-boundary package\n * @see https://github.com/bvaughn/react-error-boundary\n */\n\nexport class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {\n\tconstructor(props: ErrorBoundaryProps) {\n\t\tsuper(props);\n\n\t\tthis.state = initialState;\n\t}\n\n\tstatic getDerivedStateFromError(error: Error) {\n\t\treturn { error, hasError: true };\n\t}\n\n\toverride componentDidCatch(error: Error, info: React.ErrorInfo) {\n\t\tthis.props.onError?.({\n\t\t\terror,\n\t\t\tinfo,\n\t\t});\n\t}\n\n\toverride componentDidUpdate(prevProps: ErrorBoundaryProps, prevState: ErrorBoundaryState) {\n\t\tconst { hasError } = this.state;\n\t\tconst { resetKeys } = this.props;\n\n\t\t// == There's an edge case where if the thing that triggered the error happens to *also* be in the resetKeys array, we'd end up resetting the error boundary immediately.\n\t\t// == This would likely trigger a second error to be thrown.\n\t\t// == So we make sure that we don't check the resetKeys on the first call of cDU after the error is set.\n\n\t\tif (hasError && prevState.error !== null && hasArrayChanged(prevProps.resetKeys, resetKeys)) {\n\t\t\tthis.props.onReset?.({\n\t\t\t\tnext: resetKeys,\n\t\t\t\tprev: prevProps.resetKeys,\n\t\t\t\treason: \"keys\",\n\t\t\t});\n\n\t\t\tthis.setState(initialState);\n\t\t}\n\t}\n\n\toverride render() {\n\t\tconst { children, fallback } = this.props;\n\t\tconst { error, hasError } = this.state;\n\n\t\tlet childToRender = children;\n\n\t\tif (hasError) {\n\t\t\tswitch (true) {\n\t\t\t\tcase isFunction(fallback): {\n\t\t\t\t\tconst fallbackRenderProps = {\n\t\t\t\t\t\terror,\n\t\t\t\t\t\tresetErrorBoundary: this.#resetErrorBoundary,\n\t\t\t\t\t} satisfies FallbackProps;\n\n\t\t\t\t\tchildToRender = fallback(fallbackRenderProps);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tcase Boolean(fallback): {\n\t\t\t\t\tchildToRender = fallback;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tdefault: {\n\t\t\t\t\tconsole.warn(\"No fallback provided to error boundary\");\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tconst contextValue = {\n\t\t\terror,\n\t\t\thasError,\n\t\t\tresetErrorBoundary: this.#resetErrorBoundary,\n\t\t} satisfies ErrorBoundaryContextType;\n\n\t\treturn <ErrorBoundaryContext value={contextValue}>{childToRender}</ErrorBoundaryContext>;\n\t}\n\n\t#resetErrorBoundary = (...parameters: unknown[]) => {\n\t\tconst { error } = this.state;\n\n\t\tif (error !== null) {\n\t\t\tthis.props.onReset?.({\n\t\t\t\targs: parameters,\n\t\t\t\treason: \"imperative-api\",\n\t\t\t});\n\n\t\t\tthis.setState(initialState);\n\t\t}\n\t};\n}\n","import { useCallbackRef } from \"@zayne-labs/toolkit-react\";\nimport { useState } from \"react\";\nimport { useErrorBoundaryContext } from \"./error-boundary-context\";\n\ntype UseErrorBoundaryState<TError extends Error> =\n\t| {\n\t\t\terror: null;\n\t\t\thasError: false;\n\t }\n\t| {\n\t\t\terror: TError;\n\t\t\thasError: true;\n\t };\n\nexport const useErrorBoundary = <TError extends Error>() => {\n\tconst { resetErrorBoundary } = useErrorBoundaryContext();\n\n\tconst [state, setState] = useState<UseErrorBoundaryState<TError>>({\n\t\terror: null,\n\t\thasError: false,\n\t});\n\n\tif (state.hasError) {\n\t\tthrow state.error;\n\t}\n\n\tconst resetBoundary = useCallbackRef(() => {\n\t\tresetErrorBoundary();\n\n\t\tsetState({\n\t\t\terror: null,\n\t\t\thasError: false,\n\t\t});\n\t});\n\n\tconst showBoundary = useCallbackRef((error: TError) => {\n\t\tsetState({\n\t\t\terror,\n\t\t\thasError: true,\n\t\t});\n\t});\n\n\treturn { resetBoundary, showBoundary };\n};\n"],"mappings":";;;;;;AAQA,MAAa,CAAC,sBAAsB,wBAAwB,GAC3D,oBAA8C;CAC7C,UAAU;CACV,MAAM;CACN,cAAc;AACd,EAAC;;;;ACIH,MAAMA,eAAmC;CACxC,OAAO;CACP,UAAU;AACV;AAED,MAAM,kBAAkB,CAACC,WAAsB,CAAE,GAAEC,WAAsB,CAAE,MAAK;AAC/E,QACC,SAAS,WAAW,SAAS,UAC1B,SAAS,KAAK,CAAC,MAAM,WAAW,OAAO,GAAG,MAAM,SAAS,OAAO,CAAC;AAErE;;;;;AAOD,IAAa,gBAAb,cAAmC,UAAkD;CACpF,YAAYC,OAA2B;AACtC,QAAM,MAAM;AAEZ,OAAK,QAAQ;CACb;CAED,OAAO,yBAAyBC,OAAc;AAC7C,SAAO;GAAE;GAAO,UAAU;EAAM;CAChC;CAED,AAAS,kBAAkBA,OAAcC,MAAuB;AAC/D,OAAK,MAAM,UAAU;GACpB;GACA;EACA,EAAC;CACF;CAED,AAAS,mBAAmBC,WAA+BC,WAA+B;EACzF,MAAM,EAAE,UAAU,GAAG,KAAK;EAC1B,MAAM,EAAE,WAAW,GAAG,KAAK;AAM3B,MAAI,YAAY,UAAU,UAAU,QAAQ,gBAAgB,UAAU,WAAW,UAAU,EAAE;AAC5F,QAAK,MAAM,UAAU;IACpB,MAAM;IACN,MAAM,UAAU;IAChB,QAAQ;GACR,EAAC;AAEF,QAAK,SAAS,aAAa;EAC3B;CACD;CAED,AAAS,SAAS;EACjB,MAAM,EAAE,UAAU,UAAU,GAAG,KAAK;EACpC,MAAM,EAAE,OAAO,UAAU,GAAG,KAAK;EAEjC,IAAI,gBAAgB;AAEpB,MAAI,SACH,SAAQ,MAAR;GACC,KAAK,WAAW,SAAS,EAAE;IAC1B,MAAM,sBAAsB;KAC3B;KACA,oBAAoB,KAAKC;IACzB;AAED,oBAAgB,SAAS,oBAAoB;AAC7C;GACA;GAED,KAAK,QAAQ,SAAS,EAAE;AACvB,oBAAgB;AAChB;GACA;GAED,QACC,SAAQ,KAAK,yCAAyC;EAEvD;EAGF,MAAM,eAAe;GACpB;GACA;GACA,oBAAoB,KAAKA;EACzB;AAED,yBAAO,IAAC;GAAqB,OAAO;aAAe;IAAqC;CACxF;CAED,sBAAsB,CAAC,GAAG,eAA0B;EACnD,MAAM,EAAE,OAAO,GAAG,KAAK;AAEvB,MAAI,UAAU,MAAM;AACnB,QAAK,MAAM,UAAU;IACpB,MAAM;IACN,QAAQ;GACR,EAAC;AAEF,QAAK,SAAS,aAAa;EAC3B;CACD;AACD;;;;AC3GD,MAAa,mBAAmB,MAA4B;CAC3D,MAAM,EAAE,oBAAoB,GAAG,yBAAyB;CAExD,MAAM,CAAC,OAAO,SAAS,GAAG,SAAwC;EACjE,OAAO;EACP,UAAU;CACV,EAAC;AAEF,KAAI,MAAM,SACT,OAAM,MAAM;CAGb,MAAM,gBAAgB,eAAe,MAAM;AAC1C,sBAAoB;AAEpB,WAAS;GACR,OAAO;GACP,UAAU;EACV,EAAC;CACF,EAAC;CAEF,MAAM,eAAe,eAAe,CAACC,UAAkB;AACtD,WAAS;GACR;GACA,UAAU;EACV,EAAC;CACF,EAAC;AAEF,QAAO;EAAE;EAAe;CAAc;AACtC"}
@@ -0,0 +1,42 @@
1
+ import "react";
2
+ import { isArray, isNumber } from "@zayne-labs/toolkit-type-helpers";
3
+ import { jsx } from "react/jsx-runtime";
4
+
5
+ //#region src/components/common/for/for.tsx
6
+ function For(props) {
7
+ const { children, each, fallback = null, render } = props;
8
+ if (each == null || isNumber(each) && each === 0 || isArray(each) && each.length === 0) return fallback;
9
+ const resolvedArray = isNumber(each) ? [...Array(each).keys()] : each;
10
+ if (resolvedArray.length === 0) return fallback;
11
+ const JSXElementList = resolvedArray.map((...params) => {
12
+ return typeof children === "function" ? children(...params) : render(...params);
13
+ });
14
+ return JSXElementList;
15
+ }
16
+ function ForWithWrapper(props) {
17
+ const { as: ListContainer = "ul", children, className, each, ref, render,...restOfListProps } = props;
18
+ return /* @__PURE__ */ jsx(ListContainer, {
19
+ ref,
20
+ className,
21
+ ...restOfListProps,
22
+ children: /* @__PURE__ */ jsx(For, {
23
+ children,
24
+ each,
25
+ render
26
+ })
27
+ });
28
+ }
29
+
30
+ //#endregion
31
+ //#region src/components/common/for/getElementList.ts
32
+ const getElementList = (variant) => {
33
+ switch (variant) {
34
+ case "base": return [For];
35
+ case "withWrapper": return [ForWithWrapper];
36
+ default: return [ForWithWrapper];
37
+ }
38
+ };
39
+
40
+ //#endregion
41
+ export { For, ForWithWrapper, getElementList };
42
+ //# sourceMappingURL=for-aTw1TgJo.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"for-aTw1TgJo.js","names":["props: ForProps<TArray>","props: PolymorphicProps<TElement, ForProps<TArrayItem>>","variant?: TVariant"],"sources":["../../src/components/common/for/for.tsx","../../src/components/common/for/getElementList.ts"],"sourcesContent":["import * as React from \"react\";\n\nimport type { DiscriminatedRenderProps, PolymorphicProps } from \"@zayne-labs/toolkit-react/utils\";\nimport { type ExtractUnion, type Prettify, isArray, isNumber } from \"@zayne-labs/toolkit-type-helpers\";\n\n// prettier-ignore\ntype RenderPropFn<TArray> = (\n\titem: TArray extends readonly unknown[] ? ExtractUnion<TArray> : TArray extends number ? number : unknown,\n\tindex: number,\n\tarray: TArray extends readonly unknown[] ? TArray : TArray extends number ? number[] : unknown[]\n) => React.ReactNode;\n\nexport type ForRenderProps<TArrayItem> = DiscriminatedRenderProps<RenderPropFn<TArrayItem>>;\n\n/* eslint-disable perfectionist/sort-intersection-types -- Prefer the object to come first before the render props */\ntype ForProps<TArray> = Prettify<\n\t{\n\t\teach: TArray;\n\t\tfallback?: React.ReactNode;\n\t} & ForRenderProps<TArray>\n>;\n\n/* eslint-enable perfectionist/sort-intersection-types -- Prefer the object to come first before the render props */\n\nexport function For<const TArray>(props: ForProps<TArray>) {\n\tconst { children, each, fallback = null, render } = props;\n\n\tif (each == null || (isNumber(each) && each === 0) || (isArray(each) && each.length === 0)) {\n\t\treturn fallback;\n\t}\n\n\tconst resolvedArray = isNumber(each) ? [...Array(each).keys()] : (each as unknown[]);\n\n\tif (resolvedArray.length === 0) {\n\t\treturn fallback;\n\t}\n\n\tconst JSXElementList = resolvedArray.map((...params) => {\n\t\ttype Params = Parameters<RenderPropFn<TArray>>;\n\n\t\treturn typeof children === \"function\"\n\t\t\t? children(...(params as Params))\n\t\t\t: render(...(params as Params));\n\t});\n\n\treturn JSXElementList;\n}\n\nexport function ForWithWrapper<TArrayItem, TElement extends React.ElementType = \"ul\">(\n\tprops: PolymorphicProps<TElement, ForProps<TArrayItem>>\n) {\n\tconst { as: ListContainer = \"ul\", children, className, each, ref, render, ...restOfListProps } = props;\n\n\treturn (\n\t\t<ListContainer ref={ref} className={className} {...restOfListProps}>\n\t\t\t<For {...({ children, each, render } as ForProps<TArrayItem>)} />\n\t\t</ListContainer>\n\t);\n}\n","import { For, ForWithWrapper } from \"./for\";\n\ntype GetElementListResult<TVariant extends \"base\" | \"withWrapper\"> = TVariant extends \"base\"\n\t? [typeof For]\n\t: [typeof ForWithWrapper];\n\nconst getElementList = <TVariant extends \"base\" | \"withWrapper\" = \"withWrapper\">(\n\tvariant?: TVariant\n): GetElementListResult<TVariant> => {\n\tswitch (variant) {\n\t\tcase \"base\": {\n\t\t\treturn [For] as never;\n\t\t}\n\t\tcase \"withWrapper\": {\n\t\t\treturn [ForWithWrapper] as never;\n\t\t}\n\t\tdefault: {\n\t\t\treturn [ForWithWrapper] as never;\n\t\t}\n\t}\n};\n\nexport { getElementList };\n"],"mappings":";;;;;AAwBA,SAAgB,IAAkBA,OAAyB;CAC1D,MAAM,EAAE,UAAU,MAAM,WAAW,MAAM,QAAQ,GAAG;AAEpD,KAAI,QAAQ,QAAS,SAAS,KAAK,IAAI,SAAS,KAAO,QAAQ,KAAK,IAAI,KAAK,WAAW,EACvF,QAAO;CAGR,MAAM,gBAAgB,SAAS,KAAK,GAAG,CAAC,GAAG,MAAM,KAAK,CAAC,MAAM,AAAC,IAAI;AAElE,KAAI,cAAc,WAAW,EAC5B,QAAO;CAGR,MAAM,iBAAiB,cAAc,IAAI,CAAC,GAAG,WAAW;AAGvD,gBAAc,aAAa,aACxB,SAAS,GAAI,OAAkB,GAC/B,OAAO,GAAI,OAAkB;CAChC,EAAC;AAEF,QAAO;AACP;AAED,SAAgB,eACfC,OACC;CACD,MAAM,EAAE,IAAI,gBAAgB,MAAM,UAAU,WAAW,MAAM,KAAK,OAAQ,GAAG,iBAAiB,GAAG;AAEjG,wBACC,IAAC;EAAmB;EAAgB;EAAW,GAAI;4BAClD,IAAC;GAAW;GAAU;GAAM;IAAqC;GAClD;AAEjB;;;;ACpDD,MAAM,iBAAiB,CACtBC,YACoC;AACpC,SAAQ,SAAR;EACC,KAAK,OACJ,QAAO,CAAC,GAAI;EAEb,KAAK,cACJ,QAAO,CAAC,cAAe;EAExB,QACC,QAAO,CAAC,cAAe;CAExB;AACD"}