@molecule/app-file-dropzone-svelte 1.1.1 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,7 +3,7 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
3
3
  Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
4
4
  Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
5
5
  To change this document, edit the module-level JSDoc in src/index.ts.
6
- Generated: 2026-11-16T12:54:36.705Z
6
+ Generated: 2026-11-26T01:31:05.016Z
7
7
  -->
8
8
 
9
9
  # @molecule/app-file-dropzone-svelte
@@ -43,7 +43,7 @@ DOM for the same input.
43
43
  ## Installation
44
44
 
45
45
  ```bash
46
- npm install @molecule/app-file-dropzone-svelte @molecule/app-svelte @molecule/app-ui @molecule/app-ui-svelte svelte
46
+ npm install @molecule/app-file-dropzone-svelte @molecule/app-filesystem @molecule/app-svelte @molecule/app-ui @molecule/app-ui-svelte svelte
47
47
  ```
48
48
 
49
49
  ## API
@@ -89,13 +89,15 @@ const FileDropzone: LegacyComponentType
89
89
 
90
90
  Peer dependencies:
91
91
 
92
- - `@molecule/app-svelte` ^3.5.0
92
+ - `@molecule/app-filesystem` ^1.1.0
93
+ - `@molecule/app-svelte` ^3.13.0
93
94
  - `@molecule/app-ui` ^1.0.1
94
95
  - `@molecule/app-ui-svelte` ^1.1.0
95
96
  - `svelte` ^5.0.0
96
97
 
97
98
  ### Runtime Dependencies
98
99
 
100
+ - `@molecule/app-filesystem`
99
101
  - `@molecule/app-svelte`
100
102
  - `@molecule/app-ui`
101
103
  - `@molecule/app-ui-svelte`
@@ -6,7 +6,8 @@
6
6
  string goes through `t()` so a locale switch re-renders it.
7
7
  -->
8
8
  <script lang="ts">
9
- import { useTranslation } from '@molecule/app-svelte'
9
+ import { createBrowserFileInput } from '@molecule/app-filesystem'
10
+ import { contentNodes, useTranslation } from '@molecule/app-svelte'
10
11
  import { getClassMap } from '@molecule/app-ui'
11
12
 
12
13
  import type { FileDropzoneProps } from './types.js'
@@ -26,7 +27,12 @@
26
27
  const { t } = useTranslation()
27
28
 
28
29
  let dragging = $state(false)
29
- let inputEl: HTMLInputElement | undefined
30
+ let inputEl = $state<HTMLInputElement | undefined>()
31
+ const handlers = createBrowserFileInput(
32
+ () => inputEl,
33
+ () => ({ accept, disabled, maxSize, multiple, onFiles, onRejected }),
34
+ (file) => file,
35
+ )
30
36
 
31
37
  // Every `t(…)` runs inside a `$derived`: it reads the i18n version under the
32
38
  // hood, so this re-runs on a locale switch the way React's render-time `t`
@@ -38,107 +44,56 @@
38
44
  accept ? t('fileDropzone.accepts', { accept }, { defaultValue: 'Accepts: {{accept}}' }) : '',
39
45
  )
40
46
 
41
- /**
42
- * Filters a file list into kept and rejected files, exactly as React does: a
43
- * file over `maxSize` is rejected, then a file matching neither the accept
44
- * extension nor the accept MIME token is rejected (`'*'` accepts everything).
45
- *
46
- * @param raw - The files the user dropped or picked.
47
- * @returns The kept and rejected files.
48
- */
49
- function filter(raw: FileList | File[]): { kept: File[]; rejected: File[] } {
50
- const arr = Array.from(raw)
51
- const kept: File[] = []
52
- const rejected: File[] = []
53
- for (const f of arr) {
54
- if (maxSize && f.size > maxSize) {
55
- rejected.push(f)
56
- continue
57
- }
58
- // cheap accept check
59
- if (accept) {
60
- const okByExt = accept
61
- .split(',')
62
- .some(
63
- (a) =>
64
- a.trim() && f.name.toLowerCase().endsWith(a.trim().toLowerCase().replace(/^\./, '.')),
65
- )
66
- const okByMime = accept
67
- .split(',')
68
- .some(
69
- (a) =>
70
- a.trim() &&
71
- f.type &&
72
- (f.type === a.trim() ||
73
- (a.trim().endsWith('/*') && f.type.startsWith(a.trim().replace('/*', '/')))),
74
- )
75
- if (!okByExt && !okByMime && accept !== '*') {
76
- rejected.push(f)
77
- continue
78
- }
79
- }
80
- kept.push(f)
81
- }
82
- return { kept, rejected }
83
- }
84
-
85
- /**
86
- * Filters the given file list and dispatches kept/rejected files to the
87
- * callbacks — only the first kept file unless `multiple` is set.
88
- *
89
- * @param list - The files from the drop or the hidden input.
90
- */
91
- function dispatch(list: FileList | File[]): void {
92
- const { kept, rejected } = filter(list)
93
- if (kept.length > 0) onFiles(multiple ? kept : [kept[0]!])
94
- if (rejected.length > 0) onRejected?.(rejected)
95
- }
96
-
97
- /** Opens the native file dialog through the hidden input. */
98
- function openDialog(): void {
99
- inputEl?.click()
100
- }
101
-
102
47
  /** The base (not dragging) drop-surface style — React's dashed drop region. */
103
48
  const baseStyle =
104
49
  'border: 2px dashed var(--mol-color-border, rgba(128,128,128,0.35)); border-radius: 12px; min-height: 140px'
105
50
  /** The drag-over style: the border thickens and the fill tints. */
106
51
  const draggingStyle =
107
52
  'border: 2px dashed var(--mol-color-primary, #3366ff); border-radius: 12px; min-height: 140px; background: color-mix(in srgb, var(--mol-color-primary, #3366ff) 6%, transparent)'
53
+
54
+ // React's `children ?? default copy`: a children snippet that renders nothing (an empty list)
55
+ // falls back to the default copy, like an omitted one. A snippet's `{#each}` over no rows
56
+ // changes the DOM without changing the prop, so presence is read from the rendered DOM (the
57
+ // input and the default copy are this component's own nodes, not the children).
58
+ let rootEl = $state<HTMLDivElement | null>(null)
59
+ let dropHereEl = $state<HTMLParagraphElement | null>(null)
60
+ let acceptsEl = $state<HTMLParagraphElement | null>(null)
61
+ let childrenRenders = $state(false)
62
+ $effect(() => {
63
+ const element = rootEl
64
+ if (!element) return
65
+ const own = new Set<Node | null>([inputEl ?? null, dropHereEl, acceptsEl])
66
+ const sync = (): void => {
67
+ const present = contentNodes(element).some(
68
+ (node) => !own.has(node) && (node.nodeType === 1 || (node.textContent ?? '').trim() !== ''),
69
+ )
70
+ if (present !== childrenRenders) childrenRenders = present
71
+ }
72
+ sync()
73
+ const observer = new MutationObserver(sync)
74
+ observer.observe(element, { childList: true, characterData: true, subtree: true })
75
+ return () => observer.disconnect()
76
+ })
108
77
  </script>
109
78
 
110
79
  <div
111
80
  ondragover={(event) => {
112
- if (!disabled) {
113
- event.preventDefault()
114
- dragging = true
115
- }
81
+ if (handlers.onDragOver(event)) dragging = true
116
82
  }}
117
83
  ondragleave={() => {
118
84
  dragging = false
119
85
  }}
120
86
  ondrop={(event) => {
121
- event.preventDefault()
122
87
  dragging = false
123
- if (!disabled && event.dataTransfer?.files) dispatch(event.dataTransfer.files)
124
- }}
125
- onclick={() => {
126
- if (!disabled) openDialog()
127
- }}
128
- onkeydown={(event) => {
129
- // role="button" gets no native keyboard activation — wire Enter/Space to
130
- // open the file dialog so keyboard users can browse (preventDefault stops
131
- // Space from scrolling the page).
132
- if (disabled) return
133
- if (event.key === 'Enter' || event.key === ' ' || event.key === 'Spacebar') {
134
- event.preventDefault()
135
- openDialog()
136
- }
88
+ handlers.onDrop(event)
137
89
  }}
90
+ onclick={handlers.onClick}
91
+ onkeydown={handlers.onKeyDown}
138
92
  data-mol-id="file-dropzone"
139
93
  role="button"
140
94
  tabindex="0"
141
95
  aria-disabled={disabled}
96
+ bind:this={rootEl}
142
97
  class={cm.cn(
143
98
  cm.flex({ direction: 'col', align: 'center', justify: 'center' }),
144
99
  cm.sp('p', 6),
@@ -154,18 +109,18 @@
154
109
  {accept}
155
110
  {multiple}
156
111
  {disabled}
157
- onchange={(event) => {
158
- const files = event.currentTarget.files
159
- if (files) dispatch(files)
160
- }}
112
+ onchange={handlers.onChange}
161
113
  style="display: none"
162
114
  />
163
115
  {#if children}
164
116
  {@render children?.()}
165
- {:else}
166
- <p class={cm.cn(cm.textSize('sm'), cm.fontWeight('semibold'))}>{dropHere}</p>
117
+ {/if}
118
+ {#if !childrenRenders}
119
+ <p bind:this={dropHereEl} class={cm.cn(cm.textSize('sm'), cm.fontWeight('semibold'))}>
120
+ {dropHere}
121
+ </p>
167
122
  {#if accept}
168
- <p class={cm.cn(cm.textSize('xs'), cm.sp('mt', 1))}>{acceptsLabel}</p>
123
+ <p bind:this={acceptsEl} class={cm.cn(cm.textSize('xs'), cm.sp('mt', 1))}>{acceptsLabel}</p>
169
124
  {/if}
170
125
  {/if}
171
126
  </div>
@@ -1 +1 @@
1
- {"version":3,"file":"FileDropzone.svelte.d.ts","sourceRoot":"","sources":["../src/FileDropzone.svelte"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAsJpD,QAAA,MAAM,YAAY,uDAAwC,CAAC;AAC3D,KAAK,YAAY,GAAG,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC;AACpD,eAAe,YAAY,CAAC"}
1
+ {"version":3,"file":"FileDropzone.svelte.d.ts","sourceRoot":"","sources":["../src/FileDropzone.svelte"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAwGpD,QAAA,MAAM,YAAY,uDAAwC,CAAC;AAC3D,KAAK,YAAY,GAAG,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC;AACpD,eAAe,YAAY,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@molecule/app-file-dropzone-svelte",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "Drag-drop file upload area with click-to-browse, accept/size filtering, and a content slot",
5
5
  "homepage": "https://www.molecule.dev/packages/app-file-dropzone-svelte",
6
6
  "type": "module",
@@ -48,18 +48,20 @@
48
48
  "access": "public"
49
49
  },
50
50
  "peerDependencies": {
51
- "@molecule/app-svelte": "^3.5.0",
51
+ "@molecule/app-filesystem": "^1.1.0",
52
+ "@molecule/app-svelte": "^3.13.0",
52
53
  "@molecule/app-ui": "^1.0.1",
53
54
  "@molecule/app-ui-svelte": "^1.1.0",
54
55
  "svelte": "^5.0.0"
55
56
  },
56
57
  "devDependencies": {
58
+ "@molecule/app-filesystem": "1.1.0",
57
59
  "@molecule/app-framework-conformance": "0.0.0",
58
- "@molecule/app-i18n": "1.2.2",
60
+ "@molecule/app-i18n": "1.3.0",
59
61
  "@molecule/app-icons": "1.0.4",
60
- "@molecule/app-svelte": "3.13.0",
61
- "@molecule/app-ui": "1.5.0",
62
- "@molecule/app-ui-svelte": "1.4.1",
62
+ "@molecule/app-svelte": "3.16.0",
63
+ "@molecule/app-ui": "1.13.0",
64
+ "@molecule/app-ui-svelte": "1.9.0",
63
65
  "@sveltejs/package": "3.0.2",
64
66
  "@sveltejs/vite-plugin-svelte": "7.1.2",
65
67
  "@types/node": "26.6.5",