@enablo/swirlcn-ui 0.0.0-stage → 0.5.1
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/LICENSE +21 -0
- package/README.md +30 -2
- package/dist/chunk-3DJWDNQL.js +77 -0
- package/dist/chunk-7XUCETA6.js +447 -0
- package/dist/chunk-KZESIYUN.js +18 -0
- package/dist/flip.d.ts +98 -0
- package/dist/flip.js +158 -0
- package/dist/fonts/Inter-italic.var.woff2 +0 -0
- package/dist/fonts/Inter.var.woff2 +0 -0
- package/dist/fonts/OFL.txt +96 -0
- package/dist/index.d.ts +2459 -0
- package/dist/index.js +8439 -0
- package/dist/server.d.ts +144 -0
- package/dist/server.js +88 -0
- package/dist/spacing-BQaEPbUv.d.ts +59 -0
- package/dist/theme.css +146 -0
- package/package.json +84 -3
- package/src/components/accordion.tsx +102 -0
- package/src/components/action-list.tsx +294 -0
- package/src/components/alert-dialog.tsx +155 -0
- package/src/components/autocomplete.tsx +313 -0
- package/src/components/avatar.tsx +258 -0
- package/src/components/badge.tsx +100 -0
- package/src/components/banner.tsx +112 -0
- package/src/components/button-group.tsx +87 -0
- package/src/components/button.tsx +362 -0
- package/src/components/card.tsx +186 -0
- package/src/components/carousel.tsx +245 -0
- package/src/components/checkbox.tsx +125 -0
- package/src/components/chip.tsx +153 -0
- package/src/components/color-input.tsx +98 -0
- package/src/components/data-cell.tsx +230 -0
- package/src/components/date-input.tsx +223 -0
- package/src/components/date-picker.tsx +276 -0
- package/src/components/description-list.tsx +97 -0
- package/src/components/dialog.tsx +405 -0
- package/src/components/empty-state.tsx +63 -0
- package/src/components/file-chip.tsx +206 -0
- package/src/components/file-uploader.tsx +148 -0
- package/src/components/flip-app-header.tsx +104 -0
- package/src/components/flip-brand.tsx +77 -0
- package/src/components/flip-theme.tsx +95 -0
- package/src/components/form-control.tsx +397 -0
- package/src/components/form-group.tsx +26 -0
- package/src/components/heading.tsx +70 -0
- package/src/components/icon.tsx +119 -0
- package/src/components/image-grid.tsx +285 -0
- package/src/components/inline-notification.tsx +167 -0
- package/src/components/inline-status.tsx +100 -0
- package/src/components/input.tsx +390 -0
- package/src/components/label.tsx +28 -0
- package/src/components/lightbox.tsx +314 -0
- package/src/components/link.tsx +46 -0
- package/src/components/list.tsx +40 -0
- package/src/components/menu.tsx +530 -0
- package/src/components/option-list.tsx +582 -0
- package/src/components/pagination.tsx +336 -0
- package/src/components/popover.tsx +194 -0
- package/src/components/progress.tsx +158 -0
- package/src/components/radio-group.tsx +136 -0
- package/src/components/resource-list.tsx +686 -0
- package/src/components/search.tsx +127 -0
- package/src/components/select.tsx +456 -0
- package/src/components/separator.tsx +93 -0
- package/src/components/skeleton-box.tsx +63 -0
- package/src/components/skeleton-text.tsx +66 -0
- package/src/components/spinner.tsx +83 -0
- package/src/components/stack.tsx +86 -0
- package/src/components/status-indicator.tsx +31 -0
- package/src/components/switch.tsx +90 -0
- package/src/components/table.tsx +242 -0
- package/src/components/tabs.tsx +147 -0
- package/src/components/tag.tsx +169 -0
- package/src/components/text.tsx +126 -0
- package/src/components/textarea.tsx +130 -0
- package/src/components/theme-provider.tsx +115 -0
- package/src/components/thumbnail.tsx +250 -0
- package/src/components/time-input.tsx +123 -0
- package/src/components/toast.tsx +217 -0
- package/src/components/toggle-group.tsx +124 -0
- package/src/components/toolbar.tsx +105 -0
- package/src/components/tooltip.tsx +97 -0
- package/src/components/tree-navigation.tsx +194 -0
- package/src/components/tree-view.tsx +292 -0
- package/src/flip.ts +12 -0
- package/src/index.ts +317 -0
- package/src/lib/date-mask.ts +176 -0
- package/src/lib/field-context.ts +86 -0
- package/src/lib/flip-app-script.ts +38 -0
- package/src/lib/flip-host.ts +84 -0
- package/src/lib/flip-theme-css.ts +90 -0
- package/src/lib/focus.ts +18 -0
- package/src/lib/glyphs.ts +137 -0
- package/src/lib/spacing.ts +93 -0
- package/src/lib/theme-context.ts +27 -0
- package/src/lib/theme-script.ts +49 -0
- package/src/lib/use-mobile.ts +17 -0
- package/src/lib/utils.ts +28 -0
- package/src/server.ts +20 -0
- package/src/styles/fonts/Inter-italic.var.woff2 +0 -0
- package/src/styles/fonts/Inter.var.woff2 +0 -0
- package/src/styles/fonts/OFL.txt +96 -0
- package/src/styles/theme.css +146 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { format as formatDate, isValid, parse } from "date-fns";
|
|
3
|
+
import { cn } from "../lib/utils";
|
|
4
|
+
import { FieldContext, useField } from "../lib/field-context";
|
|
5
|
+
import { Glyph } from "../lib/glyphs";
|
|
6
|
+
import { isComplete, maskDate, normalizePattern } from "../lib/date-mask";
|
|
7
|
+
import { useMobile } from "../lib/use-mobile";
|
|
8
|
+
import { DatePicker, type DatePickerLabels } from "./date-picker";
|
|
9
|
+
import { Input, type InputProps } from "./input";
|
|
10
|
+
import { Popover, PopoverAnchor, PopoverContent, PopoverTrigger } from "./popover";
|
|
11
|
+
|
|
12
|
+
const ISO = "yyyy-MM-dd";
|
|
13
|
+
|
|
14
|
+
export interface DateInputProps
|
|
15
|
+
extends Omit<InputProps, "value" | "defaultValue" | "onChange" | "type" | "prefix" | "suffix" | "clearable"> {
|
|
16
|
+
/** The date as ISO `yyyy-MM-dd`, or "" for none. */
|
|
17
|
+
value?: string;
|
|
18
|
+
defaultValue?: string;
|
|
19
|
+
/** Called with the ISO date once a whole valid date is typed or picked, and with "" when cleared. */
|
|
20
|
+
onValueChange?: (value: string) => void;
|
|
21
|
+
/** Called with the typed text when it is a whole date that doesn't exist, such as 2026-02-31. */
|
|
22
|
+
onInvalidInput?: (text: string) => void;
|
|
23
|
+
/** How the date is shown and typed, as a date-fns pattern. Defaults to "yyyy-MM-dd". */
|
|
24
|
+
format?: string;
|
|
25
|
+
/** Defaults to "yyyy-mm-dd". */
|
|
26
|
+
placeholder?: string;
|
|
27
|
+
/**
|
|
28
|
+
* "pick" makes the field read-only on phones, so tapping it opens the
|
|
29
|
+
* picker instead of the keyboard, as in Swirl.
|
|
30
|
+
*/
|
|
31
|
+
preferredInputMode?: "input" | "pick";
|
|
32
|
+
datePickerDisableDate?: (date: Date) => boolean;
|
|
33
|
+
datePickerLabel?: string;
|
|
34
|
+
datePickerTriggerLabel?: string;
|
|
35
|
+
firstDayOfWeek?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
36
|
+
locale?: string;
|
|
37
|
+
labels?: Partial<DatePickerLabels>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const fromIso = (value: string | undefined) => {
|
|
41
|
+
if (!value) return undefined;
|
|
42
|
+
const date = parse(value, ISO, new Date());
|
|
43
|
+
return isValid(date) ? date : undefined;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Swirl's date input: a masked date field with a button that opens the
|
|
48
|
+
* `DatePicker`. Put it in a `FormControl`.
|
|
49
|
+
*
|
|
50
|
+
* <FormControl label="Due date">
|
|
51
|
+
* <DateInput format="dd.MM.yyyy" placeholder="dd.mm.yyyy" />
|
|
52
|
+
* </FormControl>
|
|
53
|
+
*
|
|
54
|
+
* Typing fills in the separators and pads single digits ("4" in a day field
|
|
55
|
+
* becomes "04"), and digits that can't make a date are ignored. The value is
|
|
56
|
+
* always ISO `yyyy-MM-dd`, whatever the display `format`.
|
|
57
|
+
*/
|
|
58
|
+
export function DateInput({
|
|
59
|
+
value: valueProp,
|
|
60
|
+
defaultValue,
|
|
61
|
+
onValueChange,
|
|
62
|
+
onInvalidInput,
|
|
63
|
+
format = ISO,
|
|
64
|
+
placeholder = "yyyy-mm-dd",
|
|
65
|
+
preferredInputMode = "input",
|
|
66
|
+
datePickerDisableDate,
|
|
67
|
+
datePickerLabel = "Date picker",
|
|
68
|
+
datePickerTriggerLabel = "Open date picker",
|
|
69
|
+
firstDayOfWeek = 0,
|
|
70
|
+
locale = "en-US",
|
|
71
|
+
labels,
|
|
72
|
+
id,
|
|
73
|
+
disabled: disabledProp,
|
|
74
|
+
readOnly: readOnlyProp,
|
|
75
|
+
className,
|
|
76
|
+
onClick,
|
|
77
|
+
...props
|
|
78
|
+
}: DateInputProps) {
|
|
79
|
+
const field = useField();
|
|
80
|
+
const mobile = useMobile();
|
|
81
|
+
const pattern = normalizePattern(format);
|
|
82
|
+
const disabled = disabledProp ?? field?.disabled ?? false;
|
|
83
|
+
const readOnly = readOnlyProp ?? field?.readOnly ?? false;
|
|
84
|
+
|
|
85
|
+
const [uncontrolled, setUncontrolled] = React.useState(defaultValue ?? "");
|
|
86
|
+
const value = valueProp ?? uncontrolled;
|
|
87
|
+
const display = (iso: string) => {
|
|
88
|
+
const date = fromIso(iso);
|
|
89
|
+
return date ? formatDate(date, pattern) : "";
|
|
90
|
+
};
|
|
91
|
+
const [text, setText] = React.useState(() => display(value));
|
|
92
|
+
// Follow a new value from outside; React's "adjust state on prop change" pattern.
|
|
93
|
+
const [shown, setShown] = React.useState(value);
|
|
94
|
+
if (value !== shown) {
|
|
95
|
+
setShown(value);
|
|
96
|
+
setText(display(value));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const [open, setOpen] = React.useState(false);
|
|
100
|
+
const generatedId = React.useId();
|
|
101
|
+
const inputId = id ?? field?.id ?? generatedId;
|
|
102
|
+
|
|
103
|
+
const commit = (iso: string) => {
|
|
104
|
+
setShown(iso);
|
|
105
|
+
if (valueProp === undefined) setUncontrolled(iso);
|
|
106
|
+
if (iso !== value) onValueChange?.(iso);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const onTextChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
110
|
+
const raw = event.target.value;
|
|
111
|
+
// Only fill in separators while typing forwards, so Backspace can remove them.
|
|
112
|
+
const masked = maskDate(raw, pattern, { eager: raw.length > text.length });
|
|
113
|
+
setText(masked);
|
|
114
|
+
if (masked === "") {
|
|
115
|
+
commit("");
|
|
116
|
+
} else if (isComplete(masked, pattern)) {
|
|
117
|
+
const date = parse(masked, pattern, new Date());
|
|
118
|
+
if (isValid(date)) commit(formatDate(date, ISO));
|
|
119
|
+
else onInvalidInput?.(masked);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const pick = (date: Date) => {
|
|
124
|
+
setText(formatDate(date, pattern));
|
|
125
|
+
commit(formatDate(date, ISO));
|
|
126
|
+
setOpen(false);
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
// With "pick" on a phone, the field opens the picker instead of the keyboard.
|
|
130
|
+
const pickOnly = preferredInputMode === "pick" && mobile;
|
|
131
|
+
const compact = field ? field.inline || field.labelPosition === "outside" : true;
|
|
132
|
+
// While the label stands in the empty box, Swirl hides the placeholder and the button.
|
|
133
|
+
const collapsed = Boolean(field && !compact && field.labelAsPlaceholder);
|
|
134
|
+
|
|
135
|
+
// Swirl's form control doesn't treat this placeholder as one: the label
|
|
136
|
+
// still sits in the empty box until the field is focused.
|
|
137
|
+
const innerField = React.useMemo(
|
|
138
|
+
() => (field ? { ...field, setHasPlaceholder: () => {} } : null),
|
|
139
|
+
[field],
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
const date = fromIso(value);
|
|
143
|
+
const trigger = !readOnly && (
|
|
144
|
+
<PopoverTrigger asChild>
|
|
145
|
+
<button
|
|
146
|
+
type="button"
|
|
147
|
+
aria-label={datePickerTriggerLabel}
|
|
148
|
+
disabled={disabled}
|
|
149
|
+
className={cn(
|
|
150
|
+
"inline-flex cursor-pointer border-0 bg-transparent p-0 text-icon-default",
|
|
151
|
+
"outline-offset-2 outline-focus focus:not-focus-visible:outline-none focus-visible:outline-2 focus-visible:outline-solid",
|
|
152
|
+
"size-6 desktop:size-5 [&>svg]:size-full disabled:cursor-default disabled:text-disabled",
|
|
153
|
+
field &&
|
|
154
|
+
cn(
|
|
155
|
+
"absolute right-0",
|
|
156
|
+
// As in Swirl: centred on a tall field's box, in line in a compact one.
|
|
157
|
+
compact ? "-top-px desktop:top-0" : "-top-3 desktop:-top-2.5",
|
|
158
|
+
collapsed && "invisible",
|
|
159
|
+
),
|
|
160
|
+
)}
|
|
161
|
+
>
|
|
162
|
+
<Glyph name="today" />
|
|
163
|
+
</button>
|
|
164
|
+
</PopoverTrigger>
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
const input = (
|
|
168
|
+
<Input
|
|
169
|
+
{...props}
|
|
170
|
+
id={inputId}
|
|
171
|
+
type="text"
|
|
172
|
+
inputMode="numeric"
|
|
173
|
+
autoComplete="off"
|
|
174
|
+
value={text}
|
|
175
|
+
onChange={onTextChange}
|
|
176
|
+
placeholder={placeholder}
|
|
177
|
+
disabled={disabled}
|
|
178
|
+
readOnly={readOnly || pickOnly}
|
|
179
|
+
onClick={(event) => {
|
|
180
|
+
onClick?.(event);
|
|
181
|
+
if (pickOnly && !readOnly && !disabled) setOpen(true);
|
|
182
|
+
}}
|
|
183
|
+
suffix={field ? undefined : trigger}
|
|
184
|
+
// Unlike a text input, Swirl greys this one out fully when disabled.
|
|
185
|
+
className={cn(field && "w-[calc(100%-1.75rem)] disabled:text-disabled", className)}
|
|
186
|
+
/>
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
return (
|
|
190
|
+
<Popover open={open} onOpenChange={(next) => setOpen(next && !disabled && !readOnly)}>
|
|
191
|
+
<PopoverAnchor asChild>
|
|
192
|
+
<div className="relative flex w-full items-center">
|
|
193
|
+
{innerField ? <FieldContext.Provider value={innerField}>{input}</FieldContext.Provider> : input}
|
|
194
|
+
{field && trigger}
|
|
195
|
+
</div>
|
|
196
|
+
</PopoverAnchor>
|
|
197
|
+
<PopoverContent
|
|
198
|
+
label={datePickerLabel}
|
|
199
|
+
align="end"
|
|
200
|
+
padded={false}
|
|
201
|
+
className="p-1"
|
|
202
|
+
onOpenAutoFocus={(event) => {
|
|
203
|
+
// Start on the chosen day, or today, so the arrow keys work at once.
|
|
204
|
+
event.preventDefault();
|
|
205
|
+
const content = event.currentTarget as HTMLElement;
|
|
206
|
+
requestAnimationFrame(() =>
|
|
207
|
+
content.querySelector<HTMLElement>('[role="grid"] button[tabindex="0"]')?.focus(),
|
|
208
|
+
);
|
|
209
|
+
}}
|
|
210
|
+
>
|
|
211
|
+
<DatePicker
|
|
212
|
+
value={date}
|
|
213
|
+
startDate={date}
|
|
214
|
+
onValueChange={pick}
|
|
215
|
+
disableDate={datePickerDisableDate}
|
|
216
|
+
firstDayOfWeek={firstDayOfWeek}
|
|
217
|
+
locale={locale}
|
|
218
|
+
labels={labels}
|
|
219
|
+
/>
|
|
220
|
+
</PopoverContent>
|
|
221
|
+
</Popover>
|
|
222
|
+
);
|
|
223
|
+
}
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { DayPicker, type DayButtonProps, type DateRange } from "react-day-picker";
|
|
3
|
+
import { cn } from "../lib/utils";
|
|
4
|
+
import { Glyph } from "../lib/glyphs";
|
|
5
|
+
|
|
6
|
+
export interface DatePickerLabels {
|
|
7
|
+
nextMonthButton: string;
|
|
8
|
+
previousMonthButton: string;
|
|
9
|
+
monthSelect: string;
|
|
10
|
+
yearSelect: string;
|
|
11
|
+
picker: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const defaultLabels: DatePickerLabels = {
|
|
15
|
+
nextMonthButton: "Next month",
|
|
16
|
+
previousMonthButton: "Previous month",
|
|
17
|
+
monthSelect: "Select month",
|
|
18
|
+
yearSelect: "Select year",
|
|
19
|
+
picker: "Choose date",
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
interface DatePickerBaseProps {
|
|
23
|
+
/** The month shown first, when no date is chosen. Defaults to today. */
|
|
24
|
+
startDate?: Date;
|
|
25
|
+
/** Return true for dates that can't be chosen. */
|
|
26
|
+
disableDate?: (date: Date) => boolean;
|
|
27
|
+
/** 0 is Sunday, as in Swirl's default; 1 is Monday. */
|
|
28
|
+
firstDayOfWeek?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
29
|
+
/** A BCP 47 locale for the month and weekday names. Defaults to "en-US". */
|
|
30
|
+
locale?: string;
|
|
31
|
+
labels?: Partial<DatePickerLabels>;
|
|
32
|
+
/** Cap the width at 17.5rem from 768px up. Defaults to true. */
|
|
33
|
+
fixedMaxWidth?: boolean;
|
|
34
|
+
className?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface SingleDatePickerProps extends DatePickerBaseProps {
|
|
38
|
+
range?: false;
|
|
39
|
+
value?: Date;
|
|
40
|
+
defaultValue?: Date;
|
|
41
|
+
onValueChange?: (date: Date) => void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface RangeDatePickerProps extends DatePickerBaseProps {
|
|
45
|
+
/** Choose a start and an end date. `onValueChange` fires once both are chosen. */
|
|
46
|
+
range: true;
|
|
47
|
+
value?: [Date, Date];
|
|
48
|
+
defaultValue?: [Date, Date];
|
|
49
|
+
onValueChange?: (range: [Date, Date]) => void;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type DatePickerProps = SingleDatePickerProps | RangeDatePickerProps;
|
|
53
|
+
|
|
54
|
+
/** One day: Swirl's round cell, with the range band drawn behind it. */
|
|
55
|
+
function DayButton({ day: _day, modifiers, className, ...props }: DayButtonProps) {
|
|
56
|
+
const middle = modifiers.range_middle;
|
|
57
|
+
const start = modifiers.range_start && !modifiers.range_end;
|
|
58
|
+
const end = modifiers.range_end && !modifiers.range_start;
|
|
59
|
+
const filled = modifiers.selected && !middle;
|
|
60
|
+
// react-day-picker moves keyboard focus by marking a day focused.
|
|
61
|
+
const ref = React.useRef<HTMLButtonElement>(null);
|
|
62
|
+
React.useEffect(() => {
|
|
63
|
+
if (modifiers.focused) ref.current?.focus();
|
|
64
|
+
}, [modifiers.focused]);
|
|
65
|
+
return (
|
|
66
|
+
<button
|
|
67
|
+
{...props}
|
|
68
|
+
ref={ref}
|
|
69
|
+
className={cn(
|
|
70
|
+
"relative z-1 flex size-full cursor-pointer items-center justify-center border-0 bg-transparent p-0",
|
|
71
|
+
"font-medium text-foreground outline-offset-2 outline-focus",
|
|
72
|
+
"focus:not-focus-visible:outline-none focus-visible:outline-2 focus-visible:outline-solid",
|
|
73
|
+
// The round cell.
|
|
74
|
+
"before:absolute before:inset-0 before:-z-1 before:rounded-full hover:before:bg-state-hovered",
|
|
75
|
+
modifiers.outside && "text-subdued",
|
|
76
|
+
// Swirl's keyboard-current date keeps a hovered fill.
|
|
77
|
+
props.tabIndex === 0 && "before:bg-state-hovered",
|
|
78
|
+
modifiers.today && "before:shadow-[inset_0_0_0_1px_var(--color-border-highlight)]",
|
|
79
|
+
filled && "text-on-primary before:bg-primary hover:before:bg-primary-hovered",
|
|
80
|
+
// The band between the ends of a range, at 20% of the primary colour.
|
|
81
|
+
(middle || start || end) &&
|
|
82
|
+
"after:absolute after:inset-y-px after:-z-2 after:bg-primary/20 after:content-['']",
|
|
83
|
+
middle && "after:inset-x-0",
|
|
84
|
+
start && "after:right-0 after:left-1/2",
|
|
85
|
+
end && "after:right-1/2 after:left-0",
|
|
86
|
+
modifiers.disabled && "pointer-events-none cursor-default text-disabled before:bg-transparent",
|
|
87
|
+
className,
|
|
88
|
+
)}
|
|
89
|
+
/>
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const sameDay = (a?: Date, b?: Date) => Boolean(a && b && a.toDateString() === b.toDateString());
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Swirl's date picker: a month calendar with month and year pickers. It
|
|
97
|
+
* selects one date, or a start and end with `range`. Arrow keys move between
|
|
98
|
+
* days, Page Up and Page Down between months.
|
|
99
|
+
*
|
|
100
|
+
* Built on react-day-picker, styled to Swirl's picker.
|
|
101
|
+
*/
|
|
102
|
+
export function DatePicker(props: DatePickerProps) {
|
|
103
|
+
const {
|
|
104
|
+
startDate,
|
|
105
|
+
disableDate,
|
|
106
|
+
firstDayOfWeek = 0,
|
|
107
|
+
locale = "en-US",
|
|
108
|
+
labels: labelsProp,
|
|
109
|
+
fixedMaxWidth = true,
|
|
110
|
+
className,
|
|
111
|
+
} = props;
|
|
112
|
+
const labels = { ...defaultLabels, ...labelsProp };
|
|
113
|
+
const range = props.range === true;
|
|
114
|
+
|
|
115
|
+
const [single, setSingle] = React.useState<Date | undefined>(
|
|
116
|
+
() => (range ? undefined : (props.defaultValue as Date | undefined)),
|
|
117
|
+
);
|
|
118
|
+
const [pending, setPending] = React.useState<DateRange | undefined>(() => {
|
|
119
|
+
const initial = range ? (props.defaultValue as [Date, Date] | undefined) : undefined;
|
|
120
|
+
return initial ? { from: initial[0], to: initial[1] } : undefined;
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const controlled = props.value !== undefined;
|
|
124
|
+
const selectedSingle = range ? undefined : controlled ? (props.value as Date) : single;
|
|
125
|
+
const controlledRange = range && controlled ? (props.value as [Date, Date]) : undefined;
|
|
126
|
+
// A controlled range shows the caller's value until the user starts a new one.
|
|
127
|
+
const [editing, setEditing] = React.useState(false);
|
|
128
|
+
const selectedRange: DateRange | undefined =
|
|
129
|
+
controlledRange && !editing ? { from: controlledRange[0], to: controlledRange[1] } : pending;
|
|
130
|
+
|
|
131
|
+
const first = range ? selectedRange?.from : selectedSingle;
|
|
132
|
+
const [month, setMonth] = React.useState(() => {
|
|
133
|
+
const date = first ?? startDate ?? new Date();
|
|
134
|
+
return new Date(date.getFullYear(), date.getMonth(), 1);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
const monthNames = React.useMemo(() => {
|
|
138
|
+
const format = new Intl.DateTimeFormat(locale, { month: "long" });
|
|
139
|
+
return Array.from({ length: 12 }, (_, i) => format.format(new Date(2000, i, 1)));
|
|
140
|
+
}, [locale]);
|
|
141
|
+
const weekday = React.useMemo(() => new Intl.DateTimeFormat(locale, { weekday: "short" }), [locale]);
|
|
142
|
+
const weekdayLong = React.useMemo(() => new Intl.DateTimeFormat(locale, { weekday: "long" }), [locale]);
|
|
143
|
+
const fullDate = React.useMemo(() => new Intl.DateTimeFormat(locale, { dateStyle: "long" }), [locale]);
|
|
144
|
+
|
|
145
|
+
const step = (by: number) => setMonth((m) => new Date(m.getFullYear(), m.getMonth() + by, 1));
|
|
146
|
+
|
|
147
|
+
const shared = {
|
|
148
|
+
month,
|
|
149
|
+
onMonthChange: setMonth,
|
|
150
|
+
hideNavigation: true,
|
|
151
|
+
showOutsideDays: true,
|
|
152
|
+
weekStartsOn: firstDayOfWeek,
|
|
153
|
+
disabled: disableDate,
|
|
154
|
+
// Our own header replaces the caption.
|
|
155
|
+
components: { DayButton, MonthCaption: () => <></> },
|
|
156
|
+
formatters: {
|
|
157
|
+
formatWeekdayName: (date: Date) => weekday.format(date),
|
|
158
|
+
},
|
|
159
|
+
labels: {
|
|
160
|
+
labelDayButton: (date: Date) => fullDate.format(date),
|
|
161
|
+
labelWeekday: (date: Date) => weekdayLong.format(date),
|
|
162
|
+
labelGrid: () => `${monthNames[month.getMonth()]} ${month.getFullYear()}`,
|
|
163
|
+
},
|
|
164
|
+
classNames: {
|
|
165
|
+
root: "w-full",
|
|
166
|
+
months: "w-full",
|
|
167
|
+
month: "w-full",
|
|
168
|
+
month_grid: "block w-full border-collapse",
|
|
169
|
+
weekdays: "mb-2 flex",
|
|
170
|
+
weekday: "inline-flex aspect-square min-h-9 min-w-9 basis-[calc(100%/7)] items-center justify-center p-0 font-normal text-subdued",
|
|
171
|
+
weeks: "block",
|
|
172
|
+
week: "flex",
|
|
173
|
+
day: "relative inline-flex aspect-square min-h-9 min-w-9 basis-[calc(100%/7)] items-center justify-center p-0",
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
return (
|
|
178
|
+
<div
|
|
179
|
+
role="group"
|
|
180
|
+
aria-label={labels.picker}
|
|
181
|
+
className={cn(
|
|
182
|
+
"bg-transparent px-4 py-2 text-sm leading-5 tracking-tighter text-foreground",
|
|
183
|
+
fixedMaxWidth && "md:max-w-[17.5rem]",
|
|
184
|
+
className,
|
|
185
|
+
)}
|
|
186
|
+
>
|
|
187
|
+
<div className="mb-2 flex items-center">
|
|
188
|
+
<span aria-live="polite" aria-atomic className="sr-only">
|
|
189
|
+
{first ? fullDate.format(first) : ""}
|
|
190
|
+
</span>
|
|
191
|
+
<span className="order-1 flex grow items-center gap-1 p-2">
|
|
192
|
+
<select
|
|
193
|
+
aria-label={labels.monthSelect}
|
|
194
|
+
title={labels.monthSelect}
|
|
195
|
+
value={month.getMonth()}
|
|
196
|
+
onChange={(event) => setMonth(new Date(month.getFullYear(), Number(event.target.value), 1))}
|
|
197
|
+
className={cn(
|
|
198
|
+
"m-0 cursor-pointer appearance-none border-0 bg-transparent p-0 text-left text-base leading-6 font-medium tracking-normal text-foreground",
|
|
199
|
+
"outline-offset-2 outline-focus focus:not-focus-visible:outline-none desktop:text-sm desktop:leading-5",
|
|
200
|
+
)}
|
|
201
|
+
>
|
|
202
|
+
{monthNames.map((name, i) => (
|
|
203
|
+
<option key={name} value={i}>
|
|
204
|
+
{name}
|
|
205
|
+
</option>
|
|
206
|
+
))}
|
|
207
|
+
</select>
|
|
208
|
+
<input
|
|
209
|
+
type="number"
|
|
210
|
+
aria-label={labels.yearSelect}
|
|
211
|
+
title={labels.yearSelect}
|
|
212
|
+
min={1}
|
|
213
|
+
max={9999}
|
|
214
|
+
value={month.getFullYear()}
|
|
215
|
+
onChange={(event) => {
|
|
216
|
+
const year = Number(event.target.value);
|
|
217
|
+
if (year >= 1 && year <= 9999) setMonth(new Date(year, month.getMonth(), 1));
|
|
218
|
+
}}
|
|
219
|
+
className={cn(
|
|
220
|
+
// Swirl keeps the browser's year spinner, shown on hover.
|
|
221
|
+
"m-0 max-w-16 cursor-pointer border-0 bg-transparent p-0 text-left text-base leading-6 font-medium tracking-normal text-foreground",
|
|
222
|
+
"outline-offset-2 outline-focus focus:not-focus-visible:outline-none desktop:text-sm desktop:leading-5",
|
|
223
|
+
)}
|
|
224
|
+
/>
|
|
225
|
+
</span>
|
|
226
|
+
{(["previous", "next"] as const).map((direction) => (
|
|
227
|
+
<button
|
|
228
|
+
key={direction}
|
|
229
|
+
type="button"
|
|
230
|
+
aria-label={direction === "previous" ? labels.previousMonthButton : labels.nextMonthButton}
|
|
231
|
+
onClick={() => step(direction === "previous" ? -1 : 1)}
|
|
232
|
+
className={cn(
|
|
233
|
+
"relative m-0 inline-flex size-10 shrink-0 cursor-pointer items-center justify-center rounded-full border-0 p-0",
|
|
234
|
+
"bg-transparent text-[var(--s-icon-highlight)] hover:bg-state-hovered active:bg-state-pressed",
|
|
235
|
+
"outline-offset-2 outline-focus focus:not-focus-visible:outline-none focus-visible:outline-2 focus-visible:outline-solid",
|
|
236
|
+
"[&>svg]:size-6",
|
|
237
|
+
direction === "previous" ? "order-2" : "order-3",
|
|
238
|
+
)}
|
|
239
|
+
>
|
|
240
|
+
<Glyph name={direction === "previous" ? "chevronLeft" : "chevronRight"} />
|
|
241
|
+
</button>
|
|
242
|
+
))}
|
|
243
|
+
</div>
|
|
244
|
+
|
|
245
|
+
{range ? (
|
|
246
|
+
<DayPicker
|
|
247
|
+
{...shared}
|
|
248
|
+
mode="range"
|
|
249
|
+
selected={selectedRange}
|
|
250
|
+
onSelect={(next, clicked) => {
|
|
251
|
+
// Swirl starts a new range once one is complete.
|
|
252
|
+
const complete = selectedRange?.from && selectedRange.to && !sameDay(selectedRange.from, selectedRange.to);
|
|
253
|
+
const value = complete ? { from: clicked, to: undefined } : next;
|
|
254
|
+
setEditing(true);
|
|
255
|
+
setPending(value);
|
|
256
|
+
if (value?.from && value.to) {
|
|
257
|
+
setEditing(false);
|
|
258
|
+
(props.onValueChange as ((r: [Date, Date]) => void) | undefined)?.([value.from, value.to]);
|
|
259
|
+
}
|
|
260
|
+
}}
|
|
261
|
+
/>
|
|
262
|
+
) : (
|
|
263
|
+
<DayPicker
|
|
264
|
+
{...shared}
|
|
265
|
+
mode="single"
|
|
266
|
+
required
|
|
267
|
+
selected={selectedSingle}
|
|
268
|
+
onSelect={(date) => {
|
|
269
|
+
if (!controlled) setSingle(date);
|
|
270
|
+
(props.onValueChange as ((d: Date) => void) | undefined)?.(date);
|
|
271
|
+
}}
|
|
272
|
+
/>
|
|
273
|
+
)}
|
|
274
|
+
</div>
|
|
275
|
+
);
|
|
276
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { cn } from "../lib/utils";
|
|
2
|
+
import { Tooltip } from "./tooltip";
|
|
3
|
+
|
|
4
|
+
export interface DescriptionListProps extends React.ComponentProps<"div"> {}
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Swirl's description list: term/description pairs, one per row.
|
|
8
|
+
*
|
|
9
|
+
* Built from ARIA roles (list, listitem, term, definition) rather than `<dl>`,
|
|
10
|
+
* as Swirl is, because a row can also hold tools and extra content, which a
|
|
11
|
+
* `<dl>` does not allow.
|
|
12
|
+
*/
|
|
13
|
+
export function DescriptionList({ className, ...props }: DescriptionListProps) {
|
|
14
|
+
return (
|
|
15
|
+
<div
|
|
16
|
+
role="list"
|
|
17
|
+
className={cn("@container/description-list m-0 block max-w-full", className)}
|
|
18
|
+
{...props}
|
|
19
|
+
/>
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface DescriptionListItemProps extends Omit<React.ComponentProps<"div">, "children"> {
|
|
24
|
+
/** The term being described. */
|
|
25
|
+
term: React.ReactNode;
|
|
26
|
+
/** The description. */
|
|
27
|
+
children?: React.ReactNode;
|
|
28
|
+
/** Shown from an info icon after the term. */
|
|
29
|
+
tooltip?: React.ReactNode;
|
|
30
|
+
/** Draw a divider under the row and pad it. On by default, as in Swirl. */
|
|
31
|
+
bordered?: boolean;
|
|
32
|
+
/** Overrides the description's 40rem maximum width. */
|
|
33
|
+
maxWidth?: React.CSSProperties["maxWidth"];
|
|
34
|
+
/** Controls at the end of the row, such as a `Button`. */
|
|
35
|
+
tools?: React.ReactNode;
|
|
36
|
+
/** Full-width content under the row, such as an `InlineNotification`. */
|
|
37
|
+
additionalContent?: React.ReactNode;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function DescriptionListItem({
|
|
41
|
+
className,
|
|
42
|
+
term,
|
|
43
|
+
children,
|
|
44
|
+
tooltip,
|
|
45
|
+
bordered = true,
|
|
46
|
+
maxWidth,
|
|
47
|
+
tools,
|
|
48
|
+
additionalContent,
|
|
49
|
+
...props
|
|
50
|
+
}: DescriptionListItemProps) {
|
|
51
|
+
return (
|
|
52
|
+
<div role="listitem" className={cn("block", className)} {...props}>
|
|
53
|
+
<div role="group" className={cn(bordered && "border-b border-border py-4")}>
|
|
54
|
+
<div
|
|
55
|
+
className={cn(
|
|
56
|
+
"flex max-w-full items-start justify-between gap-4 py-2 leading-6 text-foreground",
|
|
57
|
+
"desktop:text-sm desktop:leading-5",
|
|
58
|
+
)}
|
|
59
|
+
>
|
|
60
|
+
<div className="grow">
|
|
61
|
+
<div role="term" className="mb-1 block font-semibold wrap-break-word desktop:mb-0.5">
|
|
62
|
+
{term}
|
|
63
|
+
{tooltip && (
|
|
64
|
+
<span className="ml-1 inline-flex align-middle">
|
|
65
|
+
<Tooltip content={tooltip} side="right">
|
|
66
|
+
<span
|
|
67
|
+
tabIndex={0}
|
|
68
|
+
role="img"
|
|
69
|
+
aria-label="More information"
|
|
70
|
+
className="inline-flex rounded-full outline-none focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-focus"
|
|
71
|
+
>
|
|
72
|
+
<svg viewBox="0 0 16 16" className="size-4" aria-hidden focusable={false}>
|
|
73
|
+
<path
|
|
74
|
+
d="M7.99998 11.3333C8.18887 11.3333 8.34731 11.2693 8.47531 11.1413C8.60287 11.0138 8.66665 10.8555 8.66665 10.6666V7.98331C8.66665 7.79442 8.60287 7.63887 8.47531 7.51665C8.34731 7.39442 8.18887 7.33331 7.99998 7.33331C7.81109 7.33331 7.65287 7.39709 7.52531 7.52465C7.39731 7.65265 7.33331 7.81109 7.33331 7.99998V10.6833C7.33331 10.8722 7.39731 11.0278 7.52531 11.15C7.65287 11.2722 7.81109 11.3333 7.99998 11.3333ZM7.99998 5.99998C8.18887 5.99998 8.34731 5.93598 8.47531 5.80798C8.60287 5.68042 8.66665 5.5222 8.66665 5.33331C8.66665 5.14442 8.60287 4.98598 8.47531 4.85798C8.34731 4.73042 8.18887 4.66665 7.99998 4.66665C7.81109 4.66665 7.65287 4.73042 7.52531 4.85798C7.39731 4.98598 7.33331 5.14442 7.33331 5.33331C7.33331 5.5222 7.39731 5.68042 7.52531 5.80798C7.65287 5.93598 7.81109 5.99998 7.99998 5.99998ZM7.99998 14.6666C7.07776 14.6666 6.21109 14.4915 5.39998 14.1413C4.58887 13.7915 3.88331 13.3166 3.28331 12.7166C2.68331 12.1166 2.20842 11.4111 1.85865 10.6C1.50842 9.78887 1.33331 8.9222 1.33331 7.99998C1.33331 7.07776 1.50842 6.21109 1.85865 5.39998C2.20842 4.58887 2.68331 3.88331 3.28331 3.28331C3.88331 2.68331 4.58887 2.2082 5.39998 1.85798C6.21109 1.5082 7.07776 1.33331 7.99998 1.33331C8.9222 1.33331 9.78887 1.5082 10.6 1.85798C11.4111 2.2082 12.1166 2.68331 12.7166 3.28331C13.3166 3.88331 13.7915 4.58887 14.1413 5.39998C14.4915 6.21109 14.6666 7.07776 14.6666 7.99998C14.6666 8.9222 14.4915 9.78887 14.1413 10.6C13.7915 11.4111 13.3166 12.1166 12.7166 12.7166C12.1166 13.3166 11.4111 13.7915 10.6 14.1413C9.78887 14.4915 8.9222 14.6666 7.99998 14.6666ZM7.99998 13.3333C9.47776 13.3333 10.7362 12.814 11.7753 11.7753C12.814 10.7362 13.3333 9.47776 13.3333 7.99998C13.3333 6.5222 12.814 5.26376 11.7753 4.22465C10.7362 3.18598 9.47776 2.66665 7.99998 2.66665C6.5222 2.66665 5.26398 3.18598 4.22531 4.22465C3.1862 5.26376 2.66665 6.5222 2.66665 7.99998C2.66665 9.47776 3.1862 10.7362 4.22531 11.7753C5.26398 12.814 6.5222 13.3333 7.99998 13.3333Z"
|
|
75
|
+
fill="currentColor"
|
|
76
|
+
/>
|
|
77
|
+
</svg>
|
|
78
|
+
</span>
|
|
79
|
+
</Tooltip>
|
|
80
|
+
</span>
|
|
81
|
+
)}
|
|
82
|
+
</div>
|
|
83
|
+
<div
|
|
84
|
+
role="definition"
|
|
85
|
+
className="m-0 block max-w-[40rem] wrap-break-word"
|
|
86
|
+
style={maxWidth !== undefined ? { maxWidth } : undefined}
|
|
87
|
+
>
|
|
88
|
+
{children}
|
|
89
|
+
</div>
|
|
90
|
+
</div>
|
|
91
|
+
{tools && <div className="flex min-h-[2.625rem] shrink-0 items-center">{tools}</div>}
|
|
92
|
+
</div>
|
|
93
|
+
{additionalContent && <div className="shrink-0 basis-full pb-2">{additionalContent}</div>}
|
|
94
|
+
</div>
|
|
95
|
+
</div>
|
|
96
|
+
);
|
|
97
|
+
}
|