nish-ui 2.1.3 → 3.0.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 (78) hide show
  1. package/README.md +6 -2
  2. package/dist/chart.js +1 -1
  3. package/dist/elements-all.js +58 -58
  4. package/dist/elements-charts.js +1 -1
  5. package/dist/elements-data.js +1 -1
  6. package/dist/elements-forms.js +1 -1
  7. package/dist/elements.js +4 -4
  8. package/dist/esm/components/accordion.js +3 -2
  9. package/dist/esm/components/app-shell.js +3 -2
  10. package/dist/esm/components/avatar.js +4 -3
  11. package/dist/esm/components/breadcrumbs.js +3 -2
  12. package/dist/esm/components/calendar-base.js +292 -0
  13. package/dist/esm/components/calendar-classes.js +78 -0
  14. package/dist/esm/components/calendar.js +22 -254
  15. package/dist/esm/components/canvas/legend.js +3 -2
  16. package/dist/esm/components/canvas/node.js +3 -2
  17. package/dist/esm/components/canvas/system-canvas.js +3 -2
  18. package/dist/esm/components/carousel.js +3 -2
  19. package/dist/esm/components/charts.js +8 -7
  20. package/dist/esm/components/combobox.js +3 -2
  21. package/dist/esm/components/command.js +4 -3
  22. package/dist/esm/components/context-menu.js +3 -2
  23. package/dist/esm/components/data.js +4 -3
  24. package/dist/esm/components/date-picker.js +4 -3
  25. package/dist/esm/components/date-range-picker.js +214 -0
  26. package/dist/esm/components/display.js +7 -6
  27. package/dist/esm/components/drawer.js +3 -2
  28. package/dist/esm/components/error-boundary.js +4 -2
  29. package/dist/esm/components/feedback.js +7 -6
  30. package/dist/esm/components/file-dropzone.js +3 -2
  31. package/dist/esm/components/filters.js +4 -3
  32. package/dist/esm/components/flow.js +6 -5
  33. package/dist/esm/components/forms.js +14 -8
  34. package/dist/esm/components/hooks.js +2 -2
  35. package/dist/esm/components/hover-card.js +2 -2
  36. package/dist/esm/components/inbox.js +4 -2
  37. package/dist/esm/components/layout.js +5 -4
  38. package/dist/esm/components/media.js +3 -2
  39. package/dist/esm/components/menu.js +5 -4
  40. package/dist/esm/components/modal.js +4 -3
  41. package/dist/esm/components/more-sheet.js +3 -2
  42. package/dist/esm/components/multi-select.js +3 -2
  43. package/dist/esm/components/nav-items.js +4 -3
  44. package/dist/esm/components/nav.js +5 -4
  45. package/dist/esm/components/overlay-dialog.js +3 -2
  46. package/dist/esm/components/pagination.js +3 -2
  47. package/dist/esm/components/popover.js +2 -2
  48. package/dist/esm/components/portal.js +2 -2
  49. package/dist/esm/components/progress.js +3 -2
  50. package/dist/esm/components/provider.js +34 -5
  51. package/dist/esm/components/range-calendar.js +123 -0
  52. package/dist/esm/components/service-tokens.js +4 -3
  53. package/dist/esm/components/settings.js +4 -3
  54. package/dist/esm/components/slider.js +3 -2
  55. package/dist/esm/components/split.js +4 -3
  56. package/dist/esm/components/strings.js +178 -60
  57. package/dist/esm/components/tag-input.js +3 -2
  58. package/dist/esm/components/theme-toggle.js +3 -2
  59. package/dist/esm/components/time-field-classes.js +110 -0
  60. package/dist/esm/components/time-field.js +309 -0
  61. package/dist/esm/components/toast-card.js +155 -0
  62. package/dist/esm/components/toast-store.js +136 -0
  63. package/dist/esm/components/toast.js +38 -241
  64. package/dist/esm/components/tooltip.js +2 -2
  65. package/dist/esm/components/tree-view.js +40 -46
  66. package/dist/esm/components/virtual-table.js +3 -2
  67. package/dist/esm/index.js +6 -20
  68. package/dist/esm/runtime.js +1 -1
  69. package/dist/esm/system-canvas.js +4 -0
  70. package/dist/esm/theme.js +7 -7
  71. package/dist/format.js +1 -1
  72. package/dist/index.d.ts +172 -24
  73. package/dist/system-canvas.d.ts +183 -2
  74. package/dist/theme.d.ts +21 -3
  75. package/dist/theme.global.js +21 -136
  76. package/dist/theme.js +21 -136
  77. package/package.json +1 -1
  78. package/dist/system-canvas-BLuWd69H.d.ts +0 -183
@@ -0,0 +1,292 @@
1
+ "use client";
2
+
3
+ // src/components/calendar-base.tsx
4
+ import {
5
+ useId,
6
+ useRef,
7
+ useState,
8
+ useSyncExternalStore
9
+ } from "react";
10
+ import { cn } from "../cn.js";
11
+ import { getDefaultLocale } from "../format.js";
12
+ import {
13
+ addDays,
14
+ calendarDayClass,
15
+ calendarKeyTarget,
16
+ clampIso,
17
+ dayNumber,
18
+ firstOfMonth,
19
+ fullDate,
20
+ inRange,
21
+ isoToday,
22
+ monthGrid,
23
+ monthNames,
24
+ monthOf,
25
+ monthTitle,
26
+ parseIso,
27
+ parseMonth,
28
+ resolveLocale,
29
+ shiftMonth,
30
+ weekStartFor,
31
+ weekdayNames
32
+ } from "./calendar-classes.js";
33
+ import { isRtl, useIsoLayoutEffect } from "./client.js";
34
+ import { ChevronLeftIcon, ChevronRightIcon } from "./icons.js";
35
+ import { IconButton, Select } from "./primitives.js";
36
+ import { useNishContext, useStrings } from "./provider.js";
37
+ import { PICKER_STRINGS } from "./strings.js";
38
+ import { jsx, jsxs } from "react/jsx-runtime";
39
+ var warned = /* @__PURE__ */ new Set();
40
+ var noopSubscribe = () => () => {
41
+ };
42
+ var EMPTY_GRID = Array.from({ length: 6 }, () => Array.from({ length: 7 }, () => null));
43
+ var pad2 = (n) => String(n).padStart(2, "0");
44
+ var pad4 = (n) => String(n).padStart(4, "0");
45
+ function checkIso(component, prop, v, isMonth = false) {
46
+ if (v == null) return null;
47
+ const ok = isMonth ? parseMonth(v) !== null : parseIso(v) !== null;
48
+ if (ok) return v;
49
+ const id = `${component}:${prop}`;
50
+ if (process.env.NODE_ENV !== "production" && !warned.has(id)) {
51
+ warned.add(id);
52
+ console.warn(`${component}: "${prop}" must be ${isMonth ? "YYYY-MM" : "YYYY-MM-DD"}`);
53
+ }
54
+ return null;
55
+ }
56
+ function CalendarBase({
57
+ componentName,
58
+ initialMonth,
59
+ focusPreference,
60
+ dayLook,
61
+ onPick,
62
+ onDayFocus,
63
+ onDayHover,
64
+ onGridKeyDown,
65
+ multiselectable,
66
+ children,
67
+ month: monthProp,
68
+ defaultMonth,
69
+ onMonthChange,
70
+ min: minProp,
71
+ max: maxProp,
72
+ isDateDisabled,
73
+ locale,
74
+ weekStartsOn,
75
+ captionLayout = "label",
76
+ fromYear,
77
+ toYear,
78
+ autoFocus = false,
79
+ previousMonthLabel,
80
+ nextMonthLabel,
81
+ className,
82
+ ref,
83
+ ...rest
84
+ }) {
85
+ const strings = useStrings(PICKER_STRINGS);
86
+ const config = useNishContext();
87
+ const loc = resolveLocale(locale ?? config.locale ?? getDefaultLocale());
88
+ const ws = weekStartsOn ?? weekStartFor(loc);
89
+ const titleId = useId();
90
+ const rootRef = useRef(null);
91
+ const focusPending = useRef(false);
92
+ const min = checkIso(componentName, "min", minProp);
93
+ const max = checkIso(componentName, "max", maxProp);
94
+ const monthControlled = checkIso(componentName, "month", monthProp, true);
95
+ const defaultMonthIso = checkIso(componentName, "defaultMonth", defaultMonth, true);
96
+ const [ownMonth, setOwnMonth] = useState(() => defaultMonthIso ?? initialMonth);
97
+ const [focusedDate, setFocusedDate] = useState(null);
98
+ const today = useSyncExternalStore(
99
+ noopSubscribe,
100
+ () => isoToday(),
101
+ () => null
102
+ );
103
+ const shown = monthControlled ?? ownMonth ?? (today ? monthOf(today) : null);
104
+ const setMonth = (raw) => {
105
+ const next = clampIso(raw, "0001-01", "9999-12");
106
+ if (next === shown) return;
107
+ if (monthProp === void 0) setOwnMonth(next);
108
+ onMonthChange?.(next);
109
+ };
110
+ const disabled = (iso) => !inRange(iso, min, max) || isDateDisabled?.(iso) === true;
111
+ const inShown = (iso) => iso !== null && shown !== null && monthOf(iso) === shown;
112
+ let tabStop = null;
113
+ if (shown) {
114
+ if (inShown(focusedDate)) tabStop = focusedDate;
115
+ else if (inShown(focusPreference)) tabStop = focusPreference;
116
+ else if (inShown(today)) tabStop = today;
117
+ else {
118
+ const days = monthGrid(shown, ws).flat().filter((d) => d !== null);
119
+ tabStop = days.find((d) => !disabled(d)) ?? days[0] ?? null;
120
+ }
121
+ }
122
+ useIsoLayoutEffect(() => {
123
+ if (!focusPending.current) return;
124
+ focusPending.current = false;
125
+ if (!focusedDate) return;
126
+ rootRef.current?.querySelector(`[data-date="${focusedDate}"]`)?.focus();
127
+ });
128
+ const setRoot = (node) => {
129
+ rootRef.current = node;
130
+ if (typeof ref === "function") ref(node);
131
+ else if (ref) ref.current = node;
132
+ };
133
+ const pick = (iso) => {
134
+ if (disabled(iso)) return;
135
+ setFocusedDate(iso);
136
+ onPick(iso);
137
+ };
138
+ const onGridKey = (e) => {
139
+ onGridKeyDown?.(e);
140
+ if (e.defaultPrevented) return;
141
+ const from = e.target.closest?.("[data-date]")?.getAttribute("data-date") ?? tabStop;
142
+ if (!from) return;
143
+ const raw = calendarKeyTarget(e.key, e.shiftKey, from, ws, isRtl(e.currentTarget));
144
+ if (!raw) return;
145
+ e.preventDefault();
146
+ const target = clampIso(raw, min ?? "0001-01-01", max ?? "9999-12-31");
147
+ const moved = target !== focusedDate || monthOf(target) !== shown;
148
+ setFocusedDate(target);
149
+ setMonth(monthOf(target));
150
+ if (moved) {
151
+ focusPending.current = true;
152
+ } else {
153
+ e.currentTarget.querySelector(`[data-date="${target}"]`)?.focus();
154
+ }
155
+ };
156
+ const grid = shown ? monthGrid(shown, ws) : EMPTY_GRID;
157
+ const names = weekdayNames(loc, ws);
158
+ const dropdowns = captionLayout === "dropdowns";
159
+ const shownYear = shown ? Number(shown.slice(0, 4)) : null;
160
+ const shownMonthNo = shown ? Number(shown.slice(5, 7)) : null;
161
+ const prevDisabled = !shown || shown <= "0001-01" || min !== null && addDays(firstOfMonth(shown), -1) < min;
162
+ const nextDisabled = !shown || shown >= "9999-12" || max !== null && firstOfMonth(shiftMonth(shown, 1)) > max;
163
+ let years = [];
164
+ if (dropdowns) {
165
+ const baseYear = today ? Number(today.slice(0, 4)) : shownYear ?? 2026;
166
+ const from = fromYear ?? (min ? Number(min.slice(0, 4)) : baseYear - 100);
167
+ const to = toYear ?? (max ? Number(max.slice(0, 4)) : baseYear + 10);
168
+ for (let y = from; y <= to; y++) years.push(y);
169
+ if (shownYear !== null && !years.includes(shownYear)) years = [...years, shownYear].sort((a, b) => a - b);
170
+ }
171
+ return /* @__PURE__ */ jsxs("div", { ...rest, ref: setRoot, className: cn("inline-flex select-none flex-col gap-2", className), children: [
172
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-1", children: [
173
+ /* @__PURE__ */ jsx(
174
+ IconButton,
175
+ {
176
+ size: "sm",
177
+ label: previousMonthLabel ?? strings.previousMonth,
178
+ "aria-disabled": prevDisabled || void 0,
179
+ className: "aria-disabled:cursor-not-allowed aria-disabled:opacity-40",
180
+ onClick: () => !prevDisabled && shown && setMonth(shiftMonth(shown, -1)),
181
+ children: /* @__PURE__ */ jsx(ChevronLeftIcon, { className: "rtl:-scale-x-100" })
182
+ }
183
+ ),
184
+ /* @__PURE__ */ jsx("div", { id: titleId, "aria-live": "polite", className: cn("text-sm font-semibold text-th-hi", dropdowns && "sr-only"), children: shown ? monthTitle(shown, loc) : "" }),
185
+ dropdowns && /* @__PURE__ */ jsxs("div", { className: "flex min-w-0 items-center gap-1", children: [
186
+ /* @__PURE__ */ jsx(
187
+ Select,
188
+ {
189
+ size: "sm",
190
+ "aria-label": strings.month,
191
+ value: shownMonthNo ?? "",
192
+ disabled: !shown,
193
+ onChange: (e) => shownYear !== null && setMonth(`${pad4(shownYear)}-${pad2(Number(e.target.value))}`),
194
+ children: monthNames(loc).map((name, i) => /* @__PURE__ */ jsx("option", { value: i + 1, children: name }, i))
195
+ }
196
+ ),
197
+ /* @__PURE__ */ jsx(
198
+ Select,
199
+ {
200
+ size: "sm",
201
+ "aria-label": strings.year,
202
+ value: shownYear ?? "",
203
+ disabled: !shown,
204
+ onChange: (e) => shownMonthNo !== null && setMonth(`${pad4(Number(e.target.value))}-${pad2(shownMonthNo)}`),
205
+ children: years.map((y) => /* @__PURE__ */ jsx("option", { value: y, children: y }, y))
206
+ }
207
+ )
208
+ ] }),
209
+ /* @__PURE__ */ jsx(
210
+ IconButton,
211
+ {
212
+ size: "sm",
213
+ label: nextMonthLabel ?? strings.nextMonth,
214
+ "aria-disabled": nextDisabled || void 0,
215
+ className: "aria-disabled:cursor-not-allowed aria-disabled:opacity-40",
216
+ onClick: () => !nextDisabled && shown && setMonth(shiftMonth(shown, 1)),
217
+ children: /* @__PURE__ */ jsx(ChevronRightIcon, { className: "rtl:-scale-x-100" })
218
+ }
219
+ )
220
+ ] }),
221
+ /* @__PURE__ */ jsxs(
222
+ "table",
223
+ {
224
+ role: "grid",
225
+ "aria-labelledby": titleId,
226
+ "aria-multiselectable": multiselectable || void 0,
227
+ className: "border-collapse",
228
+ onKeyDown: onGridKey,
229
+ onPointerLeave: onDayHover ? () => onDayHover(null) : void 0,
230
+ children: [
231
+ /* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsx("tr", { children: names.map((n) => /* @__PURE__ */ jsx(
232
+ "th",
233
+ {
234
+ scope: "col",
235
+ abbr: n.long,
236
+ className: "size-10 overflow-hidden px-0.5 py-0 text-center text-xs font-medium text-th-hi3 pointer-coarse:h-11 pointer-coarse:w-[min(2.75rem,calc((100vw_-_2.6rem)/7))]",
237
+ children: n.short
238
+ },
239
+ n.long
240
+ )) }) }),
241
+ /* @__PURE__ */ jsx("tbody", { children: grid.map((row, r) => /* @__PURE__ */ jsx("tr", { children: row.map((iso, c) => {
242
+ if (!iso) {
243
+ return /* @__PURE__ */ jsx("td", { className: "p-0", children: /* @__PURE__ */ jsx(
244
+ "div",
245
+ {
246
+ "aria-hidden": "true",
247
+ className: "size-10 pointer-coarse:h-11 pointer-coarse:w-[min(2.75rem,calc((100vw_-_2.6rem)/7))]"
248
+ }
249
+ ) }, c);
250
+ }
251
+ const look = dayLook(iso);
252
+ const isDisabled = disabled(iso);
253
+ const isTabStop = iso === tabStop;
254
+ return /* @__PURE__ */ jsx("td", { role: "gridcell", "aria-selected": look.ariaSelected, className: "p-0", children: /* @__PURE__ */ jsx(
255
+ "button",
256
+ {
257
+ type: "button",
258
+ tabIndex: isTabStop ? 0 : -1,
259
+ "data-date": iso,
260
+ "data-autofocus": autoFocus && isTabStop ? "" : void 0,
261
+ "aria-label": fullDate(iso, loc),
262
+ "aria-current": iso === today ? "date" : void 0,
263
+ "aria-disabled": isDisabled || void 0,
264
+ "data-state": look.filled ? "active" : "inactive",
265
+ "data-range": look.band ?? void 0,
266
+ "data-preview": look.preview ? "" : void 0,
267
+ "data-disabled": isDisabled ? "" : void 0,
268
+ onClick: () => pick(iso),
269
+ onFocus: onDayFocus ? () => onDayFocus(iso) : void 0,
270
+ onPointerEnter: onDayHover ? (e) => {
271
+ if (e.pointerType !== "touch") onDayHover(iso);
272
+ } : void 0,
273
+ className: calendarDayClass({
274
+ filled: look.filled,
275
+ band: look.band,
276
+ today: iso === today,
277
+ disabled: isDisabled
278
+ }),
279
+ children: dayNumber(iso, loc)
280
+ }
281
+ ) }, c);
282
+ }) }, r)) })
283
+ ]
284
+ }
285
+ ),
286
+ children
287
+ ] });
288
+ }
289
+ export {
290
+ CalendarBase,
291
+ checkIso
292
+ };
@@ -1,4 +1,5 @@
1
1
  // src/components/calendar-classes.ts
2
+ import { cn } from "../cn.js";
2
3
  var ISO_RE = /^(\d{4})-(\d{2})-(\d{2})$/;
3
4
  var MONTH_RE = /^(\d{4})-(\d{2})$/;
4
5
  function pad(n, width) {
@@ -292,10 +293,80 @@ function fullDate(iso, locale) {
292
293
  utcDate(p.y, p.m, p.d)
293
294
  );
294
295
  }
296
+ function orderRange(a, b) {
297
+ return a <= b ? { from: a, to: b } : { from: b, to: a };
298
+ }
299
+ function sameRange(a, b) {
300
+ return a === b || !!a && !!b && a.from === b.from && a.to === b.to;
301
+ }
302
+ function toRange(v) {
303
+ if (!v || typeof v !== "object") return null;
304
+ const { from, to } = v;
305
+ if (typeof from !== "string" || typeof to !== "string" || !parseIso(from) || !parseIso(to)) return null;
306
+ return orderRange(from, to);
307
+ }
308
+ function firstDisabledIn(from, to, isDisabled) {
309
+ let d = from;
310
+ for (let n = 0; n < 3660 && d <= to; n++) {
311
+ if (isDisabled(d)) return d;
312
+ const next = addDays(d, 1);
313
+ if (next === d) break;
314
+ d = next;
315
+ }
316
+ return null;
317
+ }
318
+ function formatRangeField(range, locale) {
319
+ return `${formatDateField(range.from, locale)} \u2013 ${formatDateField(range.to, locale)}`;
320
+ }
321
+ function rangePlaceholder(locale, tokens) {
322
+ const one = datePlaceholder(locale, tokens);
323
+ return `${one} \u2013 ${one}`;
324
+ }
325
+ var RANGE_JUNK_RE = /[^\d\s/.,\-–—]/;
326
+ var DIGIT_RUN_RE = /\d+/g;
327
+ function parseRangeField(text, locale) {
328
+ const t = text.replace(BIDI_MARKS_RE, "").trim();
329
+ if (t === "" || RANGE_JUNK_RE.test(t)) return null;
330
+ const runs = [...t.matchAll(DIGIT_RUN_RE)];
331
+ let a;
332
+ let b;
333
+ if (runs.length === 6) {
334
+ a = t.slice(0, runs[2].index + runs[2][0].length);
335
+ b = t.slice(runs[3].index);
336
+ } else if (runs.length === 2 && runs[0][0].length === 8 && runs[1][0].length === 8) {
337
+ a = runs[0][0];
338
+ b = runs[1][0];
339
+ } else if (runs.length === 3 || runs.length === 1 && runs[0][0].length === 8) {
340
+ a = t;
341
+ b = t;
342
+ } else {
343
+ return null;
344
+ }
345
+ const from = parseDateField(a, locale);
346
+ const to = parseDateField(b, locale);
347
+ return from && to ? orderRange(from, to) : null;
348
+ }
349
+ var DAY_BASE = "flex size-10 items-center justify-center rounded-lg text-sm tabular-nums text-th-hi2 transition-colors pointer-coarse:h-11 pointer-coarse:w-[min(2.75rem,calc((100vw_-_2.6rem)/7))]";
350
+ function calendarDayClass({ filled, today, disabled, band = null }) {
351
+ return cn(
352
+ DAY_BASE,
353
+ !filled && !band && !disabled && "hover:bg-th-elevated hover:text-th-hi",
354
+ today && !filled && "font-semibold text-th-accent ring-1 ring-inset ring-th-accent/50",
355
+ band && today && !filled && "ring-th-accent",
356
+ band === "start" && "rounded-e-none",
357
+ band === "end" && "rounded-s-none",
358
+ band === "middle" && "rounded-none",
359
+ band && !filled && "bg-th-accent-soft font-medium text-th-accent forced-colors:bg-[Highlight] forced-colors:text-[HighlightText] forced-colors:forced-color-adjust-none",
360
+ filled && "bg-th-accent font-semibold text-th-on-accent forced-colors:bg-[Highlight] forced-colors:text-[HighlightText] forced-colors:forced-color-adjust-none",
361
+ filled && band && "forced-colors:outline forced-colors:outline-2 forced-colors:-outline-offset-2 forced-colors:outline-[CanvasText]",
362
+ disabled && "cursor-not-allowed text-th-ghost line-through forced-colors:text-[GrayText]"
363
+ );
364
+ }
295
365
  export {
296
366
  addDays,
297
367
  addMonths,
298
368
  addYears,
369
+ calendarDayClass,
299
370
  calendarKeyTarget,
300
371
  clampIso,
301
372
  dateFieldParts,
@@ -303,8 +374,10 @@ export {
303
374
  dayNumber,
304
375
  daysInMonth,
305
376
  endOfWeek,
377
+ firstDisabledIn,
306
378
  firstOfMonth,
307
379
  formatDateField,
380
+ formatRangeField,
308
381
  fullDate,
309
382
  inRange,
310
383
  isoToday,
@@ -312,13 +385,18 @@ export {
312
385
  monthNames,
313
386
  monthOf,
314
387
  monthTitle,
388
+ orderRange,
315
389
  parseDateField,
316
390
  parseIso,
317
391
  parseMonth,
392
+ parseRangeField,
393
+ rangePlaceholder,
318
394
  resolveLocale,
395
+ sameRange,
319
396
  shiftMonth,
320
397
  startOfWeek,
321
398
  toIso,
399
+ toRange,
322
400
  weekStartFor,
323
401
  weekday,
324
402
  weekdayNames
@@ -1,262 +1,30 @@
1
1
  "use client";
2
2
 
3
3
  // src/components/calendar.tsx
4
- import { useId, useRef, useState, useSyncExternalStore } from "react";
5
- import { cn } from "../cn.js";
6
- import { getDefaultLocale } from "../format.js";
7
- import {
8
- addDays,
9
- calendarKeyTarget,
10
- clampIso,
11
- dayNumber,
12
- firstOfMonth,
13
- fullDate,
14
- inRange,
15
- isoToday,
16
- monthGrid,
17
- monthNames,
18
- monthOf,
19
- monthTitle,
20
- parseIso,
21
- parseMonth,
22
- resolveLocale,
23
- shiftMonth,
24
- weekStartFor,
25
- weekdayNames
26
- } from "./calendar-classes.js";
27
- import { isRtl, useIsoLayoutEffect } from "./client.js";
28
- import { ChevronLeftIcon, ChevronRightIcon } from "./icons.js";
29
- import { IconButton, Select } from "./primitives.js";
30
- import { useNishConfig, useNishStrings } from "./provider.js";
31
- import { jsx, jsxs } from "react/jsx-runtime";
32
- var warned = /* @__PURE__ */ new Set();
33
- var noopSubscribe = () => () => {
34
- };
35
- var EMPTY_GRID = Array.from({ length: 6 }, () => Array.from({ length: 7 }, () => null));
36
- var pad2 = (n) => String(n).padStart(2, "0");
37
- var pad4 = (n) => String(n).padStart(4, "0");
38
- function Calendar({
39
- value,
40
- defaultValue,
41
- onValueChange,
42
- month: monthProp,
43
- defaultMonth,
44
- onMonthChange,
45
- min: minProp,
46
- max: maxProp,
47
- isDateDisabled,
48
- locale,
49
- weekStartsOn,
50
- captionLayout = "label",
51
- fromYear,
52
- toYear,
53
- autoFocus = false,
54
- previousMonthLabel,
55
- nextMonthLabel,
56
- className,
57
- ref,
58
- ...rest
59
- }) {
60
- const strings = useNishStrings();
61
- const config = useNishConfig();
62
- const loc = resolveLocale(locale ?? config.locale ?? getDefaultLocale());
63
- const ws = weekStartsOn ?? weekStartFor(loc);
64
- const titleId = useId();
65
- const rootRef = useRef(null);
66
- const focusPending = useRef(false);
67
- const check = (prop, v, isMonth = false) => {
68
- if (v == null) return null;
69
- const ok = isMonth ? parseMonth(v) !== null : parseIso(v) !== null;
70
- if (ok) return v;
71
- if (process.env.NODE_ENV !== "production" && !warned.has(prop)) {
72
- warned.add(prop);
73
- console.warn(`Calendar: "${prop}" must be ${isMonth ? "YYYY-MM" : "YYYY-MM-DD"}`);
74
- }
75
- return null;
76
- };
77
- const valueIso = check("value", value);
78
- const defaultIso = check("defaultValue", defaultValue);
79
- const min = check("min", minProp);
80
- const max = check("max", maxProp);
81
- const monthControlled = check("month", monthProp, true);
82
- const defaultMonthIso = check("defaultMonth", defaultMonth, true);
4
+ import { useState } from "react";
5
+ import { monthOf } from "./calendar-classes.js";
6
+ import { CalendarBase, checkIso } from "./calendar-base.js";
7
+ import { jsx } from "react/jsx-runtime";
8
+ function Calendar({ value, defaultValue, onValueChange, ...rest }) {
9
+ const valueIso = checkIso("Calendar", "value", value);
10
+ const defaultIso = checkIso("Calendar", "defaultValue", defaultValue);
83
11
  const controlled = value !== void 0;
84
- const [ownValue, setOwnValue] = useState(defaultIso);
85
- const selected = controlled ? valueIso : ownValue;
86
- const [ownMonth, setOwnMonth] = useState(() => defaultMonthIso ?? (selected ? monthOf(selected) : null));
87
- const [focusedDate, setFocusedDate] = useState(null);
88
- const today = useSyncExternalStore(
89
- noopSubscribe,
90
- () => isoToday(),
91
- () => null
92
- );
93
- const shown = monthControlled ?? ownMonth ?? (today ? monthOf(today) : null);
94
- const setMonth = (raw) => {
95
- const next = clampIso(raw, "0001-01", "9999-12");
96
- if (next === shown) return;
97
- if (monthProp === void 0) setOwnMonth(next);
98
- onMonthChange?.(next);
99
- };
100
- const disabled = (iso) => !inRange(iso, min, max) || isDateDisabled?.(iso) === true;
101
- const inShown = (iso) => iso !== null && shown !== null && monthOf(iso) === shown;
102
- let tabStop = null;
103
- if (shown) {
104
- if (inShown(focusedDate)) tabStop = focusedDate;
105
- else if (inShown(selected)) tabStop = selected;
106
- else if (inShown(today)) tabStop = today;
107
- else {
108
- const days = monthGrid(shown, ws).flat().filter((d) => d !== null);
109
- tabStop = days.find((d) => !disabled(d)) ?? days[0] ?? null;
12
+ const [own, setOwn] = useState(defaultIso);
13
+ const selected = controlled ? valueIso : own;
14
+ return /* @__PURE__ */ jsx(
15
+ CalendarBase,
16
+ {
17
+ ...rest,
18
+ componentName: "Calendar",
19
+ initialMonth: selected ? monthOf(selected) : null,
20
+ focusPreference: selected,
21
+ dayLook: (iso) => ({ filled: iso === selected, band: null, ariaSelected: iso === selected }),
22
+ onPick: (iso) => {
23
+ if (!controlled) setOwn(iso);
24
+ onValueChange?.(iso);
25
+ }
110
26
  }
111
- }
112
- useIsoLayoutEffect(() => {
113
- if (!focusPending.current) return;
114
- focusPending.current = false;
115
- if (!focusedDate) return;
116
- rootRef.current?.querySelector(`[data-date="${focusedDate}"]`)?.focus();
117
- });
118
- const setRoot = (node) => {
119
- rootRef.current = node;
120
- if (typeof ref === "function") ref(node);
121
- else if (ref) ref.current = node;
122
- };
123
- const pick = (iso) => {
124
- if (disabled(iso)) return;
125
- setFocusedDate(iso);
126
- if (!controlled) setOwnValue(iso);
127
- onValueChange?.(iso);
128
- };
129
- const onGridKey = (e) => {
130
- const from = e.target.closest?.("[data-date]")?.getAttribute("data-date") ?? tabStop;
131
- if (!from) return;
132
- const raw = calendarKeyTarget(e.key, e.shiftKey, from, ws, isRtl(e.currentTarget));
133
- if (!raw) return;
134
- e.preventDefault();
135
- const target = clampIso(raw, min ?? "0001-01-01", max ?? "9999-12-31");
136
- const moved = target !== focusedDate || monthOf(target) !== shown;
137
- setFocusedDate(target);
138
- setMonth(monthOf(target));
139
- if (moved) {
140
- focusPending.current = true;
141
- } else {
142
- e.currentTarget.querySelector(`[data-date="${target}"]`)?.focus();
143
- }
144
- };
145
- const grid = shown ? monthGrid(shown, ws) : EMPTY_GRID;
146
- const names = weekdayNames(loc, ws);
147
- const dropdowns = captionLayout === "dropdowns";
148
- const shownYear = shown ? Number(shown.slice(0, 4)) : null;
149
- const shownMonthNo = shown ? Number(shown.slice(5, 7)) : null;
150
- const prevDisabled = !shown || shown <= "0001-01" || min !== null && addDays(firstOfMonth(shown), -1) < min;
151
- const nextDisabled = !shown || shown >= "9999-12" || max !== null && firstOfMonth(shiftMonth(shown, 1)) > max;
152
- let years = [];
153
- if (dropdowns) {
154
- const baseYear = today ? Number(today.slice(0, 4)) : shownYear ?? 2026;
155
- const from = fromYear ?? (min ? Number(min.slice(0, 4)) : baseYear - 100);
156
- const to = toYear ?? (max ? Number(max.slice(0, 4)) : baseYear + 10);
157
- for (let y = from; y <= to; y++) years.push(y);
158
- if (shownYear !== null && !years.includes(shownYear)) years = [...years, shownYear].sort((a, b) => a - b);
159
- }
160
- return /* @__PURE__ */ jsxs("div", { ...rest, ref: setRoot, className: cn("inline-flex select-none flex-col gap-2", className), children: [
161
- /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-1", children: [
162
- /* @__PURE__ */ jsx(
163
- IconButton,
164
- {
165
- size: "sm",
166
- label: previousMonthLabel ?? strings.previousMonth,
167
- "aria-disabled": prevDisabled || void 0,
168
- className: "aria-disabled:cursor-not-allowed aria-disabled:opacity-40",
169
- onClick: () => !prevDisabled && shown && setMonth(shiftMonth(shown, -1)),
170
- children: /* @__PURE__ */ jsx(ChevronLeftIcon, { className: "rtl:-scale-x-100" })
171
- }
172
- ),
173
- /* @__PURE__ */ jsx("div", { id: titleId, "aria-live": "polite", className: cn("text-sm font-semibold text-th-hi", dropdowns && "sr-only"), children: shown ? monthTitle(shown, loc) : "" }),
174
- dropdowns && /* @__PURE__ */ jsxs("div", { className: "flex min-w-0 items-center gap-1", children: [
175
- /* @__PURE__ */ jsx(
176
- Select,
177
- {
178
- size: "sm",
179
- "aria-label": strings.month,
180
- value: shownMonthNo ?? "",
181
- disabled: !shown,
182
- onChange: (e) => shownYear !== null && setMonth(`${pad4(shownYear)}-${pad2(Number(e.target.value))}`),
183
- children: monthNames(loc).map((name, i) => /* @__PURE__ */ jsx("option", { value: i + 1, children: name }, i))
184
- }
185
- ),
186
- /* @__PURE__ */ jsx(
187
- Select,
188
- {
189
- size: "sm",
190
- "aria-label": strings.year,
191
- value: shownYear ?? "",
192
- disabled: !shown,
193
- onChange: (e) => shownMonthNo !== null && setMonth(`${pad4(Number(e.target.value))}-${pad2(shownMonthNo)}`),
194
- children: years.map((y) => /* @__PURE__ */ jsx("option", { value: y, children: y }, y))
195
- }
196
- )
197
- ] }),
198
- /* @__PURE__ */ jsx(
199
- IconButton,
200
- {
201
- size: "sm",
202
- label: nextMonthLabel ?? strings.nextMonth,
203
- "aria-disabled": nextDisabled || void 0,
204
- className: "aria-disabled:cursor-not-allowed aria-disabled:opacity-40",
205
- onClick: () => !nextDisabled && shown && setMonth(shiftMonth(shown, 1)),
206
- children: /* @__PURE__ */ jsx(ChevronRightIcon, { className: "rtl:-scale-x-100" })
207
- }
208
- )
209
- ] }),
210
- /* @__PURE__ */ jsxs("table", { role: "grid", "aria-labelledby": titleId, className: "border-collapse", onKeyDown: onGridKey, children: [
211
- /* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsx("tr", { children: names.map((n) => /* @__PURE__ */ jsx(
212
- "th",
213
- {
214
- scope: "col",
215
- abbr: n.long,
216
- className: "size-10 overflow-hidden px-0.5 py-0 text-center text-xs font-medium text-th-hi3 pointer-coarse:h-11 pointer-coarse:w-[min(2.75rem,calc((100vw_-_2.6rem)/7))]",
217
- children: n.short
218
- },
219
- n.long
220
- )) }) }),
221
- /* @__PURE__ */ jsx("tbody", { children: grid.map((row, r) => /* @__PURE__ */ jsx("tr", { children: row.map((iso, c) => {
222
- if (!iso) {
223
- return /* @__PURE__ */ jsx("td", { className: "p-0", children: /* @__PURE__ */ jsx(
224
- "div",
225
- {
226
- "aria-hidden": "true",
227
- className: "size-10 pointer-coarse:h-11 pointer-coarse:w-[min(2.75rem,calc((100vw_-_2.6rem)/7))]"
228
- }
229
- ) }, c);
230
- }
231
- const isSelected = iso === selected;
232
- const isDisabled = disabled(iso);
233
- const isTabStop = iso === tabStop;
234
- return /* @__PURE__ */ jsx("td", { role: "gridcell", "aria-selected": isSelected, className: "p-0", children: /* @__PURE__ */ jsx(
235
- "button",
236
- {
237
- type: "button",
238
- tabIndex: isTabStop ? 0 : -1,
239
- "data-date": iso,
240
- "data-autofocus": autoFocus && isTabStop ? "" : void 0,
241
- "aria-label": fullDate(iso, loc),
242
- "aria-current": iso === today ? "date" : void 0,
243
- "aria-disabled": isDisabled || void 0,
244
- "data-state": isSelected ? "active" : "inactive",
245
- "data-disabled": isDisabled ? "" : void 0,
246
- onClick: () => pick(iso),
247
- className: cn(
248
- "flex size-10 items-center justify-center rounded-lg text-sm tabular-nums text-th-hi2 transition-colors pointer-coarse:h-11 pointer-coarse:w-[min(2.75rem,calc((100vw_-_2.6rem)/7))]",
249
- !isSelected && !isDisabled && "hover:bg-th-elevated hover:text-th-hi",
250
- iso === today && !isSelected && "font-semibold text-th-accent ring-1 ring-inset ring-th-accent/50",
251
- isSelected && "bg-th-accent font-semibold text-th-on-accent forced-colors:bg-[Highlight] forced-colors:text-[HighlightText] forced-colors:forced-color-adjust-none",
252
- isDisabled && "cursor-not-allowed text-th-ghost line-through forced-colors:text-[GrayText]"
253
- ),
254
- children: dayNumber(iso, loc)
255
- }
256
- ) }, c);
257
- }) }, r)) })
258
- ] })
259
- ] });
27
+ );
260
28
  }
261
29
  export {
262
30
  Calendar