Forms
A form is a column of labelled fields with one submit. Labels stay visible, errors sit under their fields, and nothing is lost on the way out.
API Label FieldDescription FileDrop InfoPopover Input NumberInput PasswordInput Textarea Select Checkbox RadioGroup QueryBox UnsavedChangesDialog
Lay a form out as one column: a visible label above each field, the error or hint under it, and one primary button at the end that names what it saves.
Clear the email and press Save profile: the message lands under the field, focus moves to it, and the rest of the form keeps what was typed. Start typing an address and the message updates with each key, then goes once the address is whole. Discard changes goes back to what was last saved.
Do and don't#
Give every field a Label that stays visible, joined through htmlFor. A placeholder can show
an example value.
Use the placeholder as the label: it disappears with the first keystroke, and with it what the field was for.
Check a field when the form is submitted, and again as it is corrected once it has been marked wrong.
No one here is called ali.
Mark a field wrong while it is still being typed for the first time, or disable the submit button without saying what is missing.
Fields#
| The value | Use |
|---|---|
| One line of text, an email, a URL | Input with the matching type and autoComplete |
| A password | PasswordInput, with autoComplete current-password to sign in or new-password to choose one |
| A number: a count, a port | NumberInput, with min and max where there are limits |
| Several lines of text | Textarea |
| Several lines rendered as Markdown | MarkdownEditor, see Writing Markdown |
| One of a fixed list | Select, or a RadioGroup for two to five, see Choosing a control |
| Yes or no, applied on submit | Checkbox |
| A file: an avatar, an icon, a file to import | FileDrop with a FileDropTrigger |
| Text in a query language, with completion | QueryBox |
Press Create filter with Matching empty: the error lands under the QueryBox as under any
other field. Type from: in it for the completions; Enter on the query as typed submits the form.
Mark the optional fields "(optional)" in the label rather than every required one with an asterisk: most fields in a short form are required, and a reader should not have to decode a symbol.
A file#
Press Upload…, drop an image on the row, or copy one and paste it with the pointer on the row: each puts it in the avatar. A PDF gets the message under the row.
- Always give the field a
FileDropTrigger. A phone has no drag and drop, and mostly cannot paste a file either; the button is the one way in that works everywhere. - Make the zone the whole field, the row with the picture and its buttons, not the picture alone: an avatar is too small a place to aim a dragged file at.
- Name the zone with
aria-labelledby, pointing at itsLabel. That it takes a drop or a paste is said behind anInfoPopoverafter the label, not in a line beside the controls. - Pass
onReject, and say what the field takes in aFieldErrorunder it: "Choose a PNG, JPEG or WebP image." A file the app turns away after reading it, too large or not an image after all, is reported the same way, saying what happened (see Principles). - A viewer or an editor that opens the file dropped on it is a
FileDropwithpurpose="pane", put on the region withasChild; the Binary inspector takes a file dropped anywhere on its window.
Errors#
- The message is a
FieldErrorunder its field, the field getsaria-invalid, andaria-describedbypoints at the message, so a screen reader reads it with the field. A limit the reader needs before anything goes wrong, a format or a length, is aFieldDescription, wired the same way. What the field is for, when its label does not say, goes behind anInfoPopoverafter the label instead; see Writing. - After a failed submit, move focus to the first invalid field.
- Say how to fix it: "Enter an email address, like alice@example.com", not "Invalid input".
- An error from the server about the whole form, a lost connection or a conflict, sits above the buttons, where people look before pressing them again.
Leaving with unsaved changes#
A form that holds typed work guards it. When people navigate away before saving, ask with
UnsavedChangesDialog: Keep editing is its primary action, and Discard and leave is the
alternative. It opens with focus on Keep editing, so a stray Enter keeps the work. The dialog
takes no router; the app's own navigation blocker opens it.
Change the name and press Settings: the dialog asks, and Keep editing returns to the field with the name as typed. Save first, or change nothing, and Settings leaves at once.
Register the work with useDirtySource from @orz/ui/lib/unsaved-guard, and have the blocker ask
hasUnsavedWork(), which reads every source the moment a navigation starts. A form that becomes
clean and navigates in one callback (save and open the new record, delete and go to the list,
discard and go back) calls the markClean that useDirtySource returns before it navigates. The
state that callback sets has not rendered yet, so without it the source still reads dirty, and
people who have just saved are asked whether to leave without saving.
const markClean = useDirtySource(() => draft !== saved);
async function saveAndOpen() {
const { id } = await save(draft);
markClean();
setSaved(draft);
navigate(`/items/${id}`);
}Settings that apply as they change (Switches, see Choosing a control) have nothing unsaved, so a settings page of Switches needs no guard and no Save button.
On each platform#
Desktop & pointer#
- Enter in a single-line field submits the form; Tab moves through the fields in reading order.
- Two short related fields, a first and last name, may share a row on a wide screen. Everything else stays in one column.
Touch#
- The
typeandinputModepick the on-screen keyboard:emailgets an @ key,numerica digit pad.autoCompletelets the phone fill the field in. - Inputs use a 16px font on touch, so the phone does not zoom into the field on focus.
- The on-screen keyboard covers the lower half of the screen. Keep the submit button in the flow under the last field, not pinned to the bottom, where the keyboard hides it.
In the demos
- ReleasesIn Overview, edit the plan and pick another release without saving: Keep editing holds the edits. Save plan stays off until something has changed, and the saved title reaches the list.
- SettingsIn Profile, clear the email and press Save profile: the message lands under the field and focus moves there. Edit a field, then pick another section, and Keep editing guards what you typed.
- Sign inType a password shorter than 8 characters and press Sign in: the message lands under the field and focus moves there. Three in a row, and the whole form's error, Too many attempts, stands above the buttons.
- TasksNew task: submit it with an empty title, and the message lands under the field, which is checked again as you type.
- MailCompose with nobody in To and press Send: the message lands under the field. Close it with words in it, and the message waits in Drafts.
- Pull requestMerge pull request is disabled with the reason beside it, never without one. Comment stays pressable: with the box empty it says what is missing, and ⌘ or Ctrl and Enter posts too.
- Config editorBreak the JSON: the first error is named by its line under the editor, and Save waits until the file parses, while that line says why. Edit a file, then open another, and the unsaved edits are guarded.
- Binary inspectorEdit a byte, then pick another sample: the unsaved edits are guarded by the same dialog a form uses before you leave it.