182 lines · 9.0 KBRaw
| 1 | import { Checkbox as BaseCheckbox } from "@base-ui/react/checkbox"; |
| 2 | import { Radio as BaseRadio } from "@base-ui/react/radio"; |
| 3 | import { RadioGroup } from "@base-ui/react/radio-group"; |
| 4 | import { Select as BaseSelect } from "@base-ui/react/select"; |
| 5 | import { CaretUpDownIcon, CheckIcon } from "../icons"; |
| 6 | import { useId, type ComponentProps, type ReactNode } from "react"; |
| 7 | import { cn } from "./cn"; |
| 8 | |
| 9 | const FIELD = |
| 10 | "w-full rounded-control bg-field text-ink shadow-[inset_0_0_0_1px_var(--line)] placeholder:text-faint transition-[box-shadow,background-color] duration-150 hover:shadow-[inset_0_0_0_1px_color-mix(in_oklab,var(--ink)_26%,transparent)] focus:outline-none focus-visible:outline-none focus:shadow-[inset_0_0_0_1px_var(--accent-hover),0_0_0_3px_var(--accent-tint)] disabled:opacity-50 aria-invalid:shadow-[inset_0_0_0_1px_var(--danger)] aria-invalid:focus:shadow-[inset_0_0_0_1px_var(--danger),0_0_0_3px_var(--danger-tint)]"; |
| 11 | |
| 12 | interface FieldText { |
| 13 | label?: ReactNode; |
| 14 | description?: ReactNode; |
| 15 | error?: ReactNode; |
| 16 | } |
| 17 | |
| 18 | function Frame({ id, label, description, error, children, className }: FieldText & { id: string; children: ReactNode; className?: string }) { |
| 19 | return ( |
| 20 | <div className={cn("flex min-w-0 flex-col gap-1.5", className)}> |
| 21 | {label && ( |
| 22 | <label htmlFor={id} className="text-compact font-medium text-ink"> |
| 23 | {label} |
| 24 | </label> |
| 25 | )} |
| 26 | {children} |
| 27 | {error ? ( |
| 28 | <p id={`${id}-error`} className="m-0 text-xs text-danger"> |
| 29 | {error} |
| 30 | </p> |
| 31 | ) : ( |
| 32 | description && ( |
| 33 | <p id={`${id}-description`} className="m-0 text-xs text-muted"> |
| 34 | {description} |
| 35 | </p> |
| 36 | ) |
| 37 | )} |
| 38 | </div> |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | export type InputProps = FieldText & Omit<ComponentProps<"input">, "size"> & { size?: "base" | "lg" }; |
| 43 | |
| 44 | /** A single-line text field with its label, help text and error. */ |
| 45 | export function Input({ label, description, error, size = "base", className, id, ...props }: InputProps) { |
| 46 | const generated = useId(); |
| 47 | const field = id ?? generated; |
| 48 | return ( |
| 49 | <Frame id={field} label={label} description={description} error={error} className={label || description || error ? undefined : className}> |
| 50 | <input |
| 51 | id={field} |
| 52 | aria-invalid={error ? true : undefined} |
| 53 | aria-describedby={error ? `${field}-error` : description ? `${field}-description` : undefined} |
| 54 | className={cn(FIELD, size === "lg" ? "h-10 px-3 text-callout pointer-coarse:h-12" : "h-8 px-2.5 text-compact pointer-coarse:h-10", label || description || error ? className : undefined)} |
| 55 | {...props} |
| 56 | /> |
| 57 | </Frame> |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | export type InputAreaProps = FieldText & ComponentProps<"textarea"> & { onValueChange?: (value: string) => void }; |
| 62 | |
| 63 | /** A multi-line text field. `onValueChange` receives the text on every edit. */ |
| 64 | export function InputArea({ label, description, error, className, id, onValueChange, onChange, rows = 4, ...props }: InputAreaProps) { |
| 65 | const generated = useId(); |
| 66 | const field = id ?? generated; |
| 67 | return ( |
| 68 | <Frame id={field} label={label} description={description} error={error}> |
| 69 | <textarea |
| 70 | id={field} |
| 71 | rows={rows} |
| 72 | aria-invalid={error ? true : undefined} |
| 73 | aria-describedby={error ? `${field}-error` : description ? `${field}-description` : undefined} |
| 74 | onChange={(event) => { |
| 75 | onChange?.(event); |
| 76 | onValueChange?.(event.target.value); |
| 77 | }} |
| 78 | className={cn(FIELD, "resize-y px-2.5 py-2 text-compact leading-relaxed", className)} |
| 79 | {...props} |
| 80 | /> |
| 81 | </Frame> |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | export interface SelectProps { |
| 86 | label?: ReactNode; |
| 87 | name?: string; |
| 88 | items: Record<string, ReactNode>; |
| 89 | value?: string; |
| 90 | defaultValue?: string; |
| 91 | onValueChange?: (value: string) => void; |
| 92 | className?: string; |
| 93 | disabled?: boolean; |
| 94 | } |
| 95 | |
| 96 | /** A closed list of choices. It submits as a hidden form field named `name`. */ |
| 97 | export function Select({ label, name, items, value, defaultValue, onValueChange, className, disabled }: SelectProps) { |
| 98 | const id = useId(); |
| 99 | return ( |
| 100 | <div className={cn("flex min-w-0 flex-col gap-1.5", className)}> |
| 101 | {label && ( |
| 102 | <label htmlFor={id} className="text-compact font-medium text-ink"> |
| 103 | {label} |
| 104 | </label> |
| 105 | )} |
| 106 | <BaseSelect.Root items={items} name={name} value={value} defaultValue={defaultValue ?? Object.keys(items)[0]} onValueChange={(next) => onValueChange?.(next as string)} disabled={disabled}> |
| 107 | <BaseSelect.Trigger id={id} className={cn(FIELD, "flex h-8 min-w-36 cursor-pointer items-center justify-between gap-3 px-2.5 text-left text-compact pointer-coarse:h-10 data-[popup-open]:shadow-[inset_0_0_0_1px_var(--accent-hover),0_0_0_3px_var(--accent-tint)]")}> |
| 108 | <BaseSelect.Value className="truncate" /> |
| 109 | <BaseSelect.Icon className="text-faint"> |
| 110 | <CaretUpDownIcon size={14} /> |
| 111 | </BaseSelect.Icon> |
| 112 | </BaseSelect.Trigger> |
| 113 | <BaseSelect.Portal> |
| 114 | <BaseSelect.Positioner sideOffset={6} alignItemWithTrigger={false} className="z-50"> |
| 115 | <BaseSelect.Popup className="popup material-popup max-h-[var(--available-height)] min-w-[var(--anchor-width)] overflow-y-auto rounded-panel p-1 shadow-surface"> |
| 116 | <BaseSelect.List> |
| 117 | {Object.entries(items).map(([key, text]) => ( |
| 118 | <BaseSelect.Item key={key} value={key} className="flex cursor-pointer items-center justify-between gap-6 rounded-control px-2 py-1.5 text-compact text-ink outline-none data-[highlighted]:bg-hover pointer-coarse:py-2.5"> |
| 119 | <BaseSelect.ItemText>{text}</BaseSelect.ItemText> |
| 120 | <BaseSelect.ItemIndicator className="text-accent-text"> |
| 121 | <CheckIcon size={14} /> |
| 122 | </BaseSelect.ItemIndicator> |
| 123 | </BaseSelect.Item> |
| 124 | ))} |
| 125 | </BaseSelect.List> |
| 126 | </BaseSelect.Popup> |
| 127 | </BaseSelect.Positioner> |
| 128 | </BaseSelect.Portal> |
| 129 | </BaseSelect.Root> |
| 130 | </div> |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | /** A yes/no choice with its label. */ |
| 135 | export function Checkbox({ label, checked, defaultChecked, onCheckedChange, name, value, disabled, className }: { label?: ReactNode; checked?: boolean; defaultChecked?: boolean; onCheckedChange?: (checked: boolean) => void; name?: string; value?: string; disabled?: boolean; className?: string }) { |
| 136 | return ( |
| 137 | <label className={cn("inline-flex cursor-pointer items-start gap-2 text-compact text-ink has-[[data-disabled]]:cursor-not-allowed has-[[data-disabled]]:opacity-50", className)}> |
| 138 | <BaseCheckbox.Root |
| 139 | checked={checked} |
| 140 | defaultChecked={defaultChecked} |
| 141 | onCheckedChange={(next) => onCheckedChange?.(next === true)} |
| 142 | name={name} |
| 143 | value={value} |
| 144 | disabled={disabled} |
| 145 | className="mt-0.5 flex size-4 shrink-0 items-center justify-center rounded-[5px] bg-field shadow-[inset_0_0_0_1px_var(--line)] transition-[background-color,box-shadow] duration-150 data-[checked]:bg-accent data-[checked]:shadow-none" |
| 146 | > |
| 147 | <BaseCheckbox.Indicator className="text-on-accent"> |
| 148 | <CheckIcon size={11} weight="bold" /> |
| 149 | </BaseCheckbox.Indicator> |
| 150 | </BaseCheckbox.Root> |
| 151 | {label && <span>{label}</span>} |
| 152 | </label> |
| 153 | ); |
| 154 | } |
| 155 | |
| 156 | function Group({ legend, name, defaultValue, value, onValueChange, appearance, children, className }: { legend?: ReactNode; name?: string; defaultValue?: string; value?: string; onValueChange?: (value: string) => void; appearance?: "card"; children: ReactNode; className?: string }) { |
| 157 | return ( |
| 158 | <fieldset className={cn("m-0 flex min-w-0 flex-col gap-1.5 border-0 p-0", className)}> |
| 159 | {legend && <legend className="mb-1.5 p-0 text-compact font-medium text-ink">{legend}</legend>} |
| 160 | <RadioGroup name={name} defaultValue={defaultValue} value={value} onValueChange={(next) => onValueChange?.(next as string)} className={cn("flex flex-col", appearance === "card" ? "gap-2" : "gap-1.5")} data-appearance={appearance}> |
| 161 | {children} |
| 162 | </RadioGroup> |
| 163 | </fieldset> |
| 164 | ); |
| 165 | } |
| 166 | |
| 167 | function Item({ value, label, description }: { value: string; label: ReactNode; description?: ReactNode }) { |
| 168 | return ( |
| 169 | <label className="group/radio flex cursor-pointer items-start gap-3 rounded-control bg-field px-3.5 py-3 text-compact text-ink shadow-[inset_0_0_0_1px_var(--line)] transition-[background-color,box-shadow] duration-150 hover:bg-hover has-[[data-checked]]:bg-accent-tint has-[[data-checked]]:shadow-[inset_0_0_0_1.5px_var(--accent-hover)]"> |
| 170 | <BaseRadio.Root value={value} className="mt-0.5 flex size-4 shrink-0 items-center justify-center rounded-full bg-field shadow-[inset_0_0_0_1px_var(--line)] data-[checked]:shadow-[inset_0_0_0_4.5px_var(--accent)]"> |
| 171 | <BaseRadio.Indicator className="size-1.5 rounded-full bg-on-accent" /> |
| 172 | </BaseRadio.Root> |
| 173 | <span className="flex min-w-0 flex-col gap-0.5"> |
| 174 | <span className="font-medium">{label}</span> |
| 175 | {description && <span className="text-xs text-muted">{description}</span>} |
| 176 | </span> |
| 177 | </label> |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | /** One choice among several, shown as selectable cards. */ |
| 182 | export const Radio = { Group, Item }; |