@dorsk/tsumikit 0.2.11 → 0.2.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,24 +1,40 @@
1
1
  <script lang="ts">
2
2
  // Drag-and-drop file area with a click/keyboard fallback to the file dialog.
3
3
  // Emits chosen/dropped files via `onfiles`. Filters by `accept` (extensions
4
- // or mime patterns). Dependency-free; the whole zone is a labelled button so
5
- // it's keyboard-operable, and a visually-hidden <input type=file> backs it.
4
+ // or mime patterns). Dependency-free.
5
+ //
6
+ // Two modes:
7
+ // - Default (box): the whole zone is a labelled button so it's
8
+ // keyboard-operable, backed by a visually-hidden <input type=file>.
9
+ // Children, if passed, render inside the dashed box.
10
+ // - Overlay (`overlay`): wraps interactive content. Children render in
11
+ // normal flow (no box model, no click hijack); the dashed border + label
12
+ // paint as an absolutely-positioned layer only while a file is dragged
13
+ // over. Drop-only — the browse affordance stays on the host (e.g. a
14
+ // FileButton). Use this to make a whole surface/drawer/window a drop
15
+ // target.
6
16
  import type { Snippet } from 'svelte';
7
17
  import Icon from '../atoms/Icon.svelte';
8
18
 
9
19
  let {
10
20
  onfiles,
21
+ onactive,
11
22
  accept,
12
23
  multiple = true,
13
24
  disabled = false,
25
+ overlay = false,
14
26
  label = 'Drop files here',
15
27
  hint = 'or click to browse',
16
28
  children
17
29
  }: {
18
30
  onfiles: (files: File[]) => void;
31
+ /** Fired when the drag-over (active) state changes — host can dim/highlight. */
32
+ onactive?: (active: boolean) => void;
19
33
  accept?: string;
20
34
  multiple?: boolean;
21
35
  disabled?: boolean;
36
+ /** Wrap interactive content; show the drop UI as an overlay only while dragging. */
37
+ overlay?: boolean;
22
38
  label?: string;
23
39
  hint?: string;
24
40
  children?: Snippet;
@@ -27,6 +43,19 @@
27
43
  let over = $state(false);
28
44
  let input = $state<HTMLInputElement | null>(null);
29
45
 
46
+ // Depth counter for robust enter/leave detection — dragenter/dragleave fire
47
+ // for every child element the cursor crosses, so a naive boolean flickers.
48
+ let depth = 0;
49
+
50
+ function setOver(next: boolean) {
51
+ if (over === next) return;
52
+ over = next;
53
+ onactive?.(next);
54
+ }
55
+ function hasFiles(e: DragEvent): boolean {
56
+ return Array.from(e.dataTransfer?.types ?? []).includes('Files');
57
+ }
58
+
30
59
  function accepted(files: File[]): File[] {
31
60
  if (!accept) return files;
32
61
  const pats = accept.split(',').map((s) => s.trim().toLowerCase());
@@ -46,54 +75,91 @@
46
75
  if (!multiple) files = files.slice(0, 1);
47
76
  if (files.length) onfiles(files);
48
77
  }
78
+
79
+ function onDragEnter(e: DragEvent) {
80
+ if (disabled || !hasFiles(e)) return;
81
+ depth++;
82
+ if (depth === 1) setOver(true);
83
+ }
84
+ function onDragLeave(e: DragEvent) {
85
+ if (disabled || !hasFiles(e)) return;
86
+ depth = Math.max(0, depth - 1);
87
+ if (depth === 0) setOver(false);
88
+ }
89
+ function onDragOver(e: DragEvent) {
90
+ if (disabled) return;
91
+ // preventDefault is required or `drop` never fires.
92
+ e.preventDefault();
93
+ if (e.dataTransfer) e.dataTransfer.dropEffect = 'copy';
94
+ }
49
95
  function onDrop(e: DragEvent) {
50
96
  e.preventDefault();
51
- over = false;
97
+ depth = 0;
98
+ setOver(false);
52
99
  if (disabled) return;
53
100
  emit(e.dataTransfer?.files);
54
101
  }
55
102
  </script>
56
103
 
57
- <div
58
- class="dz"
59
- class:over
60
- class:disabled
61
- role="button"
62
- tabindex={disabled ? -1 : 0}
63
- aria-label="{label}. {hint}"
64
- aria-disabled={disabled || undefined}
65
- ondragover={(e) => {
66
- e.preventDefault();
67
- if (!disabled) over = true;
68
- }}
69
- ondragleave={() => (over = false)}
70
- ondrop={onDrop}
71
- onclick={() => !disabled && input?.click()}
72
- onkeydown={(e) => {
73
- if ((e.key === 'Enter' || e.key === ' ') && !disabled) {
74
- e.preventDefault();
75
- input?.click();
76
- }
77
- }}
78
- >
79
- <input
80
- bind:this={input}
81
- class="sr-only"
82
- type="file"
83
- {accept}
84
- {multiple}
85
- {disabled}
86
- tabindex="-1"
87
- onchange={(e) => emit((e.currentTarget as HTMLInputElement).files)}
88
- />
89
- {#if children}
90
- {@render children()}
91
- {:else}
92
- <Icon name="upload" size={28} />
93
- <span class="dz-label">{label}</span>
94
- <span class="dz-hint">{hint}</span>
95
- {/if}
96
- </div>
104
+ {#if overlay}
105
+ <div
106
+ class="dz-host"
107
+ class:over
108
+ class:disabled
109
+ role="presentation"
110
+ ondragenter={onDragEnter}
111
+ ondragleave={onDragLeave}
112
+ ondragover={onDragOver}
113
+ ondrop={onDrop}
114
+ >
115
+ {@render children?.()}
116
+ {#if over}
117
+ <div class="dz-overlay" aria-hidden="true">
118
+ <Icon name="upload" size={28} />
119
+ <span class="dz-label">{label}</span>
120
+ </div>
121
+ {/if}
122
+ </div>
123
+ {:else}
124
+ <div
125
+ class="dz"
126
+ class:over
127
+ class:disabled
128
+ role="button"
129
+ tabindex={disabled ? -1 : 0}
130
+ aria-label="{label}. {hint}"
131
+ aria-disabled={disabled || undefined}
132
+ ondragenter={onDragEnter}
133
+ ondragleave={onDragLeave}
134
+ ondragover={onDragOver}
135
+ ondrop={onDrop}
136
+ onclick={() => !disabled && input?.click()}
137
+ onkeydown={(e) => {
138
+ if ((e.key === 'Enter' || e.key === ' ') && !disabled) {
139
+ e.preventDefault();
140
+ input?.click();
141
+ }
142
+ }}
143
+ >
144
+ <input
145
+ bind:this={input}
146
+ class="sr-only"
147
+ type="file"
148
+ {accept}
149
+ {multiple}
150
+ {disabled}
151
+ tabindex="-1"
152
+ onchange={(e) => emit((e.currentTarget as HTMLInputElement).files)}
153
+ />
154
+ {#if children}
155
+ {@render children()}
156
+ {:else}
157
+ <Icon name="upload" size={28} />
158
+ <span class="dz-label">{label}</span>
159
+ <span class="dz-hint">{hint}</span>
160
+ {/if}
161
+ </div>
162
+ {/if}
97
163
 
98
164
  <style>
99
165
  .dz {
@@ -129,6 +195,37 @@
129
195
  opacity: 0.45;
130
196
  cursor: not-allowed;
131
197
  }
198
+
199
+ /* Overlay mode: don't impose a box model on the host layout. The wrapper is
200
+ a positioning context for the absolute overlay; otherwise it passes
201
+ through (full size, transparent). */
202
+ .dz-host {
203
+ position: relative;
204
+ display: flex;
205
+ flex-direction: column;
206
+ min-height: 0;
207
+ min-width: 0;
208
+ width: 100%;
209
+ height: 100%;
210
+ }
211
+ .dz-overlay {
212
+ position: absolute;
213
+ inset: 0;
214
+ z-index: 10;
215
+ display: flex;
216
+ flex-direction: column;
217
+ align-items: center;
218
+ justify-content: center;
219
+ gap: var(--sp-2);
220
+ text-align: center;
221
+ color: var(--text);
222
+ background: color-mix(in srgb, var(--accent) 12%, transparent);
223
+ border: 2px dashed var(--accent);
224
+ border-radius: var(--r-lg);
225
+ pointer-events: none;
226
+ backdrop-filter: blur(1px);
227
+ }
228
+
132
229
  .dz-label {
133
230
  font-weight: var(--fw-medium);
134
231
  font-size: var(--fs-sm);
@@ -1,9 +1,13 @@
1
1
  import type { Snippet } from 'svelte';
2
2
  type $$ComponentProps = {
3
3
  onfiles: (files: File[]) => void;
4
+ /** Fired when the drag-over (active) state changes — host can dim/highlight. */
5
+ onactive?: (active: boolean) => void;
4
6
  accept?: string;
5
7
  multiple?: boolean;
6
8
  disabled?: boolean;
9
+ /** Wrap interactive content; show the drop UI as an overlay only while dragging. */
10
+ overlay?: boolean;
7
11
  label?: string;
8
12
  hint?: string;
9
13
  children?: Snippet;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dorsk/tsumikit",
3
- "version": "0.2.11",
3
+ "version": "0.2.12",
4
4
  "description": "Minimal, dependency-free Svelte 5 + pure-CSS UI kit. Token-driven atoms, molecules & layouts with theming out of the box.",
5
5
  "type": "module",
6
6
  "license": "MIT",