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.
- package/dist/components/DatePicker.svelte +493 -488
- package/dist/components/Input.svelte +52 -48
- package/dist/components/MultiSelect.svelte +37 -13
- package/dist/components/Select.svelte +182 -177
- package/dist/components/Textarea.svelte +16 -11
- package/dist/components/button-styles.css +266 -263
- package/dist/components/codeeditor-styles.css +12 -2
- package/dist/components/datepicker-styles.css +70 -59
- package/dist/components/input-styles.css +399 -403
- package/dist/components/multiselect-styles.css +76 -47
- package/dist/components/select-styles.css +65 -18
- package/dist/components/textarea-styles.css +250 -210
- package/dist/components/texteditor-styles.css +13 -1
- package/dist/components/theme-base.css +6 -0
- package/dist/components/theme.css +15 -1
- package/dist/config.svelte.d.ts +8 -2
- package/dist/config.svelte.js +46 -6
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/themes/fonts.d.ts +25 -0
- package/dist/themes/fonts.js +178 -0
- package/dist/themes/index.d.ts +1 -0
- package/dist/themes/index.js +2 -1
- package/package.json +1 -1
|
@@ -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
|
|
62
|
-
//
|
|
63
|
-
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
const
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const
|
|
96
|
-
const
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
for (let i =
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
const
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
maxDate.
|
|
133
|
-
maxDate.
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
if (
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
if (view === "
|
|
154
|
-
else
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
if (view === "
|
|
160
|
-
else
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
selectedDate.
|
|
170
|
-
selectedDate.
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
let
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
//
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
//
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
const
|
|
211
|
-
const
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
if (left
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
overlayEl.style.
|
|
223
|
-
overlayEl.style.
|
|
224
|
-
overlayEl.style.
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
document.addEventListener("
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
//
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
window.addEventListener("
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
document.removeEventListener("
|
|
241
|
-
|
|
242
|
-
window.removeEventListener("
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
selectedDate
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
selectedDate
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
selectedDate
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
const
|
|
281
|
-
const
|
|
282
|
-
const
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
const
|
|
286
|
-
const
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
class
|
|
292
|
-
class:
|
|
293
|
-
class:
|
|
294
|
-
class:
|
|
295
|
-
class:
|
|
296
|
-
class:
|
|
297
|
-
bind:this={wrapperEl}
|
|
298
|
-
{...rest}
|
|
299
|
-
>
|
|
300
|
-
{#if
|
|
301
|
-
<label class="s-dp-
|
|
302
|
-
{/if}
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
>
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
<div class="s-dp-
|
|
390
|
-
{#each
|
|
391
|
-
<
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
</
|
|
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">‹</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} – {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">›</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>
|