@enablo/swirlcn-ui 0.0.0-stage → 0.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.
- 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,397 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { cn } from "../lib/utils";
|
|
3
|
+
import {
|
|
4
|
+
FieldContext,
|
|
5
|
+
type FieldContextValue,
|
|
6
|
+
type FieldFontSize,
|
|
7
|
+
type FieldLabelPosition,
|
|
8
|
+
} from "../lib/field-context";
|
|
9
|
+
import { Glyph } from "../lib/glyphs";
|
|
10
|
+
import { InlineError } from "./inline-status";
|
|
11
|
+
import { Label } from "./label";
|
|
12
|
+
import { Tooltip } from "./tooltip";
|
|
13
|
+
|
|
14
|
+
/** What a render-function child receives: the ids and state to spread onto a custom control. */
|
|
15
|
+
export interface FormControlField {
|
|
16
|
+
id: string;
|
|
17
|
+
"aria-describedby": string | undefined;
|
|
18
|
+
"aria-invalid": boolean | undefined;
|
|
19
|
+
"aria-required": boolean | undefined;
|
|
20
|
+
disabled: boolean | undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface FormControlProps extends Omit<React.ComponentProps<"div">, "children" | "prefix"> {
|
|
24
|
+
/** The field's label. */
|
|
25
|
+
label?: React.ReactNode;
|
|
26
|
+
/** Helper text under the field. Hidden while an error is shown. */
|
|
27
|
+
description?: React.ReactNode;
|
|
28
|
+
/** The error, shown under the field. It marks the field invalid. */
|
|
29
|
+
errorMessage?: React.ReactNode;
|
|
30
|
+
/** @deprecated Use `errorMessage`. */
|
|
31
|
+
error?: React.ReactNode;
|
|
32
|
+
/** Mark the field invalid without a message. */
|
|
33
|
+
invalid?: boolean;
|
|
34
|
+
disabled?: boolean;
|
|
35
|
+
readOnly?: boolean;
|
|
36
|
+
/** Mark the field required: adds an asterisk and `aria-required`. */
|
|
37
|
+
required?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* `inside` (Swirl's default) floats the label inside the field box: it sits
|
|
40
|
+
* in the box like a placeholder and moves to the top on focus or once there
|
|
41
|
+
* is a value. `outside` puts the label above a compact box.
|
|
42
|
+
*/
|
|
43
|
+
labelPosition?: FieldLabelPosition;
|
|
44
|
+
/** A compact box whose label disappears once there is a value. */
|
|
45
|
+
inline?: boolean;
|
|
46
|
+
/** Keep the label for screen readers only. */
|
|
47
|
+
hideLabel?: boolean;
|
|
48
|
+
/** Leading 20px icon inside the box. */
|
|
49
|
+
icon?: React.ReactNode;
|
|
50
|
+
/** Help text in a tooltip on an icon beside the label. Shown on desktop only, as in Swirl. */
|
|
51
|
+
tooltip?: React.ReactNode;
|
|
52
|
+
/** Quieter text after the label, shown with `labelPosition="outside"`. */
|
|
53
|
+
secondaryLabel?: React.ReactNode;
|
|
54
|
+
/** `base` keeps 16px text on desktop; `sm` uses 14px everywhere. */
|
|
55
|
+
fontSize?: FieldFontSize;
|
|
56
|
+
/** A joined box before the field, such as a country `<select>`. */
|
|
57
|
+
prefix?: React.ReactNode;
|
|
58
|
+
/** Content under the control, inside the box. */
|
|
59
|
+
bottom?: React.ReactNode;
|
|
60
|
+
/**
|
|
61
|
+
* The control: an `Input`, `Textarea` or other swirlcn field, which picks up
|
|
62
|
+
* the label, ids and state on its own. A render function receives them
|
|
63
|
+
* instead, for a custom control, and is laid out with the label above.
|
|
64
|
+
*/
|
|
65
|
+
children: React.ReactNode | ((field: FormControlField) => React.ReactNode);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Swirl's form control: the field box, label, description and error around a
|
|
70
|
+
* control. By default the label floats inside the box. Errors use
|
|
71
|
+
* `InlineError` and are announced.
|
|
72
|
+
*
|
|
73
|
+
* <FormControl label="Email" description="We'll never share it.">
|
|
74
|
+
* <Input type="email" />
|
|
75
|
+
* </FormControl>
|
|
76
|
+
*/
|
|
77
|
+
export function FormControl(props: FormControlProps) {
|
|
78
|
+
if (typeof props.children === "function") return <RenderFunctionFormControl {...props} />;
|
|
79
|
+
return <FieldFormControl {...props} />;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function FieldFormControl({
|
|
83
|
+
className,
|
|
84
|
+
label,
|
|
85
|
+
description,
|
|
86
|
+
errorMessage,
|
|
87
|
+
error,
|
|
88
|
+
invalid,
|
|
89
|
+
disabled = false,
|
|
90
|
+
readOnly = false,
|
|
91
|
+
required = false,
|
|
92
|
+
labelPosition = "inside",
|
|
93
|
+
inline = false,
|
|
94
|
+
hideLabel = false,
|
|
95
|
+
icon,
|
|
96
|
+
tooltip,
|
|
97
|
+
secondaryLabel,
|
|
98
|
+
fontSize = "default",
|
|
99
|
+
prefix,
|
|
100
|
+
bottom,
|
|
101
|
+
children,
|
|
102
|
+
onFocus,
|
|
103
|
+
onBlur,
|
|
104
|
+
...props
|
|
105
|
+
}: FormControlProps) {
|
|
106
|
+
const base = React.useId();
|
|
107
|
+
const id = `${base}-control`;
|
|
108
|
+
const descriptionId = `${base}-description`;
|
|
109
|
+
const errorId = `${base}-error`;
|
|
110
|
+
const message = errorMessage ?? error;
|
|
111
|
+
const isInvalid = invalid ?? Boolean(message);
|
|
112
|
+
|
|
113
|
+
const [filled, setFilled] = React.useState(false);
|
|
114
|
+
const [hasPlaceholder, setHasPlaceholder] = React.useState(false);
|
|
115
|
+
const [hasCounter, setHasCounter] = React.useState(false);
|
|
116
|
+
const [focused, setFocused] = React.useState(false);
|
|
117
|
+
|
|
118
|
+
const outside = labelPosition === "outside";
|
|
119
|
+
const compact = outside || inline;
|
|
120
|
+
// The label stands in for the placeholder while the field is empty and
|
|
121
|
+
// unfocused - unless the control has a placeholder of its own, which then
|
|
122
|
+
// shows instead (inline fields always use the label).
|
|
123
|
+
const labelAsPlaceholder = !outside && !filled && !focused && (inline || !hasPlaceholder);
|
|
124
|
+
const describedBy = message ? errorId : description ? descriptionId : undefined;
|
|
125
|
+
const active = focused && !disabled && !readOnly;
|
|
126
|
+
|
|
127
|
+
const context = React.useMemo<FieldContextValue>(
|
|
128
|
+
() => ({
|
|
129
|
+
id,
|
|
130
|
+
describedBy,
|
|
131
|
+
invalid: isInvalid,
|
|
132
|
+
disabled,
|
|
133
|
+
readOnly,
|
|
134
|
+
required,
|
|
135
|
+
labelPosition,
|
|
136
|
+
inline,
|
|
137
|
+
fontSize,
|
|
138
|
+
labelAsPlaceholder,
|
|
139
|
+
setFilled,
|
|
140
|
+
setHasPlaceholder,
|
|
141
|
+
setHasCounter,
|
|
142
|
+
}),
|
|
143
|
+
[id, describedBy, isInvalid, disabled, readOnly, required, labelPosition, inline, fontSize, labelAsPlaceholder],
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
const labelSize =
|
|
147
|
+
fontSize === "sm" ? "text-sm" : fontSize === "base" ? "text-base" : "text-base desktop:text-sm";
|
|
148
|
+
|
|
149
|
+
const labelText = (
|
|
150
|
+
<>
|
|
151
|
+
{label}
|
|
152
|
+
{required && (
|
|
153
|
+
<span className="ml-0.5 text-critical" aria-hidden>
|
|
154
|
+
*
|
|
155
|
+
</span>
|
|
156
|
+
)}
|
|
157
|
+
{outside && secondaryLabel && (
|
|
158
|
+
<span className="ml-1 font-normal text-subdued">{secondaryLabel}</span>
|
|
159
|
+
)}
|
|
160
|
+
{tooltip && (
|
|
161
|
+
<Tooltip content={tooltip}>
|
|
162
|
+
<span
|
|
163
|
+
tabIndex={0}
|
|
164
|
+
className="relative z-10 hidden cursor-default p-1 align-middle text-icon-default desktop:inline-flex"
|
|
165
|
+
>
|
|
166
|
+
<Glyph name="help" className="size-4" />
|
|
167
|
+
</span>
|
|
168
|
+
</Tooltip>
|
|
169
|
+
)}
|
|
170
|
+
</>
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
// The box's border and ring, shared by the prefix box.
|
|
174
|
+
const edge = cn(
|
|
175
|
+
"border border-border-strong",
|
|
176
|
+
isInvalid && "border-border-critical",
|
|
177
|
+
(disabled || readOnly) && "border-border bg-surface-raised",
|
|
178
|
+
active &&
|
|
179
|
+
(isInvalid
|
|
180
|
+
? "border-border-critical shadow-[0_0_0_1px_var(--color-border-critical)]"
|
|
181
|
+
: "border-border-highlight shadow-[0_0_0_1px_var(--color-border-highlight)]"),
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
return (
|
|
185
|
+
<FieldContext.Provider value={context}>
|
|
186
|
+
<div
|
|
187
|
+
role="group"
|
|
188
|
+
className={cn("flex w-full flex-col", className)}
|
|
189
|
+
data-disabled={disabled || undefined}
|
|
190
|
+
data-invalid={isInvalid || undefined}
|
|
191
|
+
onFocus={(event) => {
|
|
192
|
+
onFocus?.(event);
|
|
193
|
+
setFocused(true);
|
|
194
|
+
}}
|
|
195
|
+
onBlur={(event) => {
|
|
196
|
+
onBlur?.(event);
|
|
197
|
+
if (!event.currentTarget.contains(event.relatedTarget as Node | null)) setFocused(false);
|
|
198
|
+
}}
|
|
199
|
+
{...props}
|
|
200
|
+
>
|
|
201
|
+
{outside && (
|
|
202
|
+
<Label
|
|
203
|
+
htmlFor={id}
|
|
204
|
+
className={cn(
|
|
205
|
+
"mb-1 block truncate font-medium text-foreground",
|
|
206
|
+
fontSize === "base" ? "text-base leading-6" : "text-sm leading-5",
|
|
207
|
+
hideLabel && "sr-only",
|
|
208
|
+
)}
|
|
209
|
+
>
|
|
210
|
+
{labelText}
|
|
211
|
+
</Label>
|
|
212
|
+
)}
|
|
213
|
+
|
|
214
|
+
<div className="relative flex items-stretch">
|
|
215
|
+
{prefix && (
|
|
216
|
+
<div
|
|
217
|
+
className={cn(
|
|
218
|
+
"relative z-1 flex shrink-0 items-center justify-center rounded-l-sm px-4 text-foreground",
|
|
219
|
+
"bg-surface-raised",
|
|
220
|
+
// A native <select> here gets Swirl's look: no chrome, a chevron, and
|
|
221
|
+
// it fills the whole prefix box so all of it opens the select.
|
|
222
|
+
"[&>select]:-mx-4 [&>select]:h-full [&>select]:cursor-pointer [&>select]:appearance-none",
|
|
223
|
+
"[&>select]:border-0 [&>select]:bg-transparent [&>select]:pr-9 [&>select]:pl-4 [&>select]:text-inherit [&>select]:outline-none",
|
|
224
|
+
"[&>select]:text-base [&>select]:leading-6 desktop:[&>select]:text-sm desktop:[&>select]:leading-5",
|
|
225
|
+
edge,
|
|
226
|
+
// After `edge`, whose `border` would otherwise win: the field box draws the seam.
|
|
227
|
+
"border-r-0",
|
|
228
|
+
active && "border-r-transparent",
|
|
229
|
+
)}
|
|
230
|
+
>
|
|
231
|
+
{prefix}
|
|
232
|
+
{React.isValidElement(prefix) && prefix.type === "select" && (
|
|
233
|
+
<Glyph
|
|
234
|
+
name="expandMore"
|
|
235
|
+
className="pointer-events-none absolute top-1/2 right-2 size-5 -translate-y-1/2 text-icon-default"
|
|
236
|
+
/>
|
|
237
|
+
)}
|
|
238
|
+
</div>
|
|
239
|
+
)}
|
|
240
|
+
|
|
241
|
+
{/* The field box. A click anywhere in it focuses the control. */}
|
|
242
|
+
<div
|
|
243
|
+
data-field-box=""
|
|
244
|
+
className={cn(
|
|
245
|
+
"relative flex w-full flex-col justify-center gap-0.5 rounded-sm text-subdued",
|
|
246
|
+
"transition-[border-color,box-shadow] duration-150 motion-reduce:transition-none",
|
|
247
|
+
compact ? "min-h-10 px-3 py-2 desktop:min-h-[2.375rem]" : "min-h-[4.375rem] px-4 pt-8 pb-3",
|
|
248
|
+
edge,
|
|
249
|
+
prefix && "rounded-l-none",
|
|
250
|
+
prefix && active && "border-l-transparent",
|
|
251
|
+
!disabled && "cursor-text",
|
|
252
|
+
)}
|
|
253
|
+
onMouseDown={(event) => {
|
|
254
|
+
const target = event.target as HTMLElement;
|
|
255
|
+
if (target.closest("button, a, input, textarea, select, [tabindex]")) return;
|
|
256
|
+
const control = document.getElementById(id);
|
|
257
|
+
if (!control) return;
|
|
258
|
+
event.preventDefault();
|
|
259
|
+
// A button control, such as Select's trigger, opens as if clicked.
|
|
260
|
+
if (control.tagName === "BUTTON") control.click();
|
|
261
|
+
else if (control !== document.activeElement) control.focus();
|
|
262
|
+
}}
|
|
263
|
+
>
|
|
264
|
+
{icon && (
|
|
265
|
+
<span
|
|
266
|
+
aria-hidden
|
|
267
|
+
className={cn(
|
|
268
|
+
"absolute inset-y-0 my-auto flex w-5 items-center justify-center text-icon-default [&>svg]:size-5",
|
|
269
|
+
compact ? "left-3" : "left-4",
|
|
270
|
+
)}
|
|
271
|
+
>
|
|
272
|
+
{icon}
|
|
273
|
+
</span>
|
|
274
|
+
)}
|
|
275
|
+
|
|
276
|
+
{!outside && (
|
|
277
|
+
<Label
|
|
278
|
+
htmlFor={id}
|
|
279
|
+
className={cn(
|
|
280
|
+
"pointer-events-none absolute truncate text-left font-normal text-subdued",
|
|
281
|
+
compact ? "inset-x-3" : "inset-x-4",
|
|
282
|
+
icon && "ml-8",
|
|
283
|
+
inline
|
|
284
|
+
? [
|
|
285
|
+
"inset-y-0 flex items-center",
|
|
286
|
+
labelSize,
|
|
287
|
+
labelAsPlaceholder ? "opacity-100" : "opacity-0",
|
|
288
|
+
]
|
|
289
|
+
: [
|
|
290
|
+
"transition-[font-size,top] duration-150 motion-reduce:transition-none",
|
|
291
|
+
labelAsPlaceholder
|
|
292
|
+
? cn("top-0 bottom-0 flex items-center", labelSize)
|
|
293
|
+
: "top-3 text-sm leading-5",
|
|
294
|
+
],
|
|
295
|
+
hideLabel && "sr-only",
|
|
296
|
+
)}
|
|
297
|
+
>
|
|
298
|
+
<span className="pointer-events-auto truncate">{labelText}</span>
|
|
299
|
+
</Label>
|
|
300
|
+
)}
|
|
301
|
+
|
|
302
|
+
<div
|
|
303
|
+
className={cn(
|
|
304
|
+
"flex flex-col gap-2",
|
|
305
|
+
icon && "ml-8",
|
|
306
|
+
// Collapse the control while the label sits in its place.
|
|
307
|
+
!compact && labelAsPlaceholder && "max-h-0 overflow-hidden",
|
|
308
|
+
)}
|
|
309
|
+
>
|
|
310
|
+
{children as React.ReactNode}
|
|
311
|
+
{bottom && <div className="w-full">{bottom}</div>}
|
|
312
|
+
</div>
|
|
313
|
+
</div>
|
|
314
|
+
</div>
|
|
315
|
+
|
|
316
|
+
{description && !message && (
|
|
317
|
+
<span
|
|
318
|
+
id={descriptionId}
|
|
319
|
+
className={cn(
|
|
320
|
+
"mt-1 block pl-1 text-sm leading-5 text-subdued",
|
|
321
|
+
compact && hasCounter && "max-w-[calc(100%-6rem)]",
|
|
322
|
+
)}
|
|
323
|
+
>
|
|
324
|
+
{description}
|
|
325
|
+
</span>
|
|
326
|
+
)}
|
|
327
|
+
<div aria-live="polite">
|
|
328
|
+
{message && <InlineError id={errorId} size="s" message={message} className="mt-1" />}
|
|
329
|
+
</div>
|
|
330
|
+
</div>
|
|
331
|
+
</FieldContext.Provider>
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* The render-function form: the label sits above, and the custom control
|
|
337
|
+
* draws its own box. Kept for controls that are not swirlcn fields.
|
|
338
|
+
*/
|
|
339
|
+
function RenderFunctionFormControl({
|
|
340
|
+
className,
|
|
341
|
+
label,
|
|
342
|
+
description,
|
|
343
|
+
errorMessage,
|
|
344
|
+
error,
|
|
345
|
+
invalid,
|
|
346
|
+
required,
|
|
347
|
+
disabled,
|
|
348
|
+
hideLabel,
|
|
349
|
+
children,
|
|
350
|
+
// Field-box options do not apply to a custom control.
|
|
351
|
+
readOnly: _readOnly,
|
|
352
|
+
labelPosition: _labelPosition,
|
|
353
|
+
inline: _inline,
|
|
354
|
+
icon: _icon,
|
|
355
|
+
tooltip: _tooltip,
|
|
356
|
+
secondaryLabel: _secondaryLabel,
|
|
357
|
+
fontSize: _fontSize,
|
|
358
|
+
prefix: _prefix,
|
|
359
|
+
bottom: _bottom,
|
|
360
|
+
...props
|
|
361
|
+
}: FormControlProps) {
|
|
362
|
+
const base = React.useId();
|
|
363
|
+
const id = `${base}-control`;
|
|
364
|
+
const descriptionId = `${base}-description`;
|
|
365
|
+
const errorId = `${base}-error`;
|
|
366
|
+
const message = errorMessage ?? error;
|
|
367
|
+
const isInvalid = invalid ?? Boolean(message);
|
|
368
|
+
const describedBy = message ? errorId : description ? descriptionId : undefined;
|
|
369
|
+
|
|
370
|
+
return (
|
|
371
|
+
<div className={cn("flex flex-col gap-1", className)} {...props}>
|
|
372
|
+
{label && (
|
|
373
|
+
<Label
|
|
374
|
+
htmlFor={id}
|
|
375
|
+
required={required}
|
|
376
|
+
data-disabled={disabled || undefined}
|
|
377
|
+
className={hideLabel ? "sr-only" : undefined}
|
|
378
|
+
>
|
|
379
|
+
{label}
|
|
380
|
+
</Label>
|
|
381
|
+
)}
|
|
382
|
+
{(children as (field: FormControlField) => React.ReactNode)({
|
|
383
|
+
id,
|
|
384
|
+
"aria-describedby": describedBy,
|
|
385
|
+
"aria-invalid": isInvalid || undefined,
|
|
386
|
+
"aria-required": required || undefined,
|
|
387
|
+
disabled: disabled || undefined,
|
|
388
|
+
})}
|
|
389
|
+
{description && !message && (
|
|
390
|
+
<span id={descriptionId} className="block pl-1 text-sm leading-5 text-subdued">
|
|
391
|
+
{description}
|
|
392
|
+
</span>
|
|
393
|
+
)}
|
|
394
|
+
<div aria-live="polite">{message && <InlineError id={errorId} size="s" message={message} />}</div>
|
|
395
|
+
</div>
|
|
396
|
+
);
|
|
397
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { cn } from "../lib/utils";
|
|
2
|
+
|
|
3
|
+
export interface FormGroupProps extends React.ComponentProps<"div"> {
|
|
4
|
+
/** `horizontal` puts the fields side by side from 768px up. */
|
|
5
|
+
orientation?: "vertical" | "horizontal";
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Swirl's form group: spaces a set of fields, 16px apart on small screens and
|
|
10
|
+
* 24px from 768px up. Nest a horizontal group for fields that share a row,
|
|
11
|
+
* such as first and last name.
|
|
12
|
+
*/
|
|
13
|
+
export function FormGroup({ className, orientation = "vertical", ...props }: FormGroupProps) {
|
|
14
|
+
return (
|
|
15
|
+
<div
|
|
16
|
+
className={cn(
|
|
17
|
+
"flex flex-col",
|
|
18
|
+
orientation === "horizontal"
|
|
19
|
+
? "gap-4 md:flex-row md:*:min-w-0 md:*:flex-1"
|
|
20
|
+
: "gap-4 md:gap-6",
|
|
21
|
+
className,
|
|
22
|
+
)}
|
|
23
|
+
{...props}
|
|
24
|
+
/>
|
|
25
|
+
);
|
|
26
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { cn } from "../lib/utils";
|
|
2
|
+
|
|
3
|
+
export type HeadingLevel = 1 | 2 | 3 | 4 | 5 | 6;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Swirl's heading scale, as swirl-heading defines it: bold display sizes for
|
|
7
|
+
* levels 1-2, semibold below, stepping down to the body size at level 4 and
|
|
8
|
+
* the small size at 5-6. The sizes carry their own line heights.
|
|
9
|
+
*/
|
|
10
|
+
const levels = {
|
|
11
|
+
1: "text-2xl font-bold",
|
|
12
|
+
2: "text-xl font-bold",
|
|
13
|
+
3: "text-lg font-semibold",
|
|
14
|
+
4: "text-base font-semibold",
|
|
15
|
+
5: "text-sm font-semibold",
|
|
16
|
+
6: "text-sm font-semibold",
|
|
17
|
+
} as const satisfies Record<HeadingLevel, string>;
|
|
18
|
+
|
|
19
|
+
export interface HeadingProps extends React.ComponentProps<"h2"> {
|
|
20
|
+
/** Visual size *and* default element (`level={2}` renders `<h2>`). */
|
|
21
|
+
level?: HeadingLevel;
|
|
22
|
+
/** Override the element without changing the visual size. */
|
|
23
|
+
as?: React.ElementType;
|
|
24
|
+
/** Heading text. Interchangeable with `children`. */
|
|
25
|
+
text?: string;
|
|
26
|
+
align?: "start" | "center" | "end";
|
|
27
|
+
/** Even out the line lengths when the heading wraps. On by default, as in Swirl. */
|
|
28
|
+
balance?: boolean;
|
|
29
|
+
truncate?: boolean;
|
|
30
|
+
lines?: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function Heading({
|
|
34
|
+
level = 2,
|
|
35
|
+
as,
|
|
36
|
+
text,
|
|
37
|
+
align,
|
|
38
|
+
balance = true,
|
|
39
|
+
truncate,
|
|
40
|
+
lines,
|
|
41
|
+
className,
|
|
42
|
+
children,
|
|
43
|
+
style,
|
|
44
|
+
...props
|
|
45
|
+
}: HeadingProps) {
|
|
46
|
+
const Component = as ?? (`h${level}` as React.ElementType);
|
|
47
|
+
|
|
48
|
+
return (
|
|
49
|
+
<Component
|
|
50
|
+
className={cn(
|
|
51
|
+
"text-foreground",
|
|
52
|
+
levels[level],
|
|
53
|
+
align === "center" && "text-center",
|
|
54
|
+
align === "end" && "text-end",
|
|
55
|
+
balance && "text-balance",
|
|
56
|
+
truncate && !lines && "truncate",
|
|
57
|
+
lines && "overflow-hidden",
|
|
58
|
+
className,
|
|
59
|
+
)}
|
|
60
|
+
style={
|
|
61
|
+
lines
|
|
62
|
+
? { display: "-webkit-box", WebkitBoxOrient: "vertical", WebkitLineClamp: lines, ...style }
|
|
63
|
+
: style
|
|
64
|
+
}
|
|
65
|
+
{...props}
|
|
66
|
+
>
|
|
67
|
+
{children ?? text}
|
|
68
|
+
</Component>
|
|
69
|
+
);
|
|
70
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { cn } from "../lib/utils";
|
|
2
|
+
|
|
3
|
+
const sizes = {
|
|
4
|
+
16: "size-4",
|
|
5
|
+
20: "size-5",
|
|
6
|
+
24: "size-6",
|
|
7
|
+
28: "size-7",
|
|
8
|
+
32: "size-8",
|
|
9
|
+
} as const;
|
|
10
|
+
|
|
11
|
+
const colors = {
|
|
12
|
+
default: "text-icon-default",
|
|
13
|
+
strong: "text-icon-strong",
|
|
14
|
+
subdued: "text-subdued",
|
|
15
|
+
disabled: "text-disabled",
|
|
16
|
+
highlight: "text-highlight",
|
|
17
|
+
critical: "text-critical",
|
|
18
|
+
success: "text-success",
|
|
19
|
+
warning: "text-warning",
|
|
20
|
+
info: "text-info",
|
|
21
|
+
"on-action-primary": "text-on-primary",
|
|
22
|
+
"on-image": "text-on-image",
|
|
23
|
+
"on-status": "text-on-status",
|
|
24
|
+
"on-surface-highlight": "text-on-highlight",
|
|
25
|
+
"on-surface-highlight-subdued": "text-on-highlight-subdued",
|
|
26
|
+
inherit: "",
|
|
27
|
+
} as const;
|
|
28
|
+
|
|
29
|
+
/* Swirl's round icon backgrounds: the decorative hues, each with its text
|
|
30
|
+
colour for the icon, plus neutral and the page background. */
|
|
31
|
+
const wrapperColors = {
|
|
32
|
+
neutral: "bg-surface-raised text-icon-default",
|
|
33
|
+
default: "bg-background text-icon-default",
|
|
34
|
+
banana: "bg-banana-subdued text-banana-text",
|
|
35
|
+
blueberry: "bg-blueberry-subdued text-blueberry-text",
|
|
36
|
+
chilli: "bg-chilli-subdued text-chilli-text",
|
|
37
|
+
grape: "bg-grape-subdued text-grape-text",
|
|
38
|
+
kiwi: "bg-kiwi-subdued text-kiwi-text",
|
|
39
|
+
pumpkin: "bg-pumpkin-subdued text-pumpkin-text",
|
|
40
|
+
radish: "bg-radish-subdued text-radish-text",
|
|
41
|
+
} as const;
|
|
42
|
+
|
|
43
|
+
const wrapperSizes = {
|
|
44
|
+
xs: "size-4",
|
|
45
|
+
s: "size-5",
|
|
46
|
+
m: "size-6",
|
|
47
|
+
l: "size-7",
|
|
48
|
+
xl: "size-8",
|
|
49
|
+
"2xl": "size-10",
|
|
50
|
+
} as const;
|
|
51
|
+
|
|
52
|
+
export interface IconProps extends Omit<React.ComponentProps<"span">, "color"> {
|
|
53
|
+
/** Box size in px. */
|
|
54
|
+
size?: keyof typeof sizes;
|
|
55
|
+
/**
|
|
56
|
+
* Semantic icon colour. `inherit` takes the parent's colour. Defaults to
|
|
57
|
+
* `default`, or to the wrapper's own icon colour when `wrapperColor` is set.
|
|
58
|
+
*/
|
|
59
|
+
color?: keyof typeof colors;
|
|
60
|
+
/** Sit the icon on a round background in this colour, as Swirl's icon wrapper. */
|
|
61
|
+
wrapperColor?: keyof typeof wrapperColors;
|
|
62
|
+
/** Diameter of the wrapper: 16 / 20 / 24 / 28 / 32 / 40px. */
|
|
63
|
+
wrapperSize?: keyof typeof wrapperSizes;
|
|
64
|
+
/** Accessible name. Omit for a decorative icon beside visible text. */
|
|
65
|
+
label?: string;
|
|
66
|
+
children: React.ReactNode;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Sizes and colours any SVG icon with Swirl's icon tokens.
|
|
71
|
+
*
|
|
72
|
+
* Works with `@enablo/swirlcn-icons`, lucide-react, or a hand-written SVG - the child
|
|
73
|
+
* is scaled to the box rather than being expected to size itself.
|
|
74
|
+
*/
|
|
75
|
+
export function Icon({
|
|
76
|
+
className,
|
|
77
|
+
size = 24,
|
|
78
|
+
color,
|
|
79
|
+
wrapperColor,
|
|
80
|
+
wrapperSize = "m",
|
|
81
|
+
label,
|
|
82
|
+
children,
|
|
83
|
+
...props
|
|
84
|
+
}: IconProps) {
|
|
85
|
+
const icon = (
|
|
86
|
+
<span
|
|
87
|
+
role={label ? "img" : undefined}
|
|
88
|
+
aria-label={label}
|
|
89
|
+
aria-hidden={label ? undefined : true}
|
|
90
|
+
className={cn(
|
|
91
|
+
"inline-flex shrink-0 items-center justify-center",
|
|
92
|
+
sizes[size],
|
|
93
|
+
// Inside a wrapper the icon takes the wrapper's colour unless told otherwise.
|
|
94
|
+
colors[color ?? (wrapperColor ? "inherit" : "default")],
|
|
95
|
+
"[&>svg]:size-full",
|
|
96
|
+
!wrapperColor && className,
|
|
97
|
+
)}
|
|
98
|
+
{...(wrapperColor ? {} : props)}
|
|
99
|
+
>
|
|
100
|
+
{children}
|
|
101
|
+
</span>
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
if (!wrapperColor) return icon;
|
|
105
|
+
|
|
106
|
+
return (
|
|
107
|
+
<span
|
|
108
|
+
className={cn(
|
|
109
|
+
"inline-flex shrink-0 items-center justify-center rounded-full",
|
|
110
|
+
wrapperColors[wrapperColor],
|
|
111
|
+
wrapperSizes[wrapperSize],
|
|
112
|
+
className,
|
|
113
|
+
)}
|
|
114
|
+
{...props}
|
|
115
|
+
>
|
|
116
|
+
{icon}
|
|
117
|
+
</span>
|
|
118
|
+
);
|
|
119
|
+
}
|