@luxalgo/vela-ui 0.3.0 → 0.4.0
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/CHANGELOG.md +153 -132
- package/LICENSE +201 -201
- package/NOTICE +28 -28
- package/README.md +86 -86
- package/cli/engine.mjs +282 -282
- package/cli/index.mjs +154 -154
- package/dist/callout-bubble.js +2 -2
- package/dist/checkbox.js +2 -2
- package/dist/{chunk-KKS6NEGI.js → chunk-BWSOMG3G.js} +1 -1
- package/dist/chunk-GUIYFQ27.js +30 -0
- package/dist/{chunk-B2EZB5ZG.js → chunk-KUWTI2PE.js} +2 -2
- package/dist/{chunk-WWHNUKSZ.js → chunk-QGWAN3EP.js} +2 -2
- package/dist/{chunk-TBXUF5YX.js → chunk-RH53NQGE.js} +18 -4
- package/dist/{chunk-IOOYDSSK.js → chunk-SKFAYTDK.js} +1 -1
- package/dist/{chunk-OQXFQMZM.js → chunk-TGJNHUYV.js} +2 -2
- package/dist/{chunk-UEO6W37Z.js → chunk-VC7SVSDK.js} +1 -1
- package/dist/{chunk-OJ2SEMN6.js → chunk-YT2BSP7O.js} +1 -1
- package/dist/collapsible.js +1 -1
- package/dist/color-picker.js +4 -4
- package/dist/context-menu.js +3 -3
- package/dist/dialog.js +3 -3
- package/dist/donut.d.ts +1 -1
- package/dist/drawer.js +1 -1
- package/dist/dropdown-menu.js +3 -3
- package/dist/field.js +8 -8
- package/dist/{stat.d.ts → figure.d.ts} +9 -9
- package/dist/figure.js +41 -0
- package/dist/glyph-select.js +3 -3
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/manifest.json +74 -74
- package/dist/number-input.js +2 -2
- package/dist/popover.js +2 -2
- package/dist/select.d.ts +1 -1
- package/dist/select.js +4 -4
- package/dist/slider.js +1 -1
- package/dist/sort-button.js +1 -1
- package/dist/switch.js +1 -1
- package/dist/tabs.js +1 -1
- package/dist/{theme-Ct-q58xz.d.ts → theme-TefB8vtl.d.ts} +2 -2
- package/dist/theme.d.ts +2 -2
- package/dist/theme.js +1 -1
- package/dist/theme.schema.json +30 -30
- package/dist/toast.d.ts +2 -2
- package/dist/toast.js +1 -1
- package/dist/toggle-group.js +1 -1
- package/dist/tooltip.js +1 -1
- package/dist/zag.d.ts +11 -2
- package/dist/zag.js +1 -1
- package/docs/theming.md +217 -217
- package/docs/what-to-change.md +20 -20
- package/docs/writing-a-component.md +250 -250
- package/llms.txt +3 -3
- package/package.json +90 -90
- package/registry/badge.ts +119 -119
- package/registry/bar-chart.ts +215 -215
- package/registry/button.ts +387 -387
- package/registry/callout-bubble.ts +347 -347
- package/registry/card.ts +379 -379
- package/registry/checkbox.ts +234 -234
- package/registry/collapsible.ts +271 -271
- package/registry/color-picker.ts +582 -582
- package/registry/context-menu.ts +208 -208
- package/registry/data-table.ts +215 -215
- package/registry/date-picker.ts +483 -483
- package/registry/dialog.ts +551 -551
- package/registry/donut.ts +145 -145
- package/registry/drawer.ts +636 -636
- package/registry/dropdown-menu.ts +762 -762
- package/registry/empty.ts +91 -91
- package/registry/field.ts +584 -584
- package/registry/{stat.ts → figure.ts} +96 -96
- package/registry/glyph-select.ts +268 -268
- package/registry/hover-card.ts +222 -222
- package/registry/input-group.ts +229 -229
- package/registry/input.ts +181 -181
- package/registry/item.ts +376 -376
- package/registry/kbd.ts +64 -64
- package/registry/line-chart.ts +218 -218
- package/registry/month-grid.ts +328 -328
- package/registry/number-input.ts +443 -443
- package/registry/popover.ts +368 -368
- package/registry/scatter-chart.ts +221 -221
- package/registry/select.ts +620 -620
- package/registry/slider.ts +259 -259
- package/registry/sort-button.ts +189 -189
- package/registry/sparkline.ts +114 -114
- package/registry/switch.ts +205 -205
- package/registry/tabs.ts +426 -426
- package/registry/text-area.ts +186 -186
- package/registry/toast.ts +965 -965
- package/registry/toggle-group.ts +295 -295
- package/registry/tooltip.ts +156 -156
- package/registry/treemap.ts +847 -847
- package/skills/theming-vela-ui/SKILL.md +137 -137
- package/skills/using-vela-ui-components/SKILL.md +139 -139
- package/skills/writing-vela-ui-components/SKILL.md +163 -163
- package/src/components/badge.ts +119 -119
- package/src/components/bar-chart.ts +215 -215
- package/src/components/button.ts +387 -387
- package/src/components/callout-bubble.ts +347 -347
- package/src/components/card.ts +379 -379
- package/src/components/checkbox.ts +234 -234
- package/src/components/collapsible.ts +271 -271
- package/src/components/color-picker.ts +582 -582
- package/src/components/context-menu.ts +208 -208
- package/src/components/data-table.ts +215 -215
- package/src/components/date-picker.ts +483 -483
- package/src/components/dialog.ts +551 -551
- package/src/components/donut.ts +145 -145
- package/src/components/drawer.ts +636 -636
- package/src/components/dropdown-menu.ts +762 -762
- package/src/components/empty.ts +91 -91
- package/src/components/field.ts +584 -584
- package/src/components/{stat.ts → figure.ts} +96 -96
- package/src/components/glyph-select.ts +268 -268
- package/src/components/hover-card.ts +222 -222
- package/src/components/input-group.ts +229 -229
- package/src/components/input.ts +181 -181
- package/src/components/item.ts +376 -376
- package/src/components/kbd.ts +64 -64
- package/src/components/line-chart.ts +218 -218
- package/src/components/month-grid.ts +328 -328
- package/src/components/number-input.ts +443 -443
- package/src/components/popover.ts +368 -368
- package/src/components/scatter-chart.ts +221 -221
- package/src/components/select.ts +620 -620
- package/src/components/slider.ts +259 -259
- package/src/components/sort-button.ts +189 -189
- package/src/components/sparkline.ts +114 -114
- package/src/components/switch.ts +205 -205
- package/src/components/tabs.ts +426 -426
- package/src/components/text-area.ts +186 -186
- package/src/components/toast.ts +965 -965
- package/src/components/toggle-group.ts +295 -295
- package/src/components/tooltip.ts +156 -156
- package/src/components/treemap.ts +847 -847
- package/src/index.ts +24 -24
- package/src/lib/calendar.ts +117 -117
- package/src/lib/chart.ts +72 -72
- package/src/lib/color.ts +89 -89
- package/src/lib/component-index.ts +163 -163
- package/src/lib/css.ts +48 -48
- package/src/lib/define.ts +45 -45
- package/src/lib/dispose.ts +36 -36
- package/src/lib/h.ts +55 -55
- package/src/lib/headless.ts +16 -16
- package/src/lib/i18n.ts +30 -30
- package/src/lib/icons.ts +244 -244
- package/src/lib/kit-text.ts +26 -26
- package/src/lib/page.ts +24 -24
- package/src/lib/palette.ts +81 -81
- package/src/lib/placement.ts +188 -188
- package/src/lib/scope.ts +135 -135
- package/src/lib/styles.ts +53 -53
- package/src/lib/surface-events.ts +31 -31
- package/src/lib/surface-exit.ts +87 -87
- package/src/lib/t.ts +68 -68
- package/src/lib/theme.ts +684 -670
- package/src/lib/tokens.ts +110 -110
- package/src/lib/variants.ts +32 -32
- package/src/lib/zag.ts +61 -46
- package/stylelint/index.mjs +60 -60
- package/stylelint/lint.mjs +81 -81
- package/stylelint/plugin.mjs +115 -115
- package/dist/chunk-P4MTEMT7.js +0 -23
- package/dist/stat.js +0 -41
package/registry/date-picker.ts
CHANGED
|
@@ -1,483 +1,483 @@
|
|
|
1
|
-
// DatePicker: a month calendar with month and year panels behind its header. The header month
|
|
2
|
-
// and year switch to their panel; picking a year leads to its months and picking a month back
|
|
3
|
-
// to its days. Dates travel as local `YYYY-MM-DD` strings. Names, digits and the first day of
|
|
4
|
-
// the week follow the `i18n` translator's locale (English, weeks from Sunday, without one);
|
|
5
|
-
// the calendar is always Gregorian. The picker is content only: its host (a popover, a panel)
|
|
6
|
-
// owns the surface around it.
|
|
7
|
-
import { calendarSettings, monthNames, plainInteger, weekdayNames, yearBeforeMonth, yearLabel } from '@luxalgo/vela-ui/calendar';
|
|
8
|
-
import { msg, textOf } from '@luxalgo/vela-ui/i18n';
|
|
9
|
-
import type { Translator } from '@luxalgo/vela-ui/i18n';
|
|
10
|
-
import { iconEl } from '@luxalgo/vela-ui/icons';
|
|
11
|
-
import { css, defineStyles } from '@luxalgo/vela-ui';
|
|
12
|
-
import { defineComponent } from '@luxalgo/vela-ui';
|
|
13
|
-
import { h } from '@luxalgo/vela-ui';
|
|
14
|
-
import { t } from '@luxalgo/vela-ui';
|
|
15
|
-
|
|
16
|
-
export type DatePickerPanel = 'date' | 'month' | 'year';
|
|
17
|
-
|
|
18
|
-
export interface DatePickerControllerOptions {
|
|
19
|
-
/** The selected date (`YYYY-MM-DD`); the calendar opens on its month. */
|
|
20
|
-
value?: string;
|
|
21
|
-
/** "Today" for the today ring and the initial month when nothing is selected. */
|
|
22
|
-
today?: Date;
|
|
23
|
-
/** The translator whose locale names the months and weekdays, picks the first day of
|
|
24
|
-
* the week and the digits: `chart.i18n` inside a chart, `createTranslator(...)`
|
|
25
|
-
* elsewhere. Default: English, weeks starting on Sunday. */
|
|
26
|
-
i18n?: Translator;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** One cell of the current panel. `blank` cells pad the first week of a month. */
|
|
30
|
-
export interface DatePickerCell {
|
|
31
|
-
label: string;
|
|
32
|
-
/** The day (`YYYY-MM-DD`) on the date panel; the month index or the year otherwise. */
|
|
33
|
-
value: string | number;
|
|
34
|
-
checked: boolean;
|
|
35
|
-
today: boolean;
|
|
36
|
-
/** A padding year outside the decade on the year panel. */
|
|
37
|
-
outside?: boolean;
|
|
38
|
-
blank?: boolean;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export interface DatePickerController {
|
|
42
|
-
readonly panel: DatePickerPanel;
|
|
43
|
-
readonly year: number;
|
|
44
|
-
readonly month: number;
|
|
45
|
-
readonly value: string | null;
|
|
46
|
-
/** Whether the locale writes the year before the month (the header's switch order). */
|
|
47
|
-
readonly yearFirst: boolean;
|
|
48
|
-
/** The weekday column labels, starting on the locale's first day of the week. */
|
|
49
|
-
weekdays(): string[];
|
|
50
|
-
/** Header text: the month switch (hidden off the date panel) and the year switch. */
|
|
51
|
-
header(): { month: string; monthVisible: boolean; year: string; yearDisabled: boolean };
|
|
52
|
-
/** What the arrows step on the current panel. */
|
|
53
|
-
navLabels(): { prev: string; next: string };
|
|
54
|
-
cells(): DatePickerCell[];
|
|
55
|
-
/** One month, one year or ten, depending on the panel. */
|
|
56
|
-
step(dir: 1 | -1): void;
|
|
57
|
-
showMonths(): void;
|
|
58
|
-
showYears(): void;
|
|
59
|
-
/** Activate a cell: a day returns its date (the pick); a month or year only navigates. */
|
|
60
|
-
choose(cell: DatePickerCell): string | null;
|
|
61
|
-
/** Rewrite the selection without emitting (and open on its month). */
|
|
62
|
-
setValue(value: string | null): void;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const NAV = {
|
|
66
|
-
prevMonth: msg('ui.datePicker.previousMonth', 'Previous month'),
|
|
67
|
-
nextMonth: msg('ui.datePicker.nextMonth', 'Next month'),
|
|
68
|
-
prevYear: msg('ui.datePicker.previousYear', 'Previous year'),
|
|
69
|
-
nextYear: msg('ui.datePicker.nextYear', 'Next year'),
|
|
70
|
-
prevDecade: msg('ui.datePicker.previousDecade', 'Previous decade'),
|
|
71
|
-
nextDecade: msg('ui.datePicker.nextDecade', 'Next decade'),
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
/** `YYYY-MM-DD` for a local date. */
|
|
75
|
-
export function isoDate(d: Date): string {
|
|
76
|
-
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** Accept `YYYY-MM-DD` or `YYYY-M-D` and return a padded ISO date, or null if unusable. */
|
|
80
|
-
export function normalizeDateInput(raw: string): string | null {
|
|
81
|
-
const m = /^(\d{4})-(\d{1,2})-(\d{1,2})$/.exec(raw.trim());
|
|
82
|
-
if (!m) return null;
|
|
83
|
-
const y = Number(m[1]);
|
|
84
|
-
const mo = Number(m[2]);
|
|
85
|
-
const d = Number(m[3]);
|
|
86
|
-
const dt = new Date(y, mo - 1, d);
|
|
87
|
-
if (dt.getFullYear() !== y || dt.getMonth() !== mo - 1 || dt.getDate() !== d) return null;
|
|
88
|
-
return isoDate(dt);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function parseIsoDate(raw: string | null | undefined): Date | null {
|
|
92
|
-
const iso = raw ? normalizeDateInput(raw) : null;
|
|
93
|
-
if (!iso) return null;
|
|
94
|
-
const [y, mo, d] = iso.split('-').map(Number);
|
|
95
|
-
return new Date(y!, mo! - 1, d!);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/** The calendar's panels, navigation and selection, without DOM. */
|
|
99
|
-
export function datePickerController(opts: DatePickerControllerOptions = {}): DatePickerController {
|
|
100
|
-
const today = opts.today ?? new Date();
|
|
101
|
-
let selected = parseIsoDate(opts.value);
|
|
102
|
-
let year = selected?.getFullYear() ?? today.getFullYear();
|
|
103
|
-
let month = selected?.getMonth() ?? today.getMonth();
|
|
104
|
-
let panel: DatePickerPanel = 'date';
|
|
105
|
-
const i18n = opts.i18n ?? null;
|
|
106
|
-
const settings = calendarSettings(i18n);
|
|
107
|
-
// Without a translator the picker keeps its historical Sunday-first grid.
|
|
108
|
-
const weekStart = opts.i18n?.weekStart ?? 0;
|
|
109
|
-
const monthsLong = monthNames(settings, 'long');
|
|
110
|
-
const monthsShort = monthNames(settings, 'short');
|
|
111
|
-
const num = (n: number): string => plainInteger(settings, n);
|
|
112
|
-
|
|
113
|
-
const decade = (): number => Math.floor(year / 10) * 10;
|
|
114
|
-
|
|
115
|
-
return {
|
|
116
|
-
get panel() {
|
|
117
|
-
return panel;
|
|
118
|
-
},
|
|
119
|
-
get year() {
|
|
120
|
-
return year;
|
|
121
|
-
},
|
|
122
|
-
get month() {
|
|
123
|
-
return month;
|
|
124
|
-
},
|
|
125
|
-
get value() {
|
|
126
|
-
return selected ? isoDate(selected) : null;
|
|
127
|
-
},
|
|
128
|
-
yearFirst: yearBeforeMonth(settings),
|
|
129
|
-
weekdays() {
|
|
130
|
-
const names = weekdayNames(settings);
|
|
131
|
-
return Array.from({ length: 7 }, (_, i) => names[(weekStart + i) % 7]!);
|
|
132
|
-
},
|
|
133
|
-
header() {
|
|
134
|
-
return {
|
|
135
|
-
month: monthsLong[month] ?? '',
|
|
136
|
-
monthVisible: panel === 'date',
|
|
137
|
-
year: panel === 'year' ? `${num(decade())}-${num(decade() + 9)}` : yearLabel(settings, year),
|
|
138
|
-
yearDisabled: panel === 'year', // in the decade panel the year reads as its title
|
|
139
|
-
};
|
|
140
|
-
},
|
|
141
|
-
navLabels() {
|
|
142
|
-
if (panel === 'date') return { prev: textOf(i18n, NAV.prevMonth), next: textOf(i18n, NAV.nextMonth) };
|
|
143
|
-
if (panel === 'month') return { prev: textOf(i18n, NAV.prevYear), next: textOf(i18n, NAV.nextYear) };
|
|
144
|
-
return { prev: textOf(i18n, NAV.prevDecade), next: textOf(i18n, NAV.nextDecade) };
|
|
145
|
-
},
|
|
146
|
-
cells() {
|
|
147
|
-
const out: DatePickerCell[] = [];
|
|
148
|
-
if (panel === 'date') {
|
|
149
|
-
const startPad = (new Date(year, month, 1).getDay() - weekStart + 7) % 7;
|
|
150
|
-
const days = new Date(year, month + 1, 0).getDate();
|
|
151
|
-
const todayIso = isoDate(today);
|
|
152
|
-
const selectedIso = selected ? isoDate(selected) : '';
|
|
153
|
-
for (let i = 0; i < startPad; i++) out.push({ label: '', value: '', checked: false, today: false, blank: true });
|
|
154
|
-
for (let day = 1; day <= days; day++) {
|
|
155
|
-
const iso = `${year}-${String(month + 1).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
|
|
156
|
-
out.push({ label: num(day), value: iso, checked: iso === selectedIso, today: iso === todayIso });
|
|
157
|
-
}
|
|
158
|
-
return out;
|
|
159
|
-
}
|
|
160
|
-
if (panel === 'month') {
|
|
161
|
-
for (let m = 0; m < 12; m++) {
|
|
162
|
-
out.push({
|
|
163
|
-
label: monthsShort[m] ?? '',
|
|
164
|
-
value: m,
|
|
165
|
-
checked: selected?.getFullYear() === year && selected.getMonth() === m,
|
|
166
|
-
today: today.getFullYear() === year && today.getMonth() === m,
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
return out;
|
|
170
|
-
}
|
|
171
|
-
// One padding year on each side, so the decade fills the same 3×4 grid as the months.
|
|
172
|
-
const d = decade();
|
|
173
|
-
for (let y = d - 1; y <= d + 10; y++) {
|
|
174
|
-
out.push({ label: num(y), value: y, checked: selected?.getFullYear() === y, today: today.getFullYear() === y, outside: y < d || y > d + 9 });
|
|
175
|
-
}
|
|
176
|
-
return out;
|
|
177
|
-
},
|
|
178
|
-
step(dir) {
|
|
179
|
-
if (panel === 'date') {
|
|
180
|
-
month += dir;
|
|
181
|
-
if (month < 0) {
|
|
182
|
-
month = 11;
|
|
183
|
-
year -= 1;
|
|
184
|
-
}
|
|
185
|
-
if (month > 11) {
|
|
186
|
-
month = 0;
|
|
187
|
-
year += 1;
|
|
188
|
-
}
|
|
189
|
-
} else {
|
|
190
|
-
year += panel === 'month' ? dir : dir * 10;
|
|
191
|
-
}
|
|
192
|
-
},
|
|
193
|
-
showMonths() {
|
|
194
|
-
panel = 'month';
|
|
195
|
-
},
|
|
196
|
-
showYears() {
|
|
197
|
-
panel = 'year';
|
|
198
|
-
},
|
|
199
|
-
choose(cell) {
|
|
200
|
-
if (cell.blank) return null;
|
|
201
|
-
if (panel === 'date') {
|
|
202
|
-
selected = parseIsoDate(String(cell.value));
|
|
203
|
-
return selected ? isoDate(selected) : null;
|
|
204
|
-
}
|
|
205
|
-
if (panel === 'month') {
|
|
206
|
-
month = Number(cell.value);
|
|
207
|
-
panel = 'date';
|
|
208
|
-
return null;
|
|
209
|
-
}
|
|
210
|
-
year = Number(cell.value);
|
|
211
|
-
panel = 'month';
|
|
212
|
-
return null;
|
|
213
|
-
},
|
|
214
|
-
setValue(value) {
|
|
215
|
-
selected = parseIsoDate(value);
|
|
216
|
-
if (selected) {
|
|
217
|
-
year = selected.getFullYear();
|
|
218
|
-
month = selected.getMonth();
|
|
219
|
-
}
|
|
220
|
-
panel = 'date';
|
|
221
|
-
},
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
export const datePickerDef = defineComponent({
|
|
226
|
-
name: 'date-picker',
|
|
227
|
-
parts: ['root', 'head', 'title', 'switch', 'nav', 'week', 'grid', 'cells', 'blank', 'day', 'cell'],
|
|
228
|
-
settings: { radius: t.radius.sm, 'today-ring': `inset 0 0 0 1px ${t.border.strong}` },
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
export interface DatePickerOptions extends DatePickerControllerOptions {
|
|
232
|
-
/** A day was picked (`YYYY-MM-DD`). */
|
|
233
|
-
onPick?: (value: string) => void;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
export type DatePickerProps = DatePickerOptions;
|
|
237
|
-
|
|
238
|
-
const CHOOSE_MONTH = msg('ui.datePicker.chooseMonth', 'Choose month');
|
|
239
|
-
const CHOOSE_YEAR = msg('ui.datePicker.chooseYear', 'Choose year');
|
|
240
|
-
|
|
241
|
-
const injectStyles = defineStyles(
|
|
242
|
-
'vela-date-picker-styles',
|
|
243
|
-
css`
|
|
244
|
-
@layer vela.ui {
|
|
245
|
-
[data-slot='date-picker'] {
|
|
246
|
-
color: ${t.fg};
|
|
247
|
-
font: 14px ${t.font};
|
|
248
|
-
user-select: none;
|
|
249
|
-
}
|
|
250
|
-
[data-slot='date-picker-head'] {
|
|
251
|
-
display: flex;
|
|
252
|
-
align-items: center;
|
|
253
|
-
justify-content: space-between;
|
|
254
|
-
gap: 8px;
|
|
255
|
-
margin-bottom: 8px;
|
|
256
|
-
}
|
|
257
|
-
[data-slot='date-picker-title'] {
|
|
258
|
-
flex: 1;
|
|
259
|
-
display: flex;
|
|
260
|
-
align-items: center;
|
|
261
|
-
justify-content: center;
|
|
262
|
-
gap: 2px;
|
|
263
|
-
min-width: 0;
|
|
264
|
-
}
|
|
265
|
-
[data-slot='date-picker-switch'] {
|
|
266
|
-
border: none;
|
|
267
|
-
background: transparent;
|
|
268
|
-
color: ${t.fg.bright};
|
|
269
|
-
font: inherit;
|
|
270
|
-
font-weight: 600;
|
|
271
|
-
font-size: 14px;
|
|
272
|
-
padding: 2px 6px;
|
|
273
|
-
border-radius: ${datePickerDef.vars.radius};
|
|
274
|
-
cursor: pointer;
|
|
275
|
-
|
|
276
|
-
&:disabled {
|
|
277
|
-
cursor: default;
|
|
278
|
-
}
|
|
279
|
-
&:not(:disabled):hover {
|
|
280
|
-
background: ${t.hover};
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
[data-slot='date-picker-nav'] {
|
|
284
|
-
width: 24px;
|
|
285
|
-
height: 24px;
|
|
286
|
-
border: none;
|
|
287
|
-
background: transparent;
|
|
288
|
-
color: ${t.fg.muted};
|
|
289
|
-
border-radius: ${datePickerDef.vars.radius};
|
|
290
|
-
padding: 0;
|
|
291
|
-
cursor: pointer;
|
|
292
|
-
display: inline-flex;
|
|
293
|
-
align-items: center;
|
|
294
|
-
justify-content: center;
|
|
295
|
-
font-size: 14px;
|
|
296
|
-
|
|
297
|
-
&:hover {
|
|
298
|
-
background: ${t.hover};
|
|
299
|
-
color: ${t.fg.bright};
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
[data-slot='date-picker-week'],
|
|
303
|
-
[data-slot='date-picker-grid'] {
|
|
304
|
-
grid-template-columns: repeat(7, 28px);
|
|
305
|
-
gap: 2px;
|
|
306
|
-
}
|
|
307
|
-
[data-slot='date-picker-week']:where(:not([hidden])),
|
|
308
|
-
[data-slot='date-picker-grid'] {
|
|
309
|
-
display: grid;
|
|
310
|
-
}
|
|
311
|
-
[data-slot='date-picker-week'] {
|
|
312
|
-
margin-bottom: 4px;
|
|
313
|
-
color: ${t.fg.muted};
|
|
314
|
-
font-size: 11px;
|
|
315
|
-
text-align: center;
|
|
316
|
-
|
|
317
|
-
& > span {
|
|
318
|
-
line-height: 20px;
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
[data-slot='date-picker-blank'] {
|
|
322
|
-
width: 28px;
|
|
323
|
-
height: 28px;
|
|
324
|
-
}
|
|
325
|
-
[data-slot='date-picker-day'] {
|
|
326
|
-
width: 28px;
|
|
327
|
-
height: 28px;
|
|
328
|
-
border: none;
|
|
329
|
-
background: transparent;
|
|
330
|
-
color: inherit;
|
|
331
|
-
border-radius: ${datePickerDef.vars.radius};
|
|
332
|
-
padding: 0;
|
|
333
|
-
cursor: pointer;
|
|
334
|
-
font: inherit;
|
|
335
|
-
font-size: 14px;
|
|
336
|
-
|
|
337
|
-
&:hover {
|
|
338
|
-
background: ${t.hover};
|
|
339
|
-
}
|
|
340
|
-
&[data-checked] {
|
|
341
|
-
background: ${t.hover.strong};
|
|
342
|
-
color: ${t.fg.bright};
|
|
343
|
-
}
|
|
344
|
-
&[data-today]:not([data-checked]) {
|
|
345
|
-
box-shadow: ${datePickerDef.vars['today-ring']};
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
[data-slot='date-picker-cells'] {
|
|
349
|
-
display: grid;
|
|
350
|
-
grid-template-columns: repeat(3, 1fr);
|
|
351
|
-
gap: 4px;
|
|
352
|
-
width: calc(7 * 28px + 6 * 2px);
|
|
353
|
-
}
|
|
354
|
-
[data-slot='date-picker-cell'] {
|
|
355
|
-
height: 36px;
|
|
356
|
-
border: none;
|
|
357
|
-
background: transparent;
|
|
358
|
-
color: inherit;
|
|
359
|
-
border-radius: ${datePickerDef.vars.radius};
|
|
360
|
-
padding: 0 4px;
|
|
361
|
-
cursor: pointer;
|
|
362
|
-
font: inherit;
|
|
363
|
-
font-size: 14px;
|
|
364
|
-
|
|
365
|
-
&:hover {
|
|
366
|
-
background: ${t.hover};
|
|
367
|
-
}
|
|
368
|
-
&[data-checked] {
|
|
369
|
-
background: ${t.hover.strong};
|
|
370
|
-
color: ${t.fg.bright};
|
|
371
|
-
}
|
|
372
|
-
&[data-today]:not([data-checked]) {
|
|
373
|
-
box-shadow: ${datePickerDef.vars['today-ring']};
|
|
374
|
-
}
|
|
375
|
-
&[data-outside] {
|
|
376
|
-
opacity: 0.45;
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
`,
|
|
381
|
-
);
|
|
382
|
-
|
|
383
|
-
export class DatePicker {
|
|
384
|
-
readonly el: HTMLDivElement;
|
|
385
|
-
private readonly ctrl: DatePickerController;
|
|
386
|
-
private readonly prev: HTMLButtonElement;
|
|
387
|
-
private readonly next: HTMLButtonElement;
|
|
388
|
-
private readonly monthBtn: HTMLButtonElement;
|
|
389
|
-
private readonly yearBtn: HTMLButtonElement;
|
|
390
|
-
private readonly week: HTMLDivElement;
|
|
391
|
-
private readonly grid: HTMLDivElement;
|
|
392
|
-
|
|
393
|
-
constructor(private readonly opts: DatePickerOptions = {}) {
|
|
394
|
-
const doc = document;
|
|
395
|
-
injectStyles(doc);
|
|
396
|
-
this.ctrl = datePickerController(opts);
|
|
397
|
-
const button = (className: string, part: string): HTMLButtonElement => h('button', { className, slot: part, attrs: { type: 'button' } });
|
|
398
|
-
this.prev = button('vela-date-picker-nav', datePickerDef.slot('nav'));
|
|
399
|
-
this.prev.append(iconEl('chevron-left', doc));
|
|
400
|
-
this.next = button('vela-date-picker-nav', datePickerDef.slot('nav'));
|
|
401
|
-
this.next.append(iconEl('chevron-right', doc));
|
|
402
|
-
const i18n = opts.i18n ?? null;
|
|
403
|
-
this.monthBtn = button('vela-date-picker-switch', datePickerDef.slot('switch'));
|
|
404
|
-
this.monthBtn.setAttribute('aria-label', textOf(i18n, CHOOSE_MONTH));
|
|
405
|
-
this.yearBtn = button('vela-date-picker-switch', datePickerDef.slot('switch'));
|
|
406
|
-
this.yearBtn.setAttribute('aria-label', textOf(i18n, CHOOSE_YEAR));
|
|
407
|
-
const title = h('div', { className: 'vela-date-picker-title', slot: datePickerDef.slot('title') }, this.ctrl.yearFirst ? [this.yearBtn, this.monthBtn] : [this.monthBtn, this.yearBtn]);
|
|
408
|
-
const head = h('div', { className: 'vela-date-picker-head', slot: datePickerDef.slot('head') }, this.prev, title, this.next);
|
|
409
|
-
this.week = h(
|
|
410
|
-
'div',
|
|
411
|
-
{ className: 'vela-date-picker-week', slot: datePickerDef.slot('week') },
|
|
412
|
-
this.ctrl.weekdays().map((d) => h('span', { text: d })),
|
|
413
|
-
);
|
|
414
|
-
this.grid = h('div');
|
|
415
|
-
|
|
416
|
-
// Every control stops propagation: the picker usually sits in a popover whose
|
|
417
|
-
// outside-dismiss must not read a panel switch as a click elsewhere.
|
|
418
|
-
const on = (el: HTMLElement, run: () => void): void =>
|
|
419
|
-
el.addEventListener('click', (e) => {
|
|
420
|
-
e.stopPropagation();
|
|
421
|
-
run();
|
|
422
|
-
this.paint();
|
|
423
|
-
});
|
|
424
|
-
on(this.prev, () => this.ctrl.step(-1));
|
|
425
|
-
on(this.next, () => this.ctrl.step(1));
|
|
426
|
-
on(this.monthBtn, () => this.ctrl.showMonths());
|
|
427
|
-
on(this.yearBtn, () => this.ctrl.showYears());
|
|
428
|
-
|
|
429
|
-
this.el = h('div', { className: 'vela-date-picker', slot: datePickerDef.slot('root') }, head, this.week, this.grid);
|
|
430
|
-
this.paint();
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
get value(): string | null {
|
|
434
|
-
return this.ctrl.value;
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
/** Rewrite the selection without emitting (the calendar opens on its month). */
|
|
438
|
-
setValue(value: string | null): void {
|
|
439
|
-
this.ctrl.setValue(value);
|
|
440
|
-
this.paint();
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
destroy(): void {}
|
|
444
|
-
|
|
445
|
-
private paint(): void {
|
|
446
|
-
const doc = this.el.ownerDocument;
|
|
447
|
-
const head = this.ctrl.header();
|
|
448
|
-
this.monthBtn.hidden = !head.monthVisible;
|
|
449
|
-
this.monthBtn.textContent = head.month;
|
|
450
|
-
this.yearBtn.textContent = head.year;
|
|
451
|
-
this.yearBtn.disabled = head.yearDisabled;
|
|
452
|
-
const nav = this.ctrl.navLabels();
|
|
453
|
-
this.prev.setAttribute('aria-label', nav.prev);
|
|
454
|
-
this.next.setAttribute('aria-label', nav.next);
|
|
455
|
-
const dates = this.ctrl.panel === 'date';
|
|
456
|
-
this.week.hidden = !dates;
|
|
457
|
-
this.grid.className = dates ? 'vela-date-picker-grid' : 'vela-date-picker-cells';
|
|
458
|
-
this.grid.setAttribute('data-slot', datePickerDef.slot(dates ? 'grid' : 'cells'));
|
|
459
|
-
this.grid.replaceChildren(...this.ctrl.cells().map((cell) => this.cellEl(doc, cell, dates)));
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
private cellEl(doc: Document, cell: DatePickerCell, dates: boolean): HTMLElement {
|
|
463
|
-
if (cell.blank) return h('span', { className: 'vela-date-picker-blank', slot: datePickerDef.slot('blank') });
|
|
464
|
-
const b = h('button', {
|
|
465
|
-
className: dates ? 'vela-date-picker-day' : 'vela-date-picker-cell',
|
|
466
|
-
slot: datePickerDef.slot(dates ? 'day' : 'cell'),
|
|
467
|
-
attrs: { type: 'button' },
|
|
468
|
-
data: { checked: cell.checked ? '1' : null, today: cell.today ? '1' : null, outside: cell.outside ? '1' : null },
|
|
469
|
-
text: cell.label,
|
|
470
|
-
});
|
|
471
|
-
b.addEventListener('click', (e) => {
|
|
472
|
-
e.stopPropagation();
|
|
473
|
-
const picked = this.ctrl.choose(cell);
|
|
474
|
-
if (picked) this.opts.onPick?.(picked);
|
|
475
|
-
else this.paint();
|
|
476
|
-
});
|
|
477
|
-
return b;
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
export function createDatePicker(props: DatePickerProps = {}): DatePicker {
|
|
482
|
-
return new DatePicker(props);
|
|
483
|
-
}
|
|
1
|
+
// DatePicker: a month calendar with month and year panels behind its header. The header month
|
|
2
|
+
// and year switch to their panel; picking a year leads to its months and picking a month back
|
|
3
|
+
// to its days. Dates travel as local `YYYY-MM-DD` strings. Names, digits and the first day of
|
|
4
|
+
// the week follow the `i18n` translator's locale (English, weeks from Sunday, without one);
|
|
5
|
+
// the calendar is always Gregorian. The picker is content only: its host (a popover, a panel)
|
|
6
|
+
// owns the surface around it.
|
|
7
|
+
import { calendarSettings, monthNames, plainInteger, weekdayNames, yearBeforeMonth, yearLabel } from '@luxalgo/vela-ui/calendar';
|
|
8
|
+
import { msg, textOf } from '@luxalgo/vela-ui/i18n';
|
|
9
|
+
import type { Translator } from '@luxalgo/vela-ui/i18n';
|
|
10
|
+
import { iconEl } from '@luxalgo/vela-ui/icons';
|
|
11
|
+
import { css, defineStyles } from '@luxalgo/vela-ui';
|
|
12
|
+
import { defineComponent } from '@luxalgo/vela-ui';
|
|
13
|
+
import { h } from '@luxalgo/vela-ui';
|
|
14
|
+
import { t } from '@luxalgo/vela-ui';
|
|
15
|
+
|
|
16
|
+
export type DatePickerPanel = 'date' | 'month' | 'year';
|
|
17
|
+
|
|
18
|
+
export interface DatePickerControllerOptions {
|
|
19
|
+
/** The selected date (`YYYY-MM-DD`); the calendar opens on its month. */
|
|
20
|
+
value?: string;
|
|
21
|
+
/** "Today" for the today ring and the initial month when nothing is selected. */
|
|
22
|
+
today?: Date;
|
|
23
|
+
/** The translator whose locale names the months and weekdays, picks the first day of
|
|
24
|
+
* the week and the digits: `chart.i18n` inside a chart, `createTranslator(...)`
|
|
25
|
+
* elsewhere. Default: English, weeks starting on Sunday. */
|
|
26
|
+
i18n?: Translator;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** One cell of the current panel. `blank` cells pad the first week of a month. */
|
|
30
|
+
export interface DatePickerCell {
|
|
31
|
+
label: string;
|
|
32
|
+
/** The day (`YYYY-MM-DD`) on the date panel; the month index or the year otherwise. */
|
|
33
|
+
value: string | number;
|
|
34
|
+
checked: boolean;
|
|
35
|
+
today: boolean;
|
|
36
|
+
/** A padding year outside the decade on the year panel. */
|
|
37
|
+
outside?: boolean;
|
|
38
|
+
blank?: boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface DatePickerController {
|
|
42
|
+
readonly panel: DatePickerPanel;
|
|
43
|
+
readonly year: number;
|
|
44
|
+
readonly month: number;
|
|
45
|
+
readonly value: string | null;
|
|
46
|
+
/** Whether the locale writes the year before the month (the header's switch order). */
|
|
47
|
+
readonly yearFirst: boolean;
|
|
48
|
+
/** The weekday column labels, starting on the locale's first day of the week. */
|
|
49
|
+
weekdays(): string[];
|
|
50
|
+
/** Header text: the month switch (hidden off the date panel) and the year switch. */
|
|
51
|
+
header(): { month: string; monthVisible: boolean; year: string; yearDisabled: boolean };
|
|
52
|
+
/** What the arrows step on the current panel. */
|
|
53
|
+
navLabels(): { prev: string; next: string };
|
|
54
|
+
cells(): DatePickerCell[];
|
|
55
|
+
/** One month, one year or ten, depending on the panel. */
|
|
56
|
+
step(dir: 1 | -1): void;
|
|
57
|
+
showMonths(): void;
|
|
58
|
+
showYears(): void;
|
|
59
|
+
/** Activate a cell: a day returns its date (the pick); a month or year only navigates. */
|
|
60
|
+
choose(cell: DatePickerCell): string | null;
|
|
61
|
+
/** Rewrite the selection without emitting (and open on its month). */
|
|
62
|
+
setValue(value: string | null): void;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const NAV = {
|
|
66
|
+
prevMonth: msg('ui.datePicker.previousMonth', 'Previous month'),
|
|
67
|
+
nextMonth: msg('ui.datePicker.nextMonth', 'Next month'),
|
|
68
|
+
prevYear: msg('ui.datePicker.previousYear', 'Previous year'),
|
|
69
|
+
nextYear: msg('ui.datePicker.nextYear', 'Next year'),
|
|
70
|
+
prevDecade: msg('ui.datePicker.previousDecade', 'Previous decade'),
|
|
71
|
+
nextDecade: msg('ui.datePicker.nextDecade', 'Next decade'),
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/** `YYYY-MM-DD` for a local date. */
|
|
75
|
+
export function isoDate(d: Date): string {
|
|
76
|
+
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Accept `YYYY-MM-DD` or `YYYY-M-D` and return a padded ISO date, or null if unusable. */
|
|
80
|
+
export function normalizeDateInput(raw: string): string | null {
|
|
81
|
+
const m = /^(\d{4})-(\d{1,2})-(\d{1,2})$/.exec(raw.trim());
|
|
82
|
+
if (!m) return null;
|
|
83
|
+
const y = Number(m[1]);
|
|
84
|
+
const mo = Number(m[2]);
|
|
85
|
+
const d = Number(m[3]);
|
|
86
|
+
const dt = new Date(y, mo - 1, d);
|
|
87
|
+
if (dt.getFullYear() !== y || dt.getMonth() !== mo - 1 || dt.getDate() !== d) return null;
|
|
88
|
+
return isoDate(dt);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function parseIsoDate(raw: string | null | undefined): Date | null {
|
|
92
|
+
const iso = raw ? normalizeDateInput(raw) : null;
|
|
93
|
+
if (!iso) return null;
|
|
94
|
+
const [y, mo, d] = iso.split('-').map(Number);
|
|
95
|
+
return new Date(y!, mo! - 1, d!);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The calendar's panels, navigation and selection, without DOM. */
|
|
99
|
+
export function datePickerController(opts: DatePickerControllerOptions = {}): DatePickerController {
|
|
100
|
+
const today = opts.today ?? new Date();
|
|
101
|
+
let selected = parseIsoDate(opts.value);
|
|
102
|
+
let year = selected?.getFullYear() ?? today.getFullYear();
|
|
103
|
+
let month = selected?.getMonth() ?? today.getMonth();
|
|
104
|
+
let panel: DatePickerPanel = 'date';
|
|
105
|
+
const i18n = opts.i18n ?? null;
|
|
106
|
+
const settings = calendarSettings(i18n);
|
|
107
|
+
// Without a translator the picker keeps its historical Sunday-first grid.
|
|
108
|
+
const weekStart = opts.i18n?.weekStart ?? 0;
|
|
109
|
+
const monthsLong = monthNames(settings, 'long');
|
|
110
|
+
const monthsShort = monthNames(settings, 'short');
|
|
111
|
+
const num = (n: number): string => plainInteger(settings, n);
|
|
112
|
+
|
|
113
|
+
const decade = (): number => Math.floor(year / 10) * 10;
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
get panel() {
|
|
117
|
+
return panel;
|
|
118
|
+
},
|
|
119
|
+
get year() {
|
|
120
|
+
return year;
|
|
121
|
+
},
|
|
122
|
+
get month() {
|
|
123
|
+
return month;
|
|
124
|
+
},
|
|
125
|
+
get value() {
|
|
126
|
+
return selected ? isoDate(selected) : null;
|
|
127
|
+
},
|
|
128
|
+
yearFirst: yearBeforeMonth(settings),
|
|
129
|
+
weekdays() {
|
|
130
|
+
const names = weekdayNames(settings);
|
|
131
|
+
return Array.from({ length: 7 }, (_, i) => names[(weekStart + i) % 7]!);
|
|
132
|
+
},
|
|
133
|
+
header() {
|
|
134
|
+
return {
|
|
135
|
+
month: monthsLong[month] ?? '',
|
|
136
|
+
monthVisible: panel === 'date',
|
|
137
|
+
year: panel === 'year' ? `${num(decade())}-${num(decade() + 9)}` : yearLabel(settings, year),
|
|
138
|
+
yearDisabled: panel === 'year', // in the decade panel the year reads as its title
|
|
139
|
+
};
|
|
140
|
+
},
|
|
141
|
+
navLabels() {
|
|
142
|
+
if (panel === 'date') return { prev: textOf(i18n, NAV.prevMonth), next: textOf(i18n, NAV.nextMonth) };
|
|
143
|
+
if (panel === 'month') return { prev: textOf(i18n, NAV.prevYear), next: textOf(i18n, NAV.nextYear) };
|
|
144
|
+
return { prev: textOf(i18n, NAV.prevDecade), next: textOf(i18n, NAV.nextDecade) };
|
|
145
|
+
},
|
|
146
|
+
cells() {
|
|
147
|
+
const out: DatePickerCell[] = [];
|
|
148
|
+
if (panel === 'date') {
|
|
149
|
+
const startPad = (new Date(year, month, 1).getDay() - weekStart + 7) % 7;
|
|
150
|
+
const days = new Date(year, month + 1, 0).getDate();
|
|
151
|
+
const todayIso = isoDate(today);
|
|
152
|
+
const selectedIso = selected ? isoDate(selected) : '';
|
|
153
|
+
for (let i = 0; i < startPad; i++) out.push({ label: '', value: '', checked: false, today: false, blank: true });
|
|
154
|
+
for (let day = 1; day <= days; day++) {
|
|
155
|
+
const iso = `${year}-${String(month + 1).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
|
|
156
|
+
out.push({ label: num(day), value: iso, checked: iso === selectedIso, today: iso === todayIso });
|
|
157
|
+
}
|
|
158
|
+
return out;
|
|
159
|
+
}
|
|
160
|
+
if (panel === 'month') {
|
|
161
|
+
for (let m = 0; m < 12; m++) {
|
|
162
|
+
out.push({
|
|
163
|
+
label: monthsShort[m] ?? '',
|
|
164
|
+
value: m,
|
|
165
|
+
checked: selected?.getFullYear() === year && selected.getMonth() === m,
|
|
166
|
+
today: today.getFullYear() === year && today.getMonth() === m,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
return out;
|
|
170
|
+
}
|
|
171
|
+
// One padding year on each side, so the decade fills the same 3×4 grid as the months.
|
|
172
|
+
const d = decade();
|
|
173
|
+
for (let y = d - 1; y <= d + 10; y++) {
|
|
174
|
+
out.push({ label: num(y), value: y, checked: selected?.getFullYear() === y, today: today.getFullYear() === y, outside: y < d || y > d + 9 });
|
|
175
|
+
}
|
|
176
|
+
return out;
|
|
177
|
+
},
|
|
178
|
+
step(dir) {
|
|
179
|
+
if (panel === 'date') {
|
|
180
|
+
month += dir;
|
|
181
|
+
if (month < 0) {
|
|
182
|
+
month = 11;
|
|
183
|
+
year -= 1;
|
|
184
|
+
}
|
|
185
|
+
if (month > 11) {
|
|
186
|
+
month = 0;
|
|
187
|
+
year += 1;
|
|
188
|
+
}
|
|
189
|
+
} else {
|
|
190
|
+
year += panel === 'month' ? dir : dir * 10;
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
showMonths() {
|
|
194
|
+
panel = 'month';
|
|
195
|
+
},
|
|
196
|
+
showYears() {
|
|
197
|
+
panel = 'year';
|
|
198
|
+
},
|
|
199
|
+
choose(cell) {
|
|
200
|
+
if (cell.blank) return null;
|
|
201
|
+
if (panel === 'date') {
|
|
202
|
+
selected = parseIsoDate(String(cell.value));
|
|
203
|
+
return selected ? isoDate(selected) : null;
|
|
204
|
+
}
|
|
205
|
+
if (panel === 'month') {
|
|
206
|
+
month = Number(cell.value);
|
|
207
|
+
panel = 'date';
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
year = Number(cell.value);
|
|
211
|
+
panel = 'month';
|
|
212
|
+
return null;
|
|
213
|
+
},
|
|
214
|
+
setValue(value) {
|
|
215
|
+
selected = parseIsoDate(value);
|
|
216
|
+
if (selected) {
|
|
217
|
+
year = selected.getFullYear();
|
|
218
|
+
month = selected.getMonth();
|
|
219
|
+
}
|
|
220
|
+
panel = 'date';
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export const datePickerDef = defineComponent({
|
|
226
|
+
name: 'date-picker',
|
|
227
|
+
parts: ['root', 'head', 'title', 'switch', 'nav', 'week', 'grid', 'cells', 'blank', 'day', 'cell'],
|
|
228
|
+
settings: { radius: t.radius.sm, 'today-ring': `inset 0 0 0 1px ${t.border.strong}` },
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
export interface DatePickerOptions extends DatePickerControllerOptions {
|
|
232
|
+
/** A day was picked (`YYYY-MM-DD`). */
|
|
233
|
+
onPick?: (value: string) => void;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export type DatePickerProps = DatePickerOptions;
|
|
237
|
+
|
|
238
|
+
const CHOOSE_MONTH = msg('ui.datePicker.chooseMonth', 'Choose month');
|
|
239
|
+
const CHOOSE_YEAR = msg('ui.datePicker.chooseYear', 'Choose year');
|
|
240
|
+
|
|
241
|
+
const injectStyles = defineStyles(
|
|
242
|
+
'vela-date-picker-styles',
|
|
243
|
+
css`
|
|
244
|
+
@layer vela.ui {
|
|
245
|
+
[data-slot='date-picker'] {
|
|
246
|
+
color: ${t.fg};
|
|
247
|
+
font: 14px ${t.font};
|
|
248
|
+
user-select: none;
|
|
249
|
+
}
|
|
250
|
+
[data-slot='date-picker-head'] {
|
|
251
|
+
display: flex;
|
|
252
|
+
align-items: center;
|
|
253
|
+
justify-content: space-between;
|
|
254
|
+
gap: 8px;
|
|
255
|
+
margin-bottom: 8px;
|
|
256
|
+
}
|
|
257
|
+
[data-slot='date-picker-title'] {
|
|
258
|
+
flex: 1;
|
|
259
|
+
display: flex;
|
|
260
|
+
align-items: center;
|
|
261
|
+
justify-content: center;
|
|
262
|
+
gap: 2px;
|
|
263
|
+
min-width: 0;
|
|
264
|
+
}
|
|
265
|
+
[data-slot='date-picker-switch'] {
|
|
266
|
+
border: none;
|
|
267
|
+
background: transparent;
|
|
268
|
+
color: ${t.fg.bright};
|
|
269
|
+
font: inherit;
|
|
270
|
+
font-weight: 600;
|
|
271
|
+
font-size: 14px;
|
|
272
|
+
padding: 2px 6px;
|
|
273
|
+
border-radius: ${datePickerDef.vars.radius};
|
|
274
|
+
cursor: pointer;
|
|
275
|
+
|
|
276
|
+
&:disabled {
|
|
277
|
+
cursor: default;
|
|
278
|
+
}
|
|
279
|
+
&:not(:disabled):hover {
|
|
280
|
+
background: ${t.hover};
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
[data-slot='date-picker-nav'] {
|
|
284
|
+
width: 24px;
|
|
285
|
+
height: 24px;
|
|
286
|
+
border: none;
|
|
287
|
+
background: transparent;
|
|
288
|
+
color: ${t.fg.muted};
|
|
289
|
+
border-radius: ${datePickerDef.vars.radius};
|
|
290
|
+
padding: 0;
|
|
291
|
+
cursor: pointer;
|
|
292
|
+
display: inline-flex;
|
|
293
|
+
align-items: center;
|
|
294
|
+
justify-content: center;
|
|
295
|
+
font-size: 14px;
|
|
296
|
+
|
|
297
|
+
&:hover {
|
|
298
|
+
background: ${t.hover};
|
|
299
|
+
color: ${t.fg.bright};
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
[data-slot='date-picker-week'],
|
|
303
|
+
[data-slot='date-picker-grid'] {
|
|
304
|
+
grid-template-columns: repeat(7, 28px);
|
|
305
|
+
gap: 2px;
|
|
306
|
+
}
|
|
307
|
+
[data-slot='date-picker-week']:where(:not([hidden])),
|
|
308
|
+
[data-slot='date-picker-grid'] {
|
|
309
|
+
display: grid;
|
|
310
|
+
}
|
|
311
|
+
[data-slot='date-picker-week'] {
|
|
312
|
+
margin-bottom: 4px;
|
|
313
|
+
color: ${t.fg.muted};
|
|
314
|
+
font-size: 11px;
|
|
315
|
+
text-align: center;
|
|
316
|
+
|
|
317
|
+
& > span {
|
|
318
|
+
line-height: 20px;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
[data-slot='date-picker-blank'] {
|
|
322
|
+
width: 28px;
|
|
323
|
+
height: 28px;
|
|
324
|
+
}
|
|
325
|
+
[data-slot='date-picker-day'] {
|
|
326
|
+
width: 28px;
|
|
327
|
+
height: 28px;
|
|
328
|
+
border: none;
|
|
329
|
+
background: transparent;
|
|
330
|
+
color: inherit;
|
|
331
|
+
border-radius: ${datePickerDef.vars.radius};
|
|
332
|
+
padding: 0;
|
|
333
|
+
cursor: pointer;
|
|
334
|
+
font: inherit;
|
|
335
|
+
font-size: 14px;
|
|
336
|
+
|
|
337
|
+
&:hover {
|
|
338
|
+
background: ${t.hover};
|
|
339
|
+
}
|
|
340
|
+
&[data-checked] {
|
|
341
|
+
background: ${t.hover.strong};
|
|
342
|
+
color: ${t.fg.bright};
|
|
343
|
+
}
|
|
344
|
+
&[data-today]:not([data-checked]) {
|
|
345
|
+
box-shadow: ${datePickerDef.vars['today-ring']};
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
[data-slot='date-picker-cells'] {
|
|
349
|
+
display: grid;
|
|
350
|
+
grid-template-columns: repeat(3, 1fr);
|
|
351
|
+
gap: 4px;
|
|
352
|
+
width: calc(7 * 28px + 6 * 2px);
|
|
353
|
+
}
|
|
354
|
+
[data-slot='date-picker-cell'] {
|
|
355
|
+
height: 36px;
|
|
356
|
+
border: none;
|
|
357
|
+
background: transparent;
|
|
358
|
+
color: inherit;
|
|
359
|
+
border-radius: ${datePickerDef.vars.radius};
|
|
360
|
+
padding: 0 4px;
|
|
361
|
+
cursor: pointer;
|
|
362
|
+
font: inherit;
|
|
363
|
+
font-size: 14px;
|
|
364
|
+
|
|
365
|
+
&:hover {
|
|
366
|
+
background: ${t.hover};
|
|
367
|
+
}
|
|
368
|
+
&[data-checked] {
|
|
369
|
+
background: ${t.hover.strong};
|
|
370
|
+
color: ${t.fg.bright};
|
|
371
|
+
}
|
|
372
|
+
&[data-today]:not([data-checked]) {
|
|
373
|
+
box-shadow: ${datePickerDef.vars['today-ring']};
|
|
374
|
+
}
|
|
375
|
+
&[data-outside] {
|
|
376
|
+
opacity: 0.45;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
`,
|
|
381
|
+
);
|
|
382
|
+
|
|
383
|
+
export class DatePicker {
|
|
384
|
+
readonly el: HTMLDivElement;
|
|
385
|
+
private readonly ctrl: DatePickerController;
|
|
386
|
+
private readonly prev: HTMLButtonElement;
|
|
387
|
+
private readonly next: HTMLButtonElement;
|
|
388
|
+
private readonly monthBtn: HTMLButtonElement;
|
|
389
|
+
private readonly yearBtn: HTMLButtonElement;
|
|
390
|
+
private readonly week: HTMLDivElement;
|
|
391
|
+
private readonly grid: HTMLDivElement;
|
|
392
|
+
|
|
393
|
+
constructor(private readonly opts: DatePickerOptions = {}) {
|
|
394
|
+
const doc = document;
|
|
395
|
+
injectStyles(doc);
|
|
396
|
+
this.ctrl = datePickerController(opts);
|
|
397
|
+
const button = (className: string, part: string): HTMLButtonElement => h('button', { className, slot: part, attrs: { type: 'button' } });
|
|
398
|
+
this.prev = button('vela-date-picker-nav', datePickerDef.slot('nav'));
|
|
399
|
+
this.prev.append(iconEl('chevron-left', doc));
|
|
400
|
+
this.next = button('vela-date-picker-nav', datePickerDef.slot('nav'));
|
|
401
|
+
this.next.append(iconEl('chevron-right', doc));
|
|
402
|
+
const i18n = opts.i18n ?? null;
|
|
403
|
+
this.monthBtn = button('vela-date-picker-switch', datePickerDef.slot('switch'));
|
|
404
|
+
this.monthBtn.setAttribute('aria-label', textOf(i18n, CHOOSE_MONTH));
|
|
405
|
+
this.yearBtn = button('vela-date-picker-switch', datePickerDef.slot('switch'));
|
|
406
|
+
this.yearBtn.setAttribute('aria-label', textOf(i18n, CHOOSE_YEAR));
|
|
407
|
+
const title = h('div', { className: 'vela-date-picker-title', slot: datePickerDef.slot('title') }, this.ctrl.yearFirst ? [this.yearBtn, this.monthBtn] : [this.monthBtn, this.yearBtn]);
|
|
408
|
+
const head = h('div', { className: 'vela-date-picker-head', slot: datePickerDef.slot('head') }, this.prev, title, this.next);
|
|
409
|
+
this.week = h(
|
|
410
|
+
'div',
|
|
411
|
+
{ className: 'vela-date-picker-week', slot: datePickerDef.slot('week') },
|
|
412
|
+
this.ctrl.weekdays().map((d) => h('span', { text: d })),
|
|
413
|
+
);
|
|
414
|
+
this.grid = h('div');
|
|
415
|
+
|
|
416
|
+
// Every control stops propagation: the picker usually sits in a popover whose
|
|
417
|
+
// outside-dismiss must not read a panel switch as a click elsewhere.
|
|
418
|
+
const on = (el: HTMLElement, run: () => void): void =>
|
|
419
|
+
el.addEventListener('click', (e) => {
|
|
420
|
+
e.stopPropagation();
|
|
421
|
+
run();
|
|
422
|
+
this.paint();
|
|
423
|
+
});
|
|
424
|
+
on(this.prev, () => this.ctrl.step(-1));
|
|
425
|
+
on(this.next, () => this.ctrl.step(1));
|
|
426
|
+
on(this.monthBtn, () => this.ctrl.showMonths());
|
|
427
|
+
on(this.yearBtn, () => this.ctrl.showYears());
|
|
428
|
+
|
|
429
|
+
this.el = h('div', { className: 'vela-date-picker', slot: datePickerDef.slot('root') }, head, this.week, this.grid);
|
|
430
|
+
this.paint();
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
get value(): string | null {
|
|
434
|
+
return this.ctrl.value;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/** Rewrite the selection without emitting (the calendar opens on its month). */
|
|
438
|
+
setValue(value: string | null): void {
|
|
439
|
+
this.ctrl.setValue(value);
|
|
440
|
+
this.paint();
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
destroy(): void {}
|
|
444
|
+
|
|
445
|
+
private paint(): void {
|
|
446
|
+
const doc = this.el.ownerDocument;
|
|
447
|
+
const head = this.ctrl.header();
|
|
448
|
+
this.monthBtn.hidden = !head.monthVisible;
|
|
449
|
+
this.monthBtn.textContent = head.month;
|
|
450
|
+
this.yearBtn.textContent = head.year;
|
|
451
|
+
this.yearBtn.disabled = head.yearDisabled;
|
|
452
|
+
const nav = this.ctrl.navLabels();
|
|
453
|
+
this.prev.setAttribute('aria-label', nav.prev);
|
|
454
|
+
this.next.setAttribute('aria-label', nav.next);
|
|
455
|
+
const dates = this.ctrl.panel === 'date';
|
|
456
|
+
this.week.hidden = !dates;
|
|
457
|
+
this.grid.className = dates ? 'vela-date-picker-grid' : 'vela-date-picker-cells';
|
|
458
|
+
this.grid.setAttribute('data-slot', datePickerDef.slot(dates ? 'grid' : 'cells'));
|
|
459
|
+
this.grid.replaceChildren(...this.ctrl.cells().map((cell) => this.cellEl(doc, cell, dates)));
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
private cellEl(doc: Document, cell: DatePickerCell, dates: boolean): HTMLElement {
|
|
463
|
+
if (cell.blank) return h('span', { className: 'vela-date-picker-blank', slot: datePickerDef.slot('blank') });
|
|
464
|
+
const b = h('button', {
|
|
465
|
+
className: dates ? 'vela-date-picker-day' : 'vela-date-picker-cell',
|
|
466
|
+
slot: datePickerDef.slot(dates ? 'day' : 'cell'),
|
|
467
|
+
attrs: { type: 'button' },
|
|
468
|
+
data: { checked: cell.checked ? '1' : null, today: cell.today ? '1' : null, outside: cell.outside ? '1' : null },
|
|
469
|
+
text: cell.label,
|
|
470
|
+
});
|
|
471
|
+
b.addEventListener('click', (e) => {
|
|
472
|
+
e.stopPropagation();
|
|
473
|
+
const picked = this.ctrl.choose(cell);
|
|
474
|
+
if (picked) this.opts.onPick?.(picked);
|
|
475
|
+
else this.paint();
|
|
476
|
+
});
|
|
477
|
+
return b;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
export function createDatePicker(props: DatePickerProps = {}): DatePicker {
|
|
482
|
+
return new DatePicker(props);
|
|
483
|
+
}
|