@luxalgo/vela-ui 0.2.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.
Files changed (217) hide show
  1. package/CHANGELOG.md +153 -91
  2. package/LICENSE +201 -201
  3. package/NOTICE +28 -28
  4. package/README.md +86 -86
  5. package/cli/engine.mjs +282 -282
  6. package/cli/index.mjs +154 -154
  7. package/dist/badge.js +1 -1
  8. package/dist/bar-chart.d.ts +1 -1
  9. package/dist/bar-chart.js +1 -1
  10. package/dist/button.d.ts +1 -1
  11. package/dist/button.js +4 -4
  12. package/dist/callout-bubble.d.ts +1 -1
  13. package/dist/callout-bubble.js +4 -4
  14. package/dist/card.d.ts +52 -0
  15. package/dist/card.js +179 -0
  16. package/dist/checkbox.js +5 -5
  17. package/dist/{chunk-B5ZSRUDQ.js → chunk-BWSOMG3G.js} +2 -2
  18. package/dist/{chunk-G7IQLY3A.js → chunk-C3PXY2SW.js} +5 -0
  19. package/dist/{chunk-CC3DDNFC.js → chunk-DXDCKZ2M.js} +4 -4
  20. package/dist/chunk-GUIYFQ27.js +30 -0
  21. package/dist/{chunk-WGULXRWD.js → chunk-KUWTI2PE.js} +4 -4
  22. package/dist/{chunk-Z5PINFQO.js → chunk-QGWAN3EP.js} +5 -5
  23. package/dist/{chunk-V2Q4MRMS.js → chunk-RH53NQGE.js} +30 -4
  24. package/dist/{chunk-WBGSYCV2.js → chunk-SKFAYTDK.js} +4 -4
  25. package/dist/{chunk-MJ4RUGX7.js → chunk-TGJNHUYV.js} +3 -3
  26. package/dist/{chunk-WXH3EFV5.js → chunk-VC7SVSDK.js} +4 -4
  27. package/dist/{chunk-7VMN7NKJ.js → chunk-YT2BSP7O.js} +3 -3
  28. package/dist/collapsible.d.ts +1 -1
  29. package/dist/collapsible.js +3 -3
  30. package/dist/color-picker.js +5 -5
  31. package/dist/context-menu.js +5 -5
  32. package/dist/data-table.d.ts +1 -1
  33. package/dist/data-table.js +1 -1
  34. package/dist/date-picker.d.ts +1 -1
  35. package/dist/date-picker.js +2 -2
  36. package/dist/dialog.d.ts +9 -1
  37. package/dist/dialog.js +15 -5
  38. package/dist/donut.d.ts +1 -1
  39. package/dist/donut.js +1 -1
  40. package/dist/drawer.d.ts +1 -1
  41. package/dist/drawer.js +2 -2
  42. package/dist/dropdown-menu.d.ts +1 -1
  43. package/dist/dropdown-menu.js +5 -5
  44. package/dist/empty.d.ts +1 -1
  45. package/dist/empty.js +2 -2
  46. package/dist/field.js +13 -13
  47. package/dist/{stat.d.ts → figure.d.ts} +9 -9
  48. package/dist/figure.js +41 -0
  49. package/dist/glyph-select.js +5 -5
  50. package/dist/hover-card.d.ts +1 -1
  51. package/dist/hover-card.js +3 -3
  52. package/dist/icons.js +1 -1
  53. package/dist/index.d.ts +4 -2
  54. package/dist/index.js +3 -3
  55. package/dist/input-group.d.ts +1 -1
  56. package/dist/input-group.js +4 -4
  57. package/dist/input.js +2 -2
  58. package/dist/item.d.ts +1 -1
  59. package/dist/item.js +2 -2
  60. package/dist/kbd.d.ts +1 -1
  61. package/dist/kbd.js +2 -2
  62. package/dist/line-chart.d.ts +1 -1
  63. package/dist/line-chart.js +1 -1
  64. package/dist/manifest.json +691 -74
  65. package/dist/month-grid.js +1 -1
  66. package/dist/number-input.d.ts +1 -1
  67. package/dist/number-input.js +5 -5
  68. package/dist/popover.js +3 -3
  69. package/dist/scatter-chart.d.ts +1 -1
  70. package/dist/scatter-chart.js +1 -1
  71. package/dist/select.d.ts +1 -1
  72. package/dist/select.js +7 -7
  73. package/dist/slider.d.ts +1 -1
  74. package/dist/slider.js +2 -2
  75. package/dist/sort-button.js +4 -4
  76. package/dist/sparkline.d.ts +1 -1
  77. package/dist/sparkline.js +1 -1
  78. package/dist/switch.d.ts +1 -1
  79. package/dist/switch.js +3 -3
  80. package/dist/tabs.d.ts +1 -1
  81. package/dist/tabs.js +4 -4
  82. package/dist/text-area.d.ts +1 -1
  83. package/dist/text-area.js +2 -2
  84. package/dist/theme-TefB8vtl.d.ts +341 -0
  85. package/dist/theme.d.ts +42 -192
  86. package/dist/theme.js +1 -1
  87. package/dist/theme.schema.json +321 -30
  88. package/dist/toast.d.ts +49 -0
  89. package/dist/toast.js +463 -0
  90. package/dist/toggle-group.d.ts +1 -1
  91. package/dist/toggle-group.js +4 -4
  92. package/dist/tooltip.js +2 -2
  93. package/dist/treemap.d.ts +80 -0
  94. package/dist/treemap.js +470 -0
  95. package/dist/zag.d.ts +11 -2
  96. package/dist/zag.js +1 -1
  97. package/docs/theming.md +217 -217
  98. package/docs/what-to-change.md +20 -20
  99. package/docs/writing-a-component.md +250 -250
  100. package/llms.txt +6 -3
  101. package/package.json +90 -90
  102. package/registry/badge.ts +119 -119
  103. package/registry/bar-chart.ts +215 -215
  104. package/registry/button.ts +387 -379
  105. package/registry/callout-bubble.ts +347 -347
  106. package/registry/card.ts +379 -0
  107. package/registry/checkbox.ts +234 -234
  108. package/registry/collapsible.ts +271 -271
  109. package/registry/color-picker.ts +582 -582
  110. package/registry/context-menu.ts +208 -208
  111. package/registry/data-table.ts +215 -215
  112. package/registry/date-picker.ts +483 -483
  113. package/registry/dialog.ts +551 -535
  114. package/registry/donut.ts +145 -145
  115. package/registry/drawer.ts +636 -636
  116. package/registry/dropdown-menu.ts +762 -762
  117. package/registry/empty.ts +91 -91
  118. package/registry/field.ts +584 -584
  119. package/registry/{stat.ts → figure.ts} +96 -96
  120. package/registry/glyph-select.ts +268 -268
  121. package/registry/hover-card.ts +222 -222
  122. package/registry/input-group.ts +229 -229
  123. package/registry/input.ts +181 -181
  124. package/registry/item.ts +376 -376
  125. package/registry/kbd.ts +64 -64
  126. package/registry/line-chart.ts +218 -218
  127. package/registry/month-grid.ts +328 -328
  128. package/registry/number-input.ts +443 -443
  129. package/registry/popover.ts +368 -368
  130. package/registry/scatter-chart.ts +221 -221
  131. package/registry/select.ts +620 -620
  132. package/registry/slider.ts +259 -259
  133. package/registry/sort-button.ts +189 -189
  134. package/registry/sparkline.ts +114 -114
  135. package/registry/switch.ts +205 -205
  136. package/registry/tabs.ts +426 -426
  137. package/registry/text-area.ts +186 -186
  138. package/registry/toast.ts +965 -0
  139. package/registry/toggle-group.ts +295 -295
  140. package/registry/tooltip.ts +156 -156
  141. package/registry/treemap.ts +847 -0
  142. package/skills/theming-vela-ui/SKILL.md +137 -137
  143. package/skills/using-vela-ui-components/SKILL.md +139 -137
  144. package/skills/writing-vela-ui-components/SKILL.md +163 -163
  145. package/src/components/badge.ts +119 -119
  146. package/src/components/bar-chart.ts +215 -215
  147. package/src/components/button.ts +387 -379
  148. package/src/components/callout-bubble.ts +347 -347
  149. package/src/components/card.ts +379 -0
  150. package/src/components/checkbox.ts +234 -234
  151. package/src/components/collapsible.ts +271 -271
  152. package/src/components/color-picker.ts +582 -582
  153. package/src/components/context-menu.ts +208 -208
  154. package/src/components/data-table.ts +215 -215
  155. package/src/components/date-picker.ts +483 -483
  156. package/src/components/dialog.ts +551 -535
  157. package/src/components/donut.ts +145 -145
  158. package/src/components/drawer.ts +636 -636
  159. package/src/components/dropdown-menu.ts +762 -762
  160. package/src/components/empty.ts +91 -91
  161. package/src/components/field.ts +584 -584
  162. package/src/components/{stat.ts → figure.ts} +96 -96
  163. package/src/components/glyph-select.ts +268 -268
  164. package/src/components/hover-card.ts +222 -222
  165. package/src/components/input-group.ts +229 -229
  166. package/src/components/input.ts +181 -181
  167. package/src/components/item.ts +376 -376
  168. package/src/components/kbd.ts +64 -64
  169. package/src/components/line-chart.ts +218 -218
  170. package/src/components/month-grid.ts +328 -328
  171. package/src/components/number-input.ts +443 -443
  172. package/src/components/popover.ts +368 -368
  173. package/src/components/scatter-chart.ts +221 -221
  174. package/src/components/select.ts +620 -620
  175. package/src/components/slider.ts +259 -259
  176. package/src/components/sort-button.ts +189 -189
  177. package/src/components/sparkline.ts +114 -114
  178. package/src/components/switch.ts +205 -205
  179. package/src/components/tabs.ts +426 -426
  180. package/src/components/text-area.ts +186 -186
  181. package/src/components/toast.ts +965 -0
  182. package/src/components/toggle-group.ts +295 -295
  183. package/src/components/tooltip.ts +156 -156
  184. package/src/components/treemap.ts +847 -0
  185. package/src/index.ts +24 -24
  186. package/src/lib/calendar.ts +117 -117
  187. package/src/lib/chart.ts +72 -72
  188. package/src/lib/color.ts +89 -89
  189. package/src/lib/component-index.ts +163 -151
  190. package/src/lib/css.ts +48 -48
  191. package/src/lib/define.ts +45 -45
  192. package/src/lib/dispose.ts +36 -36
  193. package/src/lib/h.ts +55 -55
  194. package/src/lib/headless.ts +16 -16
  195. package/src/lib/i18n.ts +30 -30
  196. package/src/lib/icons.ts +244 -237
  197. package/src/lib/kit-text.ts +26 -26
  198. package/src/lib/page.ts +24 -24
  199. package/src/lib/palette.ts +81 -81
  200. package/src/lib/placement.ts +188 -188
  201. package/src/lib/scope.ts +135 -135
  202. package/src/lib/styles.ts +53 -53
  203. package/src/lib/surface-events.ts +31 -31
  204. package/src/lib/surface-exit.ts +87 -87
  205. package/src/lib/t.ts +68 -68
  206. package/src/lib/theme.ts +684 -664
  207. package/src/lib/tokens.ts +110 -110
  208. package/src/lib/variants.ts +32 -32
  209. package/src/lib/zag.ts +61 -46
  210. package/stylelint/index.mjs +60 -60
  211. package/stylelint/lint.mjs +81 -81
  212. package/stylelint/plugin.mjs +115 -115
  213. package/dist/chunk-P4MTEMT7.js +0 -23
  214. package/dist/stat.js +0 -41
  215. package/dist/{chunk-U3EH42YY.js → chunk-5BJV5FY5.js} +1 -1
  216. package/dist/{chunk-E2FE6V2N.js → chunk-ALAHGSLC.js} +1 -1
  217. package/dist/{chunk-C3ICYMI3.js → chunk-FYVQO5TN.js} +1 -1
@@ -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 '../lib/calendar';
8
- import { msg, textOf } from '../lib/i18n';
9
- import type { Translator } from '../lib/i18n';
10
- import { iconEl } from '../lib/icons';
11
- import { css, defineStyles } from '../lib/css';
12
- import { defineComponent } from '../lib/define';
13
- import { h } from '../lib/h';
14
- import { t } from '../lib/t';
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 '../lib/calendar';
8
+ import { msg, textOf } from '../lib/i18n';
9
+ import type { Translator } from '../lib/i18n';
10
+ import { iconEl } from '../lib/icons';
11
+ import { css, defineStyles } from '../lib/css';
12
+ import { defineComponent } from '../lib/define';
13
+ import { h } from '../lib/h';
14
+ import { t } from '../lib/t';
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
+ }