svelte-multistyle-ui 0.0.13-alpha → 0.0.14-alpha

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,488 +1,493 @@
1
- <script>
2
- import "./datepicker-styles.css";
3
- import { defaults, iconClass } from "../config.js";
4
- import { portal } from "../actions/portal.js";
5
- import { cn } from "../utils/cn.js";
6
- import dayjs from "dayjs";
7
- import customParseFormat from "dayjs/plugin/customParseFormat";
8
- dayjs.extend(customParseFormat);
9
-
10
- let {
11
- style: styleProp,
12
- theme: themeProp,
13
- value = $bindable(""),
14
- format = "YYYY-MM-DD",
15
- displayFormat = undefined,
16
- label = "",
17
- placeholder = "",
18
- disabled = false,
19
- min = undefined,
20
- max = undefined,
21
- locale = "en-US",
22
- class: className = "",
23
- ...rest
24
- } = $props();
25
-
26
- const style = $derived(styleProp ?? defaults.style);
27
- const theme = $derived(themeProp ?? defaults.theme);
28
- const styleClass = $derived(`s-datepicker-${style}`);
29
- const themeClass = $derived(`theme-${theme}`);
30
- const uid = Math.random().toString(36).slice(2, 9);
31
- const internalId = `dp-${uid}`;
32
- const dispFormat = $derived(displayFormat ?? format);
33
-
34
- // State
35
- let open = $state(false);
36
- let view = $state("date");
37
- let viewDate = $state(new Date());
38
- let selectedDate = $state(null);
39
- let isMobile = $state(false);
40
-
41
- // Format helpers — uses dayjs for full format support (MMM, MMMM, etc.)
42
- function toDayjsFmt(f) {
43
- return f.replace(/hh/g, "HH");
44
- }
45
-
46
- function fmt(date, f) {
47
- return dayjs(date).format(toDayjsFmt(f));
48
- }
49
-
50
- function parseDate(str, f) {
51
- const d = dayjs(str, toDayjsFmt(f));
52
- return d.isValid() ? d.toDate() : null;
53
- }
54
-
55
- // Derived
56
- const displayValue = $derived(
57
- value ? fmt(parseDate(value, format) || new Date(), dispFormat) : "",
58
- );
59
- const inputPlaceholder = $derived(placeholder || dispFormat.toLowerCase());
60
-
61
- // Floating-label support — mirrors Input.svelte so the label sits inside the
62
- // border (floats up on focus/value) for outlined styles, and renders no
63
- // in-field label for the non-floating styles, exactly like Input.
64
- const floatingStyles = [
65
- "material",
66
- "material3",
67
- "fluent",
68
- "carbon",
69
- "bootstrap",
70
- ];
71
- const hasFloatingLabel = $derived(floatingStyles.includes(style) && !!label);
72
- const hasValue = $derived(!!value);
73
- const floated = $derived(open || hasValue);
74
-
75
- const minDate = $derived(min ? parseDate(min, format) : null);
76
- const maxDate = $derived(max ? parseDate(max, format) : null);
77
-
78
- const viewYear = $derived(viewDate.getFullYear());
79
- const viewMonth = $derived(viewDate.getMonth());
80
- const decadeStart = $derived(Math.floor(viewYear / 12) * 12);
81
- const decadeEnd = $derived(decadeStart + 11);
82
-
83
- const months = $derived(
84
- Array.from({ length: 12 }, (_, i) =>
85
- new Date(2000, i, 1).toLocaleString(locale, { month: "short" }),
86
- ),
87
- );
88
- const weekdays = $derived(
89
- Array.from({ length: 7 }, (_, i) =>
90
- new Date(2000, 0, i + 2).toLocaleString(locale, { weekday: "short" }),
91
- ),
92
- );
93
-
94
- function calendarDays(year, month) {
95
- const fd = new Date(year, month, 1).getDay();
96
- const dim = new Date(year, month + 1, 0).getDate();
97
- const pmd = new Date(year, month, 0).getDate();
98
- const days = [];
99
- for (let i = fd - 1; i >= 0; i--) days.push({ d: pmd - i, o: true });
100
- for (let i = 1; i <= dim; i++) days.push({ d: i, o: false });
101
- let n = 1;
102
- while (days.length < 42) {
103
- days.push({ d: n++, o: true });
104
- }
105
- return days;
106
- }
107
-
108
- const calDays = $derived(calendarDays(viewYear, viewMonth));
109
- const yearList = $derived(
110
- Array.from({ length: 12 }, (_, i) => decadeStart + i),
111
- );
112
-
113
- function isToday(d, o) {
114
- if (o) return false;
115
- const t = new Date();
116
- return (
117
- d === t.getDate() &&
118
- viewMonth === t.getMonth() &&
119
- viewYear === t.getFullYear()
120
- );
121
- }
122
-
123
- function isDisabled(d, o) {
124
- if (o) return true;
125
- const dt = new Date(viewYear, viewMonth, d);
126
- const day = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate());
127
- const minDay = minDate
128
- ? new Date(minDate.getFullYear(), minDate.getMonth(), minDate.getDate())
129
- : null;
130
- const maxDay = maxDate
131
- ? new Date(
132
- maxDate.getFullYear(),
133
- maxDate.getMonth(),
134
- maxDate.getDate() + 1,
135
- )
136
- : null;
137
- if (minDay && day < minDay) return true;
138
- if (maxDay && day >= maxDay) return true;
139
- return false;
140
- }
141
-
142
- function isSelected(d, o) {
143
- if (!selectedDate || o) return false;
144
- return (
145
- d === selectedDate.getDate() &&
146
- viewMonth === selectedDate.getMonth() &&
147
- viewYear === selectedDate.getFullYear()
148
- );
149
- }
150
-
151
- // Navigation
152
- function prev() {
153
- if (view === "date") viewDate = new Date(viewYear, viewMonth - 1, 1);
154
- else if (view === "month") viewDate = new Date(viewYear - 1, viewMonth, 1);
155
- else viewDate = new Date(viewYear - 12, viewMonth, 1);
156
- }
157
-
158
- function next() {
159
- if (view === "date") viewDate = new Date(viewYear, viewMonth + 1, 1);
160
- else if (view === "month") viewDate = new Date(viewYear + 1, viewMonth, 1);
161
- else viewDate = new Date(viewYear + 12, viewMonth, 1);
162
- }
163
-
164
- function selectDay(d, o) {
165
- if (o) return;
166
- const dt = new Date(viewYear, viewMonth, d);
167
- if (selectedDate) {
168
- dt.setHours(
169
- selectedDate.getHours(),
170
- selectedDate.getMinutes(),
171
- selectedDate.getSeconds(),
172
- );
173
- }
174
- selectedDate = dt;
175
- value = fmt(dt, format);
176
- open = false;
177
- }
178
-
179
- function selectMonth(m) {
180
- viewDate = new Date(viewYear, m, 1);
181
- view = "date";
182
- }
183
-
184
- function selectYear(y) {
185
- viewDate = new Date(y, viewMonth, 1);
186
- view = "date";
187
- }
188
-
189
- function checkMobile() {
190
- isMobile = window.innerWidth < 640;
191
- }
192
-
193
- // Click outside
194
- let wrapperEl;
195
- let overlayEl;
196
-
197
- function docClick(e) {
198
- // overlay is portaled to <body>, so it is no longer inside wrapperEl —
199
- // treat it as part of the picker for outside-click purposes.
200
- if (open && wrapperEl && !wrapperEl.contains(e.target) &&
201
- !(overlayEl && overlayEl.contains(e.target))) {
202
- open = false;
203
- }
204
- }
205
-
206
- // Position the portaled overlay (fixed) anchored to the input. On mobile it
207
- // is a fullscreen sheet driven by CSS, so anchoring is skipped.
208
- function positionOverlay() {
209
- if (!overlayEl || !wrapperEl || isMobile) return;
210
- const rect = wrapperEl.getBoundingClientRect();
211
- const ovW = overlayEl.offsetWidth || 320;
212
- const ovH = overlayEl.offsetHeight || 360;
213
- let left = rect.left;
214
- const maxLeft = window.innerWidth - ovW - 8;
215
- if (left > maxLeft) left = maxLeft;
216
- if (left < 8) left = 8;
217
- let top = rect.bottom + 4;
218
- // Flip above the field if it would overflow the bottom of the viewport.
219
- if (top + ovH > window.innerHeight - 8 && rect.top - ovH - 4 > 8) {
220
- top = rect.top - ovH - 4;
221
- }
222
- overlayEl.style.position = "fixed";
223
- overlayEl.style.left = `${left}px`;
224
- overlayEl.style.top = `${top}px`;
225
- overlayEl.style.zIndex = "10000";
226
- }
227
-
228
- $effect(() => {
229
- if (!open) return;
230
- document.addEventListener("mousedown", docClick);
231
- document.addEventListener("touchstart", docClick);
232
- checkMobile();
233
- // Re-anchor on any scroll (capture: catches scroll inside ancestor cards)
234
- // and on resize; reposition once the portaled overlay is mounted.
235
- const reposition = () => positionOverlay();
236
- window.addEventListener("scroll", reposition, true);
237
- window.addEventListener("resize", reposition);
238
- queueMicrotask(reposition);
239
- return () => {
240
- document.removeEventListener("mousedown", docClick);
241
- document.removeEventListener("touchstart", docClick);
242
- window.removeEventListener("scroll", reposition, true);
243
- window.removeEventListener("resize", reposition);
244
- };
245
- });
246
-
247
- // Init from value
248
- $effect(() => {
249
- if (value) {
250
- const d = parseDate(value, format);
251
- if (d) {
252
- selectedDate = d;
253
- viewDate = new Date(d.getFullYear(), d.getMonth(), 1);
254
- }
255
- }
256
- });
257
-
258
- // Time handlers
259
- function setHour(h) {
260
- if (!selectedDate) selectedDate = new Date();
261
- selectedDate = new Date(selectedDate);
262
- selectedDate.setHours(Math.min(23, Math.max(0, h)));
263
- value = fmt(selectedDate, format);
264
- }
265
-
266
- function setMinute(m) {
267
- if (!selectedDate) selectedDate = new Date();
268
- selectedDate = new Date(selectedDate);
269
- selectedDate.setMinutes(Math.min(59, Math.max(0, m)));
270
- value = fmt(selectedDate, format);
271
- }
272
-
273
- function setSecond(s) {
274
- if (!selectedDate) selectedDate = new Date();
275
- selectedDate = new Date(selectedDate);
276
- selectedDate.setSeconds(Math.min(59, Math.max(0, s)));
277
- value = fmt(selectedDate, format);
278
- }
279
-
280
- const timeEnabled = $derived(/hh|mm|ss/.test(format));
281
- const showHours = $derived(timeEnabled && /hh/.test(format));
282
- const showMinutes = $derived(timeEnabled && /mm/.test(format));
283
- const showSeconds = $derived(timeEnabled && /ss/.test(format));
284
-
285
- const currentHour = $derived(selectedDate ? selectedDate.getHours() : 0);
286
- const currentMinute = $derived(selectedDate ? selectedDate.getMinutes() : 0);
287
- const currentSecond = $derived(selectedDate ? selectedDate.getSeconds() : 0);
288
- </script>
289
-
290
- <div
291
- class={cn("s-datepicker-wrapper", styleClass, themeClass, className)}
292
- class:s-datepicker-open={open}
293
- class:focused={open}
294
- class:floated
295
- class:has-value={hasValue}
296
- class:disabled
297
- bind:this={wrapperEl}
298
- {...rest}
299
- >
300
- {#if hasFloatingLabel}
301
- <label class="s-dp-floating-label" for={internalId}>{label}</label>
302
- {/if}
303
- <input
304
- id={internalId}
305
- type="text"
306
- readonly
307
- value={displayValue}
308
- placeholder={hasFloatingLabel ? "" : inputPlaceholder}
309
- {disabled}
310
- onfocus={() => {
311
- if (!disabled) open = true;
312
- }}
313
- onclick={() => {
314
- if (!disabled) open = true;
315
- }}
316
- role="combobox"
317
- aria-expanded={open}
318
- aria-haspopup="dialog"
319
- />
320
- <span class="s-datepicker-trigger-icon {iconClass}" aria-hidden="true"
321
- >calendar_today</span
322
- >
323
-
324
- {#if open}
325
- <div
326
- class="s-datepicker-overlay {styleClass} {themeClass}"
327
- class:s-datepicker-mobile={isMobile}
328
- bind:this={overlayEl}
329
- use:portal
330
- role="dialog"
331
- aria-modal="true"
332
- aria-label="Date picker"
333
- >
334
- <!-- Header -->
335
- <div class="s-dp-header">
336
- <button
337
- type="button"
338
- class="s-dp-nav-btn"
339
- onclick={prev}
340
- aria-label="Previous">&lsaquo;</button
341
- >
342
- <div class="s-dp-header-labels">
343
- {#if view === "date"}
344
- <button
345
- type="button"
346
- class="s-dp-header-label"
347
- onclick={() => (view = "month")}
348
- >
349
- {new Date(2000, viewMonth, 1).toLocaleString(locale, {
350
- month: "long",
351
- })}
352
- </button>
353
- <button
354
- type="button"
355
- class="s-dp-header-label"
356
- onclick={() => (view = "year")}
357
- >
358
- {viewYear}
359
- </button>
360
- {:else if view === "month"}
361
- <button
362
- type="button"
363
- class="s-dp-header-label s-dp-header-label-active"
364
- onclick={() => (view = "year")}
365
- >
366
- {viewYear}
367
- </button>
368
- {:else}
369
- <span class="s-dp-header-label s-dp-header-label-active"
370
- >{decadeStart} &ndash; {decadeEnd}</span
371
- >
372
- {/if}
373
- </div>
374
- <button
375
- type="button"
376
- class="s-dp-nav-btn"
377
- onclick={next}
378
- aria-label="Next">&rsaquo;</button
379
- >
380
- </div>
381
-
382
- <!-- Date view -->
383
- {#if view === "date"}
384
- <div class="s-dp-weekdays">
385
- {#each weekdays as wd}
386
- <span class="s-dp-weekday">{wd}</span>
387
- {/each}
388
- </div>
389
- <div class="s-dp-days-grid">
390
- {#each calDays as cd}
391
- <button
392
- type="button"
393
- class="s-dp-day"
394
- class:s-dp-day-other={cd.o}
395
- class:s-dp-today={isToday(cd.d, cd.o)}
396
- class:s-dp-selected={isSelected(cd.d, cd.o)}
397
- class:s-dp-day-disabled={isDisabled(cd.d, cd.o)}
398
- onclick={() => selectDay(cd.d, cd.o)}
399
- disabled={cd.o || isDisabled(cd.d, cd.o)}>{cd.d}</button
400
- >
401
- {/each}
402
- </div>
403
-
404
- <!-- Month view -->
405
- {:else if view === "month"}
406
- <div class="s-dp-months-grid">
407
- {#each months as m, i}
408
- <button
409
- type="button"
410
- class="s-dp-month"
411
- class:s-dp-selected={selectedDate &&
412
- i === selectedDate.getMonth() &&
413
- viewYear === selectedDate.getFullYear()}
414
- onclick={() => selectMonth(i)}>{m}</button
415
- >
416
- {/each}
417
- </div>
418
-
419
- <!-- Year view (decade) -->
420
- {:else}
421
- <div class="s-dp-years-grid">
422
- {#each yearList as y (y)}
423
- <button
424
- type="button"
425
- class="s-dp-year"
426
- class:s-dp-selected={selectedDate &&
427
- y === selectedDate.getFullYear()}
428
- onclick={() => selectYear(y)}>{y}</button
429
- >
430
- {/each}
431
- </div>
432
- {/if}
433
-
434
- <!-- Time picker -->
435
- {#if timeEnabled}
436
- <div class="s-dp-time">
437
- <span class="s-dp-time-label">Time</span>
438
- <div class="s-dp-time-inputs">
439
- {#if showHours}
440
- <input
441
- type="number"
442
- class="s-dp-time-input"
443
- min={0}
444
- max={23}
445
- value={currentHour}
446
- oninput={(e) => setHour(parseInt(e.target.value) || 0)}
447
- aria-label="Hour"
448
- />
449
- <span class="s-dp-time-sep">:</span>
450
- {/if}
451
- {#if showMinutes}
452
- <input
453
- type="number"
454
- class="s-dp-time-input"
455
- min={0}
456
- max={59}
457
- value={currentMinute}
458
- oninput={(e) => setMinute(parseInt(e.target.value) || 0)}
459
- aria-label="Minute"
460
- />
461
- {/if}
462
- {#if showSeconds}
463
- <span class="s-dp-time-sep">:</span>
464
- <input
465
- type="number"
466
- class="s-dp-time-input"
467
- min={0}
468
- max={59}
469
- value={currentSecond}
470
- oninput={(e) => setSecond(parseInt(e.target.value) || 0)}
471
- aria-label="Second"
472
- />
473
- {/if}
474
- </div>
475
- </div>
476
- {/if}
477
-
478
- <!-- Mobile done button -->
479
- {#if isMobile}
480
- <button
481
- type="button"
482
- class="s-dp-done-btn"
483
- onclick={() => (open = false)}>Done</button
484
- >
485
- {/if}
486
- </div>
487
- {/if}
488
- </div>
1
+ <script>
2
+ import "./datepicker-styles.css";
3
+ import { defaults, iconClass } from "../config.js";
4
+ import { portal } from "../actions/portal.js";
5
+ import { cn } from "../utils/cn.js";
6
+ import dayjs from "dayjs";
7
+ import customParseFormat from "dayjs/plugin/customParseFormat";
8
+ dayjs.extend(customParseFormat);
9
+
10
+ let {
11
+ style: styleProp,
12
+ theme: themeProp,
13
+ value = $bindable(""),
14
+ format = "YYYY-MM-DD",
15
+ displayFormat = undefined,
16
+ label = "",
17
+ placeholder = "",
18
+ disabled = false,
19
+ min = undefined,
20
+ max = undefined,
21
+ locale = "en-US",
22
+ class: className = "",
23
+ ...rest
24
+ } = $props();
25
+
26
+ const style = $derived(styleProp ?? defaults.style);
27
+ const theme = $derived(themeProp ?? defaults.theme);
28
+ const styleClass = $derived(`s-datepicker-${style}`);
29
+ const themeClass = $derived(`theme-${theme}`);
30
+ const uid = Math.random().toString(36).slice(2, 9);
31
+ const internalId = `dp-${uid}`;
32
+ const dispFormat = $derived(displayFormat ?? format);
33
+
34
+ // State
35
+ let open = $state(false);
36
+ let view = $state("date");
37
+ let viewDate = $state(new Date());
38
+ let selectedDate = $state(null);
39
+ let isMobile = $state(false);
40
+
41
+ // Format helpers — uses dayjs for full format support (MMM, MMMM, etc.)
42
+ function toDayjsFmt(f) {
43
+ return f.replace(/hh/g, "HH");
44
+ }
45
+
46
+ function fmt(date, f) {
47
+ return dayjs(date).format(toDayjsFmt(f));
48
+ }
49
+
50
+ function parseDate(str, f) {
51
+ const d = dayjs(str, toDayjsFmt(f));
52
+ return d.isValid() ? d.toDate() : null;
53
+ }
54
+
55
+ // Derived
56
+ const displayValue = $derived(
57
+ value ? fmt(parseDate(value, format) || new Date(), dispFormat) : "",
58
+ );
59
+ const inputPlaceholder = $derived(placeholder || dispFormat.toLowerCase());
60
+
61
+ // Floating-label support — mirrors Input.svelte. Material / Material 3 float
62
+ // the label inside the border; every other style renders a static label above.
63
+ const floatingLabelStyles = ["material", "material3"];
64
+ const useFloatingLabel = $derived(
65
+ floatingLabelStyles.includes(style) && !!label,
66
+ );
67
+ const useAboveLabel = $derived(
68
+ !floatingLabelStyles.includes(style) && (label || placeholder),
69
+ );
70
+ const displayLabel = $derived(label || placeholder);
71
+ const hasValue = $derived(!!value);
72
+ const floated = $derived(open || hasValue);
73
+
74
+ const minDate = $derived(min ? parseDate(min, format) : null);
75
+ const maxDate = $derived(max ? parseDate(max, format) : null);
76
+
77
+ const viewYear = $derived(viewDate.getFullYear());
78
+ const viewMonth = $derived(viewDate.getMonth());
79
+ const decadeStart = $derived(Math.floor(viewYear / 12) * 12);
80
+ const decadeEnd = $derived(decadeStart + 11);
81
+
82
+ const months = $derived(
83
+ Array.from({ length: 12 }, (_, i) =>
84
+ new Date(2000, i, 1).toLocaleString(locale, { month: "short" }),
85
+ ),
86
+ );
87
+ const weekdays = $derived(
88
+ Array.from({ length: 7 }, (_, i) =>
89
+ new Date(2000, 0, i + 2).toLocaleString(locale, { weekday: "short" }),
90
+ ),
91
+ );
92
+
93
+ function calendarDays(year, month) {
94
+ const fd = new Date(year, month, 1).getDay();
95
+ const dim = new Date(year, month + 1, 0).getDate();
96
+ const pmd = new Date(year, month, 0).getDate();
97
+ const days = [];
98
+ for (let i = fd - 1; i >= 0; i--) days.push({ d: pmd - i, o: true });
99
+ for (let i = 1; i <= dim; i++) days.push({ d: i, o: false });
100
+ let n = 1;
101
+ while (days.length < 42) {
102
+ days.push({ d: n++, o: true });
103
+ }
104
+ return days;
105
+ }
106
+
107
+ const calDays = $derived(calendarDays(viewYear, viewMonth));
108
+ const yearList = $derived(
109
+ Array.from({ length: 12 }, (_, i) => decadeStart + i),
110
+ );
111
+
112
+ function isToday(d, o) {
113
+ if (o) return false;
114
+ const t = new Date();
115
+ return (
116
+ d === t.getDate() &&
117
+ viewMonth === t.getMonth() &&
118
+ viewYear === t.getFullYear()
119
+ );
120
+ }
121
+
122
+ function isDisabled(d, o) {
123
+ if (o) return true;
124
+ const dt = new Date(viewYear, viewMonth, d);
125
+ const day = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate());
126
+ const minDay = minDate
127
+ ? new Date(minDate.getFullYear(), minDate.getMonth(), minDate.getDate())
128
+ : null;
129
+ const maxDay = maxDate
130
+ ? new Date(
131
+ maxDate.getFullYear(),
132
+ maxDate.getMonth(),
133
+ maxDate.getDate() + 1,
134
+ )
135
+ : null;
136
+ if (minDay && day < minDay) return true;
137
+ if (maxDay && day >= maxDay) return true;
138
+ return false;
139
+ }
140
+
141
+ function isSelected(d, o) {
142
+ if (!selectedDate || o) return false;
143
+ return (
144
+ d === selectedDate.getDate() &&
145
+ viewMonth === selectedDate.getMonth() &&
146
+ viewYear === selectedDate.getFullYear()
147
+ );
148
+ }
149
+
150
+ // Navigation
151
+ function prev() {
152
+ if (view === "date") viewDate = new Date(viewYear, viewMonth - 1, 1);
153
+ else if (view === "month") viewDate = new Date(viewYear - 1, viewMonth, 1);
154
+ else viewDate = new Date(viewYear - 12, viewMonth, 1);
155
+ }
156
+
157
+ function next() {
158
+ if (view === "date") viewDate = new Date(viewYear, viewMonth + 1, 1);
159
+ else if (view === "month") viewDate = new Date(viewYear + 1, viewMonth, 1);
160
+ else viewDate = new Date(viewYear + 12, viewMonth, 1);
161
+ }
162
+
163
+ function selectDay(d, o) {
164
+ if (o) return;
165
+ const dt = new Date(viewYear, viewMonth, d);
166
+ if (selectedDate) {
167
+ dt.setHours(
168
+ selectedDate.getHours(),
169
+ selectedDate.getMinutes(),
170
+ selectedDate.getSeconds(),
171
+ );
172
+ }
173
+ selectedDate = dt;
174
+ value = fmt(dt, format);
175
+ open = false;
176
+ }
177
+
178
+ function selectMonth(m) {
179
+ viewDate = new Date(viewYear, m, 1);
180
+ view = "date";
181
+ }
182
+
183
+ function selectYear(y) {
184
+ viewDate = new Date(y, viewMonth, 1);
185
+ view = "date";
186
+ }
187
+
188
+ function checkMobile() {
189
+ isMobile = window.innerWidth < 640;
190
+ }
191
+
192
+ // Click outside
193
+ let wrapperEl;
194
+ let overlayEl;
195
+
196
+ function docClick(e) {
197
+ // overlay is portaled to <body>, so it is no longer inside wrapperEl —
198
+ // treat it as part of the picker for outside-click purposes.
199
+ if (open && wrapperEl && !wrapperEl.contains(e.target) &&
200
+ !(overlayEl && overlayEl.contains(e.target))) {
201
+ open = false;
202
+ }
203
+ }
204
+
205
+ // Position the portaled overlay (fixed) anchored to the input. On mobile it
206
+ // is a fullscreen sheet driven by CSS, so anchoring is skipped.
207
+ function positionOverlay() {
208
+ if (!overlayEl || !wrapperEl || isMobile) return;
209
+ const rect = wrapperEl.getBoundingClientRect();
210
+ const ovW = overlayEl.offsetWidth || 320;
211
+ const ovH = overlayEl.offsetHeight || 360;
212
+ let left = rect.left;
213
+ const maxLeft = window.innerWidth - ovW - 8;
214
+ if (left > maxLeft) left = maxLeft;
215
+ if (left < 8) left = 8;
216
+ let top = rect.bottom + 4;
217
+ // Flip above the field if it would overflow the bottom of the viewport.
218
+ if (top + ovH > window.innerHeight - 8 && rect.top - ovH - 4 > 8) {
219
+ top = rect.top - ovH - 4;
220
+ }
221
+ overlayEl.style.position = "fixed";
222
+ overlayEl.style.left = `${left}px`;
223
+ overlayEl.style.top = `${top}px`;
224
+ overlayEl.style.zIndex = "10000";
225
+ }
226
+
227
+ $effect(() => {
228
+ if (!open) return;
229
+ document.addEventListener("mousedown", docClick);
230
+ document.addEventListener("touchstart", docClick);
231
+ checkMobile();
232
+ // Re-anchor on any scroll (capture: catches scroll inside ancestor cards)
233
+ // and on resize; reposition once the portaled overlay is mounted.
234
+ const reposition = () => positionOverlay();
235
+ window.addEventListener("scroll", reposition, true);
236
+ window.addEventListener("resize", reposition);
237
+ queueMicrotask(reposition);
238
+ return () => {
239
+ document.removeEventListener("mousedown", docClick);
240
+ document.removeEventListener("touchstart", docClick);
241
+ window.removeEventListener("scroll", reposition, true);
242
+ window.removeEventListener("resize", reposition);
243
+ };
244
+ });
245
+
246
+ // Init from value
247
+ $effect(() => {
248
+ if (value) {
249
+ const d = parseDate(value, format);
250
+ if (d) {
251
+ selectedDate = d;
252
+ viewDate = new Date(d.getFullYear(), d.getMonth(), 1);
253
+ }
254
+ }
255
+ });
256
+
257
+ // Time handlers
258
+ function setHour(h) {
259
+ if (!selectedDate) selectedDate = new Date();
260
+ selectedDate = new Date(selectedDate);
261
+ selectedDate.setHours(Math.min(23, Math.max(0, h)));
262
+ value = fmt(selectedDate, format);
263
+ }
264
+
265
+ function setMinute(m) {
266
+ if (!selectedDate) selectedDate = new Date();
267
+ selectedDate = new Date(selectedDate);
268
+ selectedDate.setMinutes(Math.min(59, Math.max(0, m)));
269
+ value = fmt(selectedDate, format);
270
+ }
271
+
272
+ function setSecond(s) {
273
+ if (!selectedDate) selectedDate = new Date();
274
+ selectedDate = new Date(selectedDate);
275
+ selectedDate.setSeconds(Math.min(59, Math.max(0, s)));
276
+ value = fmt(selectedDate, format);
277
+ }
278
+
279
+ const timeEnabled = $derived(/hh|mm|ss/.test(format));
280
+ const showHours = $derived(timeEnabled && /hh/.test(format));
281
+ const showMinutes = $derived(timeEnabled && /mm/.test(format));
282
+ const showSeconds = $derived(timeEnabled && /ss/.test(format));
283
+
284
+ const currentHour = $derived(selectedDate ? selectedDate.getHours() : 0);
285
+ const currentMinute = $derived(selectedDate ? selectedDate.getMinutes() : 0);
286
+ const currentSecond = $derived(selectedDate ? selectedDate.getSeconds() : 0);
287
+ </script>
288
+
289
+ <div
290
+ class={cn("s-datepicker-wrapper", styleClass, themeClass, className)}
291
+ class:s-datepicker-open={open}
292
+ class:focused={open}
293
+ class:floated
294
+ class:has-value={hasValue}
295
+ class:disabled
296
+ class:above-label={useAboveLabel}
297
+ bind:this={wrapperEl}
298
+ {...rest}
299
+ >
300
+ {#if useAboveLabel}
301
+ <label class="s-dp-label-above" for={internalId}>{displayLabel}</label>
302
+ {/if}
303
+ {#if useFloatingLabel}
304
+ <label class="s-dp-floating-label" for={internalId}>{label}</label>
305
+ {/if}
306
+ <div class="s-dp-field">
307
+ <input
308
+ id={internalId}
309
+ type="text"
310
+ readonly
311
+ value={displayValue}
312
+ placeholder={useFloatingLabel ? "" : inputPlaceholder}
313
+ {disabled}
314
+ onfocus={() => {
315
+ if (!disabled) open = true;
316
+ }}
317
+ onclick={() => {
318
+ if (!disabled) open = true;
319
+ }}
320
+ role="combobox"
321
+ aria-expanded={open}
322
+ aria-haspopup="dialog"
323
+ />
324
+ <span class="s-datepicker-trigger-icon {iconClass}" aria-hidden="true"
325
+ >calendar_today</span
326
+ >
327
+ </div>
328
+
329
+ {#if open}
330
+ <div
331
+ class="s-datepicker-overlay {styleClass} {themeClass}"
332
+ class:s-datepicker-mobile={isMobile}
333
+ bind:this={overlayEl}
334
+ use:portal
335
+ role="dialog"
336
+ aria-modal="true"
337
+ aria-label="Date picker"
338
+ >
339
+ <!-- Header -->
340
+ <div class="s-dp-header">
341
+ <button
342
+ type="button"
343
+ class="s-dp-nav-btn"
344
+ onclick={prev}
345
+ aria-label="Previous">&lsaquo;</button
346
+ >
347
+ <div class="s-dp-header-labels">
348
+ {#if view === "date"}
349
+ <button
350
+ type="button"
351
+ class="s-dp-header-label"
352
+ onclick={() => (view = "month")}
353
+ >
354
+ {new Date(2000, viewMonth, 1).toLocaleString(locale, {
355
+ month: "long",
356
+ })}
357
+ </button>
358
+ <button
359
+ type="button"
360
+ class="s-dp-header-label"
361
+ onclick={() => (view = "year")}
362
+ >
363
+ {viewYear}
364
+ </button>
365
+ {:else if view === "month"}
366
+ <button
367
+ type="button"
368
+ class="s-dp-header-label s-dp-header-label-active"
369
+ onclick={() => (view = "year")}
370
+ >
371
+ {viewYear}
372
+ </button>
373
+ {:else}
374
+ <span class="s-dp-header-label s-dp-header-label-active"
375
+ >{decadeStart} &ndash; {decadeEnd}</span
376
+ >
377
+ {/if}
378
+ </div>
379
+ <button
380
+ type="button"
381
+ class="s-dp-nav-btn"
382
+ onclick={next}
383
+ aria-label="Next">&rsaquo;</button
384
+ >
385
+ </div>
386
+
387
+ <!-- Date view -->
388
+ {#if view === "date"}
389
+ <div class="s-dp-weekdays">
390
+ {#each weekdays as wd}
391
+ <span class="s-dp-weekday">{wd}</span>
392
+ {/each}
393
+ </div>
394
+ <div class="s-dp-days-grid">
395
+ {#each calDays as cd}
396
+ <button
397
+ type="button"
398
+ class="s-dp-day"
399
+ class:s-dp-day-other={cd.o}
400
+ class:s-dp-today={isToday(cd.d, cd.o)}
401
+ class:s-dp-selected={isSelected(cd.d, cd.o)}
402
+ class:s-dp-day-disabled={isDisabled(cd.d, cd.o)}
403
+ onclick={() => selectDay(cd.d, cd.o)}
404
+ disabled={cd.o || isDisabled(cd.d, cd.o)}>{cd.d}</button
405
+ >
406
+ {/each}
407
+ </div>
408
+
409
+ <!-- Month view -->
410
+ {:else if view === "month"}
411
+ <div class="s-dp-months-grid">
412
+ {#each months as m, i}
413
+ <button
414
+ type="button"
415
+ class="s-dp-month"
416
+ class:s-dp-selected={selectedDate &&
417
+ i === selectedDate.getMonth() &&
418
+ viewYear === selectedDate.getFullYear()}
419
+ onclick={() => selectMonth(i)}>{m}</button
420
+ >
421
+ {/each}
422
+ </div>
423
+
424
+ <!-- Year view (decade) -->
425
+ {:else}
426
+ <div class="s-dp-years-grid">
427
+ {#each yearList as y (y)}
428
+ <button
429
+ type="button"
430
+ class="s-dp-year"
431
+ class:s-dp-selected={selectedDate &&
432
+ y === selectedDate.getFullYear()}
433
+ onclick={() => selectYear(y)}>{y}</button
434
+ >
435
+ {/each}
436
+ </div>
437
+ {/if}
438
+
439
+ <!-- Time picker -->
440
+ {#if timeEnabled}
441
+ <div class="s-dp-time">
442
+ <span class="s-dp-time-label">Time</span>
443
+ <div class="s-dp-time-inputs">
444
+ {#if showHours}
445
+ <input
446
+ type="number"
447
+ class="s-dp-time-input"
448
+ min={0}
449
+ max={23}
450
+ value={currentHour}
451
+ oninput={(e) => setHour(parseInt(e.target.value) || 0)}
452
+ aria-label="Hour"
453
+ />
454
+ <span class="s-dp-time-sep">:</span>
455
+ {/if}
456
+ {#if showMinutes}
457
+ <input
458
+ type="number"
459
+ class="s-dp-time-input"
460
+ min={0}
461
+ max={59}
462
+ value={currentMinute}
463
+ oninput={(e) => setMinute(parseInt(e.target.value) || 0)}
464
+ aria-label="Minute"
465
+ />
466
+ {/if}
467
+ {#if showSeconds}
468
+ <span class="s-dp-time-sep">:</span>
469
+ <input
470
+ type="number"
471
+ class="s-dp-time-input"
472
+ min={0}
473
+ max={59}
474
+ value={currentSecond}
475
+ oninput={(e) => setSecond(parseInt(e.target.value) || 0)}
476
+ aria-label="Second"
477
+ />
478
+ {/if}
479
+ </div>
480
+ </div>
481
+ {/if}
482
+
483
+ <!-- Mobile done button -->
484
+ {#if isMobile}
485
+ <button
486
+ type="button"
487
+ class="s-dp-done-btn"
488
+ onclick={() => (open = false)}>Done</button
489
+ >
490
+ {/if}
491
+ </div>
492
+ {/if}
493
+ </div>