@dorsk/tsumikit 0.2.10 → 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.
@@ -5,6 +5,11 @@
5
5
  // switches to the monospace family. `autoresize` opts into the grow-with-
6
6
  // content action without the call-site wiring `use:` itself. `bind:el`
7
7
  // exposes the underlying element for focus/measure.
8
+ //
9
+ // `resize` replaces the native (un-themeable) resize grip with our own drag
10
+ // handle on the top or bottom edge, styled like the Modal/AppShell grips: a
11
+ // centered pill that's a thicker portion of the border. `autoresize` wins —
12
+ // content-driven sizing leaves no manual handle.
8
13
  import type { HTMLTextareaAttributes } from 'svelte/elements';
9
14
  import { autoresize as autoresizeAction } from '../../autoresize';
10
15
 
@@ -12,6 +17,9 @@
12
17
  mono?: boolean;
13
18
  autoresize?: boolean;
14
19
  size?: 'sm' | 'md';
20
+ /** Manual resize handle edge, or 'none' to disable. Ignored when
21
+ * `autoresize` is set. Defaults to a bottom handle. */
22
+ resize?: 'none' | 'top' | 'bottom';
15
23
  /** Error state: danger border + aria-invalid (also styles if a consumer
16
24
  * sets aria-invalid directly). */
17
25
  invalid?: boolean;
@@ -24,38 +32,100 @@
24
32
  mono = false,
25
33
  autoresize = false,
26
34
  size = 'md',
35
+ resize = 'bottom',
27
36
  invalid = false,
28
37
  class: klass = '',
29
38
  value = $bindable(),
30
39
  el = $bindable(null),
31
40
  ...rest
32
41
  }: Props = $props();
42
+
43
+ const showHandle = $derived(!autoresize && resize !== 'none');
44
+
45
+ // --- manual resize drag (mirrors AppShell/Modal: rAF-throttled pointer drag) ---
46
+ let dragging = $state(false);
47
+ let rafId = 0;
48
+ let startY = 0;
49
+ let startH = 0;
50
+ let lastY = 0;
51
+
52
+ function startDrag(e: PointerEvent) {
53
+ if (!el) return;
54
+ dragging = true;
55
+ startY = lastY = e.clientY;
56
+ startH = el.offsetHeight;
57
+ (e.currentTarget as HTMLElement).setPointerCapture(e.pointerId);
58
+ e.preventDefault();
59
+ }
60
+ function onDrag(e: PointerEvent) {
61
+ if (!dragging || !el) return;
62
+ lastY = e.clientY;
63
+ if (rafId) return;
64
+ rafId = requestAnimationFrame(() => {
65
+ rafId = 0;
66
+ if (!el) return;
67
+ // Top handle grows upward (drag up = taller), bottom grows downward.
68
+ const delta = resize === 'top' ? startY - lastY : lastY - startY;
69
+ el.style.height = `${Math.max(0, startH + delta)}px`;
70
+ });
71
+ }
72
+ function endDrag(e: PointerEvent) {
73
+ if (!dragging) return;
74
+ dragging = false;
75
+ if (rafId) {
76
+ cancelAnimationFrame(rafId);
77
+ rafId = 0;
78
+ }
79
+ try {
80
+ (e.currentTarget as HTMLElement).releasePointerCapture(e.pointerId);
81
+ } catch {
82
+ /* already released */
83
+ }
84
+ }
33
85
  </script>
34
86
 
35
- {#if autoresize}
36
- <textarea
37
- bind:this={el}
38
- class="textarea {klass}"
39
- class:mono
40
- class:textarea-sm={size === 'sm'}
41
- bind:value
42
- use:autoresizeAction={typeof value === 'string' ? value : ''}
43
- {...rest}
44
- aria-invalid={invalid || undefined}
45
- ></textarea>
46
- {:else}
47
- <textarea
48
- bind:this={el}
49
- class="textarea {klass}"
50
- class:mono
51
- class:textarea-sm={size === 'sm'}
52
- bind:value
53
- {...rest}
54
- aria-invalid={invalid || undefined}
55
- ></textarea>
56
- {/if}
87
+ <div class="textarea-wrap" class:dragging>
88
+ {#if autoresize}
89
+ <textarea
90
+ bind:this={el}
91
+ class="textarea {klass}"
92
+ class:mono
93
+ class:textarea-sm={size === 'sm'}
94
+ bind:value
95
+ use:autoresizeAction={typeof value === 'string' ? value : ''}
96
+ {...rest}
97
+ aria-invalid={invalid || undefined}
98
+ ></textarea>
99
+ {:else}
100
+ <textarea
101
+ bind:this={el}
102
+ class="textarea {klass}"
103
+ class:mono
104
+ class:textarea-sm={size === 'sm'}
105
+ bind:value
106
+ {...rest}
107
+ aria-invalid={invalid || undefined}
108
+ ></textarea>
109
+ {/if}
110
+ {#if showHandle}
111
+ <div
112
+ class="resize-handle resize-{resize}"
113
+ onpointerdown={startDrag}
114
+ onpointermove={onDrag}
115
+ onpointerup={endDrag}
116
+ onpointercancel={endDrag}
117
+ role="separator"
118
+ aria-orientation="horizontal"
119
+ aria-label="Resize"
120
+ ></div>
121
+ {/if}
122
+ </div>
57
123
 
58
124
  <style>
125
+ .textarea-wrap {
126
+ position: relative;
127
+ display: flex;
128
+ }
59
129
  .textarea {
60
130
  width: 100%;
61
131
  padding: var(--sp-3);
@@ -64,8 +134,11 @@
64
134
  border-radius: var(--r-md);
65
135
  color: var(--text);
66
136
  transition: border-color 0.12s var(--ease);
67
- resize: vertical;
68
- min-height: 5rem;
137
+ /* Custom handle replaces the native grip; never show the native one. */
138
+ resize: none;
139
+ /* Match the single-row height of Button/Input so a `rows={1}` textarea
140
+ lines up with them; the native `rows` attribute grows it from here. */
141
+ min-height: 2.5rem;
69
142
  font-family: inherit;
70
143
  }
71
144
  .textarea:focus {
@@ -75,7 +148,7 @@
75
148
  .textarea-sm {
76
149
  padding: var(--sp-2);
77
150
  font-size: var(--fs-sm);
78
- min-height: 4rem;
151
+ min-height: 2rem;
79
152
  }
80
153
  .textarea[aria-invalid='true'],
81
154
  .textarea[aria-invalid='true']:focus {
@@ -84,4 +157,43 @@
84
157
  .mono {
85
158
  font-family: var(--font-mono);
86
159
  }
160
+
161
+ /* Drag handle: a thin full-width strip on an edge, with a centered pill grip
162
+ (a thicker portion of the border) that brightens to the accent on hover /
163
+ while dragging — mirroring the Modal and AppShell resize grips. */
164
+ .resize-handle {
165
+ position: absolute;
166
+ left: 0;
167
+ right: 0;
168
+ height: 12px;
169
+ cursor: ns-resize;
170
+ touch-action: none;
171
+ }
172
+ .resize-bottom {
173
+ bottom: 0;
174
+ }
175
+ .resize-top {
176
+ top: 0;
177
+ }
178
+ .resize-handle::after {
179
+ content: '';
180
+ position: absolute;
181
+ left: 50%;
182
+ transform: translateX(-50%);
183
+ width: 28px;
184
+ height: 3px;
185
+ border-radius: 999px;
186
+ background: var(--border-strong);
187
+ transition: background 0.12s var(--ease);
188
+ }
189
+ .resize-bottom::after {
190
+ bottom: 3px;
191
+ }
192
+ .resize-top::after {
193
+ top: 3px;
194
+ }
195
+ .resize-handle:hover::after,
196
+ .dragging .resize-handle::after {
197
+ background: var(--accent);
198
+ }
87
199
  </style>
@@ -3,6 +3,9 @@ type Props = HTMLTextareaAttributes & {
3
3
  mono?: boolean;
4
4
  autoresize?: boolean;
5
5
  size?: 'sm' | 'md';
6
+ /** Manual resize handle edge, or 'none' to disable. Ignored when
7
+ * `autoresize` is set. Defaults to a bottom handle. */
8
+ resize?: 'none' | 'top' | 'bottom';
6
9
  /** Error state: danger border + aria-invalid (also styles if a consumer
7
10
  * sets aria-invalid directly). */
8
11
  invalid?: boolean;
@@ -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;
@@ -98,10 +98,11 @@
98
98
  padding: var(--sp-2);
99
99
  aspect-ratio: 1;
100
100
  }
101
- /* Emoji glyph tracks the text size like the SVG Icon does (1em). */
101
+ /* Emoji glyph is bumped above the text size at 1em a paperclip is hard to
102
+ read, so render it a touch larger for legibility. */
102
103
  .emoji {
103
104
  display: inline-flex;
104
- font-size: 1em;
105
+ font-size: 1.35em;
105
106
  line-height: 1;
106
107
  }
107
108
  .file-btn.primary {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dorsk/tsumikit",
3
- "version": "0.2.10",
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",