@leitware/composables 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +387 -0
- package/dist/presets/brutalist.css +298 -0
- package/dist/presets/default.css +298 -0
- package/dist/presets/signal-pop.css +369 -0
- package/dist/styles.css +2 -0
- package/package.json +216 -0
- package/rules/composables.md +244 -0
- package/src/_internal/accordion.tsx +93 -0
- package/src/_internal/ai-elements/attachments.tsx +385 -0
- package/src/_internal/ai-elements/chain-of-thought.tsx +197 -0
- package/src/_internal/ai-elements/code-block.tsx +516 -0
- package/src/_internal/ai-elements/confirmation.tsx +150 -0
- package/src/_internal/ai-elements/conversation.tsx +148 -0
- package/src/_internal/ai-elements/inline-citation.tsx +248 -0
- package/src/_internal/ai-elements/message.tsx +292 -0
- package/src/_internal/ai-elements/plan.tsx +126 -0
- package/src/_internal/ai-elements/prompt-input.tsx +1285 -0
- package/src/_internal/ai-elements/reasoning.tsx +208 -0
- package/src/_internal/ai-elements/shimmer.tsx +72 -0
- package/src/_internal/ai-elements/sources.tsx +53 -0
- package/src/_internal/ai-elements/suggestion.tsx +48 -0
- package/src/_internal/ai-elements/task.tsx +64 -0
- package/src/_internal/ai-elements/tool.tsx +156 -0
- package/src/_internal/alert-dialog.tsx +159 -0
- package/src/_internal/alert.tsx +88 -0
- package/src/_internal/aspect-ratio.tsx +23 -0
- package/src/_internal/avatar.tsx +58 -0
- package/src/_internal/badge.tsx +60 -0
- package/src/_internal/breadcrumb.tsx +104 -0
- package/src/_internal/bubble.tsx +125 -0
- package/src/_internal/button-group.tsx +78 -0
- package/src/_internal/button.tsx +62 -0
- package/src/_internal/calendar.tsx +179 -0
- package/src/_internal/card.tsx +83 -0
- package/src/_internal/carousel.tsx +215 -0
- package/src/_internal/checkbox.tsx +26 -0
- package/src/_internal/code-block.tsx +53 -0
- package/src/_internal/collapsible.tsx +15 -0
- package/src/_internal/combobox.tsx +264 -0
- package/src/_internal/command.tsx +155 -0
- package/src/_internal/container.tsx +35 -0
- package/src/_internal/dialog.tsx +132 -0
- package/src/_internal/direction.tsx +12 -0
- package/src/_internal/divider.tsx +162 -0
- package/src/_internal/dropdown-menu.tsx +247 -0
- package/src/_internal/dropzone.tsx +481 -0
- package/src/_internal/empty.tsx +94 -0
- package/src/_internal/field.tsx +221 -0
- package/src/_internal/grid-overlay.tsx +48 -0
- package/src/_internal/grid.tsx +109 -0
- package/src/_internal/hover-card.tsx +47 -0
- package/src/_internal/icon.tsx +45 -0
- package/src/_internal/input-group.tsx +146 -0
- package/src/_internal/input.tsx +24 -0
- package/src/_internal/item.tsx +187 -0
- package/src/_internal/kbd.tsx +28 -0
- package/src/_internal/label.tsx +18 -0
- package/src/_internal/layout-bento.tsx +386 -0
- package/src/_internal/layout.tsx +174 -0
- package/src/_internal/marker.tsx +66 -0
- package/src/_internal/message.tsx +85 -0
- package/src/_internal/native-select.tsx +50 -0
- package/src/_internal/pagination.tsx +108 -0
- package/src/_internal/popover.tsx +83 -0
- package/src/_internal/progress.tsx +59 -0
- package/src/_internal/radio-group.tsx +36 -0
- package/src/_internal/resizable.tsx +44 -0
- package/src/_internal/scroll-area.tsx +48 -0
- package/src/_internal/select.tsx +187 -0
- package/src/_internal/separator.tsx +19 -0
- package/src/_internal/sheet.tsx +122 -0
- package/src/_internal/sidebar.tsx +687 -0
- package/src/_internal/skeleton.tsx +13 -0
- package/src/_internal/slider.tsx +60 -0
- package/src/_internal/sonner.tsx +51 -0
- package/src/_internal/spacer.tsx +42 -0
- package/src/_internal/spinner.tsx +16 -0
- package/src/_internal/stack.tsx +78 -0
- package/src/_internal/switch.tsx +31 -0
- package/src/_internal/table.tsx +84 -0
- package/src/_internal/tabs.tsx +74 -0
- package/src/_internal/textarea.tsx +19 -0
- package/src/_internal/toggle-group.tsx +99 -0
- package/src/_internal/toggle.tsx +42 -0
- package/src/_internal/tooltip.tsx +55 -0
- package/src/_internal/typography.tsx +129 -0
- package/src/hooks/use-mobile.ts +19 -0
- package/src/hooks/use-numeric-input.ts +119 -0
- package/src/index.ts +443 -0
- package/src/lib/numeric-input.ts +88 -0
- package/src/lib/utils.ts +39 -0
- package/src/opinionated/accordion.tsx +92 -0
- package/src/opinionated/ai/attachments.tsx +27 -0
- package/src/opinionated/ai/chain-of-thought.tsx +16 -0
- package/src/opinionated/ai/code-block.tsx +22 -0
- package/src/opinionated/ai/confirmation.tsx +16 -0
- package/src/opinionated/ai/conversation.tsx +13 -0
- package/src/opinionated/ai/index.ts +15 -0
- package/src/opinionated/ai/inline-citation.tsx +30 -0
- package/src/opinionated/ai/message.tsx +26 -0
- package/src/opinionated/ai/plan.tsx +18 -0
- package/src/opinionated/ai/prompt-input.tsx +29 -0
- package/src/opinionated/ai/reasoning.tsx +9 -0
- package/src/opinionated/ai/shimmer.tsx +4 -0
- package/src/opinionated/ai/sources.tsx +10 -0
- package/src/opinionated/ai/suggestion.tsx +6 -0
- package/src/opinionated/ai/task.tsx +12 -0
- package/src/opinionated/ai/tool.tsx +14 -0
- package/src/opinionated/alert-dialog.tsx +143 -0
- package/src/opinionated/alert.tsx +99 -0
- package/src/opinionated/aspect-ratio.tsx +12 -0
- package/src/opinionated/avatar.tsx +59 -0
- package/src/opinionated/badge.tsx +33 -0
- package/src/opinionated/breadcrumb.tsx +99 -0
- package/src/opinionated/bubble.tsx +9 -0
- package/src/opinionated/button.tsx +59 -0
- package/src/opinionated/calendar.tsx +20 -0
- package/src/opinionated/card.tsx +83 -0
- package/src/opinionated/carousel.tsx +99 -0
- package/src/opinionated/checkbox.tsx +11 -0
- package/src/opinionated/code-block.tsx +12 -0
- package/src/opinionated/collapsible.tsx +123 -0
- package/src/opinionated/combobox.tsx +123 -0
- package/src/opinionated/container.tsx +12 -0
- package/src/opinionated/dialog.tsx +124 -0
- package/src/opinionated/direction.tsx +6 -0
- package/src/opinionated/divider.tsx +13 -0
- package/src/opinionated/dropdown-menu.tsx +131 -0
- package/src/opinionated/dropzone.tsx +39 -0
- package/src/opinionated/empty.tsx +191 -0
- package/src/opinionated/field.tsx +26 -0
- package/src/opinionated/form-calendar-popover.tsx +138 -0
- package/src/opinionated/form-checkbox.tsx +107 -0
- package/src/opinionated/form-combobox.tsx +181 -0
- package/src/opinionated/form-date-of-birth.tsx +193 -0
- package/src/opinionated/form-dropzone.tsx +126 -0
- package/src/opinionated/form-input-group.tsx +184 -0
- package/src/opinionated/form-input.tsx +118 -0
- package/src/opinionated/form-multi-combobox.tsx +213 -0
- package/src/opinionated/form-multi-dropzone.tsx +141 -0
- package/src/opinionated/form-radiogroup.tsx +116 -0
- package/src/opinionated/form-select.tsx +176 -0
- package/src/opinionated/form-slider.tsx +100 -0
- package/src/opinionated/form-switch.tsx +117 -0
- package/src/opinionated/form-textarea.tsx +79 -0
- package/src/opinionated/grid-overlay.tsx +11 -0
- package/src/opinionated/grid.tsx +14 -0
- package/src/opinionated/hover-card.tsx +59 -0
- package/src/opinionated/icon.tsx +14 -0
- package/src/opinionated/input-group.tsx +20 -0
- package/src/opinionated/input.tsx +11 -0
- package/src/opinionated/item.tsx +182 -0
- package/src/opinionated/kbd.tsx +16 -0
- package/src/opinionated/label.tsx +11 -0
- package/src/opinionated/layout-bento.tsx +29 -0
- package/src/opinionated/layout.tsx +26 -0
- package/src/opinionated/list.tsx +94 -0
- package/src/opinionated/marker.tsx +7 -0
- package/src/opinionated/message.tsx +8 -0
- package/src/opinionated/native-select.tsx +51 -0
- package/src/opinionated/pagination.tsx +204 -0
- package/src/opinionated/popover.tsx +68 -0
- package/src/opinionated/progress.tsx +48 -0
- package/src/opinionated/radio-group.tsx +13 -0
- package/src/opinionated/resizable.tsx +2 -0
- package/src/opinionated/scroll-area.tsx +82 -0
- package/src/opinionated/select.tsx +101 -0
- package/src/opinionated/separator.tsx +11 -0
- package/src/opinionated/sheet.tsx +120 -0
- package/src/opinionated/sidebar.tsx +68 -0
- package/src/opinionated/skeleton.tsx +246 -0
- package/src/opinionated/slider.tsx +11 -0
- package/src/opinionated/sonner.tsx +15 -0
- package/src/opinionated/spacer.tsx +11 -0
- package/src/opinionated/stack.tsx +18 -0
- package/src/opinionated/switch.tsx +11 -0
- package/src/opinionated/table.tsx +11 -0
- package/src/opinionated/tabs.tsx +68 -0
- package/src/opinionated/textarea.tsx +11 -0
- package/src/opinionated/theme-injector.tsx +31 -0
- package/src/opinionated/toggle-group.tsx +63 -0
- package/src/opinionated/toggle.tsx +75 -0
- package/src/opinionated/token-config-panel/color-editor.tsx +85 -0
- package/src/opinionated/token-config-panel/helpers.ts +31 -0
- package/src/opinionated/token-config-panel/index.tsx +398 -0
- package/src/opinionated/token-config-panel/json-import-export.tsx +89 -0
- package/src/opinionated/token-config-panel/preset-manager.ts +31 -0
- package/src/opinionated/token-config-panel/presets-data.ts +23 -0
- package/src/opinionated/token-config-panel/spacing-editor.tsx +325 -0
- package/src/opinionated/token-config-panel/types.ts +39 -0
- package/src/opinionated/token-config-panel/typography-editor.tsx +145 -0
- package/src/opinionated/tooltip.tsx +62 -0
- package/src/opinionated/typography.tsx +7 -0
- package/src/scripts/lint-no-arbitrary.ts +155 -0
- package/src/styles/composable.css +68 -0
- package/src/styles/presets/brutalist.css +298 -0
- package/src/styles/presets/default.css +298 -0
- package/src/styles/presets/signal-pop.css +369 -0
- package/src/styles/presets-data/brutalist.ts +325 -0
- package/src/styles/presets-data/default.ts +315 -0
- package/src/styles/presets-data/index.ts +45 -0
- package/src/styles/presets-data/signal-pop.ts +393 -0
- package/src/styles/tokens/base.css +71 -0
- package/src/styles/tokens/components.css +150 -0
- package/src/styles/tokens/palette.css +795 -0
- package/src/styles/tokens/registry.ts +324 -0
- package/src/styles/tokens/semantic.css +266 -0
- package/src/styles/tokens/tailwind-theme.css +247 -0
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
import { FileIcon, UploadIcon, XIcon } from 'lucide-react'
|
|
2
|
+
import * as React from 'react'
|
|
3
|
+
import { type Accept, type DropzoneOptions, type FileRejection, useDropzone } from 'react-dropzone'
|
|
4
|
+
|
|
5
|
+
import { Button } from './button'
|
|
6
|
+
import { cn, FOCUS_RING } from '../lib/utils'
|
|
7
|
+
|
|
8
|
+
const DEFAULT_MAX_SIZE = 10 * 1024 * 1024 // 10MB
|
|
9
|
+
const DEFAULT_ACCEPT: Accept = { 'application/pdf': ['.pdf'] }
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Common file type presets for the `accept` prop.
|
|
13
|
+
*
|
|
14
|
+
* The `accept` prop uses MIME types as keys and file extensions as values.
|
|
15
|
+
* This follows the HTML input accept attribute format used by react-dropzone.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // Single file type
|
|
19
|
+
* accept={ACCEPT_PRESETS.PDF}
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* // Multiple file types (spread and merge)
|
|
23
|
+
* accept={{ ...ACCEPT_PRESETS.IMAGES, ...ACCEPT_PRESETS.PDF }}
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* // Custom accept format: { [MIME_TYPE]: ['.extension', ...] }
|
|
27
|
+
* accept={{
|
|
28
|
+
* 'image/png': ['.png'],
|
|
29
|
+
* 'image/jpeg': ['.jpg', '.jpeg'],
|
|
30
|
+
* 'application/pdf': ['.pdf'],
|
|
31
|
+
* 'text/csv': ['.csv'],
|
|
32
|
+
* 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': ['.xlsx'],
|
|
33
|
+
* }}
|
|
34
|
+
*/
|
|
35
|
+
const ACCEPT_PRESETS = {
|
|
36
|
+
/** PDF documents */
|
|
37
|
+
PDF: { 'application/pdf': ['.pdf'] },
|
|
38
|
+
/** Common image formats (PNG, JPEG, GIF, WebP) */
|
|
39
|
+
IMAGES: {
|
|
40
|
+
'image/png': ['.png'],
|
|
41
|
+
'image/jpeg': ['.jpg', '.jpeg'],
|
|
42
|
+
'image/gif': ['.gif'],
|
|
43
|
+
'image/webp': ['.webp'],
|
|
44
|
+
},
|
|
45
|
+
/** CSV files */
|
|
46
|
+
CSV: { 'text/csv': ['.csv'] },
|
|
47
|
+
/** Excel files (xlsx, xls) */
|
|
48
|
+
EXCEL: {
|
|
49
|
+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': ['.xlsx'],
|
|
50
|
+
'application/vnd.ms-excel': ['.xls'],
|
|
51
|
+
},
|
|
52
|
+
/** Word documents (docx, doc) */
|
|
53
|
+
WORD: {
|
|
54
|
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': ['.docx'],
|
|
55
|
+
'application/msword': ['.doc'],
|
|
56
|
+
},
|
|
57
|
+
/** Common document types (PDF, Word, Excel) */
|
|
58
|
+
DOCUMENTS: {
|
|
59
|
+
'application/pdf': ['.pdf'],
|
|
60
|
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': ['.docx'],
|
|
61
|
+
'application/msword': ['.doc'],
|
|
62
|
+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': ['.xlsx'],
|
|
63
|
+
'application/vnd.ms-excel': ['.xls'],
|
|
64
|
+
},
|
|
65
|
+
} as const satisfies Record<string, Accept>
|
|
66
|
+
|
|
67
|
+
// Utilities
|
|
68
|
+
function formatFileSize(bytes: number): string {
|
|
69
|
+
if (bytes < 1024) return `${bytes} B`
|
|
70
|
+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(2)} KB`
|
|
71
|
+
return `${(bytes / (1024 * 1024)).toFixed(2)} MB`
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function getAcceptDescription(accept: Accept): string {
|
|
75
|
+
const extensions = Object.values(accept).flat()
|
|
76
|
+
if (extensions.length === 0) return 'any file'
|
|
77
|
+
|
|
78
|
+
// Map common extensions to readable names
|
|
79
|
+
const extensionNames: Record<string, string> = {
|
|
80
|
+
'.pdf': 'PDF',
|
|
81
|
+
'.png': 'PNG',
|
|
82
|
+
'.jpg': 'JPG',
|
|
83
|
+
'.jpeg': 'JPEG',
|
|
84
|
+
'.gif': 'GIF',
|
|
85
|
+
'.webp': 'WebP',
|
|
86
|
+
'.csv': 'CSV',
|
|
87
|
+
'.xlsx': 'Excel',
|
|
88
|
+
'.xls': 'Excel',
|
|
89
|
+
'.docx': 'Word',
|
|
90
|
+
'.doc': 'Word',
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const uniqueNames = [
|
|
94
|
+
...new Set(extensions.map((ext) => extensionNames[ext] || ext.replace('.', '').toUpperCase())),
|
|
95
|
+
]
|
|
96
|
+
return uniqueNames.join(', ')
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Get a human-readable rejection message for a file rejection error code.
|
|
101
|
+
* Error codes come from react-dropzone's validation.
|
|
102
|
+
*
|
|
103
|
+
* @param code - Error code from react-dropzone (file-too-large, file-invalid-type, file-too-small, too-many-files)
|
|
104
|
+
* @param maxSizeBytes - Max file size for error message
|
|
105
|
+
* @param accept - Accept config for error message
|
|
106
|
+
* @param maxFiles - Max files for error message (optional)
|
|
107
|
+
*/
|
|
108
|
+
function getRejectionMessage(
|
|
109
|
+
code: string,
|
|
110
|
+
maxSizeBytes: number,
|
|
111
|
+
accept: Accept,
|
|
112
|
+
maxFiles?: number,
|
|
113
|
+
): string {
|
|
114
|
+
switch (code) {
|
|
115
|
+
case 'file-too-large':
|
|
116
|
+
return `File is too large. Maximum size is ${formatFileSize(maxSizeBytes)}.`
|
|
117
|
+
case 'file-too-small':
|
|
118
|
+
return 'File is too small.'
|
|
119
|
+
case 'file-invalid-type':
|
|
120
|
+
return `Invalid file type. Please upload a ${getAcceptDescription(accept)} file.`
|
|
121
|
+
case 'too-many-files':
|
|
122
|
+
return maxFiles
|
|
123
|
+
? `Too many files. Maximum is ${maxFiles} file${maxFiles === 1 ? '' : 's'}.`
|
|
124
|
+
: 'Too many files selected.'
|
|
125
|
+
default:
|
|
126
|
+
return 'File could not be accepted.'
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Context
|
|
131
|
+
interface DropZoneContextValue {
|
|
132
|
+
getRootProps: ReturnType<typeof useDropzone>['getRootProps']
|
|
133
|
+
getInputProps: ReturnType<typeof useDropzone>['getInputProps']
|
|
134
|
+
/** File is being dragged over the dropzone */
|
|
135
|
+
isDragActive: boolean
|
|
136
|
+
/** File being dragged matches accept criteria */
|
|
137
|
+
isDragAccept: boolean
|
|
138
|
+
/** File being dragged does NOT match accept criteria */
|
|
139
|
+
isDragReject: boolean
|
|
140
|
+
/** Dropzone is focused (keyboard navigation) */
|
|
141
|
+
isFocused: boolean
|
|
142
|
+
/** File dialog is currently open */
|
|
143
|
+
isFileDialogActive: boolean
|
|
144
|
+
/** Programmatically open the file dialog */
|
|
145
|
+
open: () => void
|
|
146
|
+
disabled: boolean
|
|
147
|
+
hasError: boolean
|
|
148
|
+
hasFiles: boolean
|
|
149
|
+
accept: Accept
|
|
150
|
+
maxSizeBytes: number
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const DropZoneContext = React.createContext<DropZoneContextValue | null>(null)
|
|
154
|
+
|
|
155
|
+
function useDropZoneContext() {
|
|
156
|
+
const context = React.useContext(DropZoneContext)
|
|
157
|
+
if (!context) {
|
|
158
|
+
throw new Error('DropZone components must be used within a DropZone')
|
|
159
|
+
}
|
|
160
|
+
return context
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Root component props for the DropZone.
|
|
165
|
+
*
|
|
166
|
+
* Most options are passed directly to react-dropzone's useDropzone hook.
|
|
167
|
+
* @see https://react-dropzone.js.org/#src for full documentation
|
|
168
|
+
*/
|
|
169
|
+
interface DropZoneProps {
|
|
170
|
+
/**
|
|
171
|
+
* Callback when files are dropped or selected.
|
|
172
|
+
* Receives both accepted and rejected files.
|
|
173
|
+
*/
|
|
174
|
+
onDrop: DropzoneOptions['onDrop']
|
|
175
|
+
/**
|
|
176
|
+
* Callback fired only when files are accepted.
|
|
177
|
+
* Use this when you only care about valid files.
|
|
178
|
+
*/
|
|
179
|
+
onDropAccepted?: DropzoneOptions['onDropAccepted']
|
|
180
|
+
/**
|
|
181
|
+
* Callback fired only when files are rejected.
|
|
182
|
+
* Use this for custom rejection handling.
|
|
183
|
+
*/
|
|
184
|
+
onDropRejected?: DropzoneOptions['onDropRejected']
|
|
185
|
+
/**
|
|
186
|
+
* Accepted file types. Uses MIME types as keys and extensions as values.
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* // Accept only PDFs
|
|
190
|
+
* accept={{ 'application/pdf': ['.pdf'] }}
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* // Accept images
|
|
194
|
+
* accept={{
|
|
195
|
+
* 'image/png': ['.png'],
|
|
196
|
+
* 'image/jpeg': ['.jpg', '.jpeg'],
|
|
197
|
+
* }}
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* // Use presets
|
|
201
|
+
* accept={ACCEPT_PRESETS.IMAGES}
|
|
202
|
+
*
|
|
203
|
+
* @default { 'application/pdf': ['.pdf'] }
|
|
204
|
+
*/
|
|
205
|
+
accept?: Accept
|
|
206
|
+
/** Maximum file size in bytes. @default 10MB */
|
|
207
|
+
maxSizeBytes?: number
|
|
208
|
+
/** Minimum file size in bytes. @default 0 */
|
|
209
|
+
minSizeBytes?: number
|
|
210
|
+
/** Maximum number of files (only relevant when multiple=true) */
|
|
211
|
+
maxFiles?: number
|
|
212
|
+
/** Allow multiple file selection. @default false */
|
|
213
|
+
multiple?: boolean
|
|
214
|
+
/** Disable the dropzone. @default false */
|
|
215
|
+
disabled?: boolean
|
|
216
|
+
/** Disable click to open file dialog. @default false */
|
|
217
|
+
noClick?: boolean
|
|
218
|
+
/** Disable keyboard navigation. @default false */
|
|
219
|
+
noKeyboard?: boolean
|
|
220
|
+
/** Disable drag and drop. @default false */
|
|
221
|
+
noDrag?: boolean
|
|
222
|
+
/**
|
|
223
|
+
* Custom validator function for additional file validation.
|
|
224
|
+
* Return null if valid, or FileError/FileError[] if invalid.
|
|
225
|
+
*/
|
|
226
|
+
validator?: DropzoneOptions['validator']
|
|
227
|
+
/** Called when file dialog is opened */
|
|
228
|
+
onFileDialogOpen?: DropzoneOptions['onFileDialogOpen']
|
|
229
|
+
/** Called when file dialog is cancelled */
|
|
230
|
+
onFileDialogCancel?: DropzoneOptions['onFileDialogCancel']
|
|
231
|
+
/** Visual state: show error styling */
|
|
232
|
+
hasError?: boolean
|
|
233
|
+
/** Visual state: files are present */
|
|
234
|
+
hasFiles?: boolean
|
|
235
|
+
children: React.ReactNode
|
|
236
|
+
className?: string
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function DropZone({
|
|
240
|
+
onDrop,
|
|
241
|
+
onDropAccepted,
|
|
242
|
+
onDropRejected,
|
|
243
|
+
accept = DEFAULT_ACCEPT,
|
|
244
|
+
maxSizeBytes = DEFAULT_MAX_SIZE,
|
|
245
|
+
minSizeBytes,
|
|
246
|
+
maxFiles,
|
|
247
|
+
multiple = false,
|
|
248
|
+
disabled = false,
|
|
249
|
+
noClick = false,
|
|
250
|
+
noKeyboard = false,
|
|
251
|
+
noDrag = false,
|
|
252
|
+
validator,
|
|
253
|
+
onFileDialogOpen,
|
|
254
|
+
onFileDialogCancel,
|
|
255
|
+
hasError = false,
|
|
256
|
+
hasFiles = false,
|
|
257
|
+
children,
|
|
258
|
+
className,
|
|
259
|
+
}: DropZoneProps) {
|
|
260
|
+
const {
|
|
261
|
+
getRootProps,
|
|
262
|
+
getInputProps,
|
|
263
|
+
isDragActive,
|
|
264
|
+
isDragAccept,
|
|
265
|
+
isDragReject,
|
|
266
|
+
isFocused,
|
|
267
|
+
isFileDialogActive,
|
|
268
|
+
open,
|
|
269
|
+
} = useDropzone({
|
|
270
|
+
onDrop,
|
|
271
|
+
onDropAccepted,
|
|
272
|
+
onDropRejected,
|
|
273
|
+
accept,
|
|
274
|
+
maxSize: maxSizeBytes,
|
|
275
|
+
minSize: minSizeBytes,
|
|
276
|
+
maxFiles,
|
|
277
|
+
multiple,
|
|
278
|
+
disabled,
|
|
279
|
+
noClick,
|
|
280
|
+
noKeyboard,
|
|
281
|
+
noDrag,
|
|
282
|
+
validator,
|
|
283
|
+
onFileDialogOpen,
|
|
284
|
+
onFileDialogCancel,
|
|
285
|
+
})
|
|
286
|
+
|
|
287
|
+
const contextValue = React.useMemo(
|
|
288
|
+
() => ({
|
|
289
|
+
getRootProps,
|
|
290
|
+
getInputProps,
|
|
291
|
+
isDragActive,
|
|
292
|
+
isDragAccept,
|
|
293
|
+
isDragReject,
|
|
294
|
+
isFocused,
|
|
295
|
+
isFileDialogActive,
|
|
296
|
+
open,
|
|
297
|
+
disabled,
|
|
298
|
+
hasError,
|
|
299
|
+
hasFiles,
|
|
300
|
+
accept,
|
|
301
|
+
maxSizeBytes,
|
|
302
|
+
}),
|
|
303
|
+
[
|
|
304
|
+
getRootProps,
|
|
305
|
+
getInputProps,
|
|
306
|
+
isDragActive,
|
|
307
|
+
isDragAccept,
|
|
308
|
+
isDragReject,
|
|
309
|
+
isFocused,
|
|
310
|
+
isFileDialogActive,
|
|
311
|
+
open,
|
|
312
|
+
disabled,
|
|
313
|
+
hasError,
|
|
314
|
+
hasFiles,
|
|
315
|
+
accept,
|
|
316
|
+
maxSizeBytes,
|
|
317
|
+
],
|
|
318
|
+
)
|
|
319
|
+
|
|
320
|
+
return (
|
|
321
|
+
<DropZoneContext.Provider value={contextValue}>
|
|
322
|
+
<div className={className}>{children}</div>
|
|
323
|
+
</DropZoneContext.Provider>
|
|
324
|
+
)
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// Drop area
|
|
328
|
+
interface DropZoneAreaProps {
|
|
329
|
+
children: React.ReactNode
|
|
330
|
+
className?: string
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function DropZoneArea({ children, className }: DropZoneAreaProps) {
|
|
334
|
+
const { getRootProps, isDragActive, isDragAccept, isDragReject, disabled, hasError, hasFiles } =
|
|
335
|
+
useDropZoneContext()
|
|
336
|
+
|
|
337
|
+
return (
|
|
338
|
+
<div
|
|
339
|
+
{...getRootProps()}
|
|
340
|
+
data-slot="dropzone-area"
|
|
341
|
+
data-drag-active={isDragActive || undefined}
|
|
342
|
+
data-drag-accept={isDragAccept || undefined}
|
|
343
|
+
data-drag-reject={isDragReject || undefined}
|
|
344
|
+
className={cn(
|
|
345
|
+
'rounded-xl border p-8 transition-[opacity,box-shadow] focus-visible:outline-none',
|
|
346
|
+
FOCUS_RING,
|
|
347
|
+
disabled ? 'cursor-not-allowed opacity-50' : 'cursor-pointer',
|
|
348
|
+
isDragActive && 'border-primary bg-accent',
|
|
349
|
+
isDragReject && 'border-stroke-critical bg-surface-critical',
|
|
350
|
+
hasError && 'border-stroke-critical',
|
|
351
|
+
hasFiles && 'p-4',
|
|
352
|
+
className,
|
|
353
|
+
)}
|
|
354
|
+
>
|
|
355
|
+
{children}
|
|
356
|
+
</div>
|
|
357
|
+
)
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// Hidden input
|
|
361
|
+
function DropZoneInput({ className }: { className?: string }) {
|
|
362
|
+
const { getInputProps } = useDropZoneContext()
|
|
363
|
+
return <input {...getInputProps()} className={className} />
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// Empty state content
|
|
367
|
+
interface DropZoneContentProps {
|
|
368
|
+
title?: string
|
|
369
|
+
description?: string
|
|
370
|
+
className?: string
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function DropZoneContent({ title = 'Upload files', description, className }: DropZoneContentProps) {
|
|
374
|
+
const { isDragActive, isDragAccept, isDragReject, accept, maxSizeBytes } = useDropZoneContext()
|
|
375
|
+
|
|
376
|
+
const defaultDescription = `Accepts ${getAcceptDescription(accept)}, max ${formatFileSize(maxSizeBytes)}.`
|
|
377
|
+
|
|
378
|
+
const getDragMessage = () => {
|
|
379
|
+
if (isDragReject) return 'File type not accepted'
|
|
380
|
+
if (isDragAccept) return 'Drop your file here'
|
|
381
|
+
if (isDragActive) return 'Drop your file here'
|
|
382
|
+
return 'Drag and drop or click to upload'
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
return (
|
|
386
|
+
<div className={cn('flex flex-col items-center gap-1 text-center', className)}>
|
|
387
|
+
<div
|
|
388
|
+
className={cn(
|
|
389
|
+
'text-muted-foreground mb-3 flex size-10 items-center justify-center rounded-lg border transition-[opacity,box-shadow]',
|
|
390
|
+
isDragAccept && 'border-primary text-primary',
|
|
391
|
+
isDragReject && 'border-stroke-critical text-danger',
|
|
392
|
+
)}
|
|
393
|
+
>
|
|
394
|
+
<UploadIcon className="size-5" />
|
|
395
|
+
</div>
|
|
396
|
+
<span className="text-sm font-medium">{title}</span>
|
|
397
|
+
<span className={cn('text-muted-foreground text-xs', isDragReject && 'text-danger')}>
|
|
398
|
+
{getDragMessage()}
|
|
399
|
+
</span>
|
|
400
|
+
<span className="text-muted-foreground mt-0.5 text-xs">
|
|
401
|
+
{description || defaultDescription}
|
|
402
|
+
</span>
|
|
403
|
+
</div>
|
|
404
|
+
)
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// File display
|
|
408
|
+
interface DropZoneFileProps {
|
|
409
|
+
file: File
|
|
410
|
+
onRemove: () => void
|
|
411
|
+
disabled?: boolean
|
|
412
|
+
className?: string
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
function DropZoneFile({ file, onRemove, disabled = false, className }: DropZoneFileProps) {
|
|
416
|
+
const handleRemove = (e: React.MouseEvent) => {
|
|
417
|
+
e.stopPropagation()
|
|
418
|
+
onRemove()
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
return (
|
|
422
|
+
<div className={cn('flex items-center gap-3', className)}>
|
|
423
|
+
<div className="text-muted-foreground flex size-10 shrink-0 items-center justify-center rounded-lg border">
|
|
424
|
+
<FileIcon className="size-5" />
|
|
425
|
+
</div>
|
|
426
|
+
<div className="flex min-w-0 flex-1 flex-col gap-0.5">
|
|
427
|
+
<span className="truncate text-sm font-medium">{file.name}</span>
|
|
428
|
+
<span className="text-muted-foreground text-xs">{formatFileSize(file.size)}</span>
|
|
429
|
+
</div>
|
|
430
|
+
<Button
|
|
431
|
+
variant="destructive"
|
|
432
|
+
size="icon-sm"
|
|
433
|
+
onClick={handleRemove}
|
|
434
|
+
disabled={disabled}
|
|
435
|
+
aria-label="Remove file"
|
|
436
|
+
>
|
|
437
|
+
<XIcon className="size-3.5" />
|
|
438
|
+
</Button>
|
|
439
|
+
</div>
|
|
440
|
+
)
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// File list for multiple files
|
|
444
|
+
interface DropZoneFileListProps {
|
|
445
|
+
files: File[]
|
|
446
|
+
onRemove: (index: number) => void
|
|
447
|
+
disabled?: boolean
|
|
448
|
+
className?: string
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function DropZoneFileList({ files, onRemove, disabled = false, className }: DropZoneFileListProps) {
|
|
452
|
+
return (
|
|
453
|
+
<div className={cn('flex flex-col gap-2', className)}>
|
|
454
|
+
{files.map((file, index) => (
|
|
455
|
+
<DropZoneFile
|
|
456
|
+
key={`${file.name}-${file.lastModified}`}
|
|
457
|
+
file={file}
|
|
458
|
+
onRemove={() => onRemove(index)}
|
|
459
|
+
disabled={disabled}
|
|
460
|
+
/>
|
|
461
|
+
))}
|
|
462
|
+
</div>
|
|
463
|
+
)
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
export {
|
|
467
|
+
DropZone,
|
|
468
|
+
DropZoneArea,
|
|
469
|
+
DropZoneInput,
|
|
470
|
+
DropZoneContent,
|
|
471
|
+
DropZoneFile,
|
|
472
|
+
DropZoneFileList,
|
|
473
|
+
useDropZoneContext,
|
|
474
|
+
formatFileSize,
|
|
475
|
+
getAcceptDescription,
|
|
476
|
+
getRejectionMessage,
|
|
477
|
+
DEFAULT_MAX_SIZE,
|
|
478
|
+
DEFAULT_ACCEPT,
|
|
479
|
+
ACCEPT_PRESETS,
|
|
480
|
+
}
|
|
481
|
+
export type { DropZoneProps, DropZoneFileProps, Accept, FileRejection }
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { cva, type VariantProps } from 'class-variance-authority'
|
|
2
|
+
|
|
3
|
+
import { cn } from '../lib/utils'
|
|
4
|
+
|
|
5
|
+
function Empty({ className, ...props }: React.ComponentProps<'div'>) {
|
|
6
|
+
return (
|
|
7
|
+
<div
|
|
8
|
+
data-slot="empty"
|
|
9
|
+
className={cn(
|
|
10
|
+
'gap-4 rounded-xl border-dashed p-6 flex w-full min-w-0 flex-1 flex-col items-center justify-center text-center text-balance',
|
|
11
|
+
className,
|
|
12
|
+
)}
|
|
13
|
+
{...props}
|
|
14
|
+
/>
|
|
15
|
+
)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function EmptyHeader({ className, ...props }: React.ComponentProps<'div'>) {
|
|
19
|
+
return (
|
|
20
|
+
<div
|
|
21
|
+
data-slot="empty-header"
|
|
22
|
+
className={cn('gap-1 flex max-w-sm flex-col items-center', className)}
|
|
23
|
+
{...props}
|
|
24
|
+
/>
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const emptyMediaVariants = cva(
|
|
29
|
+
'mb-2 flex shrink-0 items-center justify-center [&_svg]:pointer-events-none [&_svg]:shrink-0',
|
|
30
|
+
{
|
|
31
|
+
variants: {
|
|
32
|
+
variant: {
|
|
33
|
+
default: 'bg-transparent',
|
|
34
|
+
icon: "bg-muted text-foreground flex size-8 shrink-0 items-center justify-center rounded-md [&_svg:not([class*='size-'])]:size-4",
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
defaultVariants: {
|
|
38
|
+
variant: 'default',
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
function EmptyMedia({
|
|
44
|
+
className,
|
|
45
|
+
variant = 'default',
|
|
46
|
+
...props
|
|
47
|
+
}: React.ComponentProps<'div'> & VariantProps<typeof emptyMediaVariants>) {
|
|
48
|
+
return (
|
|
49
|
+
<div
|
|
50
|
+
data-slot="empty-icon"
|
|
51
|
+
data-variant={variant}
|
|
52
|
+
className={cn(emptyMediaVariants({ variant, className }))}
|
|
53
|
+
{...props}
|
|
54
|
+
/>
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function EmptyTitle({ className, ...props }: React.ComponentProps<'div'>) {
|
|
59
|
+
return (
|
|
60
|
+
<div
|
|
61
|
+
data-slot="empty-title"
|
|
62
|
+
className={cn('text-sm font-medium tracking-tight', className)}
|
|
63
|
+
{...props}
|
|
64
|
+
/>
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function EmptyDescription({ className, ...props }: React.ComponentProps<'p'>) {
|
|
69
|
+
return (
|
|
70
|
+
<div
|
|
71
|
+
data-slot="empty-description"
|
|
72
|
+
className={cn(
|
|
73
|
+
'text-xs/relaxed text-muted-foreground [&>a:hover]:text-primary [&>a]:underline [&>a]:underline-offset-4',
|
|
74
|
+
className,
|
|
75
|
+
)}
|
|
76
|
+
{...props}
|
|
77
|
+
/>
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function EmptyContent({ className, ...props }: React.ComponentProps<'div'>) {
|
|
82
|
+
return (
|
|
83
|
+
<div
|
|
84
|
+
data-slot="empty-content"
|
|
85
|
+
className={cn(
|
|
86
|
+
'gap-2 text-xs/relaxed flex w-full max-w-sm min-w-0 flex-col items-center text-balance',
|
|
87
|
+
className,
|
|
88
|
+
)}
|
|
89
|
+
{...props}
|
|
90
|
+
/>
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export { Empty, EmptyHeader, EmptyTitle, EmptyDescription, EmptyContent, EmptyMedia }
|