@xds/react 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Accordion.js +191 -33
- package/dist/Avatar.js +65 -6
- package/dist/AvatarStack.js +101 -6
- package/dist/Badge.js +61 -4
- package/dist/Banner.js +161 -5
- package/dist/Button.js +152 -24
- package/dist/Card.js +171 -12
- package/dist/Checkbox.js +138 -3
- package/dist/Chip.js +71 -4
- package/dist/ChipBar.js +36 -3
- package/dist/CountBadge.js +62 -9
- package/dist/DatePicker.js +421 -25
- package/dist/DateRangePicker.js +362 -40
- package/dist/Defaults.js +42 -7
- package/dist/Dialog.js +128 -24
- package/dist/Divider.js +29 -3
- package/dist/EmptyState.js +83 -3
- package/dist/Icon.js +74 -4
- package/dist/IconButton.js +113 -18
- package/dist/ListRow.js +180 -10
- package/dist/MediaCard.js +273 -11
- package/dist/Menu.js +234 -58
- package/dist/ProgressBar.js +63 -4
- package/dist/RadioGroup.js +173 -12
- package/dist/SearchField.js +148 -16
- package/dist/SectionHeader.js +121 -5
- package/dist/SegmentedControl.js +104 -5
- package/dist/Select.js +307 -55
- package/dist/Sidebar.js +212 -19
- package/dist/Skeleton.js +61 -5
- package/dist/Slider.js +142 -35
- package/dist/Spinner.js +93 -4
- package/dist/Switch.js +114 -3
- package/dist/Tabs.js +130 -14
- package/dist/Text.js +42 -4
- package/dist/TextField.js +209 -26
- package/dist/Thumbnail.js +146 -7
- package/dist/Toast.js +118 -30
- package/dist/Tooltip.js +154 -20
- package/dist/base.d.ts +11 -0
- package/dist/base.js +10 -0
- package/dist/modal-popover.js +32 -10
- package/dist/picture.d.ts +6 -4
- package/dist/picture.js +72 -17
- package/dist/portal.js +24 -4
- package/dist/presence.js +30 -9
- package/dist/preview.d.ts +14 -3
- package/dist/preview.js +118 -74
- package/dist/root.js +1 -0
- package/dist/trigger.d.ts +7 -0
- package/dist/trigger.js +41 -8
- package/eject/files/Accordion.tsx +157 -0
- package/eject/files/Avatar.tsx +41 -0
- package/eject/files/AvatarStack.tsx +47 -0
- package/eject/files/Badge.tsx +33 -0
- package/eject/files/Banner.tsx +70 -0
- package/eject/files/Button.tsx +88 -0
- package/eject/files/Card.tsx +85 -0
- package/eject/files/Checkbox.tsx +59 -0
- package/eject/files/Chip.tsx +43 -0
- package/eject/files/ChipBar.tsx +31 -0
- package/eject/files/CountBadge.tsx +50 -0
- package/eject/files/DatePicker.tsx +192 -0
- package/eject/files/DateRangePicker.tsx +271 -0
- package/eject/files/Dialog.tsx +108 -0
- package/eject/files/Divider.tsx +13 -0
- package/eject/files/EmptyState.tsx +39 -0
- package/eject/files/Icon.tsx +38 -0
- package/eject/files/IconButton.tsx +62 -0
- package/eject/files/ListRow.tsx +86 -0
- package/eject/files/MediaCard.tsx +111 -0
- package/eject/files/Menu.tsx +168 -0
- package/eject/files/ProgressBar.tsx +34 -0
- package/eject/files/RadioGroup.tsx +112 -0
- package/eject/files/SearchField.tsx +88 -0
- package/eject/files/SectionHeader.tsx +56 -0
- package/eject/files/SegmentedControl.tsx +57 -0
- package/eject/files/Select.tsx +228 -0
- package/eject/files/Sidebar.tsx +149 -0
- package/eject/files/Skeleton.tsx +28 -0
- package/eject/files/Slider.tsx +109 -0
- package/eject/files/Spinner.tsx +36 -0
- package/eject/files/Switch.tsx +53 -0
- package/eject/files/Tabs.tsx +71 -0
- package/eject/files/Text.tsx +31 -0
- package/eject/files/TextField.tsx +122 -0
- package/eject/files/Thumbnail.tsx +66 -0
- package/eject/files/Toast.tsx +88 -0
- package/eject/files/Tooltip.tsx +120 -0
- package/eject/files/accordion.css +89 -0
- package/eject/files/accordion.ts +67 -0
- package/eject/files/avatar-stack.ts +57 -0
- package/eject/files/avatar.ts +42 -0
- package/eject/files/badge.ts +30 -0
- package/eject/files/banner.ts +68 -0
- package/eject/files/base.css +38 -0
- package/eject/files/button.css +241 -0
- package/eject/files/button.ts +106 -0
- package/eject/files/card.ts +58 -0
- package/eject/files/checkbox.ts +32 -0
- package/eject/files/chip-bar.ts +16 -0
- package/eject/files/chip.ts +35 -0
- package/eject/files/count-badge.ts +32 -0
- package/eject/files/date-picker.css +352 -0
- package/eject/files/date-picker.ts +250 -0
- package/eject/files/date-range-picker.ts +323 -0
- package/eject/files/dialog.css +139 -0
- package/eject/files/dialog.ts +15 -0
- package/eject/files/divider.ts +5 -0
- package/eject/files/empty-state.ts +22 -0
- package/eject/files/icon.ts +36 -0
- package/eject/files/list-row.ts +66 -0
- package/eject/files/media-card.ts +66 -0
- package/eject/files/menu.css +176 -0
- package/eject/files/menu.ts +84 -0
- package/eject/files/parts.ts +17 -0
- package/eject/files/progress-bar.ts +23 -0
- package/eject/files/radio-group.css +160 -0
- package/eject/files/radio-group.ts +24 -0
- package/eject/files/section-header.ts +32 -0
- package/eject/files/segmented-control.ts +30 -0
- package/eject/files/select.css +285 -0
- package/eject/files/select.ts +36 -0
- package/eject/files/sidebar.ts +75 -0
- package/eject/files/skeleton.ts +51 -0
- package/eject/files/slider.css +136 -0
- package/eject/files/slider.ts +65 -0
- package/eject/files/spinner.ts +22 -0
- package/eject/files/switch.ts +34 -0
- package/eject/files/tabs.css +112 -0
- package/eject/files/tabs.ts +35 -0
- package/eject/files/text-field.css +229 -0
- package/eject/files/text-field.ts +48 -0
- package/eject/files/text.ts +38 -0
- package/eject/files/thumbnail.ts +51 -0
- package/eject/files/toast.ts +31 -0
- package/eject/files/tooltip.css +104 -0
- package/eject/files/tooltip.ts +21 -0
- package/eject/manifest.json +814 -0
- package/package.json +9 -4
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Ejected from `@xds/react` 0.2.0 by `xds eject`. This copy is the app's own: change it
|
|
2
|
+
// as you need. `xds eject --list` says which ejected files differ from what was copied.
|
|
3
|
+
|
|
4
|
+
import type { RootProps } from "@xds/react/base";
|
|
5
|
+
import { type ChipBaseProps, chipClass, chipIcon } from "./chip.ts";
|
|
6
|
+
import { Icon } from "./Icon.tsx";
|
|
7
|
+
import { classes, rootPart } from "./parts.ts";
|
|
8
|
+
|
|
9
|
+
export type ChipProps = ChipBaseProps &
|
|
10
|
+
RootProps & {
|
|
11
|
+
onPress?: () => void;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A choice that filters what is shown. Unlike a Badge it is pressed. A selected chip is tinted,
|
|
16
|
+
* outlined and marked with a check, so the state does not depend on color.
|
|
17
|
+
*
|
|
18
|
+
* It is a toggle button: assistive technology hears whether it is pressed.
|
|
19
|
+
*/
|
|
20
|
+
export function Chip({
|
|
21
|
+
label,
|
|
22
|
+
selected = false,
|
|
23
|
+
onPress,
|
|
24
|
+
iconStart,
|
|
25
|
+
disabled = false,
|
|
26
|
+
className,
|
|
27
|
+
style,
|
|
28
|
+
}: ChipProps) {
|
|
29
|
+
const icon = chipIcon({ selected, iconStart });
|
|
30
|
+
return (
|
|
31
|
+
<button
|
|
32
|
+
type="button"
|
|
33
|
+
aria-pressed={selected}
|
|
34
|
+
disabled={disabled}
|
|
35
|
+
onClick={onPress}
|
|
36
|
+
className={classes(rootPart("Chip"), chipClass(selected), className)}
|
|
37
|
+
style={style}
|
|
38
|
+
>
|
|
39
|
+
{icon && <Icon name={icon} size="sm" />}
|
|
40
|
+
{label}
|
|
41
|
+
</button>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Ejected from `@xds/react` 0.2.0 by `xds eject`. This copy is the app's own: change it
|
|
2
|
+
// as you need. `xds eject --list` says which ejected files differ from what was copied.
|
|
3
|
+
|
|
4
|
+
import type { RootProps } from "@xds/react/base";
|
|
5
|
+
import type { ReactNode } from "react";
|
|
6
|
+
import { type ChipBarBaseProps, chipBarClass } from "./chip-bar.ts";
|
|
7
|
+
import { classes, rootPart } from "./parts.ts";
|
|
8
|
+
|
|
9
|
+
export type ChipBarProps = ChipBarBaseProps &
|
|
10
|
+
RootProps & {
|
|
11
|
+
/** The chips. Which of them are selected is the app's business: the bar holds no state. */
|
|
12
|
+
children: ReactNode;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* One row of chips. It scrolls sideways when the chips do not fit and never wraps, so the
|
|
17
|
+
* content under it stays where it is.
|
|
18
|
+
*/
|
|
19
|
+
export function ChipBar({ label, className, style, children }: ChipBarProps) {
|
|
20
|
+
return (
|
|
21
|
+
// A group, not a fieldset: a fieldset cannot be a scrolling flex row in every browser.
|
|
22
|
+
<div
|
|
23
|
+
role="group"
|
|
24
|
+
aria-label={label}
|
|
25
|
+
className={classes(rootPart("ChipBar"), chipBarClass, className)}
|
|
26
|
+
style={style}
|
|
27
|
+
>
|
|
28
|
+
{children}
|
|
29
|
+
</div>
|
|
30
|
+
);
|
|
31
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// Ejected from `@xds/react` 0.2.0 by `xds eject`. This copy is the app's own: change it
|
|
2
|
+
// as you need. `xds eject --list` says which ejected files differ from what was copied.
|
|
3
|
+
|
|
4
|
+
import type { RootProps } from "@xds/react/base";
|
|
5
|
+
import type { ReactNode } from "react";
|
|
6
|
+
import {
|
|
7
|
+
type CountBadgeBaseProps,
|
|
8
|
+
countBadgeAnchorClass,
|
|
9
|
+
countBadgeClass,
|
|
10
|
+
countBadgeText,
|
|
11
|
+
} from "./count-badge.ts";
|
|
12
|
+
import { classes, rootPart } from "./parts.ts";
|
|
13
|
+
|
|
14
|
+
export type CountBadgeProps = CountBadgeBaseProps &
|
|
15
|
+
RootProps & {
|
|
16
|
+
/** What the count belongs to: an icon, an avatar, a button. The badge sits on its corner. */
|
|
17
|
+
children?: ReactNode;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A count of something waiting: unread messages, items in a cart. On its own it is laid out
|
|
22
|
+
* inline; around a child it sits on the child's top end corner.
|
|
23
|
+
*/
|
|
24
|
+
export function CountBadge({ count, max, label, className, style, children }: CountBadgeProps) {
|
|
25
|
+
// On its own the badge is the root; around a child the anchor is.
|
|
26
|
+
const anchored = children !== undefined;
|
|
27
|
+
const badge = count > 0 && (
|
|
28
|
+
<span
|
|
29
|
+
{...(label ? { role: "img", "aria-label": label } : {})}
|
|
30
|
+
className={
|
|
31
|
+
anchored
|
|
32
|
+
? countBadgeClass(true)
|
|
33
|
+
: classes(rootPart("CountBadge"), countBadgeClass(false), className)
|
|
34
|
+
}
|
|
35
|
+
style={anchored ? undefined : style}
|
|
36
|
+
>
|
|
37
|
+
{countBadgeText(count, max)}
|
|
38
|
+
</span>
|
|
39
|
+
);
|
|
40
|
+
if (!anchored) return badge || null;
|
|
41
|
+
return (
|
|
42
|
+
<span
|
|
43
|
+
className={classes(rootPart("CountBadge"), countBadgeAnchorClass, className)}
|
|
44
|
+
style={style}
|
|
45
|
+
>
|
|
46
|
+
{children}
|
|
47
|
+
{badge}
|
|
48
|
+
</span>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
// Ejected from `@xds/react` 0.2.0 by `xds eject`. This copy is the app's own: change it
|
|
2
|
+
// as you need. `xds eject --list` says which ejected files differ from what was copied.
|
|
3
|
+
|
|
4
|
+
import { Portal, type RootProps, useModalPopover, usePresence } from "@xds/react/base";
|
|
5
|
+
import * as datePicker from "@zag-js/date-picker";
|
|
6
|
+
import { mergeProps, normalizeProps, useMachine } from "@zag-js/react";
|
|
7
|
+
import { type MouseEvent, useEffect, useId, useRef } from "react";
|
|
8
|
+
import {
|
|
9
|
+
DATE_PICKER_NAV_CLASS,
|
|
10
|
+
type DatePickerBaseProps,
|
|
11
|
+
datePickerContentAttrs,
|
|
12
|
+
datePickerIds,
|
|
13
|
+
datePickerLocale,
|
|
14
|
+
datePickerMachineProps,
|
|
15
|
+
datePickerTriggerAttrs,
|
|
16
|
+
focusOpeningDay,
|
|
17
|
+
formatDay,
|
|
18
|
+
type IsoDate,
|
|
19
|
+
keepCalendarFocus,
|
|
20
|
+
keepDayFocused,
|
|
21
|
+
} from "./date-picker.ts";
|
|
22
|
+
import { Icon } from "./Icon.tsx";
|
|
23
|
+
import { classes } from "./parts.ts";
|
|
24
|
+
|
|
25
|
+
export type { IsoDate } from "./date-picker.ts";
|
|
26
|
+
|
|
27
|
+
export type DatePickerProps = DatePickerBaseProps &
|
|
28
|
+
RootProps & {
|
|
29
|
+
onChange: (value: IsoDate) => void;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A field for choosing one day. Closed, it looks like a Select: the chosen day in the locale's
|
|
34
|
+
* medium form, or the placeholder. Pressed, it opens a calendar of one month under itself, on
|
|
35
|
+
* the chosen day, or on today while none is. A press on a day, or Enter on the focused one,
|
|
36
|
+
* calls `onChange` with the day as `YYYY-MM-DD` and closes the calendar; Escape or a press
|
|
37
|
+
* outside closes it without a choice.
|
|
38
|
+
*
|
|
39
|
+
* The field is a button that says it opens a dialog and whether it is open. In the calendar the
|
|
40
|
+
* arrow keys move a day or a week, Page Up and Page Down a month (a year with Shift), and Home
|
|
41
|
+
* and End to the first and the last day of the month, never past `min` or `max`. Tab goes round
|
|
42
|
+
* the month buttons and the focused day. The chosen day is selected for assistive technology,
|
|
43
|
+
* and today is the current date.
|
|
44
|
+
*/
|
|
45
|
+
export function DatePicker({ onChange, className, style, ...props }: DatePickerProps) {
|
|
46
|
+
const { label, value, placeholder, description, error, name } = props;
|
|
47
|
+
const id = useId();
|
|
48
|
+
const ids = datePickerIds(id);
|
|
49
|
+
const locale = datePickerLocale();
|
|
50
|
+
const service = useMachine(
|
|
51
|
+
datePicker.machine,
|
|
52
|
+
datePickerMachineProps(props, { id, locale, onChange }),
|
|
53
|
+
);
|
|
54
|
+
const api = datePicker.connect(service, normalizeProps);
|
|
55
|
+
const trigger = api.getTriggerProps();
|
|
56
|
+
const positioner = useRef<HTMLDivElement>(null);
|
|
57
|
+
const surface = useRef<HTMLDivElement>(null);
|
|
58
|
+
useModalPopover(api.open, () => positioner.current);
|
|
59
|
+
const present = usePresence(api.open, () => [surface.current]);
|
|
60
|
+
const focused = api.focusedValue.toString();
|
|
61
|
+
const month = api.visibleRange.start.toString();
|
|
62
|
+
useEffect(() => keepDayFocused(api.open, surface.current), [api.open, focused, month]);
|
|
63
|
+
const invalid = error !== undefined;
|
|
64
|
+
return (
|
|
65
|
+
<div
|
|
66
|
+
{...mergeProps(api.getRootProps(), {
|
|
67
|
+
className: classes("{{prefix}}-date-picker", className),
|
|
68
|
+
style,
|
|
69
|
+
})}
|
|
70
|
+
>
|
|
71
|
+
{/* A span: the field is a button, which a <label> cannot point at. */}
|
|
72
|
+
<span id={ids.label} className="{{prefix}}-date-picker__label">
|
|
73
|
+
{label}
|
|
74
|
+
</span>
|
|
75
|
+
{/* What Zag places the calendar against. */}
|
|
76
|
+
<div {...api.getControlProps()} className="{{prefix}}-date-picker__control">
|
|
77
|
+
<button
|
|
78
|
+
{...trigger}
|
|
79
|
+
{...datePickerTriggerAttrs(props, ids)}
|
|
80
|
+
type="button"
|
|
81
|
+
onClick={(event: MouseEvent<HTMLButtonElement>) => {
|
|
82
|
+
focusOpeningDay(api, props);
|
|
83
|
+
trigger.onClick?.(event);
|
|
84
|
+
}}
|
|
85
|
+
className="{{prefix}}-date-picker__trigger"
|
|
86
|
+
>
|
|
87
|
+
<span id={ids.value} className="{{prefix}}-date-picker__value">
|
|
88
|
+
{value === undefined ? placeholder : formatDay(value, locale)}
|
|
89
|
+
</span>
|
|
90
|
+
<Icon name="calendar" size="sm" className="text-xds-field-placeholder" />
|
|
91
|
+
</button>
|
|
92
|
+
</div>
|
|
93
|
+
{/* One line under the box: the help, or the error in its place. */}
|
|
94
|
+
{(invalid || description) && (
|
|
95
|
+
<div className="{{prefix}}-date-picker__footer">
|
|
96
|
+
{invalid ? (
|
|
97
|
+
<span id={ids.error} className="{{prefix}}-date-picker__error">
|
|
98
|
+
{error}
|
|
99
|
+
</span>
|
|
100
|
+
) : (
|
|
101
|
+
<span id={ids.description} className="{{prefix}}-date-picker__description">
|
|
102
|
+
{description}
|
|
103
|
+
</span>
|
|
104
|
+
)}
|
|
105
|
+
</div>
|
|
106
|
+
)}
|
|
107
|
+
{/* What a form submits. Zag gives the name to its text input, which is not drawn. */}
|
|
108
|
+
{name !== undefined && <input type="hidden" name={name} value={value ?? ""} />}
|
|
109
|
+
<Portal>
|
|
110
|
+
{api.open && (
|
|
111
|
+
// Takes a press outside, which then only closes the calendar. The focus stays in the
|
|
112
|
+
// calendar until it hands it back to the field.
|
|
113
|
+
// oxlint-disable-next-line jsx-a11y/no-static-element-interactions -- not a control: the handler only keeps the focus where it is
|
|
114
|
+
<div
|
|
115
|
+
className="{{prefix}}-date-picker__underlay"
|
|
116
|
+
onMouseDown={(event) => event.preventDefault()}
|
|
117
|
+
/>
|
|
118
|
+
)}
|
|
119
|
+
<div
|
|
120
|
+
{...api.getPositionerProps()}
|
|
121
|
+
ref={positioner}
|
|
122
|
+
className="{{prefix}}-date-picker__positioner"
|
|
123
|
+
>
|
|
124
|
+
{/* Shown until its exit animation ends; Zag would hide it as it closes. */}
|
|
125
|
+
{/* oxlint-disable-next-line jsx-a11y/no-static-element-interactions -- a dialog: datePickerContentAttrs gives it the role */}
|
|
126
|
+
<div
|
|
127
|
+
{...api.getContentProps()}
|
|
128
|
+
{...datePickerContentAttrs(ids)}
|
|
129
|
+
ref={surface}
|
|
130
|
+
hidden={!present}
|
|
131
|
+
onKeyDown={(event) => keepCalendarFocus(event, surface.current)}
|
|
132
|
+
className="{{prefix}}-date-picker__content"
|
|
133
|
+
>
|
|
134
|
+
<div className="{{prefix}}-date-picker__header">
|
|
135
|
+
<button
|
|
136
|
+
{...api.getPreviousTriggerProps()}
|
|
137
|
+
type="button"
|
|
138
|
+
className={DATE_PICKER_NAV_CLASS}
|
|
139
|
+
>
|
|
140
|
+
<Icon name="chevron-left" size="sm" />
|
|
141
|
+
</button>
|
|
142
|
+
<span {...api.getRangeTextProps()} className="{{prefix}}-date-picker__heading">
|
|
143
|
+
{api.visibleRangeText.start}
|
|
144
|
+
</span>
|
|
145
|
+
<button
|
|
146
|
+
{...api.getNextTriggerProps()}
|
|
147
|
+
type="button"
|
|
148
|
+
className={DATE_PICKER_NAV_CLASS}
|
|
149
|
+
>
|
|
150
|
+
<Icon name="chevron-right" size="sm" />
|
|
151
|
+
</button>
|
|
152
|
+
</div>
|
|
153
|
+
<table {...api.getTableProps({ view: "day" })} className="{{prefix}}-date-picker__grid">
|
|
154
|
+
<thead {...api.getTableHeadProps({ view: "day" })}>
|
|
155
|
+
<tr {...api.getTableRowProps({ view: "day" })}>
|
|
156
|
+
{api.weekDays.map((weekDay) => (
|
|
157
|
+
<th key={weekDay.long} scope="col" className="{{prefix}}-date-picker__weekday">
|
|
158
|
+
{weekDay.narrow}
|
|
159
|
+
</th>
|
|
160
|
+
))}
|
|
161
|
+
</tr>
|
|
162
|
+
</thead>
|
|
163
|
+
<tbody {...api.getTableBodyProps({ view: "day" })}>
|
|
164
|
+
{/* Keyed by the day, not by the object: Zag hands out new dates on every
|
|
165
|
+
change, and a cell made anew would drop the focus it holds when a move is
|
|
166
|
+
held at `min` or `max`. */}
|
|
167
|
+
{api.weeks.map((week) => (
|
|
168
|
+
<tr key={week[0]?.toString()} {...api.getTableRowProps({ view: "day" })}>
|
|
169
|
+
{week.map((date) => (
|
|
170
|
+
<td
|
|
171
|
+
key={date.toString()}
|
|
172
|
+
{...api.getDayTableCellProps({ value: date })}
|
|
173
|
+
className="{{prefix}}-date-picker__cell"
|
|
174
|
+
>
|
|
175
|
+
<div
|
|
176
|
+
{...api.getDayTableCellTriggerProps({ value: date })}
|
|
177
|
+
className="{{prefix}}-date-picker__day"
|
|
178
|
+
>
|
|
179
|
+
{date.day}
|
|
180
|
+
</div>
|
|
181
|
+
</td>
|
|
182
|
+
))}
|
|
183
|
+
</tr>
|
|
184
|
+
))}
|
|
185
|
+
</tbody>
|
|
186
|
+
</table>
|
|
187
|
+
</div>
|
|
188
|
+
</div>
|
|
189
|
+
</Portal>
|
|
190
|
+
</div>
|
|
191
|
+
);
|
|
192
|
+
}
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
// Ejected from `@xds/react` 0.2.0 by `xds eject`. This copy is the app's own: change it
|
|
2
|
+
// as you need. `xds eject --list` says which ejected files differ from what was copied.
|
|
3
|
+
|
|
4
|
+
import { Portal, type RootProps, useModalPopover, usePresence } from "@xds/react/base";
|
|
5
|
+
import * as datePicker from "@zag-js/date-picker";
|
|
6
|
+
import { mergeProps, normalizeProps, useMachine } from "@zag-js/react";
|
|
7
|
+
import { type MouseEvent, useEffect, useId, useRef, useState } from "react";
|
|
8
|
+
import {
|
|
9
|
+
DATE_PICKER_NAV_CLASS,
|
|
10
|
+
datePickerContentAttrs,
|
|
11
|
+
datePickerIds,
|
|
12
|
+
datePickerLocale,
|
|
13
|
+
datePickerTriggerAttrs,
|
|
14
|
+
focusOpeningDay,
|
|
15
|
+
keepCalendarFocus,
|
|
16
|
+
keepDayFocused,
|
|
17
|
+
} from "./date-picker.ts";
|
|
18
|
+
import {
|
|
19
|
+
type DateRange,
|
|
20
|
+
type DateRangePickerBaseProps,
|
|
21
|
+
dateRangeMachineProps,
|
|
22
|
+
endRangeChoiceOnClose,
|
|
23
|
+
formatRange,
|
|
24
|
+
keepRangePreview,
|
|
25
|
+
type RangeDraft,
|
|
26
|
+
rangeDayClick,
|
|
27
|
+
rangeGridKeyDown,
|
|
28
|
+
rangeMonths,
|
|
29
|
+
rangeMonthViews,
|
|
30
|
+
rangeSummary,
|
|
31
|
+
shownRange,
|
|
32
|
+
} from "./date-range-picker.ts";
|
|
33
|
+
import { Icon } from "./Icon.tsx";
|
|
34
|
+
import { classes } from "./parts.ts";
|
|
35
|
+
|
|
36
|
+
export type { DateRange } from "./date-range-picker.ts";
|
|
37
|
+
|
|
38
|
+
export type DateRangePickerProps = DateRangePickerBaseProps &
|
|
39
|
+
RootProps & {
|
|
40
|
+
onChange: (value: DateRange) => void;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A field for choosing a range of days, a start and an end, as for a stay. Closed, it looks like
|
|
45
|
+
* a DatePicker and shows both days in the locale's medium form ("Mar 10 – 14, 2026"), or the
|
|
46
|
+
* placeholder. Pressed, it opens a calendar under itself, two months side by side where the
|
|
47
|
+
* window has room and one where it does not, on the start's month, or on today's while nothing
|
|
48
|
+
* is chosen.
|
|
49
|
+
*
|
|
50
|
+
* The first day pressed starts the range, and the band previews it to the day under the pointer
|
|
51
|
+
* or the keyboard's focus. The second ends it, if the days between are at least `minLength` and
|
|
52
|
+
* at most `maxLength`, which leave the other days unavailable meanwhile: `onChange` gets the
|
|
53
|
+
* range and the calendar closes. A day before the start starts again from there. Escape or a
|
|
54
|
+
* press outside closes the calendar without a change.
|
|
55
|
+
*
|
|
56
|
+
* The keys are DatePicker's, and Page Up and Page Down move a month whether one or two are
|
|
57
|
+
* shown. Each month is a grid named by its month; every day of the range is selected, and the
|
|
58
|
+
* two ends are named by `startLabel` and `endLabel` before the day.
|
|
59
|
+
*/
|
|
60
|
+
export function DateRangePicker({ onChange, className, style, ...props }: DateRangePickerProps) {
|
|
61
|
+
const { label, value, placeholder, description, error, startName, endName } = props;
|
|
62
|
+
const id = useId();
|
|
63
|
+
const ids = datePickerIds(id);
|
|
64
|
+
const locale = datePickerLocale();
|
|
65
|
+
// A start alone while the end is chosen; null while the calendar shows the chosen range.
|
|
66
|
+
const [draft, setDraft] = useState<RangeDraft | null>(null);
|
|
67
|
+
const [months, setMonths] = useState(1);
|
|
68
|
+
const shown = shownRange(value, draft);
|
|
69
|
+
const service = useMachine(
|
|
70
|
+
datePicker.machine,
|
|
71
|
+
dateRangeMachineProps(props, shown, { id, locale, months, onDraft: setDraft, onChange }),
|
|
72
|
+
);
|
|
73
|
+
const api = datePicker.connect(service, normalizeProps);
|
|
74
|
+
const trigger = api.getTriggerProps();
|
|
75
|
+
const positioner = useRef<HTMLDivElement>(null);
|
|
76
|
+
const surface = useRef<HTMLDivElement>(null);
|
|
77
|
+
useModalPopover(api.open, () => positioner.current);
|
|
78
|
+
const present = usePresence(api.open, () => [surface.current]);
|
|
79
|
+
const focused = api.focusedValue.toString();
|
|
80
|
+
const month = api.visibleRange.start.toString();
|
|
81
|
+
useEffect(() => keepDayFocused(api.open, surface.current), [api.open, focused, month]);
|
|
82
|
+
// oxlint-disable-next-line react/exhaustive-deps -- only as the calendar opens or closes
|
|
83
|
+
useEffect(() => endRangeChoiceOnClose(service, api.open), [api.open]);
|
|
84
|
+
// oxlint-disable-next-line react/exhaustive-deps -- only once the focused day has moved
|
|
85
|
+
useEffect(() => keepRangePreview(service, shown, api.open), [focused]);
|
|
86
|
+
const summary = rangeSummary(shown, props, locale);
|
|
87
|
+
const invalid = error !== undefined;
|
|
88
|
+
return (
|
|
89
|
+
<div
|
|
90
|
+
{...mergeProps(api.getRootProps(), {
|
|
91
|
+
className: classes("{{prefix}}-date-picker {{prefix}}-date-range-picker", className),
|
|
92
|
+
style,
|
|
93
|
+
})}
|
|
94
|
+
>
|
|
95
|
+
{/* A span: the field is a button, which a <label> cannot point at. */}
|
|
96
|
+
<span id={ids.label} className="{{prefix}}-date-picker__label">
|
|
97
|
+
{label}
|
|
98
|
+
</span>
|
|
99
|
+
{/* What Zag places the calendar against. */}
|
|
100
|
+
<div {...api.getControlProps()} className="{{prefix}}-date-picker__control">
|
|
101
|
+
<button
|
|
102
|
+
{...trigger}
|
|
103
|
+
{...datePickerTriggerAttrs(props, ids)}
|
|
104
|
+
type="button"
|
|
105
|
+
onClick={(event: MouseEvent<HTMLButtonElement>) => {
|
|
106
|
+
if (!api.open) setMonths(rangeMonths());
|
|
107
|
+
focusOpeningDay(api, { ...props, value: value?.start });
|
|
108
|
+
trigger.onClick?.(event);
|
|
109
|
+
}}
|
|
110
|
+
className="{{prefix}}-date-picker__trigger"
|
|
111
|
+
>
|
|
112
|
+
<span id={ids.value} className="{{prefix}}-date-picker__value">
|
|
113
|
+
{value === undefined ? placeholder : formatRange(value, locale)}
|
|
114
|
+
</span>
|
|
115
|
+
<Icon name="calendar" size="sm" className="text-xds-field-placeholder" />
|
|
116
|
+
</button>
|
|
117
|
+
</div>
|
|
118
|
+
{/* One line under the box: the help, or the error in its place. */}
|
|
119
|
+
{(invalid || description) && (
|
|
120
|
+
<div className="{{prefix}}-date-picker__footer">
|
|
121
|
+
{invalid ? (
|
|
122
|
+
<span id={ids.error} className="{{prefix}}-date-picker__error">
|
|
123
|
+
{error}
|
|
124
|
+
</span>
|
|
125
|
+
) : (
|
|
126
|
+
<span id={ids.description} className="{{prefix}}-date-picker__description">
|
|
127
|
+
{description}
|
|
128
|
+
</span>
|
|
129
|
+
)}
|
|
130
|
+
</div>
|
|
131
|
+
)}
|
|
132
|
+
{/* What a form submits. Zag gives the names to its text inputs, which are not drawn. */}
|
|
133
|
+
{startName !== undefined && (
|
|
134
|
+
<input type="hidden" name={startName} value={value?.start ?? ""} />
|
|
135
|
+
)}
|
|
136
|
+
{endName !== undefined && <input type="hidden" name={endName} value={value?.end ?? ""} />}
|
|
137
|
+
<Portal>
|
|
138
|
+
{api.open && (
|
|
139
|
+
// Takes a press outside, which then only closes the calendar. The focus stays in the
|
|
140
|
+
// calendar until it hands it back to the field.
|
|
141
|
+
// oxlint-disable-next-line jsx-a11y/no-static-element-interactions -- not a control: the handler only keeps the focus where it is
|
|
142
|
+
<div
|
|
143
|
+
className="{{prefix}}-date-picker__underlay"
|
|
144
|
+
onMouseDown={(event) => event.preventDefault()}
|
|
145
|
+
/>
|
|
146
|
+
)}
|
|
147
|
+
<div
|
|
148
|
+
{...api.getPositionerProps()}
|
|
149
|
+
ref={positioner}
|
|
150
|
+
className="{{prefix}}-date-picker__positioner"
|
|
151
|
+
>
|
|
152
|
+
{/* Shown until its exit animation ends; Zag would hide it as it closes. */}
|
|
153
|
+
{/* oxlint-disable-next-line jsx-a11y/no-static-element-interactions -- a dialog: datePickerContentAttrs gives it the role */}
|
|
154
|
+
<div
|
|
155
|
+
{...api.getContentProps()}
|
|
156
|
+
{...datePickerContentAttrs(ids)}
|
|
157
|
+
ref={surface}
|
|
158
|
+
hidden={!present}
|
|
159
|
+
onKeyDown={(event) => keepCalendarFocus(event, surface.current)}
|
|
160
|
+
className="{{prefix}}-date-picker__content {{prefix}}-date-range-picker__content"
|
|
161
|
+
>
|
|
162
|
+
<div className="{{prefix}}-date-range-picker__summary" aria-live="polite">
|
|
163
|
+
<span>{summary.start}</span>
|
|
164
|
+
<span>{summary.end}</span>
|
|
165
|
+
</div>
|
|
166
|
+
<div className="{{prefix}}-date-range-picker__months">
|
|
167
|
+
{rangeMonthViews(api, months).map((view) => {
|
|
168
|
+
const table = api.getTableProps({ view: "day", id: view.table.id });
|
|
169
|
+
return (
|
|
170
|
+
<div key={view.index} className="{{prefix}}-date-range-picker__month">
|
|
171
|
+
<div className="{{prefix}}-date-picker__header">
|
|
172
|
+
{view.index === 0 ? (
|
|
173
|
+
<button
|
|
174
|
+
{...api.getPreviousTriggerProps()}
|
|
175
|
+
type="button"
|
|
176
|
+
className={DATE_PICKER_NAV_CLASS}
|
|
177
|
+
>
|
|
178
|
+
<Icon name="chevron-left" size="sm" />
|
|
179
|
+
</button>
|
|
180
|
+
) : (
|
|
181
|
+
<span className="{{prefix}}-date-range-picker__nav-space" />
|
|
182
|
+
)}
|
|
183
|
+
<span
|
|
184
|
+
{...api.getRangeTextProps()}
|
|
185
|
+
className="{{prefix}}-date-picker__heading"
|
|
186
|
+
>
|
|
187
|
+
{view.name}
|
|
188
|
+
</span>
|
|
189
|
+
{view.index === months - 1 ? (
|
|
190
|
+
<button
|
|
191
|
+
{...api.getNextTriggerProps()}
|
|
192
|
+
type="button"
|
|
193
|
+
className={DATE_PICKER_NAV_CLASS}
|
|
194
|
+
>
|
|
195
|
+
<Icon name="chevron-right" size="sm" />
|
|
196
|
+
</button>
|
|
197
|
+
) : (
|
|
198
|
+
<span className="{{prefix}}-date-range-picker__nav-space" />
|
|
199
|
+
)}
|
|
200
|
+
</div>
|
|
201
|
+
{/* oxlint-disable-next-line jsx-a11y/no-noninteractive-element-interactions -- a grid: Zag's props give it the role, and Enter on a day before the start is handled first */}
|
|
202
|
+
<table
|
|
203
|
+
{...table}
|
|
204
|
+
{...view.table}
|
|
205
|
+
onKeyDown={(event) =>
|
|
206
|
+
rangeGridKeyDown(event, api, shown, props, table.onKeyDown)
|
|
207
|
+
}
|
|
208
|
+
className="{{prefix}}-date-picker__grid"
|
|
209
|
+
>
|
|
210
|
+
<thead {...api.getTableHeadProps({ view: "day" })}>
|
|
211
|
+
<tr {...api.getTableRowProps({ view: "day" })}>
|
|
212
|
+
{api.weekDays.map((weekDay) => (
|
|
213
|
+
<th
|
|
214
|
+
key={weekDay.long}
|
|
215
|
+
scope="col"
|
|
216
|
+
className="{{prefix}}-date-picker__weekday"
|
|
217
|
+
>
|
|
218
|
+
{weekDay.narrow}
|
|
219
|
+
</th>
|
|
220
|
+
))}
|
|
221
|
+
</tr>
|
|
222
|
+
</thead>
|
|
223
|
+
<tbody {...api.getTableBodyProps({ view: "day" })}>
|
|
224
|
+
{/* Keyed by the day, as in DatePicker: a cell made anew would drop the
|
|
225
|
+
focus it holds when a move is held at `min` or `max`. */}
|
|
226
|
+
{view.weeks.map((week) => (
|
|
227
|
+
<tr key={week[0]?.toString()} {...api.getTableRowProps({ view: "day" })}>
|
|
228
|
+
{week.map((date) => {
|
|
229
|
+
const cell = { value: date, visibleRange: view.visibleRange };
|
|
230
|
+
const day = api.getDayTableCellTriggerProps(cell);
|
|
231
|
+
return (
|
|
232
|
+
<td
|
|
233
|
+
key={date.toString()}
|
|
234
|
+
{...api.getDayTableCellProps(cell)}
|
|
235
|
+
className="{{prefix}}-date-picker__cell"
|
|
236
|
+
>
|
|
237
|
+
{/* oxlint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions -- a button: Zag's props give it the role, and its grid takes the keys */}
|
|
238
|
+
<div
|
|
239
|
+
{...day}
|
|
240
|
+
onClick={(event) =>
|
|
241
|
+
rangeDayClick(
|
|
242
|
+
event,
|
|
243
|
+
api,
|
|
244
|
+
shown,
|
|
245
|
+
props,
|
|
246
|
+
date,
|
|
247
|
+
view.visibleRange,
|
|
248
|
+
day.onClick,
|
|
249
|
+
)
|
|
250
|
+
}
|
|
251
|
+
className="{{prefix}}-date-picker__day"
|
|
252
|
+
>
|
|
253
|
+
{date.day}
|
|
254
|
+
</div>
|
|
255
|
+
</td>
|
|
256
|
+
);
|
|
257
|
+
})}
|
|
258
|
+
</tr>
|
|
259
|
+
))}
|
|
260
|
+
</tbody>
|
|
261
|
+
</table>
|
|
262
|
+
</div>
|
|
263
|
+
);
|
|
264
|
+
})}
|
|
265
|
+
</div>
|
|
266
|
+
</div>
|
|
267
|
+
</div>
|
|
268
|
+
</Portal>
|
|
269
|
+
</div>
|
|
270
|
+
);
|
|
271
|
+
}
|