Touch & mobile
Hit areas and room between targets from the components, content clear of the notch and the on-screen keyboard, and no gesture as the only way.
API Button Checkbox DropdownMenu HighlightedInput Input RadioGroup Select Switch Tabs Textarea Toolbar Toggle SegmentedControl InfoPopover List Table FilterMenu QueryBox Reorder Sheet Toaster useVisualViewportInsets useScrollInsets useAutoHideHeader useMediaQuery MarkdownEditor
On a coarse pointer every part looks as it does under a mouse, and the components give each control a 36px hit area and keep neighbouring targets 36px apart by themselves; the app keeps its own controls to the same rule, its edges clear of the notch and the keyboard, and every gesture backed by something to tap.
On a phone this is already the touch layout; on a desk, press Touch above the example. Nothing
is drawn larger: the checkboxes, the ⋯ buttons and Compose keep their size and take a
36px hit area around it, and the rows of each ⋯ menu are 36px tall. The
conversations' rows are taller than that already, so they do not grow at all. On a phone each ⋯
menu also rises from the bottom edge as a sheet the whole width, titled with what it acts on, in
reach of the thumb and clear of the keyboard; Overlays
has the details. Touch alone keeps the desk's width, and the menu beside its button.
Hit areas#
Apple gives a control on iOS 44 × 44pt by default and 28 × 28pt at the least, and counts the space
between controls as much as their size
(Apple HIG: Accessibility);
WCAG asks for 24px (2.5.8,
AA) and 44px (2.5.5, AAA).
@orz/ui takes 36px on a coarse pointer, through its touch: variant, and spends it on
what keeps a finger on the right target rather than on drawing a dense screen larger:
- Every control keeps the size its style draws it at, and takes a transparent
::after36px square, centred on it, as its hit area. - Neighbouring targets stand 36px apart, centre to centre, so no two hit areas overlap.
- Only what has no other way to make that room changes size: a row a finger taps, a strip of small controls along the edge of a box that clips, and a field, whose text grows to 16px.
| Control | Pointer | Touch, drawn | Touch, hit area |
|---|---|---|---|
Button, every size | 22–32 | unchanged | 36 |
Switch (36 × 20), Checkbox, RadioGroupItem (16) | — | unchanged | 36 |
Tabs trigger | 28 | unchanged | 36 |
| Part | Pointer | Touch |
|---|---|---|
Input, HighlightedInput (so QueryBox's field) | 28 | 32, at 16px text |
The search field of FilterMenu and of a filtering DropdownMenu submenu | 32 | unchanged, at 16px text |
Select trigger and SelectButton, default / sm | 28 / 24 | 32 / 24 |
DropdownMenu items, Select items, FilterMenu rows | 28 | 36 |
QueryBox suggestions | 32 | 36 |
RadioGroup rows | 12 apart | 20 apart, centres 36 apart |
Sizes are in CSS pixels. "Pointer" is what a mouse gets, and what the Density
page's sizes are. A Select trigger is as tall as a field, so the two line up in a form, and its
text grows with a field's, to 16px; a SelectButton, which a
FilterMenu that picks one value in a form opens from, does the same.
Press Touch and Pointer in turn. The field and the select change, to make room for the field's larger text, and so does the tabs' bar, a strip along the edge of a frame that clips, which grows to 36px. Save profile, the checkbox, the radio buttons and the switch keep their size and take hit areas, and under Notifications the radio rows move apart so their hit areas do not overlap. Open the time zone select to see its items made 36px rows.
touch: follows the primary pointer, so a touchscreen laptop driven by its trackpad keeps the
pointer sizes. data-pointer="coarse" or "fine" on an element forces either set on everything
inside it, which is how the Touch button above previews a phone on a desk. Where they nest, the
nearest one decides: a Pointer example on a page set to touch sizes is drawn for a pointer.
Your own controls#
A control you build yourself takes the same room the same way:
- A row that is tapped is at least 36px tall:
touch:min-h-touch. - A small control in a dense row keeps its size and adds the hit area:
relative touch:hit-area. The::afterbelongs to the positioned element, sorelative(or any positioning) is required. On touch the control has no baseline and lines up with text by its middle, so a flex or grid row that lines it up withitems-baselineaddstouch:items-center. It also keeps the room its neighbours need, as the next section says. - A link or a text button on a line of text, such as a name in a header or a command at the end of
a sentence, takes
relative touch:hit-area-inlineinstead. Its hit area is the same, and it keeps its baseline, so its words stay on the line. Unlikehit-area, it does not keep the hit area out of a scroller's overflow, so leave the rest of the hit area above and below it, as a strip 36px tall or a padding does. Text cut short (truncate) would cut the hit area off at its own height, so on touch it clips only along the line:touch:overflow-x-clip touch:overflow-y-visible. A clip lets a flex or grid item shrink no narrower than its words, so it is never cut and runs past its cell; such an item addstouch:min-w-0, the room to shrink thattruncategives it elsewhere. - A field takes
touch:h-field-touch, the height its 16px text needs, as Text fields says. - Anything else that changes for a finger, such as a keyboard shortcut hint, uses
touch:too, not Tailwind'spointer-coarse:, which nodata-pointerreaches. - Tools that hover reveals hide only under
can-hover:, which holds where the primary pointer can hover, so they stay shown on a phone and under a fine pointer that cannot hover. Adata-pointerancestor sets it too.
Spacing between targets#
On touch no two neighbouring targets stand closer than 36px, centre to centre. Two that stand side by side, each narrower than that, or one above the other, each shorter, are at least that far apart, so their hit areas do not overlap: a tap on one never lands on the other, and which comes first in the DOM decides nothing.
@orz/ui's parts keep that room themselves, so a row or a toolbar of them writes nothing for touch:
- An icon
Button, and so aToolbarButtonand every⋯, reserves the rest of 36px beside it as margin. A margin you give it inclassName(ml-autoto push it to a bar's end) replaces that room on its side. - A text
Buttonis at least 36px wide: it is as wide as its words, which CSS cannot read to leave the rest beside it, so a short label is drawn that wide. A segment of aSegmentedControland a tab are at least that wide too, since their faces join. - A
Togglekeeps the room above and below it, so the lines of a set of chips that wraps stand apart, and is at least 36px wide. - An
InfoPopover's (i) keeps the room above and below it, so the line of the label it stands after is 36px tall. - A
ListItemthat holds a control or is one, aTablerow that holds one (a box, a select, a button, a field, aTableRowLink), a menu's item and a tree's row are at least 36px tall, so their targets stand that far from the next row's; aRadioGroupspaces its rows the same. - A strip of small controls along the edge of a box that clips, such as a
Cardheader that holds an action or a link, a block's header and its Show all bar, aPanetoolbar or title bar, a framedTabsbar or the attachment viewer's title bar, is at least 36px tall, its controls centred in it. MarkdownEditor's completion rows are 36px tall, and the toolbars it puts up after a paste, the paste's options and the offer to compare two images, are a strip that tall, each button keeping its size and taking a 36px hit area, as a textButtondoes.
With that room, fewer commands fit on a Toolbar at the same width. It hides no words on touch,
since no tooltip would say them under a finger: a command that does not fit moves into its ⋯
whole, words and all (Toolbars).
A small control you draw yourself reserves the room the same way: touch:mx-hit-xs beside it for
an xs one, hit-sm for an sm one, hit for a default one, hit-lg for a large one. A stack of
them outside a ListItem gives each row touch:min-h-touch, and a row of buttons that wraps keeps
its lines apart by twice the token, touch:gap-y-[calc(2*var(--spacing-hit))] for default ones.
The tokens follow the style's control sizes, so the centres stand 36px apart in every
style.
Four cases may stand closer:
- A control and its own label. A
Checkbox,RadioGroupItemorSwitchand the text that toggles it do the same thing, so these controls reserve no room beside them. A link in that text is another target: keep it away from the control, or make the whole row the label. - A small control beside a target at least 36px across in the direction they neighbour: its width for two side by side, its height for one above the other, such as a row's link, a tree row or a field. The small one's hit area may take a strip of the large one, which keeps at least 36px of its own.
- Marks placed by data on a track, such as a seek bar's chapter marks under its thumb. Where they stand is the data's, not the layout's.
- Links in running text. A link inside a sentence stands where the words put it, and the line
height of the text decides how far it is from a link on the next line, as WCAG's inline exception
allows (2.5.8). A list
of links on their own lines is not running text: its rows take
touch:min-h-touch.
Inside a box that clips or scrolls (overflow-hidden, overflow-auto), a hit area that crosses the
box's edge is cut off there; it never makes the box scroll. An icon button's own room already keeps
its hit area inside the box at the side. Above and below a control, and beside one that keeps no
room of its own, leave the rest of the hit area between the control and the edge: give the strip
that holds them touch:min-h-touch and centre them in it, or pad by the token of the control's
size (touch:pr-hit-xs beside an xs one). @orz/ui's own frames, bars and table rows already leave
it for their controls.
Keep the default sizes and let a coarse pointer add the hit areas and the room between them. One layout serves the mouse and the thumb.
Draw controls larger for touch, or branch on screen width to do it: the room between targets is what keeps a thumb on the right one, a tablet is wide and touched, and a narrow desktop window is still clicked.
Text fields#
- Fields are 16px text on touch at every width:
Input,Textarea,HighlightedInput, the search field of aFilterMenuor of a filteringDropdownMenusubmenu, a log's search, andCodeEditorandMarkdownEditor, read-only ones too, as they still take the focus, all are. Safari zooms the page into a field whose text is smaller (how Safari decides), and leaves it zoomed after, on a tablet as on a phone. A field you build takestext-base md:text-ui touch:text-baseon its own element, since one that inherits its size takes the small text of the bar or the row it stands in, andh-control touch:h-field-touchfor the height that text needs, so it lines up with ours. - Give a field the keyboard it needs:
type="email",inputMode="numeric",autoComplete, andenterKeyHint("search","send","next") for the label on the return key. - Do not focus a field on arrival on a phone; that raises the keyboard over a page the person came
to read. Ask
useMediaQuery(POINTER_FINE)first, as Desktop & pointer shows.FilterMenuasks for itself: without a fine pointer it opens with the focus on its list, so the keyboard stays down over the rows until the search field is tapped. A filteringDropdownMenusubmenu does the same: without a fine pointer its filter box waits for a tap. - Never turn off pinch zoom (
maximum-scale=1,user-scalable=no): people who need larger text zoom (WCAG 1.4.4).
Safe areas#
A phone's screen has a notch or an island at the top, rounded corners, and a home indicator at the
bottom; in landscape the notch is on a side. With viewport-fit=cover in the viewport meta tag the
page draws edge to edge, under all of them, and each edge that holds content pads itself by the
matching inset:
<header className="sticky top-0 pt-[env(safe-area-inset-top)] pl-[max(1rem,env(safe-area-inset-left))] pr-[max(1rem,env(safe-area-inset-right))]">
<footer className="fixed inset-x-0 bottom-0 pb-[max(0.5rem,env(safe-area-inset-bottom))]">max() keeps the usual padding where the inset is zero, which is everywhere but a phone. A
background reaches the edge; only the content moves in.
Sheet and Toaster pad their own edges already. A className padding on SheetContent adds to
the inset rather than replacing it; a Toaster given its own offset or mobileOffset replaces
the inset with it, so add the inset there yourself. What is left to the app is its own fixed and
sticky bars and its page gutters; anything that sticks under a header padded this way is offset by
the same env(safe-area-inset-top).
The on-screen keyboard#
A keyboard opening shrinks only the visual viewport: the page keeps its height, and a sticky
bottom: 0 lands behind the keyboard. Two hooks keep the page in the strip that is left:
useVisualViewportInsets(), mounted once at the app's root, publishes--vv-topand--vv-bottomon<html>. A sticky edge rides the keyboard withbottom: var(--vv-bottom, 0px), so a composer stays right above it.useScrollInsets({ top, bottom })declares the page's sticky header and composer, so everyscrollIntoView, and the anchor and step landings ofrevealBlock, stop clear of them and of the keyboard.
import { useScrollInsets } from "@orz/ui/hooks/scroll-insets";
import { useVisualViewportInsets } from "@orz/ui/hooks/viewport-chrome";
function Thread() {
const header = useRef<HTMLElement>(null);
const composer = useRef<HTMLFormElement>(null);
useVisualViewportInsets();
useScrollInsets({ top: [header], bottom: [composer] });
return (
<>
<header ref={header} className="sticky top-0">…</header>
<main>…</main>
<form
ref={composer}
className="sticky"
style={{ bottom: "var(--vv-bottom, 0px)" }}
>
…
</form>
</>
);
}A Dialog sits mid-screen with the keyboard under it; keep one that takes input to a field or two,
so its buttons stay above the keyboard. Anything longer is a page or a Sheet.
Auto-hiding header#
On a phone a sticky header is a large share of a small screen. On a long page that is read by
scrolling, a thread or a log, useAutoHideHeader(header, enabled) slides it away as the reader
scrolls down and back as they scroll up, the way Safari's toolbar does.
import { useElementHeight } from "@orz/ui/hooks/use-element-height";
import { SM_UP, useMediaQuery } from "@orz/ui/hooks/use-media-query";
import { belowHeader, useAutoHideHeader } from "@orz/ui/hooks/viewport-chrome";
const header = useRef<HTMLElement>(null);
useAutoHideHeader(header, !useMediaQuery(SM_UP), pathname);
const height = useElementHeight(() => header.current, 56);
<header
ref={header}
data-header-follow=""
className="sticky"
style={{ top: belowHeader(0) }}
>
…
<h2 data-header-follow="" className="sticky" style={{ top: belowHeader(height - 1) }}>- Enable it on a phone only; on a wider screen the header costs little and moving it distracts.
- Mount it once, in the app's shell, so whether the header moves never depends on which page the reader is on. Pass the route's path as its third argument: each new page then arrives with the header in view, as a full load would.
- A layer pinned under the header (a list's group heading, a toolbar) takes
top: belowHeader(offset)anddata-header-followtoo, or a strip of page opens between the two while the header moves. Pin it one pixel under the header's height, so it tucks under the header's border: at some zoom levels Firefox snaps the two to different device-pixel rows and leaves a hairline of page between them. - Only the reader's own scrolling moves it. A jump to an anchor keeps it where it is, and it settles fully in or out when scrolling stops.
- Do not use it on a short page, a form, or a page whose header holds the action people came for: that action then disappears under their thumb.
Gestures#
| Gesture | Who owns it | What the app does |
|---|---|---|
| Swipe from the left or right edge | The system: back and forward | Nothing within 20px or so of the edge, so the back swipe always works |
| Pull down at the top | The browser: refresh | Leaves the page's overscroll alone; overscroll-contain on a panel that scrolls inside the page |
| Pinch | The browser: zoom | Never disabled; a canvas that zooms its own content (an image viewer) takes it only inside itself |
| Long press | The system: select text, preview a link | Nothing a control does not also do: a long press that selects a row repeats the row's checkbox, and a SelectionBar leads back out |
| Swipe on a toast, a row | The app | Only as a shortcut: the toast also leaves on its own, the row's action is also in its menu, a delete still offers Undo |
| Drag a row by its handle | The app | Only from the handle: anywhere else on the row the page scrolls, and the row's pair of move buttons does the same with taps (Reordering) |
Every gesture an app adds has a control that does the same thing with one tap, and anything that follows a path or needs two fingers works with a single pointer too (WCAG 2.5.1). A tap acts when the finger lifts, not when it lands, so a finger that lands on the wrong control can slide off; @orz/ui's buttons already do.
In the example at the top, archive a conversation from its ⋯ menu: the toast that says so can be
swiped away, leaves on its own, and holds the Undo.
A selectable tree starts selecting from the row's ⋯ menu, its Select, since a link's long
press is the system's; after the first row a tap toggles a row instead of going, and the menu's
Clear selection or the SelectionBar's brings the taps back to going.
Dialog and Sheet lock the page behind them while they are open, and let a scroller inside them,
a code diff included, scroll on its own.
On each platform#
- Pointer. The compact sizes from Density, hover, shortcuts: Desktop & pointer.
- Touch. Everything on this page. Preview it on a desk with the Touch button above any example, or Storybook's Pointer toolbar; check it on a real phone before shipping, because neither emulates the keyboard, the notch or the system's gestures.
In the demos
- SettingsOn a phone the sections are a list that pushes each one, with labels on the leading edge and controls on the trailing edge, as the phone's own settings are.
- Sign inOn a phone the module is the whole screen. Forgot password? leads to a code field that opens the number pad and offers the code from the mail, and the email fields bring the email keyboard.
- AdminOn a phone the workspace menu keeps only its mark, and the sections that do not fit wait in the ⋯ at the end of the bar.
- TasksOn a phone the filters that do not fit wait in the toolbar's ⋯, the board shows a column at a time with the next at the edge and lifts a card held still under a finger, and a task opens as a Sheet that fills the screen.
- MailOn a phone a long press selects a conversation, Compose sits under the thumb, and the reply box rides above the on-screen keyboard.
- NotesOn a phone a note opens to be read, and the on-screen keyboard comes up only when you press Edit.
- Pull requestPress Touch in the bar above: the jobs' rows make room for a thumb, Details and Re-run keep their size and take hit areas, and a log or a diff scrolls inside its own box.
- RepositoryOn a phone the tree fills the screen; a file or folder replaces it, with a link back to the files at the top.
- Config editorOn a phone the file tree folds into a Select in the file's heading line, its rows at a thumb's size, and the error line under the editor grows to one too.
- Binary inspectorOn a phone the structure is a Select above the bytes and the inspector a Sheet, while Revert… keeps its word because nothing undoes it.