@nexub/react 7.3.0 → 7.5.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 (38) hide show
  1. package/accessibility.json +1 -1
  2. package/dist/components/Avatar/Avatar.d.ts +3 -0
  3. package/dist/components/Avatar/Avatar.js +28 -5
  4. package/dist/components/Avatar/index.d.ts +1 -1
  5. package/dist/components/Button/Button.d.ts +2 -1
  6. package/dist/components/Button/Button.js +37 -7
  7. package/dist/components/Combobox/Combobox.d.ts +6 -9
  8. package/dist/components/Combobox/Combobox.js +30 -9
  9. package/dist/components/DataTable/DataTable.d.ts +5 -1
  10. package/dist/components/DataTable/DataTable.js +6 -2
  11. package/dist/components/DatePicker/DatePicker.d.ts +16 -1
  12. package/dist/components/DatePicker/DatePicker.js +100 -26
  13. package/dist/components/FileUpload/FileUpload.js +86 -32
  14. package/dist/components/Input/Input.d.ts +6 -0
  15. package/dist/components/Input/Input.js +47 -5
  16. package/dist/components/Progress/Progress.d.ts +4 -0
  17. package/dist/components/Progress/Progress.js +73 -20
  18. package/dist/components/Select/Select.d.ts +18 -3
  19. package/dist/components/Select/Select.js +170 -71
  20. package/dist/components/Skeleton/Skeleton.d.ts +9 -0
  21. package/dist/components/Skeleton/Skeleton.js +18 -1
  22. package/dist/components/Skeleton/index.d.ts +2 -2
  23. package/dist/components/Skeleton/index.js +2 -1
  24. package/dist/components/Slider/Slider.d.ts +10 -0
  25. package/dist/components/Slider/Slider.js +48 -3
  26. package/dist/components/Slider/index.d.ts +1 -1
  27. package/dist/components/Switch/Switch.d.ts +6 -0
  28. package/dist/components/Switch/Switch.js +29 -2
  29. package/dist/components/Toast/Toaster.js +68 -23
  30. package/dist/components/Toast/index.d.ts +1 -1
  31. package/dist/components/Toast/useToast.d.ts +18 -1
  32. package/dist/components/Toast/useToast.js +14 -0
  33. package/dist/hooks/useAsyncAction.d.ts +27 -0
  34. package/dist/hooks/useAsyncAction.js +36 -0
  35. package/dist/index.d.ts +7 -4
  36. package/dist/index.js +4 -1
  37. package/dist/styles.css +1 -1
  38. package/package.json +6 -6
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "7.3.0",
2
+ "version": "7.5.0",
3
3
  "standard": "WCAG 2.2 Level AA",
4
4
  "method": {
5
5
  "automated": "axe-core rules tagged wcag2a, wcag2aa, wcag21a, wcag21aa and wcag22aa, run on every Storybook story in light and dark, and on open popups (menus, listboxes, dialogs), on every pull request; a new violation fails the build.",
@@ -3,11 +3,14 @@ import { type VariantProps } from 'class-variance-authority';
3
3
  export declare const avatarVariants: (props?: ({
4
4
  size?: "xs" | "sm" | "md" | "lg" | "xl" | null | undefined;
5
5
  } & import("class-variance-authority/types").ClassProp) | undefined) => string;
6
+ export type AvatarStatus = 'online' | 'away' | 'busy' | 'offline';
6
7
  export interface AvatarProps extends React.HTMLAttributes<HTMLSpanElement>, VariantProps<typeof avatarVariants> {
7
8
  src?: string;
8
9
  name?: string;
9
10
  fallback?: React.ReactNode;
10
11
  badge?: boolean;
11
12
  badgeColor?: string;
13
+ /** Presence dot, added to the accessible name ("Ada Lovelace, Away"). Replaces `badge` when both are set. */
14
+ status?: AvatarStatus;
12
15
  }
13
16
  export declare const Avatar: React.ForwardRefExoticComponent<AvatarProps & React.RefAttributes<HTMLSpanElement>>;
@@ -4,6 +4,7 @@ import * as React from "react";
4
4
  import { cva } from "class-variance-authority";
5
5
  import { cn } from "../../utils/cn.js";
6
6
  import { safeSrc } from "../../utils/safeHref.js";
7
+ import { useNexubMessages } from "../NexubProvider/NexubProvider.js";
7
8
  const avatarVariants = cva(
8
9
  "relative inline-flex items-center justify-center shrink-0 rounded-full overflow-hidden bg-[var(--color-surface-container-high)] text-[var(--color-on-surface-muted)] font-medium select-none",
9
10
  {
@@ -28,6 +29,12 @@ const badgeSizeMap = {
28
29
  lg: "h-3 w-3",
29
30
  xl: "h-3.5 w-3.5"
30
31
  };
32
+ const statusColor = {
33
+ online: "bg-[var(--color-success)]",
34
+ away: "bg-[var(--color-warning)]",
35
+ busy: "bg-[var(--color-error)]",
36
+ offline: "bg-[var(--color-outline)]"
37
+ };
31
38
  function getInitials(name) {
32
39
  const parts = name.trim().split(/\s+/);
33
40
  const first = parts[0] ?? "";
@@ -44,19 +51,30 @@ const Avatar = React.forwardRef(
44
51
  fallback,
45
52
  badge,
46
53
  badgeColor,
54
+ status,
47
55
  ...props
48
56
  }, ref) => {
49
57
  const src = safeSrc(srcProp);
50
58
  const [failedSrc, setFailedSrc] = React.useState();
59
+ const [pendingSrc, setPendingSrc] = React.useState();
60
+ const hideUntilLoaded = React.useCallback(
61
+ (img) => {
62
+ if (img && !img.complete) setPendingSrc(img.getAttribute("src") ?? void 0);
63
+ },
64
+ []
65
+ );
66
+ const messages = useNexubMessages();
67
+ const statusLabel = status ? messages.presence[status] : void 0;
68
+ const label = [name, statusLabel].filter(Boolean).join(", ") || void 0;
51
69
  const showImage = Boolean(src) && failedSrc !== src;
52
70
  const initials = name ? getInitials(name) : null;
53
- const labelled = Boolean(name || props["aria-label"] || props["aria-labelledby"]);
71
+ const labelled = Boolean(label || props["aria-label"] || props["aria-labelledby"]);
54
72
  return /* @__PURE__ */ jsxs(
55
73
  "span",
56
74
  {
57
75
  ref,
58
76
  className: cn(avatarVariants({ size }), className),
59
- "aria-label": name ?? void 0,
77
+ "aria-label": label,
60
78
  role: labelled ? "img" : void 0,
61
79
  "aria-hidden": labelled ? void 0 : true,
62
80
  ...props,
@@ -66,7 +84,12 @@ const Avatar = React.forwardRef(
66
84
  {
67
85
  src,
68
86
  alt: name ?? "",
69
- className: "h-full w-full object-cover",
87
+ ref: hideUntilLoaded,
88
+ className: cn(
89
+ "h-full w-full object-cover transition-opacity duration-effects-default ease-effects-default",
90
+ pendingSrc === src && "opacity-0"
91
+ ),
92
+ onLoad: () => setPendingSrc(void 0),
70
93
  onError: () => setFailedSrc(src)
71
94
  }
72
95
  ) : initials ? /* @__PURE__ */ jsx("span", { "aria-hidden": "true", children: initials }) : fallback ? fallback : (
@@ -82,13 +105,13 @@ const Avatar = React.forwardRef(
82
105
  }
83
106
  )
84
107
  ),
85
- badge && /* @__PURE__ */ jsx(
108
+ (badge || status) && /* @__PURE__ */ jsx(
86
109
  "span",
87
110
  {
88
111
  className: cn(
89
112
  "absolute bottom-0 end-0 rounded-full ring-2 ring-[var(--color-surface)]",
90
113
  badgeSizeMap[size ?? "md"],
91
- badgeColor ?? "bg-[var(--color-success)]"
114
+ status ? statusColor[status] : badgeColor ?? "bg-[var(--color-success)]"
92
115
  ),
93
116
  "aria-hidden": "true"
94
117
  }
@@ -1,2 +1,2 @@
1
1
  export { Avatar, avatarVariants } from './Avatar.js';
2
- export type { AvatarProps } from './Avatar.js';
2
+ export type { AvatarProps, AvatarStatus } from './Avatar.js';
@@ -14,7 +14,8 @@ export interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElemen
14
14
  /** Async feedback. `loading` blocks clicks (aria-disabled, focus kept),
15
15
  * `success` draws a check, `error` shakes once. Not applied with `asChild`. */
16
16
  status?: ButtonStatus;
17
- /** Alias for `status="loading"`. */
17
+ /** Alias for `status="loading"`. A `type="submit"` button also shows loading
18
+ * while its parent `<form action>` is pending (React 19 `useFormStatus`). */
18
19
  loading?: boolean;
19
20
  /** Announced while loading. @default 'Loading' */
20
21
  loadingLabel?: string;
@@ -1,6 +1,7 @@
1
1
  "use client";
2
2
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
3
3
  import * as React from "react";
4
+ import { useFormStatus } from "react-dom";
4
5
  import { Slot } from "@radix-ui/react-slot";
5
6
  import { Loader2 } from "@nexub/icons";
6
7
  import { cva } from "class-variance-authority";
@@ -67,6 +68,22 @@ const buttonVariants = cva(
67
68
  }
68
69
  );
69
70
  const CheckIcon = () => /* @__PURE__ */ jsx("svg", { className: "h-4 w-4", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2.5", strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: /* @__PURE__ */ jsx("path", { className: "nx-check-draw", pathLength: 1, d: "M20 6 9 17l-5-5" }) });
71
+ const SPINNER_DELAY = 150;
72
+ const SPINNER_MIN = 400;
73
+ function useSpinner(busy) {
74
+ const [shown, setShown] = React.useState(busy);
75
+ const shownAt = React.useRef(0);
76
+ React.useEffect(() => {
77
+ if (shown) shownAt.current = Date.now();
78
+ }, [shown]);
79
+ React.useEffect(() => {
80
+ if (busy === shown) return;
81
+ const wait = busy ? SPINNER_DELAY : Math.max(0, SPINNER_MIN - (Date.now() - shownAt.current));
82
+ const t = setTimeout(() => setShown(busy), wait);
83
+ return () => clearTimeout(t);
84
+ }, [busy, shown]);
85
+ return shown;
86
+ }
70
87
  const Button = React.forwardRef(
71
88
  ({
72
89
  className,
@@ -89,7 +106,9 @@ const Button = React.forwardRef(
89
106
  children,
90
107
  ...props
91
108
  }, ref) => {
92
- const status = statusProp ?? (loading ? "loading" : "idle");
109
+ const formPending = useFormStatus().pending && props.type === "submit";
110
+ const status = statusProp ?? (loading || formPending ? "loading" : "idle");
111
+ const spinner = useSpinner(status === "loading");
93
112
  const handlePointerMove = usePointerSheen(magnetic, onPointerMove);
94
113
  const sharedProps = {
95
114
  className: cn(
@@ -111,7 +130,7 @@ const Button = React.forwardRef(
111
130
  }
112
131
  const busy = status === "loading";
113
132
  const swapLabel = status === "success" && successLabel !== void 0;
114
- const overlay = busy ? /* @__PURE__ */ jsx(Loader2, { className: "h-4 w-4 animate-spin", "aria-hidden": "true" }) : status === "success" && !swapLabel ? /* @__PURE__ */ jsx(CheckIcon, {}) : null;
133
+ const overlay = spinner ? /* @__PURE__ */ jsx(Loader2, { className: "h-4 w-4 animate-spin", "aria-hidden": "true" }) : status === "success" && !swapLabel ? /* @__PURE__ */ jsx(CheckIcon, {}) : null;
115
134
  const announcement = status === "loading" ? loadingLabel : status === "success" ? successLabel ?? "Done" : status === "error" ? errorLabel : "";
116
135
  const button = /* @__PURE__ */ jsxs(
117
136
  "button",
@@ -130,14 +149,25 @@ const Button = React.forwardRef(
130
149
  "data-status": status === "idle" ? void 0 : status,
131
150
  ...sharedProps,
132
151
  children: [
133
- /* @__PURE__ */ jsx("span", { className: cn("contents", overlay && "text-transparent [&_img]:invisible [&_svg]:invisible"), children: swapLabel ? /* @__PURE__ */ jsxs(Fragment, { children: [
134
- /* @__PURE__ */ jsx(CheckIcon, {}),
135
- successLabel
136
- ] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
152
+ successLabel === void 0 ? /* @__PURE__ */ jsxs("span", { className: cn("contents", overlay && "text-transparent [&_img]:invisible [&_svg]:invisible"), children: [
137
153
  leadingIcon,
138
154
  children,
139
155
  trailingIcon
140
- ] }) }),
156
+ ] }) : (
157
+ // Both labels share one grid cell, so the button is as wide as the wider one
158
+ // whichever shows; the hidden one keeps its space.
159
+ /* @__PURE__ */ jsxs("span", { className: cn("inline-grid", overlay && "text-transparent [&_img]:invisible [&_svg]:invisible"), children: [
160
+ /* @__PURE__ */ jsxs("span", { className: cn("col-start-1 row-start-1 inline-flex items-center justify-center gap-2", swapLabel && "invisible"), "aria-hidden": swapLabel || void 0, children: [
161
+ leadingIcon,
162
+ children,
163
+ trailingIcon
164
+ ] }),
165
+ /* @__PURE__ */ jsxs("span", { className: cn("col-start-1 row-start-1 inline-flex items-center justify-center gap-2", !swapLabel && "invisible"), "aria-hidden": !swapLabel || void 0, children: [
166
+ swapLabel ? /* @__PURE__ */ jsx(CheckIcon, {}) : /* @__PURE__ */ jsx("span", { className: "h-4 w-4" }),
167
+ successLabel
168
+ ] })
169
+ ] })
170
+ ),
141
171
  overlay && /* @__PURE__ */ jsx("span", { className: "absolute inset-0 flex items-center justify-center", "aria-hidden": "true", children: overlay })
142
172
  ]
143
173
  }
@@ -1,4 +1,9 @@
1
1
  import * as React from 'react';
2
+ import { Command } from 'cmdk';
3
+ export interface ComboboxListProps extends React.ComponentProps<typeof Command.List> {
4
+ /** Options are loading: shows a loading row, sets aria-busy and hides Empty. */
5
+ loading?: boolean;
6
+ }
2
7
  export declare const Combobox: {
3
8
  Root: React.ForwardRefExoticComponent<Omit<{
4
9
  children?: React.ReactNode;
@@ -25,15 +30,7 @@ export declare const Combobox: {
25
30
  value?: string;
26
31
  onValueChange?: (search: string) => void;
27
32
  } & React.RefAttributes<HTMLInputElement>, "ref"> & React.RefAttributes<HTMLInputElement>>;
28
- List: React.ForwardRefExoticComponent<Omit<{
29
- children?: React.ReactNode;
30
- } & Pick<Pick<React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "key" | keyof React.HTMLAttributes<HTMLDivElement>> & {
31
- ref?: React.Ref<HTMLDivElement>;
32
- } & {
33
- asChild?: boolean;
34
- }, "asChild" | "key" | keyof React.HTMLAttributes<HTMLDivElement>> & {
35
- label?: string;
36
- } & React.RefAttributes<HTMLDivElement>, "ref"> & React.RefAttributes<HTMLDivElement>>;
33
+ List: React.ForwardRefExoticComponent<Omit<ComboboxListProps, "ref"> & React.RefAttributes<HTMLDivElement>>;
37
34
  Empty: React.ForwardRefExoticComponent<Omit<{
38
35
  children?: React.ReactNode;
39
36
  } & Pick<Pick<React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "key" | keyof React.HTMLAttributes<HTMLDivElement>> & {
@@ -1,8 +1,10 @@
1
1
  "use client";
2
- import { jsx } from "react/jsx-runtime";
2
+ import { jsx, jsxs } from "react/jsx-runtime";
3
3
  import * as React from "react";
4
4
  import { Command } from "cmdk";
5
5
  import { cn } from "../../utils/cn.js";
6
+ import { useNexubMessages } from "../NexubProvider/NexubProvider.js";
7
+ import { Spinner } from "../Spinner/Spinner.js";
6
8
  function scrollWithinList(el) {
7
9
  if (!el) return;
8
10
  el.scrollIntoView = () => {
@@ -47,14 +49,33 @@ const Input = React.forwardRef(
47
49
  );
48
50
  Input.displayName = "Combobox.Input";
49
51
  const List = React.forwardRef(
50
- ({ className, ...props }, ref) => /* @__PURE__ */ jsx(
51
- Command.List,
52
- {
53
- ref,
54
- className: cn("max-h-64 overflow-y-auto overflow-x-hidden p-1", className),
55
- ...props
56
- }
57
- )
52
+ ({ className, loading, children, ...props }, ref) => {
53
+ const messages = useNexubMessages();
54
+ return /* @__PURE__ */ jsxs(
55
+ Command.List,
56
+ {
57
+ ref,
58
+ "aria-busy": loading || void 0,
59
+ className: cn(
60
+ // cmdk measures its content into --cmdk-list-height; box-content keeps the padding outside it.
61
+ "box-content h-[var(--cmdk-list-height)] max-h-64 overflow-y-auto overflow-x-hidden p-1",
62
+ "transition-[height] duration-spatial-default ease-spatial-default motion-reduce:transition-none",
63
+ loading && "[&_[cmdk-empty]]:hidden",
64
+ className
65
+ ),
66
+ ...props,
67
+ children: [
68
+ loading && // Not Command.Loading: its role="progressbar" isn't allowed inside a listbox
69
+ // (axe aria-required-children). The listbox's aria-busy carries the state.
70
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2 px-2.5 py-2 text-body-sm text-[var(--color-on-surface-muted)]", children: [
71
+ /* @__PURE__ */ jsx(Spinner, { size: "sm", "aria-hidden": "true" }),
72
+ messages.loading
73
+ ] }),
74
+ children
75
+ ]
76
+ }
77
+ );
78
+ }
58
79
  );
59
80
  List.displayName = "Combobox.List";
60
81
  const Empty = React.forwardRef(
@@ -91,5 +91,9 @@ export interface DataTableProps<TData> extends React.HTMLAttributes<HTMLDivEleme
91
91
  onRowOrderChange?: (data: TData[]) => void;
92
92
  /** Names a row in "Move ‹label›" and the move announcements. @default its position */
93
93
  getRowLabel?: (row: TData) => string;
94
+ /** Shows skeleton rows (one page's worth) and sets aria-busy on the table. */
95
+ loading?: boolean;
96
+ /** Shown in a full-width row when there are no rows. @default messages.noResults */
97
+ emptyState?: React.ReactNode;
94
98
  }
95
- export declare function DataTable<TData>({ columns, data, enableSorting, enableRowSelection, selectionColumn, rowSelection: rowSelectionProp, onRowSelectionChange, getRowId, bulkActions, density: densityProp, defaultDensity, onDensityChange, columnVisibility: columnVisibilityProp, onColumnVisibilityChange, viewOptions, toolbar, enableColumnResizing, columnSizing: columnSizingProp, onColumnSizingChange, enableColumnReordering, columnOrder: columnOrderProp, onColumnOrderChange, columnPinning: columnPinningProp, onColumnPinningChange, maxHeight, grid, rowWindow, pagination, getSubRows, expanded: expandedProp, defaultExpanded, onExpandedChange, onRowOrderChange, getRowLabel, caption, captionHidden, className, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, ...props }: DataTableProps<TData>): React.JSX.Element;
99
+ export declare function DataTable<TData>({ columns, data, enableSorting, enableRowSelection, selectionColumn, rowSelection: rowSelectionProp, onRowSelectionChange, getRowId, bulkActions, density: densityProp, defaultDensity, onDensityChange, columnVisibility: columnVisibilityProp, onColumnVisibilityChange, viewOptions, toolbar, enableColumnResizing, columnSizing: columnSizingProp, onColumnSizingChange, enableColumnReordering, columnOrder: columnOrderProp, onColumnOrderChange, columnPinning: columnPinningProp, onColumnPinningChange, maxHeight, grid, rowWindow, pagination, getSubRows, expanded: expandedProp, defaultExpanded, onExpandedChange, onRowOrderChange, getRowLabel, loading, emptyState, caption, captionHidden, className, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, ...props }: DataTableProps<TData>): React.JSX.Element;
@@ -23,6 +23,7 @@ import { Button } from "../Button/Button.js";
23
23
  import { DropdownMenu } from "../DropdownMenu/DropdownMenu.js";
24
24
  import { createGridNavigation } from "./gridNavigation.js";
25
25
  import { useReorder } from "../Sortable/reorder.js";
26
+ import { Skeleton } from "../Skeleton/Skeleton.js";
26
27
  const alignText = {
27
28
  start: "text-start",
28
29
  end: "text-end",
@@ -170,6 +171,8 @@ function DataTable({
170
171
  onExpandedChange,
171
172
  onRowOrderChange,
172
173
  getRowLabel,
174
+ loading = false,
175
+ emptyState,
173
176
  caption,
174
177
  captionHidden = false,
175
178
  className,
@@ -429,6 +432,7 @@ function DataTable({
429
432
  "aria-label": ariaLabel,
430
433
  "aria-labelledby": ariaLabelledBy,
431
434
  "aria-rowcount": rowWin.virtual ? rows.length + 1 : void 0,
435
+ "aria-busy": loading || void 0,
432
436
  role: grid ? "grid" : void 0,
433
437
  onKeyDown: grid ? gridNav.onKeyDown : void 0,
434
438
  onFocus: grid ? gridNav.onFocus : void 0,
@@ -528,7 +532,7 @@ function DataTable({
528
532
  header.id
529
533
  );
530
534
  }) }, headerGroup.id)) }),
531
- /* @__PURE__ */ jsx(TableBody, { children: rows.length ? /* @__PURE__ */ jsxs(Fragment, { children: [
535
+ /* @__PURE__ */ jsx(TableBody, { children: loading ? Array.from({ length: pagination?.pageSize ?? 5 }, (_, i) => /* @__PURE__ */ jsx(TableRow, { "data-loading": "", className: "hover:bg-transparent", children: table.getVisibleLeafColumns().map((c) => /* @__PURE__ */ jsx(TableCell, { className: cn(compact && "py-1.5"), children: /* @__PURE__ */ jsx(Skeleton, {}) }, c.id)) }, i)) : rows.length ? /* @__PURE__ */ jsxs(Fragment, { children: [
532
536
  rowWin.before > 0 && /* @__PURE__ */ jsx(Spacer, { height: rowWin.before }),
533
537
  rowWin.indexes.map((i) => {
534
538
  const row = rows[i];
@@ -564,7 +568,7 @@ function DataTable({
564
568
  {
565
569
  colSpan: table.getVisibleLeafColumns().length,
566
570
  className: "h-24 text-center text-[var(--color-on-surface-muted)]",
567
- children: messages.noResults
571
+ children: emptyState ?? messages.noResults
568
572
  }
569
573
  ) }) })
570
574
  ]
@@ -1,5 +1,5 @@
1
1
  import * as React from 'react';
2
- import { type ChevronProps, type Labels } from 'react-day-picker';
2
+ import { type ChevronProps, type Labels, type Matcher } from 'react-day-picker';
3
3
  import { type Locale } from 'date-fns';
4
4
  /**
5
5
  * A date-fns Locale wins (date-fns 'PP'); else with a NexubProvider locale,
@@ -34,6 +34,21 @@ export interface DatePickerProps {
34
34
  'aria-label'?: string;
35
35
  /** date-fns Locale for the calendar and trigger text; wins over NexubProvider's `dateLocale`. */
36
36
  locale?: Locale;
37
+ /** Days that can't be picked (react-day-picker Matcher), e.g. `{ dayOfWeek: [0, 6] }`. */
38
+ disabledDates?: Matcher | Matcher[];
39
+ /** Quick picks under the calendar (Today, In a week…); choosing one sets the value and closes. */
40
+ presets?: {
41
+ label: string;
42
+ date: Date;
43
+ }[];
44
+ /**
45
+ * The trigger is a text input that also accepts a typed date in the locale's
46
+ * short format (date-fns `P`, e.g. 01/15/2024). Enter or blur commits; an
47
+ * invalid or unavailable date stays as typed, marked aria-invalid, and the
48
+ * value is unchanged. `id` and `aria-label` go to the input, `ref` to the
49
+ * calendar button.
50
+ */
51
+ typeable?: boolean;
37
52
  }
38
53
  export declare const DatePicker: React.ForwardRefExoticComponent<DatePickerProps & React.RefAttributes<HTMLButtonElement>>;
39
54
  export {};
@@ -2,13 +2,14 @@
2
2
  import { jsx, jsxs } from "react/jsx-runtime";
3
3
  import * as React from "react";
4
4
  import * as RadixPopover from "@radix-ui/react-popover";
5
- import { DayPicker } from "react-day-picker";
6
- import { format } from "date-fns";
5
+ import { DayPicker, dateMatchModifiers } from "react-day-picker";
6
+ import { format, isValid, parse } from "date-fns";
7
7
  import { CalendarDays, ChevronLeft, ChevronRight } from "@nexub/icons";
8
8
  import { cn } from "../../utils/cn.js";
9
9
  import { useIntlLocale, useLocale, useNexubMessages } from "../NexubProvider/NexubProvider.js";
10
- import { field, fieldFocusVisible } from "../../utils/control.js";
10
+ import { field, fieldDisabled, fieldFocusVisible, fieldFocusWithin, fieldInvalid } from "../../utils/control.js";
11
11
  import { floatingSurface } from "../../utils/surface.js";
12
+ import { Button } from "../Button/Button.js";
12
13
  function formatDate(date, locale, dateLocale) {
13
14
  if (dateLocale) return format(date, "PP", { locale: dateLocale });
14
15
  return locale ? new Intl.DateTimeFormat(locale, { dateStyle: "medium" }).format(date) : format(date, "PP");
@@ -71,7 +72,18 @@ const calendarClassNames = {
71
72
  today: "[&>button]:border-[var(--color-primary)]",
72
73
  outside: "[&:not([data-selected]):not([data-disabled])>button]:text-[var(--color-on-surface-muted)]",
73
74
  disabled: "",
74
- hidden: "invisible"
75
+ hidden: "invisible",
76
+ // Month change (animate): the months crossfade. react-day-picker adds each of
77
+ // these with classList.add, so one class apiece. Keyframes from @nexub/themes
78
+ // core.css; reduced motion collapses them and animationend still cleans up.
79
+ caption_after_enter: "animate-[nx-fade-in_var(--motion-spring-effects-default-duration)_var(--motion-spring-effects-default-easing)]",
80
+ caption_before_enter: "animate-[nx-fade-in_var(--motion-spring-effects-default-duration)_var(--motion-spring-effects-default-easing)]",
81
+ weeks_after_enter: "animate-[nx-fade-in_var(--motion-spring-effects-default-duration)_var(--motion-spring-effects-default-easing)]",
82
+ weeks_before_enter: "animate-[nx-fade-in_var(--motion-spring-effects-default-duration)_var(--motion-spring-effects-default-easing)]",
83
+ caption_after_exit: "animate-[nx-fade-out_var(--motion-spring-effects-fast-duration)_var(--motion-spring-effects-fast-easing)_forwards]",
84
+ caption_before_exit: "animate-[nx-fade-out_var(--motion-spring-effects-fast-duration)_var(--motion-spring-effects-fast-easing)_forwards]",
85
+ weeks_after_exit: "animate-[nx-fade-out_var(--motion-spring-effects-fast-duration)_var(--motion-spring-effects-fast-easing)_forwards]",
86
+ weeks_before_exit: "animate-[nx-fade-out_var(--motion-spring-effects-fast-duration)_var(--motion-spring-effects-fast-easing)_forwards]"
75
87
  };
76
88
  const DatePicker = React.forwardRef(
77
89
  function DatePicker2({
@@ -84,15 +96,75 @@ const DatePicker = React.forwardRef(
84
96
  className,
85
97
  id,
86
98
  "aria-label": ariaLabel,
87
- locale: localeProp
99
+ locale: localeProp,
100
+ disabledDates,
101
+ presets,
102
+ typeable = false
88
103
  }, ref) {
89
104
  const [open, setOpen] = React.useState(false);
90
105
  const messages = useNexubMessages();
91
106
  const placeholder = placeholderProp ?? messages.pickDate;
92
107
  const calendar = useCalendarLocale(localeProp);
93
108
  const display = value ? calendar.format(value) : placeholder;
109
+ const [text, setText] = React.useState(null);
110
+ const dateLocale = calendar.dayPickerProps.locale;
111
+ const typed = text?.trim() ? parse(text.trim(), "P", /* @__PURE__ */ new Date(), { locale: dateLocale }) : void 0;
112
+ const invalid = typed !== void 0 && (!isValid(typed) || minDate !== void 0 && typed < minDate || maxDate !== void 0 && typed > maxDate || disabledDates !== void 0 && dateMatchModifiers(typed, disabledDates));
113
+ const commit = () => {
114
+ if (text === null || invalid) return;
115
+ onChange?.(typed);
116
+ setText(null);
117
+ };
118
+ const pick = (date) => {
119
+ onChange?.(date);
120
+ setText(null);
121
+ setOpen(false);
122
+ };
94
123
  return /* @__PURE__ */ jsxs(RadixPopover.Root, { open, onOpenChange: setOpen, children: [
95
- /* @__PURE__ */ jsx(RadixPopover.Trigger, { asChild: true, children: /* @__PURE__ */ jsxs(
124
+ typeable ? /* @__PURE__ */ jsx(RadixPopover.Anchor, { asChild: true, children: /* @__PURE__ */ jsxs(
125
+ "div",
126
+ {
127
+ className: cn(
128
+ "flex h-control-md w-full items-center gap-1 ps-3.5 pe-1",
129
+ field,
130
+ fieldFocusWithin,
131
+ invalid && fieldInvalid,
132
+ disabled && fieldDisabled,
133
+ className
134
+ ),
135
+ children: [
136
+ /* @__PURE__ */ jsx(
137
+ "input",
138
+ {
139
+ id,
140
+ type: "text",
141
+ disabled,
142
+ "aria-label": ariaLabel,
143
+ "aria-invalid": invalid || void 0,
144
+ placeholder,
145
+ value: text ?? (value ? format(value, "P", { locale: dateLocale }) : ""),
146
+ onChange: (e) => setText(e.target.value),
147
+ onBlur: commit,
148
+ onKeyDown: (e) => {
149
+ if (e.key === "Enter") commit();
150
+ },
151
+ className: "min-w-0 flex-1 bg-transparent text-body-md outline-none placeholder:text-[var(--color-on-surface-muted)] disabled:cursor-not-allowed"
152
+ }
153
+ ),
154
+ /* @__PURE__ */ jsx(RadixPopover.Trigger, { asChild: true, children: /* @__PURE__ */ jsx(
155
+ "button",
156
+ {
157
+ ref,
158
+ type: "button",
159
+ disabled,
160
+ "aria-label": messages.pickDate,
161
+ className: "nx-state-layer inline-flex h-8 w-8 shrink-0 items-center justify-center rounded-full disabled:pointer-events-none",
162
+ children: /* @__PURE__ */ jsx(CalendarDays, { size: "sm", "aria-hidden": "true" })
163
+ }
164
+ ) })
165
+ ]
166
+ }
167
+ ) }) : /* @__PURE__ */ jsx(RadixPopover.Trigger, { asChild: true, children: /* @__PURE__ */ jsxs(
96
168
  "button",
97
169
  {
98
170
  ref,
@@ -115,7 +187,7 @@ const DatePicker = React.forwardRef(
115
187
  ]
116
188
  }
117
189
  ) }),
118
- /* @__PURE__ */ jsx(RadixPopover.Portal, { children: /* @__PURE__ */ jsx(
190
+ /* @__PURE__ */ jsx(RadixPopover.Portal, { children: /* @__PURE__ */ jsxs(
119
191
  RadixPopover.Content,
120
192
  {
121
193
  align: "start",
@@ -124,25 +196,27 @@ const DatePicker = React.forwardRef(
124
196
  floatingSurface,
125
197
  "nx-motion nx-pop origin-[var(--radix-popover-content-transform-origin)]"
126
198
  ),
127
- children: /* @__PURE__ */ jsx(
128
- DayPicker,
129
- {
130
- mode: "single",
131
- selected: value,
132
- defaultMonth: value,
133
- onSelect: (date) => {
134
- onChange?.(date);
135
- setOpen(false);
136
- },
137
- startMonth: minDate,
138
- endMonth: maxDate,
139
- autoFocus: true,
140
- showOutsideDays: true,
141
- className: "relative p-3",
142
- classNames: calendarClassNames,
143
- ...calendar.dayPickerProps
144
- }
145
- )
199
+ children: [
200
+ /* @__PURE__ */ jsx(
201
+ DayPicker,
202
+ {
203
+ mode: "single",
204
+ selected: value,
205
+ defaultMonth: value,
206
+ onSelect: pick,
207
+ disabled: disabledDates,
208
+ startMonth: minDate,
209
+ endMonth: maxDate,
210
+ autoFocus: true,
211
+ animate: true,
212
+ showOutsideDays: true,
213
+ className: "relative p-3",
214
+ classNames: calendarClassNames,
215
+ ...calendar.dayPickerProps
216
+ }
217
+ ),
218
+ presets && presets.length > 0 && /* @__PURE__ */ jsx("div", { className: "flex flex-wrap gap-1 border-t border-[var(--color-outline-variant)] p-2", children: presets.map((p) => /* @__PURE__ */ jsx(Button, { variant: "ghost", size: "xs", onClick: () => pick(p.date), children: p.label }, p.label)) })
219
+ ]
146
220
  }
147
221
  ) })
148
222
  ] });