@stacksjs/components 0.2.931 → 0.2.933

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.
@@ -0,0 +1,4 @@
1
+ import type { DefineComponent } from '@stacksjs/stx';
2
+ declare const __dtsx_component__: __DtsxStxComponentType;
3
+ declare type __DtsxStxComponentType = DefineComponent<{}> & { readonly __stxEmits?: ["pick", "clear"] }
4
+ export default __dtsx_component__;
@@ -0,0 +1,18 @@
1
+ export declare interface FileDropProps {
2
+ file?: File | Blob | null
3
+ accept?: string
4
+ disabled?: boolean
5
+ stage?: string
6
+ stageLabel?: string
7
+ progress?: number
8
+ label?: string
9
+ activeLabel?: string
10
+ hint?: string
11
+ chooseLabel?: string
12
+ removeLabel?: string
13
+ previewLabel?: string
14
+ hidePreviewLabel?: string
15
+ progressLabel?: string
16
+ testId?: string
17
+ }
18
+ export { default as FileDrop } from './FileDrop';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/components",
3
3
  "type": "module",
4
- "version": "0.2.931",
4
+ "version": "0.2.933",
5
5
  "description": "A modern component library built with STX, featuring headwind utility classes and TypeScript syntax highlighting.",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",
7
7
  "license": "MIT",
@@ -144,8 +144,8 @@
144
144
  "dependencies": {
145
145
  "@iconify-json/f7": "^1.2.2",
146
146
  "@stacksjs/ts-css": "^0.3.11",
147
- "@stacksjs/stx": "0.2.931",
148
- "bun-plugin-stx": "0.2.931",
147
+ "@stacksjs/stx": "0.2.933",
148
+ "bun-plugin-stx": "0.2.933",
149
149
  "ts-syntax-highlighter": "^0.2.20",
150
150
  "ts-video-player": "^0.1.11"
151
151
  },
package/src/index.ts CHANGED
@@ -41,6 +41,8 @@ export * from './ui/dialog'
41
41
  export * from './ui/drawer'
42
42
  export * from './ui/dropdown'
43
43
  export * from './ui/form'
44
+ // Pick a file by dropping or choosing it, see it, and watch it go.
45
+ export * from './ui/file-drop'
44
46
  export * from './ui/graph-view'
45
47
  export * from './ui/grouped-list'
46
48
  export * from './ui/image'
@@ -0,0 +1,120 @@
1
+ <script client>
2
+ /**
3
+ * Where a file is picked on a web page: drop one on it, or press it to choose.
4
+ *
5
+ * Once picked it shows the file: a frame of a video or the picture itself, its
6
+ * name and size, a Preview that plays a video before it is sent, and Remove.
7
+ * While the file is on its way it shows one bar and what is happening, in the
8
+ * words the page passes (`stage` and `stageLabel`): a video being made smaller
9
+ * on the device, then uploaded, then stored, say.
10
+ *
11
+ * It only picks and shows. The page owns the file and the upload:
12
+ * `@pick` hands it the file, `@clear` asks it to drop it, and it tells this the
13
+ * stage and how far through it is. Pair it with `prepareVideoUpload`
14
+ * (`@stacksjs/video/browser`) and `sendUpload` (`@stacksjs/browser`).
15
+ */
16
+ const file = useReactiveProp('file', null)
17
+ const accept = useReactiveProp('accept', 'video/mp4,video/quicktime,video/webm,.mp4,.mov,.m4v,.webm')
18
+ const disabled = useReactiveProp('disabled', false)
19
+ /** '' when nothing is on its way; any other value shows the bar. */
20
+ const stage = useReactiveProp('stage', '')
21
+ /** What `stage` is, in the page's words ("Uploading… 40%"). */
22
+ const stageLabel = useReactiveProp('stageLabel', '')
23
+ /** 0 to 100 across the whole trip. */
24
+ const progress = useReactiveProp('progress', 0)
25
+ const label = useReactiveProp('label', 'Drop a file here, or choose one')
26
+ const activeLabel = useReactiveProp('activeLabel', 'Drop it to add it')
27
+ const hint = useReactiveProp('hint', '')
28
+ const chooseLabel = useReactiveProp('chooseLabel', 'Choose a file')
29
+ const removeLabel = useReactiveProp('removeLabel', 'Remove')
30
+ const previewLabel = useReactiveProp('previewLabel', 'Preview')
31
+ const hidePreviewLabel = useReactiveProp('hidePreviewLabel', 'Hide preview')
32
+ const progressLabel = useReactiveProp('progressLabel', 'Upload progress')
33
+ const testId = useReactiveProp('testId', 'file-drop')
34
+ const emit = defineEmits(['pick', 'clear'])
35
+
36
+ const over = state(false)
37
+ const previewing = state(false)
38
+ const frame = state('')
39
+ const frameFailed = state(false)
40
+
41
+ const busy = derived(() => !!stage())
42
+ const kind = derived(() => {
43
+ const type = String(file()?.type || '')
44
+ const name = String(file()?.name || '')
45
+ if (type.startsWith('image/') || /\.(?:png|jpe?g|gif|webp|avif|heic)$/i.test(name)) return 'image'
46
+ if (type.startsWith('video/') || /\.(?:mp4|m4v|mov|webm)$/i.test(name)) return 'video'
47
+ return 'file'
48
+ })
49
+ const size = derived(() => {
50
+ const bytes = Number(file()?.size) || 0
51
+ if (!bytes) return ''
52
+ if (bytes < 1024 * 1024) return `${Math.max(1, Math.round(bytes / 1024))} KB`
53
+ const megabytes = bytes / 1024 / 1024
54
+ return `${megabytes.toFixed(megabytes < 10 ? 1 : 0)} MB`
55
+ })
56
+ const bar = derived(() => Math.max(0, Math.min(100, Number(progress()) || 0)))
57
+ const canPreview = derived(() => kind() === 'video' && !!frame() && !frameFailed() && !busy())
58
+
59
+ // The picked file as an address the page can draw from, released when it changes or the drop goes away.
60
+ let url = ''
61
+ watch(file, (next) => {
62
+ if (url) URL.revokeObjectURL(url)
63
+ url = next && typeof Blob !== 'undefined' && next instanceof Blob ? URL.createObjectURL(next) : ''
64
+ frameFailed.set(false)
65
+ previewing.set(false)
66
+ frame.set(url)
67
+ }, { immediate: true })
68
+ onDestroy(() => { if (url) URL.revokeObjectURL(url) })
69
+
70
+ function take(picked) {
71
+ if (!picked || disabled() || busy()) return
72
+ emit('pick', picked)
73
+ }
74
+ function choose(event) {
75
+ const input = event.target
76
+ const picked = input.files && input.files[0] ? input.files[0] : null
77
+ // The same file can be chosen again after it was removed.
78
+ input.value = ''
79
+ take(picked)
80
+ }
81
+ function drop(event) {
82
+ over.set(false)
83
+ const files = event.dataTransfer ? event.dataTransfer.files : null
84
+ take(files && files[0] ? files[0] : null)
85
+ }
86
+ function enter() { if (!disabled() && !busy()) over.set(true) }
87
+ </script>
88
+
89
+ <div class="w-full min-w-0" :data-testid="testId()" data-file-drop>
90
+ <label :if="!file()" @dragover.prevent="enter()" @dragleave="over.set(false)" @drop.prevent="drop($event)"
91
+ class="flex flex-col gap-1 items-center justify-center px-4 py-6 min-h-28 text-center border-2 border-dashed rounded-xl transition-colors cursor-pointer"
92
+ :class="over() ? 'border-accent bg-accent-subtle' : 'border-line hover:border-line-strong'"
93
+ data-file-drop-zone>
94
+ <i aria-hidden="true" class="h-6 w-6 text-fg-muted i-lucide-upload"></i>
95
+ <span class="font-medium text-fg-strong text-sm">{{ over() ? activeLabel() : label() }}</span>
96
+ <span :if="hint()" class="text-fg-muted text-xs">{{ hint() }}</span>
97
+ <input type="file" :accept="accept()" :disabled="disabled()" @change="choose($event)" :aria-label="chooseLabel()" class="sr-only" data-file-drop-input />
98
+ </label>
99
+
100
+ <div :if="file()" class="flex gap-3 items-center p-2 bg-surface-sunken rounded-xl" data-file-drop-picked>
101
+ <span class="flex overflow-hidden items-center justify-center shrink-0 h-16 w-16 bg-panel rounded-lg">
102
+ <video :if="kind() === 'video' && frame() && !frameFailed()" :src="frame() + '#t=0.1'" muted playsinline preload="metadata" @error="frameFailed.set(true)" class="object-cover h-full w-full" aria-hidden="true"></video>
103
+ <img :if="kind() === 'image' && frame() && !frameFailed()" :src="frame()" alt="" @error="frameFailed.set(true)" class="object-cover h-full w-full" />
104
+ <i :if="kind() === 'file' || !frame() || frameFailed()" aria-hidden="true" class="h-6 w-6 text-fg-muted" :class="kind() === 'video' ? 'i-lucide-video' : kind() === 'image' ? 'i-lucide-image' : 'i-lucide-file'"></i>
105
+ </span>
106
+ <span class="flex flex-1 flex-col min-w-0">
107
+ <span class="font-medium text-fg-strong text-sm truncate" data-file-drop-name>{{ file()?.name }}</span>
108
+ <span :if="!busy()" class="text-fg-muted text-xs">{{ size() }}</span>
109
+ <span :if="busy()" class="text-fg text-xs" role="status" data-file-drop-stage>{{ stageLabel() }}</span>
110
+ <span :if="busy()" class="block overflow-hidden mt-1 h-1.5 bg-panel rounded-full" role="progressbar" :aria-label="progressLabel()" aria-valuemin="0" aria-valuemax="100" :aria-valuenow="Math.round(bar())">
111
+ <span class="block h-full bg-accent-solid rounded-full transition-all" :style="{ width: bar() + '%' }"></span>
112
+ </span>
113
+ </span>
114
+ <button :if="canPreview()" type="button" @click="previewing.set(!previewing())" :aria-expanded="previewing()" class="shrink-0 px-2 min-h-11 font-medium text-fg text-xs hover:text-fg-strong rounded-lg" data-file-drop-preview-toggle>{{ previewing() ? hidePreviewLabel() : previewLabel() }}</button>
115
+ <button :if="!busy()" type="button" @click="emit('clear')" :disabled="disabled()" :aria-label="removeLabel()" class="flex items-center justify-center shrink-0 h-11 w-11 text-fg-muted hover:text-fg-strong rounded-lg" data-file-drop-remove>
116
+ <i aria-hidden="true" class="h-4 w-4 i-lucide-x"></i>
117
+ </button>
118
+ </div>
119
+ <video :if="canPreview() && previewing()" :src="frame()" controls playsinline autoplay muted class="block mt-2 max-h-80 w-full bg-black rounded-xl" data-file-drop-preview></video>
120
+ </div>
@@ -0,0 +1,24 @@
1
+ export { default as FileDrop } from './FileDrop.stx'
2
+
3
+ export interface FileDropProps {
4
+ /** The picked file, owned by the page. */
5
+ file?: File | Blob | null
6
+ /** The input's accept list. Default: MP4, MOV and WebM video. */
7
+ accept?: string
8
+ disabled?: boolean
9
+ /** '' when nothing is on its way; anything else shows the bar. */
10
+ stage?: string
11
+ /** What `stage` is, in the page's words. */
12
+ stageLabel?: string
13
+ /** 0 to 100 across the whole trip. */
14
+ progress?: number
15
+ label?: string
16
+ activeLabel?: string
17
+ hint?: string
18
+ chooseLabel?: string
19
+ removeLabel?: string
20
+ previewLabel?: string
21
+ hidePreviewLabel?: string
22
+ progressLabel?: string
23
+ testId?: string
24
+ }