From 58839b0956a77b7312e1e11c39875f13c951834e Mon Sep 17 00:00:00 2001 From: olestang Date: Wed, 30 Sep 2026 10:44:31 +0200 Subject: [PATCH 1/7] feat: add form UI components --- .../src/components/ui/Checkbox/Checkbox.css | 131 ++++++++ .../src/components/ui/Checkbox/Checkbox.tsx | 60 ++++ apps/web/src/components/ui/Field/Field.css | 87 +++++ apps/web/src/components/ui/Field/Field.tsx | 58 ++++ apps/web/src/components/ui/Radio/Radio.css | 199 +++++++++++ apps/web/src/components/ui/Radio/Radio.tsx | 40 +++ .../src/components/ui/Radio/RadioContext.ts | 26 ++ .../src/components/ui/Radio/RadioGroup.tsx | 68 ++++ .../src/components/ui/Textarea/Textarea.css | 96 ++++++ .../src/components/ui/Textarea/Textarea.tsx | 60 ++++ .../ui/ToggleSwitch/ToggleSwitch.css | 311 ++++++++++++++++++ .../ui/ToggleSwitch/ToggleSwitch.tsx | 119 +++++++ .../src/components/ui/internal/FieldText.tsx | 35 ++ .../src/components/ui/internal/fieldA11y.ts | 32 ++ 14 files changed, 1322 insertions(+) create mode 100644 apps/web/src/components/ui/Checkbox/Checkbox.css create mode 100644 apps/web/src/components/ui/Checkbox/Checkbox.tsx create mode 100644 apps/web/src/components/ui/Field/Field.css create mode 100644 apps/web/src/components/ui/Field/Field.tsx create mode 100644 apps/web/src/components/ui/Radio/Radio.css create mode 100644 apps/web/src/components/ui/Radio/Radio.tsx create mode 100644 apps/web/src/components/ui/Radio/RadioContext.ts create mode 100644 apps/web/src/components/ui/Radio/RadioGroup.tsx create mode 100644 apps/web/src/components/ui/Textarea/Textarea.css create mode 100644 apps/web/src/components/ui/Textarea/Textarea.tsx create mode 100644 apps/web/src/components/ui/ToggleSwitch/ToggleSwitch.css create mode 100644 apps/web/src/components/ui/ToggleSwitch/ToggleSwitch.tsx create mode 100644 apps/web/src/components/ui/internal/FieldText.tsx create mode 100644 apps/web/src/components/ui/internal/fieldA11y.ts diff --git a/apps/web/src/components/ui/Checkbox/Checkbox.css b/apps/web/src/components/ui/Checkbox/Checkbox.css new file mode 100644 index 0000000..297fbeb --- /dev/null +++ b/apps/web/src/components/ui/Checkbox/Checkbox.css @@ -0,0 +1,131 @@ +@layer components { + .checkbox { + display: flex; + flex-direction: column; + gap: var(--space-1); + + width: fit-content; + max-width: 100%; + } + + .checkbox__label { + display: inline-flex; + align-items: center; + gap: var(--space-2); + + min-height: var(--target-size-min); + + color: var(--color-text-primary); + + cursor: pointer; + } + + .checkbox__control { + appearance: none; + + display: grid; + flex: 0 0 auto; + place-content: center; + + width: var(--icon-size-md); + height: var(--icon-size-md); + margin: 0; + + color: var(--color-text-inverse); + background-color: var(--color-surface); + + border: + var(--border-width-thin) + solid + var(--color-border-input); + + border-radius: var(--radius-sm); + + cursor: pointer; + + transition: + background-color var(--duration-fast) var(--ease-standard), + border-color var(--duration-fast) var(--ease-standard), + box-shadow var(--duration-fast) var(--ease-standard); + } + + .checkbox__control::before { + content: ""; + + width: 0.35rem; + height: 0.65rem; + + border-right: var(--border-width-medium) solid currentColor; + border-bottom: var(--border-width-medium) solid currentColor; + + transform: rotate(45deg) scale(0); + transform-origin: center; + + transition: + transform var(--duration-fast) var(--ease-standard); + } + + .checkbox__control:hover:not(:disabled) { + background-color: var(--color-surface-hover); + border-color: var(--color-border-input-hover); + } + + .checkbox__control:checked { + background-color: var(--color-brand-primary); + border-color: var(--color-brand-primary); + } + + .checkbox__control:checked::before { + transform: rotate(45deg) scale(1); + } + + .checkbox__control:checked:hover:not(:disabled) { + background-color: var(--color-brand-primary-hover); + border-color: var(--color-brand-primary-hover); + } + + .checkbox--error .checkbox__control { + border-color: var(--color-feedback-error); + } + + .checkbox__control:disabled { + color: var(--color-disabled-text); + background-color: var(--color-disabled-surface); + border-color: var(--color-disabled-border); + + cursor: not-allowed; + } + + .checkbox__control:disabled + .checkbox__text { + color: var(--color-disabled-text); + + cursor: not-allowed; + } + + .checkbox__text { + line-height: var(--line-height-normal); + } + + .checkbox__required { + margin-inline-start: var(--space-1); + + color: var(--color-feedback-error); + } + + .checkbox__help, + .checkbox__error { + padding-inline-start: + calc(var(--icon-size-md) + var(--space-2)); + + font-size: var(--font-size-sm); + line-height: var(--line-height-snug); + } + + .checkbox__help { + color: var(--color-text-secondary); + } + + .checkbox__error { + color: var(--color-feedback-error); + } +} \ No newline at end of file diff --git a/apps/web/src/components/ui/Checkbox/Checkbox.tsx b/apps/web/src/components/ui/Checkbox/Checkbox.tsx new file mode 100644 index 0000000..63d616c --- /dev/null +++ b/apps/web/src/components/ui/Checkbox/Checkbox.tsx @@ -0,0 +1,60 @@ +import type { ComponentPropsWithRef, ReactNode } from "react"; + +import { FieldError, FieldHelp, RequiredMarker } from "../internal/FieldText"; +import { useFieldA11y } from "../internal/fieldA11y"; +import "./Checkbox.css"; + +export type CheckboxProps = Omit, "type" | "size"> & { + label: ReactNode; + helpText?: ReactNode; + error?: ReactNode; +}; + +function Checkbox({ + label, + helpText, + error, + id, + required, + disabled, + className = "", + "aria-describedby": ariaDescribedBy, + ...props +}: CheckboxProps) { + const { controlId, helpId, errorId, describedBy, invalid } = useFieldA11y({ + id, + helpText, + error, + ariaDescribedBy, + }); + + const classes = ["checkbox", error && "checkbox--error", className] + .filter(Boolean) + .join(" "); + + return ( +
+ + + {helpText} + {error} +
+ ); +} + +export default Checkbox; diff --git a/apps/web/src/components/ui/Field/Field.css b/apps/web/src/components/ui/Field/Field.css new file mode 100644 index 0000000..be6fc85 --- /dev/null +++ b/apps/web/src/components/ui/Field/Field.css @@ -0,0 +1,87 @@ +@layer components { + .field { + display: flex; + flex-direction: column; + gap: var(--space-2); + + width: 100%; + min-width: 0; + } + + .field__label { + width: fit-content; + + color: var(--color-text-primary); + + font-size: var(--font-size-sm); + font-weight: var(--font-weight-semibold); + line-height: var(--line-height-snug); + } + + .field__required { + margin-inline-start: var(--space-1); + + color: var(--color-feedback-error); + } + + .field__input { + width: 100%; + min-width: 0; + min-height: var(--control-height-md); + + padding-inline: var(--space-3); + + color: var(--color-text-primary); + background-color: var(--color-surface); + + border: + var(--border-width-thin) + solid + var(--color-border-input); + + border-radius: var(--radius-md); + + transition: + border-color var(--duration-fast) var(--ease-standard), + background-color var(--duration-fast) var(--ease-standard), + box-shadow var(--duration-fast) var(--ease-standard); + } + + .field__input:hover:not(:disabled) { + border-color: var(--color-border-input-hover); + } + + .field__input:focus-visible { + border-color: var(--color-focus-ring); + } + + .field__input--error { + border-color: var(--color-feedback-error); + } + + .field__input--error:hover { + border-color: var(--color-feedback-error); + } + + .field__input:disabled { + color: var(--color-disabled-text); + background-color: var(--color-disabled-surface); + border-color: var(--color-disabled-border); + + cursor: not-allowed; + } + + .field__help, + .field__error { + font-size: var(--font-size-sm); + line-height: var(--line-height-snug); + } + + .field__help { + color: var(--color-text-secondary); + } + + .field__error { + color: var(--color-feedback-error); + } +} \ No newline at end of file diff --git a/apps/web/src/components/ui/Field/Field.tsx b/apps/web/src/components/ui/Field/Field.tsx new file mode 100644 index 0000000..14dc412 --- /dev/null +++ b/apps/web/src/components/ui/Field/Field.tsx @@ -0,0 +1,58 @@ +import type { ComponentPropsWithRef, ReactNode } from "react"; + +import { FieldError, FieldHelp, RequiredMarker } from "../internal/FieldText"; +import { useFieldA11y } from "../internal/fieldA11y"; +import "./Field.css"; + +export type FieldProps = Omit, "size"> & { + label: ReactNode; + helpText?: ReactNode; + error?: ReactNode; +}; + +function Field({ + label, + helpText, + error, + id, + required, + disabled, + className = "", + "aria-describedby": ariaDescribedBy, + ...props +}: FieldProps) { + const { controlId, helpId, errorId, describedBy, invalid } = useFieldA11y({ + id, + helpText, + error, + ariaDescribedBy, + }); + + const inputClasses = ["field__input", error && "field__input--error", className] + .filter(Boolean) + .join(" "); + + return ( +
+ + + + + {helpText} + {error} +
+ ); +} + +export default Field; diff --git a/apps/web/src/components/ui/Radio/Radio.css b/apps/web/src/components/ui/Radio/Radio.css new file mode 100644 index 0000000..47534fa --- /dev/null +++ b/apps/web/src/components/ui/Radio/Radio.css @@ -0,0 +1,199 @@ +@layer components { + /* Group */ + + .radio-group { + display: flex; + flex-direction: column; + gap: var(--space-2); + + min-width: 0; + margin: 0; + padding: 0; + + border: 0; + } + + .radio-group__legend { + padding: 0; + + color: var(--color-text-primary); + + font-size: var(--font-size-sm); + font-weight: var(--font-weight-semibold); + line-height: var(--line-height-snug); + } + + .radio-group__required { + margin-inline-start: var(--space-1); + + color: var(--color-feedback-error); + } + + .radio-group__help, + .radio-group__error { + font-size: var(--font-size-sm); + line-height: var(--line-height-snug); + } + + .radio-group__help { + color: var(--color-text-secondary); + } + + .radio-group__error { + margin-block-start: calc(var(--space-1) * -1); + + color: var(--color-feedback-error); + } + + /* Options */ + + .radio-group__options { + display: flex; + gap: var(--space-2); + } + + /* + * When there is no help text, the options sit directly + * after the legend. Add a little extra separation so the + * group title does not feel crowded against the first radio. + * + * Combined with the group's existing space-2 gap, this gives + * a total separation of space-3. + */ + .radio-group__legend + .radio-group__options { + margin-block-start: var(--space-1); + } + + .radio-group__options--vertical { + flex-direction: column; + } + + .radio-group__options--horizontal { + flex-direction: row; + flex-wrap: wrap; + column-gap: var(--space-6); + } + + /* Radio */ + + .radio { + display: inline-flex; + align-items: flex-start; + gap: var(--space-2); + + width: fit-content; + max-width: 100%; + min-height: var(--target-size-min); + + color: var(--color-text-primary); + + cursor: pointer; + } + + .radio__control { + appearance: none; + + display: grid; + flex: 0 0 auto; + place-content: center; + + width: var(--icon-size-md); + height: var(--icon-size-md); + + margin: 0; + margin-block-start: var(--space-1); + + background-color: var(--color-surface); + + border: + var(--border-width-medium) + solid + var(--color-choice-border); + + border-radius: var(--radius-full); + + cursor: pointer; + + transition: + background-color var(--duration-fast) var(--ease-standard), + border-color var(--duration-fast) var(--ease-standard), + box-shadow var(--duration-fast) var(--ease-standard); + } + + /* Inner selected dot */ + + .radio__control::before { + content: ""; + + width: 0.5rem; + height: 0.5rem; + + background-color: var(--color-brand-primary); + border-radius: var(--radius-full); + + transform: scale(0); + + transition: + transform var(--duration-fast) var(--ease-standard), + background-color var(--duration-fast) var(--ease-standard); + } + + /* Hover */ + + .radio__control:hover:not(:disabled) { + border-color: var(--color-choice-border-hover); + } + + /* Selected */ + + .radio__control:checked { + background-color: var(--color-surface); + border-color: var(--color-brand-primary); + } + + .radio__control:checked::before { + transform: scale(1); + } + + .radio__control:checked:hover:not(:disabled) { + border-color: var(--color-brand-primary-hover); + } + + .radio__control:checked:hover:not(:disabled)::before { + background-color: var(--color-brand-primary-hover); + } + + /* Error */ + + .radio-group--error + .radio__control:not(:checked) { + border-color: var(--color-feedback-error); + } + + /* Disabled */ + + .radio__control:disabled { + background-color: var(--color-disabled-surface); + border-color: var(--color-disabled-border); + + cursor: not-allowed; + } + + .radio__control:disabled::before { + background-color: var(--color-disabled-text); + } + + .radio__control:disabled + .radio__text { + color: var(--color-disabled-text); + + cursor: not-allowed; + } + + /* Label text */ + + .radio__text { + min-width: 0; + + line-height: var(--line-height-normal); + } +} \ No newline at end of file diff --git a/apps/web/src/components/ui/Radio/Radio.tsx b/apps/web/src/components/ui/Radio/Radio.tsx new file mode 100644 index 0000000..b9044bb --- /dev/null +++ b/apps/web/src/components/ui/Radio/Radio.tsx @@ -0,0 +1,40 @@ +import type { ComponentPropsWithRef, ReactNode } from "react"; + +import { useRadioGroupContext } from "./RadioContext"; +import "./Radio.css"; + +export type RadioProps = Omit, "type" | "name" | "required"> & { + label: ReactNode; + value: string; +}; + +function Radio({ + label, + value, + className = "", + "aria-describedby": ariaDescribedBy, + "aria-invalid": ariaInvalid, + ...props +}: RadioProps) { + const group = useRadioGroupContext(); + const describedBy = [ariaDescribedBy, group.describedBy].filter(Boolean).join(" ") || undefined; + const inputClasses = ["radio__control", className].filter(Boolean).join(" "); + + return ( + + ); +} + +export default Radio; diff --git a/apps/web/src/components/ui/Radio/RadioContext.ts b/apps/web/src/components/ui/Radio/RadioContext.ts new file mode 100644 index 0000000..931f47a --- /dev/null +++ b/apps/web/src/components/ui/Radio/RadioContext.ts @@ -0,0 +1,26 @@ +import { + createContext, + useContext, +} from "react"; + +export type RadioGroupContextValue = { + name: string; + required: boolean; + describedBy?: string; + invalid: boolean; +}; + +export const RadioGroupContext = + createContext(null); + +export function useRadioGroupContext() { + const context = useContext(RadioGroupContext); + + if (!context) { + throw new Error( + "Radio must be used inside a RadioGroup.", + ); + } + + return context; +} \ No newline at end of file diff --git a/apps/web/src/components/ui/Radio/RadioGroup.tsx b/apps/web/src/components/ui/Radio/RadioGroup.tsx new file mode 100644 index 0000000..a5406c9 --- /dev/null +++ b/apps/web/src/components/ui/Radio/RadioGroup.tsx @@ -0,0 +1,68 @@ +import type { ComponentPropsWithRef, ReactNode } from "react"; + +import { RequiredMarker } from "../internal/FieldText"; +import { useFieldA11y } from "../internal/fieldA11y"; +import { RadioGroupContext } from "./RadioContext"; +import "./Radio.css"; + +export type RadioGroupLayout = "vertical" | "horizontal"; + +export type RadioGroupProps = Omit, "name"> & { + name: string; + label: ReactNode; + helpText?: ReactNode; + error?: ReactNode; + required?: boolean; + layout?: RadioGroupLayout; + children: ReactNode; +}; + +function RadioGroup({ + name, + label, + helpText, + error, + required = false, + layout = "vertical", + children, + className = "", + disabled, + id, + "aria-describedby": ariaDescribedBy, + ...props +}: RadioGroupProps) { + const { controlId, helpId, errorId, describedBy } = useFieldA11y({ + id, + helpText, + error, + ariaDescribedBy, + }); + + const classes = ["radio-group", error && "radio-group--error", className] + .filter(Boolean) + .join(" "); + const optionsClasses = ["radio-group__options", `radio-group__options--${layout}`].join(" "); + + return ( + +
+ + {label} + {required && } + + + {helpText &&
{helpText}
} +
{children}
+ {error && } +
+
+ ); +} + +export default RadioGroup; diff --git a/apps/web/src/components/ui/Textarea/Textarea.css b/apps/web/src/components/ui/Textarea/Textarea.css new file mode 100644 index 0000000..fb90804 --- /dev/null +++ b/apps/web/src/components/ui/Textarea/Textarea.css @@ -0,0 +1,96 @@ +@layer components { + .textarea { + display: flex; + flex-direction: column; + gap: var(--space-2); + + width: 100%; + min-width: 0; + } + + .textarea__label { + width: fit-content; + + color: var(--color-text-primary); + + font-size: var(--font-size-sm); + font-weight: var(--font-weight-semibold); + line-height: var(--line-height-snug); + } + + .textarea__required { + margin-inline-start: var(--space-1); + + color: var(--color-feedback-error); + } + + .textarea__control { + display: block; + + width: 100%; + min-width: 0; + min-height: 7rem; + + padding: + var(--space-3) + var(--space-3); + + color: var(--color-text-primary); + background-color: var(--color-surface); + + line-height: var(--line-height-normal); + + border: + var(--border-width-thin) + solid + var(--color-border-input); + + border-radius: var(--radius-md); + + resize: vertical; + + transition: + border-color var(--duration-fast) var(--ease-standard), + background-color var(--duration-fast) var(--ease-standard), + box-shadow var(--duration-fast) var(--ease-standard); + } + + .textarea__control:hover:not(:disabled) { + border-color: var(--color-border-input-hover); + } + + .textarea__control:focus-visible { + border-color: var(--color-focus-ring); + } + + .textarea__control--error { + border-color: var(--color-feedback-error); + } + + .textarea__control--error:hover:not(:disabled) { + border-color: var(--color-feedback-error); + } + + .textarea__control:disabled { + color: var(--color-disabled-text); + background-color: var(--color-disabled-surface); + border-color: var(--color-disabled-border); + + cursor: not-allowed; + resize: none; + } + + .textarea__help, + .textarea__error { + font-size: var(--font-size-sm); + line-height: var(--line-height-snug); + } + + .textarea__help { + color: var(--color-text-secondary); + } + + .textarea__error { + color: var(--color-feedback-error); + } +} \ No newline at end of file diff --git a/apps/web/src/components/ui/Textarea/Textarea.tsx b/apps/web/src/components/ui/Textarea/Textarea.tsx new file mode 100644 index 0000000..163ac3a --- /dev/null +++ b/apps/web/src/components/ui/Textarea/Textarea.tsx @@ -0,0 +1,60 @@ +import type { ComponentPropsWithRef, ReactNode } from "react"; + +import { FieldError, FieldHelp, RequiredMarker } from "../internal/FieldText"; +import { useFieldA11y } from "../internal/fieldA11y"; +import "./Textarea.css"; + +export type TextareaProps = ComponentPropsWithRef<"textarea"> & { + label: ReactNode; + helpText?: ReactNode; + error?: ReactNode; +}; + +function Textarea({ + label, + helpText, + error, + id, + required, + disabled, + className = "", + rows = 5, + "aria-describedby": ariaDescribedBy, + ...props +}: TextareaProps) { + const { controlId, helpId, errorId, describedBy, invalid } = useFieldA11y({ + id, + helpText, + error, + ariaDescribedBy, + }); + + const textareaClasses = ["textarea__control", error && "textarea__control--error", className] + .filter(Boolean) + .join(" "); + + return ( +
+ + +