Build UI with the built-in controllers
This guide shows you how to use the Stimulus controllers that Phem ships for common UI patterns: a section that opens, an accordion, a section that shows from the value of a field, a popover, a tooltip, a menu, a menu bar, a navigation menu, a context menu, a combobox, a select, a sortable list, a modal dialog, a drawer, tabs, a toggle button, a switch or a checkbox, a toggle group, a toolbar, a radio group, a button that is not a <button>, an avatar, a field with validation, a number field, a one-time code field, a slider, a scroll area, a table of contents that marks the section in view, a theme switch, toasts, a copy button, a count of characters, an image input with a preview, a password that shows, a "select all" box, a local time, the focus after an event and a confirmation. Their state belongs to the client, so it stays after the morph of an event.
The controllers follow the design of Base UI: they ship no CSS, and your markup gives all the elements. A UI library can put its own styled markup on them.
Enable the controllers#
The controllers are off by default. Enable each one that you use in assets/controllers.json:
"@phem/phem": {
"optimistic": { "enabled": true, "fetch": "lazy" },
"collapsible": { "enabled": true, "fetch": "lazy" },
"accordion": { "enabled": true, "fetch": "lazy" },
"depends": { "enabled": true, "fetch": "lazy" },
"dialog": { "enabled": true, "fetch": "lazy" },
"popover": { "enabled": true, "fetch": "lazy" },
"tooltip": { "enabled": true, "fetch": "lazy" },
"menu": { "enabled": true, "fetch": "lazy" },
"context-menu": { "enabled": true, "fetch": "lazy" },
"combobox": { "enabled": true, "fetch": "lazy" },
"tabs": { "enabled": true, "fetch": "lazy" },
"toast": { "enabled": true, "fetch": "lazy" },
"clipboard": { "enabled": true, "fetch": "lazy" },
"autofocus": { "enabled": true, "fetch": "lazy" },
"confirm": { "enabled": true, "fetch": "lazy" },
"sortable": { "enabled": true, "fetch": "lazy" },
"count": { "enabled": true, "fetch": "lazy" },
"reveal": { "enabled": true, "fetch": "lazy" },
"checkbox-group": { "enabled": true, "fetch": "lazy" },
"time": { "enabled": true, "fetch": "lazy" },
"scrollspy": { "enabled": true, "fetch": "lazy" },
"image-input": { "enabled": true, "fetch": "lazy" },
"toggle": { "enabled": true, "fetch": "lazy" },
"toggle-group": { "enabled": true, "fetch": "lazy" },
"toolbar": { "enabled": true, "fetch": "lazy" },
"radio-group": { "enabled": true, "fetch": "lazy" },
"avatar": { "enabled": true, "fetch": "lazy" },
"button": { "enabled": true, "fetch": "lazy" },
"field": { "enabled": true, "fetch": "lazy" },
"form": { "enabled": true, "fetch": "lazy" },
"number-field": { "enabled": true, "fetch": "lazy" },
"otp-field": { "enabled": true, "fetch": "lazy" },
"slider": { "enabled": true, "fetch": "lazy" },
"scroll-area": { "enabled": true, "fetch": "lazy" },
"select": { "enabled": true, "fetch": "lazy" },
"menubar": { "enabled": true, "fetch": "lazy" },
"navigation-menu": { "enabled": true, "fetch": "lazy" },
"drawer-provider": { "enabled": true, "fetch": "lazy" }
}For phem-optimistic, see Update the page before the server answers.
Learn the conventions#
All the controllers use the same rules. When you know them, you can style each controller.
Parts#
Each element of a pattern is a part: a Stimulus target with a name, such as trigger, popup, arrow, close, item or panel. Mark each part with data-<controller>-target:
<div data-controller="phem-popover">
<button data-phem-popover-target="trigger">Share</button>
<div data-phem-popover-target="popup" hidden>...</div>
</div>The editors complete the controller names in data-controller, the parts and the -value attributes, and show their docs on hover.
The controller adds the roles and the ARIA attributes of the pattern, such as role="dialog", aria-expanded and aria-controls. An attribute that your markup gives stays. A controller gives no id to a part. The morph of an event matches the elements by their id, and removes an id that the server render does not have. When an ARIA attribute must point to a part with no id, the controller uses an element reference of ARIA, such as ariaControlsElements. The browser then shows the attribute with an empty value, for example aria-controls="". When the part has an id, the attribute gets the id.
State in data attributes#
The controller shows its state in data attributes. Use them in your CSS, or as Tailwind variants (data-open:opacity-100):
| Attribute | Part | When |
|---|---|---|
data-open, data-closed |
The root, the popup, the panel | The part is open, or closed. The nav, the lists and the arrow of a navigation menu get data-open while an item is open |
data-popup-open |
A trigger | Its popup is open |
data-panel-open |
A trigger of phem-collapsible |
Its panel is open |
data-side, data-align |
A positioned popup, its arrow, the popup of a navigation menu | The side and the alignment after a flip: top, bottom, left, right, inline-start, inline-end, and start, center, end |
data-highlighted |
An item of a menu or of a combobox | The item has the focus, or the highlight |
data-selected |
An item of a combobox with multiple |
The item is chosen (aria-selected="true") |
data-expanded |
The phem-toast element, a toast |
The pointer or the focus is on the stack |
data-swiping, data-swipe-direction |
A toast, a drawer | The user swipes the part, and the direction of the swipe (a toast gets the direction at the first move of the swipe) |
data-pressed |
A toggle button, an item of a toggle group, the trigger of a menu, a context menu, a popover or a select | The button is pressed, or its popup is open |
data-checked, data-unchecked, data-indeterminate |
A switch, a checkbox, a radio, their indicator | The control is checked, not checked, or mixed |
data-valid, data-invalid, data-dirty, data-touched, data-filled, data-focused |
The parts of a field | The state of the validation and of the input of the user |
data-dragging, data-scrolling |
A slider, a scroll area | The user drags a thumb, or scrolls |
data-disabled |
An item, a tab | The element is disabled |
data-readonly, data-required |
A form control | The control is read-only, or required |
data-orientation |
The parts of phem-tabs |
horizontal or vertical |
data-active |
A tab, a link of phem-scrollspy |
The tab is selected, or the section of the link is in view |
data-starting-style, data-ending-style |
A part that opens or closes | The first frame of the open, and the time of the close |
data-instant |
A popup, the positioner of a navigation menu | The cause of a change that Base UI makes with no animation (click, dismiss, focus, delay, group), or the first open of a navigation menu. The CSS stops the animation with [data-instant] { transition: none } |
Animations#
A part that opens gets data-starting-style for its first frame. A part that closes gets data-ending-style, and gets hidden only when its animations end. An animation that runs forever (a spinner in the part) does not delay the close. A popup in the top layer (a popover popup, a <dialog>) gets data-starting-style before it shows, so its open animation and the open animation of its ::backdrop run. Use a CSS transition between the two states:
.popup {
transition: opacity 150ms, transform 150ms;
transform-origin: var(--transform-origin);
}
.popup[data-starting-style],
.popup[data-ending-style] {
opacity: 0;
transform: scale(0.95);
}
.popup[data-instant] {
transition: none;
}A popup gets data-instant with the cause of a change that Base UI makes with no animation: click (Enter or Space), dismiss (Escape), focus (the keyboard focus), delay (a tooltip of a group) and group (a move from one menu of a menubar to another). The attribute does not stop the animation. Add [data-instant] { transition: none } (Tailwind data-instant:transition-none) to make this change at once, as in Base UI. With no such rule, the change animates. A change with the pointer removes the attribute.
A transition can go back in the middle: a popup that opens again before the end of its close animation does not jump. A keyframe animation on [data-closed] also works. A part with no animation closes at once.
Press outside#
A popup, a dialog that is not a modal and a navigation menu close at a press outside them. A press that starts in the popup and ends outside it (for example a text selection with the mouse) does not close it, as in Base UI. A press in a popup closes only the popups that opened after it. A menu and a select close at the press of the mouse (the pointerdown). For a touch, they close at the end of the touch (in 1 s), or when the touch moves more than 10 px (a scroll), as in Base UI. A long touch does not close them.
Position#
phem-popover, phem-tooltip, phem-menu, phem-context-menu, phem-combobox, phem-select and phem-navigation-menu place their popup next to their anchor (the trigger, or the input). The popup gets its top and left, so a transform of the popup stays free for an animation. A popup in the top layer (an open popover) gets position: absolute and a place in the page, as in Base UI: a scroll of the page moves it with its anchor in the same frame. Other popups, and a popup with an anchor in a fixed or sticky box or at the point of the pointer (phem-context-menu), get position: fixed and a place in the viewport. These values set the position:
| Value | Default | Effect |
|---|---|---|
side |
bottom (top for a tooltip) |
The side of the anchor: top, bottom, left, right, inline-start or inline-end |
align |
center (start for a menu and a combobox) |
The alignment on the other axis: start, center or end |
side-offset |
0 |
The distance (px) from the anchor |
align-offset |
0 |
The shift (px) on the other axis |
collision-padding |
5 |
The space (px) to keep from the edges of the viewport |
collision-padding-inline |
the same as collision-padding |
The space (px) to keep from the left and the right edges only |
flip |
true |
false keeps the popup on its side when it does not fit |
arrow-padding |
5 |
The space (px) between the arrow and the corners of the popup |
When the popup does not fit on its side, it goes to the other side (a flip). On the other axis, it moves to stay in the viewport (a shift). It follows its anchor on a scroll and on a resize. It goes to its place at once: a CSS transition of top and left does not animate the first place, or a move after a scroll or a resize, as in Base UI. The transition animates only the move of an open popup to another trigger (see Open one tooltip from several triggers). The popup gets these CSS variables:
--anchor-widthand--anchor-height: the size of the anchor, for example for a list as wide as its input--available-widthand--available-height: the space from the anchor to the edge of the viewport, for amax-height--transform-origin: the point of the anchor, for a scale animation. A popup withalignstartorendand no arrow grows from its aligned edge (0%or100%on the other axis, as in Base UI). On a top or a bottom side in a right-to-left page,startis the right edge (100%). When the popup shifts more than 1 px to stay in the viewport, the origin goes back to the point of the anchor
The arrow part gets its left (on a top or bottom side) or its top (on a left or right side), so it points at the center of the anchor. The value starts at the padding box of the popup, so give the arrow position: absolute. Put it on the edge of the popup with [data-side]:
.arrow[data-side="bottom"] { top: -8px; }
.arrow[data-side="top"] { bottom: -8px; rotate: 180deg; }An element around the popup with overflow: hidden, a transform or a filter can cut or move a position: fixed popup. Add the popover="manual" attribute to the popup: the controller shows it in the top layer of the page, above all the other elements.
The morph#
The state of a controller belongs to the client. The morph after an event does not change the state attributes and the position. The open value (or the active value of phem-tabs) is the state: the server render gives its first value, and a server render with a new value sets the state. For example, a handler opens a dialog with data-phem-dialog-open-value={{ editing }}. A state that the user changed after the request of the event left keeps the value of the user, as a bound field does: the server render did not know it. For example, the user presses an arrow key of a radio group with phem:live two times, and the answer of the first key does not check the first radio again.
Show and hide a section#
phem-collapsible shows and hides its panels in place, for a "more" text, a section of a form or an item of an accordion:
<div data-controller="phem-collapsible">
<button data-phem-collapsible-target="trigger">Details</button>
<div data-phem-collapsible-target="panel" hidden>
<button onClick={{ star }}>Star</button>
</div>
</div>A click on a trigger opens and closes the panel. The toggle, open and close actions do the same from another element. After a click, at the end of the open animation, the browser shows:
<div data-controller="phem-collapsible" data-phem-collapsible-open-value="true" data-open>
<button data-phem-collapsible-target="trigger" aria-controls="" aria-expanded="true" data-panel-open>Details</button>
<div data-phem-collapsible-target="panel" data-open
style="--collapsible-panel-height: auto; --collapsible-panel-width: auto;">...</div>
</div>The panel has no id, so the trigger points to it with an element reference (ariaControlsElements). When each panel has an id, the trigger gets the ids in aria-controls. An aria-controls that your markup gives stays.
The controller element and the panel get data-open or data-closed. During an open and a close, they also get data-starting-style and data-ending-style (see Animations), as the root of a Base UI Collapsible. So a style of the element can change with the panel, for example the icon of the trigger: .section[data-ending-style] .icon { rotate: 0deg; }. The data-ending-style of the element stays until the end of the animations of the panel.
The panel gets the size of its content in --collapsible-panel-height and --collapsible-panel-width, for a transition of its height. During the open, the size is in px, so the transition goes from 0 to the height of the content. At the end of the open animations, the two variables become auto, so content that grows after the open (an image that loads, a morph that adds a line) is not cut. A panel that is open on load gets auto at once. After the morph, an open panel also gets auto, unless its open animation still runs: then it keeps the size in px until the end of the animation. A close measures the panel again in px, so the close starts from its real height. At the end of the close, the two variables become 0px. A panel with hidden="until-found" stays in the layout (content-visibility: hidden, not display: none), so with 0px it does not get its open height again as an empty block. The controller measures the content with no alignment (justify-content, align-items, align-content and justify-items), so a panel that puts its content at its end (justify-content: flex-end) gets the correct size:
.panel { height: var(--collapsible-panel-height); overflow: hidden; transition: height 200ms; }
.panel[data-starting-style], .panel[data-ending-style] { height: 0; }An open panel has auto in the variables, and auto is not a length. So do not use the variables in calc(), in max-height, or in a keyframe of an open panel. Use them only in height (or width), and put the closed size on [data-starting-style] and [data-ending-style], as in the example. For example, change max-height: calc(var(--collapsible-panel-height) + 1rem) to height: var(--collapsible-panel-height) with a padding-block of 0.5rem on the panel.
Set data-phem-collapsible-open-value="true" to open it on load. With data-phem-collapsible-hidden-until-found-value="true", a closed panel has hidden="until-found": the search of the browser (Ctrl+F) finds its text and opens it. This open has no animation, as in Base UI: the browser scrolls to the text, and a height animation would move the text away. The panel gets data-instant and the auto size at once. A panel with data-phem-collapsible-hidden-until-found does the same. A collapsible does not close on Escape or on a click outside. For a dropdown, use phem-popover or phem-menu.
Remember a dismissed banner#
Set data-phem-collapsible-persist-value to a key, and the state goes in localStorage. A banner that the user closed stays closed after a reload:
<div data-controller="phem-collapsible"
data-phem-collapsible-open-value="true"
data-phem-collapsible-persist-value="promo-2026">
<div data-phem-collapsible-target="panel">
New plans for 2026.
<button data-phem-collapsible-target="trigger" aria-label="Dismiss">×</button>
</div>
</div>After a click on the trigger, localStorage has phem-collapsible:promo-2026 with the value false. On the next load, the stored state wins over the open value of the server render, and the panel closes when the controller connects. Use a new key for a new banner. When the key changes, the controller takes the stored state of the new key, or the open value of the server render.
Only a click on a trigger and the toggle, open and close actions store the state. An open by the search of the browser (hiddenUntilFound) or by a new open value of the server render does not. The other collapsibles with the same key get the stored state at once, also in the other tabs. When the browser blocks localStorage (for example in some private windows), the state lives until the reload.
The server renders the banner open, so a dismissed banner shows until the controller connects, and then closes.
Open one item of an accordion at a time#
phem-accordion shows a list of items that open and close. When one item opens, the other items close:
<div data-controller="phem-accordion">
<div data-value="shipping" data-phem-accordion-target="item">
<h3 data-phem-accordion-target="header"><button id="shipping-trigger" data-phem-accordion-target="trigger">Shipping</button></h3>
<div id="shipping-panel" data-phem-accordion-target="panel" hidden>Two days.</div>
</div>
<div data-value="returns" data-phem-accordion-target="item">
<h3 data-phem-accordion-target="header"><button id="returns-trigger" data-phem-accordion-target="trigger">Returns</button></h3>
<div id="returns-panel" data-phem-accordion-target="panel" hidden>Thirty days.</div>
</div>
</div>Each item has one trigger and one panel. A click on a trigger opens and closes its item. Tab moves the focus between the triggers, as the accordion pattern of the ARIA Authoring Practices says now. With data-phem-accordion-roving-focus-value="true", ArrowDown, ArrowUp, Home and End also move the focus between the triggers. Then, with data-phem-accordion-orientation-value="horizontal", ArrowRight and ArrowLeft move it, and from the last trigger ArrowDown goes to the first one, unless data-phem-accordion-loop-focus-value="false". A header part (the heading around a trigger) gets data-open or data-closed, and each part of an item gets data-index (the position of the item, from 0). A disabled trigger (disabled or aria-disabled="true") does not open, and does not get the focus: its item, its header, the trigger and its panel get data-disabled, also when the morph of an event changes the trigger. The controller element and each panel get data-orientation (vertical, or the orientation value). After a click on Returns, at the end of the open animation, the browser shows:
<div data-value="returns" data-phem-accordion-target="item" data-open data-index="1">
<h3 data-phem-accordion-target="header" data-open data-index="1">
<button id="returns-trigger" data-phem-accordion-target="trigger" aria-expanded="true"
aria-controls="returns-panel" data-panel-open data-index="1">Returns</button>
</h3>
<div id="returns-panel" data-phem-accordion-target="panel" role="region"
aria-labelledby="returns-trigger" data-open data-index="1" data-orientation="vertical"
style="--accordion-panel-height: auto; --accordion-panel-width: auto;">Thirty days.</div>
</div>Give each trigger and each panel an id, for example id={{ 'faq-' ~ key ~ '-panel' }} in a loop. The controller links a trigger and its panel with aria-controls and aria-labelledby only when both have an id. It gives them no id itself: the morph of an event does not match an element whose id the server render does not have, and replaces it, with the state of its content.
Set data-phem-accordion-multiple-value="true" to let more than one item open. The open value is the list of the open items. An item is its data-value attribute, its key, or its index. The server opens an item with data-phem-accordion-open-value={{ ['returns'] }}. A panel gets --accordion-panel-height and --accordion-panel-width, for a transition of its height, with the same px, auto and 0px values as a panel of phem-collapsible. With data-phem-accordion-hidden-until-found-value="true", the search of the browser (Ctrl+F) opens the item that has the text, with no animation, as a panel of phem-collapsible. To do this for one panel only, add data-phem-accordion-hidden-until-found to the panel.
Show a section from the value of a field#
phem-depends shows a panel when a field has a value, with no round trip. Use it for an "Other" option that asks for a text, or for a checkbox that shows more fields:
<div data-controller="phem-depends">
<select phem:bind={{ reason }} data-phem-depends-target="field">
<option value="price">Price</option>
<option value="other">Other</option>
</select>
<div data-phem-depends-target="panel" data-phem-depends-match="other" hidden>
<input phem:bind={{ reasonOther }} aria-label="Other reason" />
</div>
</div>A panel shows when the value of the field is one of the words of its data-phem-depends-match attribute, for example data-phem-depends-match="price quality". With data-phem-depends-match and no value, a panel shows when the field has a value. After the user selects Other, the browser shows:
<div data-controller="phem-depends" data-field-value="other">
...
<div data-phem-depends-target="panel" data-phem-depends-match="other" data-open>
<input aria-label="Other reason" ... />
</div>
</div>The field targets are one field:
| Field | Its value |
|---|---|
A <select>, a text field |
The value. An empty text has no value |
| A checkbox | checked or unchecked |
A group of checkboxes: more than one checkbox, a phem:bind list, or a name that ends with [] |
The checked values: a panel shows when one of its words is checked |
| A group of radio buttons | The value of the checked button |
A hidden panel disables its form controls, so a form submit does not send them. A control that the controller disables gets data-phem-depends-disabled, and a control that the server render disables stays disabled. The controller element gets the words of the field in data-field-value. A phem:bind property keeps its value when its panel hides, so the handler must not use the value of a hidden field. The server must also validate the fields: the controller only changes what the user sees. The state comes from the field, so it stays right after the morph of an event.
Show a popover#
phem-popover shows a popup next to its trigger, for a share box, a filter form or a date picker:
<div data-controller="phem-popover" data-phem-popover-align-value="end">
<button data-phem-popover-target="trigger">Share</button>
<div data-phem-popover-target="popup" popover="manual" hidden>
<div data-phem-popover-target="arrow"></div>
<h2 data-phem-popover-target="title" id="share-title">Share</h2>
<p data-phem-popover-target="description">Send the link to a friend.</p>
<button onClick={{ copyLink }}>Copy the link</button>
<button data-phem-popover-target="close">Close</button>
</div>
</div>A click on the trigger opens and closes the popup, and a click on a close part closes it. The popup is open after the morph of the Copy the link event, at its place. After a click, the browser shows:
<button data-phem-popover-target="trigger" aria-haspopup="dialog" aria-expanded="true" data-popup-open data-pressed>Share</button>
<div data-phem-popover-target="popup" popover="manual" role="dialog" aria-labelledby="share-title" aria-describedby="" tabindex="-1"
data-open data-side="bottom" data-align="end"
style="position: fixed; top: 61px; left: 240px; --anchor-width: 52px; --available-height: 654px; ...">The popup gets role="dialog", aria-labelledby (the title part) and aria-describedby (the description part), so a screen reader names the popup. A popup with an aria-label keeps it, and gets no aria-labelledby. A part with an id gets a link with its id, as the title above. A part with no id, as the description above, gets a link with the element references of ARIA (ariaLabelledByElements, ariaDescribedByElements), and the browser shows aria-describedby="". The trigger points to a popup with no id with ariaControlsElements. The controller gives the parts no id, because the morph must match each element to the server render.
Open moves the focus into the popup: to its [autofocus] element, or else to the first element that Tab goes to (a field, a button, a link), or else to the popup itself. After an open with a touch, the focus goes to the popup, so the keyboard of the device does not show. Escape closes the popup. When the focus is in the popup, it goes back to the trigger: Escape on a popup that the pointer opened keeps the focus where it is. A click outside and the focus out of the popover close it too. After a click outside on an element that takes no focus (a text, an image), the focus goes back to the trigger, as in Base UI. After a click outside on an element that takes the focus (a field), the focus stays there. Safari does not give the focus to a button or a link at a click, so there a click on a button outside also gives the focus back to the trigger, as in Base UI. A popup that the hover opened keeps the focus where it is. Each time the focus goes back to the trigger (Escape, a close part, a click outside), it uses preventScroll: the page does not scroll to the trigger. Set data-phem-popover-dismiss-value="false" to keep it open on Escape, on a click outside and on the focus out. Then Escape goes to the popup or the dialog around the popover, which closes.
With data-phem-popover-open-on-hover-value="true", the pointer on the trigger opens the popup after the delay value (300 ms), and the pointer out of the trigger and of the popup closes it after the close-delay value (0 ms: at once). The pointer can go from the trigger to the popup, also on a diagonal path, as the safePolygon of Base UI. While it is in the safe space, the popup stays open. The safe space is the triangle from the point where the pointer left the trigger to the popup, and the gap between the trigger and the popup. For example, with data-phem-popover-side-offset-value="8", the pointer crosses the 8 px below the trigger, and the popup closes only when the pointer goes out of that space. A pointer that leaves the trigger away from the popup closes it at once. A popover with open-on-hover is good for a preview card, because a touch screen and the keyboard can still open it.
Open one popover from several triggers#
One popover can have several triggers, as the tooltips of Open one tooltip from several triggers. Each trigger part is a trigger, and an element anywhere in the page with data-phem-popover-for and the id of the controller element is a detached trigger. A click on a trigger opens the popover next to it. While the popover is open, a click on another trigger moves it there, and a click on the same trigger closes it. With open-on-hover, the pointer on another trigger moves it at once. A click on a detached trigger is not a click outside.
The viewport part shows the content of the trigger: give the trigger a data-phem-popover-payload, and put a <template data-payload> with the same value in the viewport. The viewport shows a copy of the content of the template. A payload with no template shows as text.
<button data-phem-popover-for="panels" data-phem-popover-payload="notifications">Notifications</button>
<button data-phem-popover-for="panels" data-phem-popover-payload="profile">Profile</button>
<div id="panels" data-controller="phem-popover">
<div data-phem-popover-target="popup" popover="manual" hidden>
<div data-phem-popover-target="viewport">
<template data-payload="notifications">
<h2 data-phem-popover-target="title">Notifications</h2>
<p data-phem-popover-target="description">You are all caught up.</p>
</template>
<template data-payload="profile">
<h2 data-phem-popover-target="title">Jason Eventon</h2>
<a href="/settings">Profile settings</a>
</template>
</div>
</div>
</div>After a click on Profile, the browser shows:
<button data-phem-popover-for="panels" data-phem-popover-payload="profile" aria-haspopup="dialog" aria-expanded="true" data-popup-open data-pressed>Profile</button>
<div id="panels" data-controller="phem-popover" data-phem-popover-open-value="true" data-phem-popover-trigger-id-value="">
<div data-phem-popover-target="popup" role="dialog" aria-labelledby="" data-open ...>
<div data-phem-popover-target="viewport">
<template data-payload="notifications">...</template>
<template data-payload="profile">...</template>
<div data-current data-open>
<h2 data-phem-popover-target="title">Jason Eventon</h2>
<a href="/settings">Profile settings</a>
</div>
</div>
</div>
</div>The popup gets the name of the title part of the content. The other triggers get aria-expanded="false". The animations of the move, of the size and of the content, data-activation-direction, and the trigger-id value are the same as for a tooltip. The open action takes a trigger-id param: <button data-action="phem-popover#open" data-phem-popover-trigger-id-param="profile">.
Show a preview card of a link#
A popover with open-on-hover and a link trigger (<a href>) is a preview card, as the PreviewCard of Base UI. Base UI waits 600 ms to open it and 300 ms to close it:
<div data-controller="phem-popover" data-phem-popover-open-on-hover-value="true"
data-phem-popover-delay-value="600" data-phem-popover-close-delay-value="300">
<a href="/users/ada" data-phem-popover-target="trigger">Ada</a>
<div data-phem-popover-target="popup" hidden>
<img src="/ada.png" alt="" /> <a href="/users/ada/posts">Posts</a>
</div>
</div>A click on the link opens the link, not the popup. The keyboard focus on the link opens the popup after the delay, and the focus out closes it at once: the close delay is for the pointer. A popup that the focus opened gets data-instant="focus", also when the focus out closes it, so a [data-instant] rule can stop its animation, as in Base UI. Escape closes the card with data-instant="dismiss", and the pointer removes the attribute. The focus stays on the link. A click outside closes the card, and the focus does not move. The card only adds to the link, for a mouse and for the eyes: the link stays the only part that Tab goes to. So the link gets no aria-haspopup, aria-expanded or aria-controls, and the popup gets no role. The popup stays in the accessibility tree, as in Base UI: a screen reader can read it in the reading order of the page. Its links and fields get tabindex="-1": Tab goes from the link to the next element of the page. Put the information of the card on the page of the link too. When the pointer is on Ada, the browser shows:
<a href="/users/ada" data-phem-popover-target="trigger" data-popup-open>Ada</a>
<div data-phem-popover-target="popup" data-open data-side="bottom" data-align="center" style="...">
<img src="/ada.png" alt=""> <a href="/users/ada/posts" tabindex="-1">Posts</a>
</div>A preview card can have several links, as a popover with several triggers: give each link data-phem-popover-for and a data-phem-popover-payload, and put the cards in <template data-payload> parts of the viewport. The pointer on another link of an open card moves the card at once. The keyboard focus on another link moves it too, and the focus out to that link does not close it. The open action does not move the focus into a preview card: the focus stays on the element that called it.
<p>
Discover <a href="/wiki/Typography" data-phem-popover-for="cards" data-phem-popover-payload="typography">typography</a>
or <a href="/wiki/Art" data-phem-popover-for="cards" data-phem-popover-payload="art">art</a>.
</p>
<div id="cards" data-controller="phem-popover" data-phem-popover-open-on-hover-value="true"
data-phem-popover-delay-value="600" data-phem-popover-close-delay-value="300">
<div data-phem-popover-target="popup" hidden>
<div data-phem-popover-target="viewport">
<template data-payload="typography">…</template>
<template data-payload="art">…</template>
</div>
</div>
</div>Show a tooltip#
phem-tooltip shows a short text next to an element, on hover and on the focus of the keyboard:
<button onClick={{ archive }} data-controller="phem-tooltip" aria-label="Archive">
<svg aria-hidden="true">...</svg>
<span data-phem-tooltip-target="popup" hidden>Archive</span>
</button>The trigger is the controller element, or a trigger part in it. The pointer on the trigger opens the tooltip after the delay value (600 ms), and the pointer out closes it after the close-delay value (0 ms). The pointer can go on the tooltip, unless data-phem-tooltip-hoverable-value="false": it can cross the gap between the trigger and the tooltip (the side-offset value), as for a popover with open-on-hover. A pointer that goes away from the tooltip closes it. The focus of the keyboard (:focus-visible) opens it at once. Escape, the blur and a press on the trigger close it: the tooltip closes at the press of the pointer (pointerdown), before the click, and at the click of Enter or Space, as in Base UI. Set data-phem-tooltip-close-on-click-value="false" to keep it open on a press.
A tooltip that opens less than 400 ms after another tooltip closed opens with no delay, and gets data-instant="delay", so a [data-instant] rule can stop its animation, as in a toolbar. The tooltip that closed also gets data-instant="delay" while its close animation runs, so with the [data-instant] rule the page shows one tooltip at a time. A focus that moves from one trigger to the next with Tab also ends the close of the other tooltip at once:
[data-phem-tooltip-target="popup"][data-instant] { transition: none }While the tooltip is open, the browser shows:
<button data-controller="phem-tooltip" aria-label="Archive" data-popup-open>
<span data-phem-tooltip-target="popup" data-open data-side="top" data-align="center" style="...">Archive</span>
</button>When the text of the tooltip is the accessible name of the trigger, as above, the tooltip gets no ARIA: a screen reader reads the name one time. The name is the text of the aria-labelledby elements, else aria-label, else the text of the trigger (with the alt of an image), else title. A tooltip with another text describes the trigger: the popup gets role="tooltip", and the trigger gets aria-describedby. The link stays while the tooltip is closed, so a screen reader reads the description before the user hovers or focuses the trigger:
<button data-controller="phem-tooltip" aria-label="Archive">
<span id="archive-tip" data-phem-tooltip-target="popup" hidden>Moves the message out of the inbox</span>
</button>The browser shows:
<button data-controller="phem-tooltip" aria-label="Archive" aria-describedby="archive-tip">
<span id="archive-tip" data-phem-tooltip-target="popup" hidden role="tooltip">Moves the message out of the inbox</span>
</button>A popup with no id gets the link with an element reference (ariaDescribedByElements). A role on the popup and an aria-describedby on the trigger that your markup gives stay. With a viewport part, the text changes with the trigger, so only the trigger of the open tooltip gets aria-describedby.
A tooltip helps only the users of a pointer and of a keyboard. A touch shows no tooltip. Give the trigger its own label (here aria-label) for a touch screen and for a screen reader. Do not put important information only in a tooltip: use a popover with open-on-hover.
Open one tooltip from several triggers#
One tooltip can have several triggers. Each trigger part in the controller element is a trigger. A trigger can also be anywhere in the page: give it data-phem-tooltip-for with the id of the controller element, as the detached triggers of Base UI. The tooltip goes next to the trigger that opened it, and shows the data-phem-tooltip-payload text of this trigger in its viewport part:
<div class="toolbar">
<button data-phem-tooltip-for="tools" data-phem-tooltip-payload="Listen to audio preview" aria-label="Listen to audio preview">…</button>
<button data-phem-tooltip-for="tools" data-phem-tooltip-payload="Set a timer" aria-label="Set a timer">…</button>
</div>
<span id="tools" data-controller="phem-tooltip">
<span data-phem-tooltip-target="popup" popover="manual" hidden>
<span data-phem-tooltip-target="viewport"></span>
</span>
</span>While the tooltip is open, the pointer or the keyboard focus on another trigger moves it at once, with no delay. The trigger of the tooltip gets data-popup-open. A trigger with no payload shows the content of the server render of the viewport. A payload can also name a <template data-payload> in the viewport, for a content with markup (see Open one popover from several triggers). When the pointer goes from Listen to audio preview to Set a timer, the browser shows:
<span data-phem-tooltip-target="viewport" data-activation-direction="right">
<span data-previous data-closed data-ending-style style="position: absolute; top: 4px; left: 8px; width: 149px;">Listen to audio preview</span>
<span data-current data-open data-starting-style>Set a timer</span>
</span>The content is in a <span> when the viewport is a <span>, and in a <div> for another viewport. A <div> in a <span> is not valid HTML, and its text goes on a new line.
The new content gets data-starting-style for its first frame. The old content keeps its place with position: absolute, gets data-ending-style, and goes when its animations end. data-activation-direction is the direction of the new trigger from the old one: up to two words, left or right, and up or down. Match one word with ~=, as [data-activation-direction~="right"]. The popup gets the size of its content in --popup-width and --popup-height: they are auto at rest, and px values during a change (see Build a navigation menu). Animate the move, the size and the content with CSS:
[data-phem-tooltip-target="popup"] {
width: var(--popup-width, auto);
height: var(--popup-height, auto);
transition: top .35s, left .35s, width .35s, height .35s;
}
[data-current], [data-previous] { transition: translate .35s, opacity .175s }
[data-activation-direction~="right"] [data-current][data-starting-style] { translate: 50%; opacity: 0 }
[data-activation-direction~="right"] [data-previous][data-ending-style] { translate: -50%; opacity: 0 }The open value is the state, and the trigger-id value is the id of the trigger of the open tooltip. The server opens the tooltip next to a trigger with data-phem-tooltip-open-value="true" and data-phem-tooltip-trigger-id-value="timer", and a new render moves it or closes it. An element in the controller element opens the tooltip with the show action, and a trigger-id param for another trigger than the first one:
<button data-action="phem-tooltip#show" data-phem-tooltip-trigger-id-param="timer">Open</button>Build a menu of actions#
phem-menu shows a list of actions in a popup, with the keyboard of a menu:
<div data-controller="phem-menu">
<button data-phem-menu-target="trigger">Actions</button>
<div data-phem-menu-target="popup" hidden>
<button data-phem-menu-target="item" onClick={{ rename }}>Rename</button>
<button data-phem-menu-target="item" onClick={{ duplicate }} disabled={{ locked }}>Duplicate</button>
<div data-phem-menu-target="separator"></div>
<div data-phem-menu-target="group">
<span data-phem-menu-target="groupLabel">View</span>
<button data-phem-menu-target="item" role="menuitemcheckbox" aria-checked={{ pinned ? 'true' : 'false' }}
data-phem-menu-close-on-click="false" onClick={{ togglePin }}>Pin</button>
</div>
<button data-phem-menu-target="item" onClick={{ delete }}>Delete</button>
</div>
</div>The parts get these roles: the popup menu, an item menuitem, a group group (with aria-labelledby to its groupLabel), and a separator separator. An item can have the role menuitemcheckbox or menuitemradio: see Add checkbox items and radio items.
| Key | On | Action |
|---|---|---|
| Click, Enter, Space | The trigger | A press of the mouse opens or closes the menu. A touch, Enter and Space use the click. The keyboard puts the focus on the first item |
| ArrowDown | The trigger | Opens the menu on the first item |
| ArrowDown, ArrowUp | An item | Moves the focus (ArrowRight and ArrowLeft with data-phem-menu-orientation-value="horizontal") |
| Home, End | An item | Moves the focus to the first item, or to the last item |
| A letter | An item | Moves the focus to the next item that starts with the letter. A letter that matches no item starts a new text |
| Enter, Space | An item | Clicks the item |
| Escape | The menu | Closes the menu, and gives the focus back to the trigger |
| Tab | The menu | Closes the menu |
| Shift+Tab | The menu | Closes the menu, and gives the focus to the trigger |
The menu opens at the press of the mouse on the trigger, as in Base UI, and not at the click. So a press on the trigger, a move to an item and a release 200 ms or more after the press click the item: its handler runs, and the menu closes. A release before 200 ms does nothing, so a quick click on the trigger does not run the item that opens under the pointer. A release outside the menu keeps it open. To stop the open, cancel the pointerdown of the trigger: a click listener comes after the open. ArrowUp on a closed trigger does nothing.
The keys and the pointer skip an item with the disabled attribute, which cannot get the focus. An item with aria-disabled="true" gets the focus and data-highlighted, as a disabled item of a Base UI menu, so a keyboard user can find it. A click or Enter on it does nothing: its handler does not run, and the menu stays open. The first focus of a menu that opens skips the two kinds. For example, <div data-phem-menu-target="item" aria-disabled="true" onClick={{ archive }}>Archive</div> gets data-disabled, and ArrowDown stops on it. From the last item, ArrowDown goes to the first item, unless data-phem-menu-loop-focus-value="false". A move of the pointer on an item gives it the focus, unless data-phem-menu-highlight-item-on-hover-value="false". An item that comes under a pointer that does not move, for example when the menu opens under it, does not get the focus: the focus of the keyboard stays. The item with the focus gets data-highlighted. When the pointer goes out of the item, and not to another item (to a separator, a label or out of the popup), the item loses the highlight: the focus goes to the popup. The highlight of the keyboard stays when the pointer goes out.
After a change with the keyboard, the popup gets data-instant with the cause. Add a [data-instant] { transition: none } rule to make the change with no animation, as in Base UI. The value is click after Enter or Space on the trigger or on an item, and dismiss after Escape. With no such rule, a keyboard change and an Escape close animate. A change with the pointer removes the attribute:
<div data-phem-menu-target="popup" role="menu" data-closed data-instant="dismiss" hidden>...</div>A click on an item runs its own handler, and closes the menu. The item with data-phem-menu-close-on-click="false" keeps the menu open, as Pin above: after its event, the morph shows the new aria-checked in the open menu. A press outside (each button, the right button too) and the focus out of the menu close it. After a press outside, the focus goes to the trigger, unless the press gives the focus to another element, for example a text field. With data-phem-menu-open-on-hover-value="true", the pointer on the trigger opens the menu after the delay value (100 ms).
The menu sends phem-menu:open when it opens and phem-menu:close when it closes. A script can open the menu with the focus on an item: menu.open("first") or menu.open("last"). The open action (data-action="phem-menu#open") puts the focus on the popup.
The trigger gets aria-haspopup="menu" and aria-expanded, and aria-controls, data-popup-open and data-pressed while the menu is open, as in Base UI. An aria-controls of the server render stays, also on a closed menu. The trigger of a submenu does not get data-pressed. The popup and a groupLabel need no id: with no id, the trigger and the group point to them with element references (ariaControlsElements and ariaLabelledByElements). With an id, they get aria-controls and aria-labelledby. The item with the highlight gets tabindex="0", and the other items get tabindex="-1".
To put menus in a bar, as the menu bar of an application, see Build a menu bar. In a vertical menubar, ArrowRight (ArrowLeft in a right-to-left page) opens the menu in place of ArrowDown and ArrowUp, and its popup goes on the inline-end side of the trigger.
Add checkbox items and radio items#
An item with role="menuitemcheckbox" or role="menuitemradio" is an item that the user checks. With no handler, the browser keeps its state:
<div data-controller="phem-menu">
<button data-phem-menu-target="trigger">View</button>
<div data-phem-menu-target="popup" hidden>
<button data-phem-menu-target="item" role="menuitemcheckbox" aria-checked="true" data-phem-menu-close-on-click="false">Word wrap</button>
<div data-phem-menu-target="separator"></div>
<div data-phem-menu-target="group">
<button data-phem-menu-target="item" role="menuitemradio" data-value="list" aria-checked="true" data-phem-menu-close-on-click="false">List</button>
<button data-phem-menu-target="item" role="menuitemradio" data-value="grid" aria-checked="false" data-phem-menu-close-on-click="false">Grid</button>
</div>
</div>
</div>A click on a checkbox item checks or unchecks it. A click on a radio item checks it, and unchecks the other radio items of its group: the radio items in the same group part, or the radio items in no group. A disabled item does not change. The item gets data-checked or data-unchecked with its aria-checked. With data-phem-menu-close-on-click="false", the menu stays open, as in a menu of Base UI. After a click on Grid, the browser shows:
<button data-phem-menu-target="item" role="menuitemradio" data-value="list" aria-checked="false" tabindex="-1" data-unchecked>List</button>
<button data-phem-menu-target="item" role="menuitemradio" data-value="grid" aria-checked="true" tabindex="-1" data-checked data-highlighted>Grid</button>The aria-checked of the server render is the first state. The state of the browser stays after the morph of an event. When the server render gives a new aria-checked, the new value sets the state: a radio item that the server checks unchecks the other radio items of its group. A page in the Turbo cache has the state of the server render.
The item sends phem-menu:change (phem-context-menu:change in a context menu) after a change. The detail has checked, and value (the data-value of the item):
<div data-controller="phem-menu" data-action="phem-menu:change->layout#set">An item with a handler (onClick) takes its state from the server, as Pin in the first menu. A click does not change the item in the browser and sends no phem-menu:change: the handler changes the state, and the morph shows the new aria-checked. So a handler that fails or refuses the change leaves the item as it was.
Add a submenu#
Put a phem-menu in the popup of a menu. Its trigger is an item of the menu around it:
<div data-controller="phem-menu">
<button data-phem-menu-target="trigger">Actions</button>
<div data-phem-menu-target="popup" hidden>
<button data-phem-menu-target="item" onClick={{ rename }}>Rename</button>
<div data-controller="phem-menu">
<button data-phem-menu-target="trigger">Move to</button>
<div data-phem-menu-target="popup" hidden>
{% for folder in folders %}
<button key={{ folder.id }} data-phem-menu-target="item" onClick={{ move(folder.id) }}>{{ folder.name }}</button>
{% endfor %}
</div>
</div>
<button data-phem-menu-target="item" onClick={{ delete }}>Delete</button>
</div>
</div>The arrow keys of the menu go to Move to as to the other items. The pointer on it opens the submenu after the delay value (100 ms), and the focus stays on it. While the submenu is open, the pointer on another item of the menu gives it the focus after 150 ms, so the pointer can cross an item on its way to the submenu. The pointer that leaves Move to away from the submenu (for example straight down to the next item) closes the submenu at once, as in Base UI, also when the popup of the submenu goes far below its trigger. When the submenu closes before the end of the 150 ms, the item gets the focus at once. The 150 ms apply only before the pointer enters the submenu. In the submenu, the pointer removes the highlight from Move to (the focus stays on it). When the pointer goes from the submenu to empty space, the submenu stays open, as in Base UI. The pointer on another item of the menu closes it at once. A trigger with aria-disabled="true" does not open the submenu. Set data-phem-menu-open-on-hover-value="false" on the submenu to open it only with a click or a key. After ArrowRight on Move to, the browser shows:
<button data-phem-menu-target="trigger" role="menuitem" tabindex="-1" aria-haspopup="menu"
aria-controls="" aria-expanded="true" data-popup-open>Move to</button>
<div data-phem-menu-target="popup" role="menu" tabindex="-1" data-nested
data-open data-side="inline-end" data-align="start" style="...">
<button data-phem-menu-target="item" role="menuitem" tabindex="0" data-highlighted>Inbox</button>The popup of a submenu gets data-nested. aria-controls="" is the attribute of an element reference: the popup has no id. The trigger has no aria-controls while the submenu is closed.
| Key | On | Action |
|---|---|---|
| Click, Enter, Space, ArrowRight | The trigger of the submenu | Opens the submenu. The keyboard puts the focus on its first item |
| ArrowLeft, Escape | The submenu | Closes the submenu, and gives the focus back to its trigger |
| Shift+Tab | The submenu | Closes the submenu, and gives the focus back to its trigger |
In a right-to-left page, ArrowLeft opens the submenu and ArrowRight closes it. The popup of a submenu goes on the inline-end side of its trigger. A click on an item of a submenu runs its handler and closes all the menus. When the menu closes, its submenus close too.
Lock the page while the menu is open#
Set data-phem-menu-modal-value="true" for a modal menu. While it is open, the page does not scroll, and the elements out of the menu get no hover and no click: the body gets pointer-events: none, and the popup pointer-events: auto. A press outside closes the menu, and the element under the pointer does not get the click. The width of the scroll bar stays, so the page does not move. A Turbo visit gives the page its styles back before Turbo keeps the page in its cache.
A menu that the pointer opened (data-phem-menu-open-on-hover-value="true") does not lock the page, as in Base UI: the lock would take the pointer from the trigger, and the menu would close. The same menu locks the page when a click or a key opens it.
Open one menu from several triggers#
One menu can have several triggers, as a popover (see Open one popover from several triggers). Each trigger part is a trigger, and an element anywhere in the page with data-phem-menu-for and the id of the controller element is a detached trigger. A submenu has only its own trigger. The menu opens next to the trigger that opened it, and Escape gives the focus back to this trigger. While a menu that is not modal is open, a press on another trigger moves it there (with open-on-hover, the pointer does it). A modal menu closes at a press on another trigger, as in Base UI: the lock of the page takes the pointer from the other triggers.
The viewport part shows the content of the trigger: give the trigger a data-phem-menu-payload, and put a <template data-payload> with the same value in the viewport. The items of the copy are items of the menu.
<button data-phem-menu-for="file-menu" data-phem-menu-payload="file">File</button>
<button data-phem-menu-for="file-menu" data-phem-menu-payload="edit">Edit</button>
<div id="file-menu" data-controller="phem-menu">
<div data-phem-menu-target="popup" popover="manual" hidden>
<div data-phem-menu-target="viewport">
<template data-payload="file">
<button data-phem-menu-target="item" onClick={{ newFile }}>New</button>
</template>
<template data-payload="edit">
<button data-phem-menu-target="item" onClick={{ undo }}>Undo</button>
<button data-phem-menu-target="item" onClick={{ redo }}>Redo</button>
</template>
</div>
</div>
</div>After a click on Edit, the browser shows:
<button data-phem-menu-for="file-menu" data-phem-menu-payload="edit" aria-haspopup="menu" aria-expanded="true" data-popup-open data-pressed>Edit</button>
<div id="file-menu" data-controller="phem-menu" data-phem-menu-open-value="true">
<div data-phem-menu-target="popup" role="menu" tabindex="-1" data-open ...>
<div data-phem-menu-target="viewport">
<template data-payload="file">...</template>
<template data-payload="edit">...</template>
<div data-current>
<button data-phem-menu-target="item" role="menuitem" tabindex="-1">Undo</button>
<button data-phem-menu-target="item" role="menuitem" tabindex="-1">Redo</button>
</div>
</div>
</div>
</div>The other triggers get aria-expanded="false". The animations of the move, of the size (--popup-width and --popup-height) and of the content, data-activation-direction, and the trigger-id value are the same as for a tooltip. The open action takes a trigger-id param: <button data-action="phem-menu#open" data-phem-menu-trigger-id-param="edit-trigger">.
Filter the items#
An input part filters the items of the list part, as the FilterProvider of Base UI. Its text hides the items (and the triggers of the submenus) whose text does not contain it, with no case, no accents and no punctuation. The data-label attribute of an item replaces its text. A group part with no item to show gets hidden, and the empty part shows only when no item matches. A click on the clear part empties the input. The query resets when the menu closes: the input dispatches an input event, so that a server filter also gets the empty query. The filter shows again only the items that it hid: an item that the server render hides stays hidden.
<div data-controller="phem-menu" data-phem-menu-auto-highlight-value="input">
<button data-phem-menu-target="trigger">Actions</button>
<div data-phem-menu-target="popup" popover="manual" hidden>
<input data-phem-menu-target="input" aria-label="Filter actions">
<button data-phem-menu-target="clear" aria-label="Clear">×</button>
<div data-phem-menu-target="empty" hidden>No actions found.</div>
<div data-phem-menu-target="list">
<button data-phem-menu-target="item" onClick={{ save }}>Save</button>
<button data-phem-menu-target="item" onClick={{ saveAs }}>Save as…</button>
<button data-phem-menu-target="item" data-label="Move to folder" onClick={{ move }}>Move</button>
</div>
</div>
</div>After a click on Actions and the text as, the browser shows:
<div data-phem-menu-target="popup" role="dialog" tabindex="-1" data-open ...>
<input data-phem-menu-target="input" aria-label="Filter actions" role="searchbox" aria-controls="..." aria-activedescendant="...">
<button data-phem-menu-target="clear" aria-label="Clear">×</button>
<div data-phem-menu-target="empty" hidden>No actions found.</div>
<div data-phem-menu-target="list" role="menu">
<button data-phem-menu-target="item" role="menuitem" hidden>Save</button>
<button data-phem-menu-target="item" role="menuitem" data-highlighted>Save as…</button>
<button data-phem-menu-target="item" role="menuitem" data-label="Move to folder" hidden>Move</button>
</div>
</div>The popup is a dialog with the searchbox and the menu. An open with a click or the keyboard gives the focus to the input. An open on hover or with a touch gives it to the popup, so the keyboard of the device does not show. The arrow keys move the highlight while the input keeps the focus (aria-activedescendant), and Enter clicks the item with the highlight. During an IME composition, the arrow keys and Enter go to the IME, not to the items. The highlight on another item (with the pointer or the arrow keys) closes an open submenu, as the focus does in a menu with no filter. The input gets data-highlighted when no item has the highlight. With the auto-highlight value input, the first match gets the highlight while the query is not empty, and with always also when it is empty. Tab closes the menu, and Shift+Tab gives the focus to the trigger. A submenu filters its items only with its own input part.
The keys of a submenu work in the input, as in Base UI:
| Key | Where | Result |
|---|---|---|
| ArrowRight, Enter | The input, with the highlight on the trigger of a submenu | Opens the submenu. The input of the submenu gets the focus and its first item the highlight, or else its first item gets the focus. ArrowRight opens it only when the caret is at the end of the text: else it moves the caret |
| ArrowLeft | The input of a submenu | Closes the submenu, when the caret is at the start of the text: else it moves the caret |
| ArrowLeft, Escape | The submenu | Closes the submenu. The input of the menu gets the focus back, and the trigger keeps the highlight |
In a right-to-left page, ArrowLeft opens the submenu and ArrowRight closes it.
With data-phem-menu-filter-value="false", the controller hides no item: the server renders the items of the query, for example with an onInput handler on the input. The empty part and the highlight still follow the items.
Build a menu bar#
phem-menubar is a bar of menus, as the menu bar of an application. Each menu of the bar is a phem-menu in the menubar element:
<div data-controller="phem-menubar">
<div data-controller="phem-menu">
<button data-phem-menu-target="trigger">File</button>
<div data-phem-menu-target="popup" hidden>
<button data-phem-menu-target="item" onClick={{ create }}>New</button>
<button data-phem-menu-target="item" onClick={{ save }}>Save</button>
<div data-controller="phem-menu">
<button data-phem-menu-target="trigger">Export</button>
<div data-phem-menu-target="popup" hidden>
<button data-phem-menu-target="item" onClick={{ export('pdf') }}>PDF</button>
</div>
</div>
</div>
</div>
<div data-controller="phem-menu">
<button data-phem-menu-target="trigger">Edit</button>
<div data-phem-menu-target="popup" hidden>
<button data-phem-menu-target="item" onClick={{ undo }}>Undo</button>
</div>
</div>
</div>Each menu keeps its own open state (its open value), also after the morph of an event. Only one menu of the bar is open at a time: when a menu opens, the other menus close. The items, the submenus and the keys in a menu are the ones of phem-menu.
| Value | Default | Description |
|---|---|---|
orientation |
horizontal |
horizontal or vertical: the arrow keys between the triggers |
loopFocus |
true |
From the last trigger, the arrow key goes to the first trigger |
modal |
true |
While a menu is open, the page does not scroll, and the elements out of the menubar get no pointer events |
disabled |
false |
No menu of the bar opens |
Set a value with data-phem-menubar-<name>-value, for example data-phem-menubar-orientation-value="vertical".
| Key | On | Action |
|---|---|---|
| ArrowRight, ArrowLeft | A trigger | Moves the focus to the next or to the previous trigger (ArrowDown and ArrowUp in a vertical menubar) |
| Home, End | A trigger | Moves the focus to the first or to the last trigger |
| ArrowDown, Enter, Space | A trigger | Opens the menu on its first item (ArrowRight in a vertical menubar) |
| ArrowRight, ArrowLeft | An open menu | Closes the menu, and opens the next or the previous menu. Its popup gets the focus, and no item is highlighted |
| Shift+Tab | An open menu | Gives the focus to its trigger. The menu stays open |
| ArrowRight | The trigger of a submenu | Opens the submenu |
| ArrowLeft | A submenu | Closes the submenu |
| ArrowLeft | An open menu of a vertical menubar | Closes the menu, and gives the focus back to its trigger |
| Escape | An open menu | Closes the menu, and gives the focus back to its trigger |
The triggers have one tab stop: the trigger of the last menu that had the focus. The arrow keys skip a disabled trigger. In a right-to-left page, ArrowLeft and ArrowRight go the other way.
While a menu is open, the pointer on another trigger opens its menu at once, and closes the first menu. The two popups get data-instant="group" for this change, so a [data-instant] rule can stop their animation. With no such rule, the change animates. A menu of the bar also gets data-instant="group" when an arrow key on its trigger opens it, when the focus out of it closes it, and when another menu of the bar opens. As a menu with no menubar, it gets data-instant="click" for Enter and Space, and data-instant="dismiss" for Escape. The first click on that trigger keeps its menu open. A modal menubar locks the page as a modal menu does, but the menubar and its popups keep the pointer events (pointer-events: auto on the menubar), so the pointer can go to the other triggers. A press outside closes the menu. A menu of the bar opens at the click on its trigger, not at the press.
The menubar element gets role="menubar", aria-orientation, data-orientation, data-modal (with the modal value), data-disabled and aria-disabled="true" (with the disabled value), and data-has-submenu-open while a menu is open. The triggers get role="menuitem", tabindex and data-disabled. In a vertical menubar, the popup of a menu goes on the inline-end side of its trigger. After a click on Edit, a click on Undo (an item with data-phem-menu-close-on-click="false") and ArrowRight, the browser shows:
<div data-controller="phem-menubar" style="pointer-events: auto;" role="menubar" aria-orientation="horizontal"
data-orientation="horizontal" data-modal="" data-has-submenu-open="">
<div data-controller="phem-menu">
<button data-phem-menu-target="trigger" aria-expanded="false" tabindex="-1" aria-haspopup="menu"
role="menuitem">File</button>
<div id="file-popup" data-phem-menu-target="popup" hidden="" data-closed="" role="menu" tabindex="-1">...</div>
</div>
<div data-controller="phem-menu" data-phem-menu-open-value="false">
<button data-phem-menu-target="trigger" aria-expanded="false" tabindex="-1" role="menuitem" ...>Edit</button>
<div id="edit-popup" data-phem-menu-target="popup" role="menu" tabindex="-1" data-closed="" hidden="">...</div>
</div>
<div data-controller="phem-menu" data-phem-menu-open-value="true">
<button data-phem-menu-target="trigger" aria-expanded="true" tabindex="0" role="menuitem" aria-haspopup="menu"
aria-controls="help-popup" data-pressed="" data-popup-open="">Help</button>
<div id="help-popup" data-phem-menu-target="popup" role="menu" tabindex="-1" data-open=""
data-side="bottom" data-align="start" style="position: fixed; inset: 29px auto auto 92px; ...">
<button data-phem-menu-target="item" role="menuitem" tabindex="-1">About</button>
</div>
</div>
</div>Build a navigation menu#
phem-navigation-menu shows the links of a site, with a panel of links (a content) under some items. The content of the open item shows in one shared popup:
<nav data-controller="phem-navigation-menu" data-phem-navigation-menu-side-offset-value="8">
<ul data-phem-navigation-menu-target="list">
<li data-phem-navigation-menu-target="item" data-value="overview">
<button data-phem-navigation-menu-target="trigger">
Overview <span data-phem-navigation-menu-target="icon">▾</span>
</button>
<div data-phem-navigation-menu-target="content" hidden>
<a data-phem-navigation-menu-target="link" href="/start">Quick start</a>
<a data-phem-navigation-menu-target="link" href="/releases">Releases</a>
</div>
</li>
<li data-phem-navigation-menu-target="item" data-value="guides">
<button data-phem-navigation-menu-target="trigger">Guides</button>
<div data-phem-navigation-menu-target="content" hidden>
<a data-phem-navigation-menu-target="link" href="/styling">Styling</a>
</div>
</li>
<li data-phem-navigation-menu-target="item">
<a data-phem-navigation-menu-target="link" href="/blog" aria-current="page">Blog</a>
</li>
</ul>
<div data-phem-navigation-menu-target="backdrop" hidden></div>
<div data-phem-navigation-menu-target="positioner" hidden>
<div data-phem-navigation-menu-target="popup">
<div data-phem-navigation-menu-target="arrow"></div>
<div data-phem-navigation-menu-target="viewport"></div>
</div>
</div>
</nav>Each item has a trigger and a content, or a link. The value of an item is its data-value attribute, its key (key={{ ... }}), or its position. The value value is the state: the value of the open item, or empty when the menu is closed. Set it to open an item on load, and a handler can set it in a new render.
The contents stay in their items in the server render. With a viewport part, the content of the open item moves into the viewport while it is open, as in Base UI, and goes back to its item when it closes. Before a morph, the open content also goes back to its item, so the morph sees the markup of the server render, and after the morph it goes into the viewport again. The browser keeps the focus in the content when it can move an element with its state (moveBefore()); else the controller gives the focus back. With no viewport, the content shows in its item, and the CSS of the app places it.
| Value | Default | Description |
|---|---|---|
value |
empty | The value of the open item |
delay |
50 |
The time (ms) of the pointer on a trigger before its content opens |
closeDelay |
50 |
The time (ms) of the pointer out of the menu before it closes |
orientation |
horizontal |
horizontal or vertical: the arrow keys of the list |
loopFocus |
true |
From the last trigger or link, the arrow key goes to the first one |
side, align, sideOffset, alignOffset, collisionPadding, arrowPadding |
bottom, center, 0, 0, 5, 5 |
The position of the popup next to the trigger of the open item, as in Position |
The pointer on a trigger opens its content after the delay value, also when another item is open. The pointer out of the triggers and of the popup closes the menu after the closeDelay value. A click on a trigger opens and closes its content. A click less than 500 ms after the pointer opened the content keeps it open, and a later click closes it, as in Base UI. A menu that a click or a key opened does not close when the pointer goes out. A click outside, Escape and the focus out of the menu close it. A link with data-phem-navigation-menu-close-on-click="true" closes the menu. Escape, this link and the close action give the focus back to the trigger as Escape does in the table below. The pointer out, a click outside and the focus out do not move the focus.
| Key | On | Action |
|---|---|---|
| ArrowRight, ArrowLeft | A trigger or a link of the list | Moves the focus to the next or to the previous one (ArrowDown and ArrowUp in a vertical menu) |
| ArrowDown, Enter, Space | A trigger | Opens the content. The focus stays on the trigger (ArrowRight in a vertical menu) |
| Tab | The trigger of the open item | Moves the focus into the content in the viewport |
| Shift+Tab | The first element of the content | Moves the focus back to the trigger |
| Tab | The last element of the content | Moves the focus to the next trigger. The menu stays open until the focus goes out of it |
| Tab | The last trigger or link of the list | Moves the focus to the next element of the page, not into the content. The menu closes |
| ArrowDown, ArrowUp, ArrowRight, ArrowLeft | A link of a content, or a trigger of a nested menu in it | Moves the focus to the next or to the previous link or trigger of the content, and from the last one to the first one. A field in the content keeps its arrow keys |
| Escape | The menu | Closes the menu. The focus goes back to the trigger when it is in the content, in the popup, on the trigger or on the page body, as in Base UI. The focus on another trigger stays |
The arrow keys of the list skip a disabled trigger, and do not go to the links in a content. They stop at the first and at the last one, as in Base UI: set data-phem-navigation-menu-loop-focus-value="true" to go from the last one to the first one. Home and End do not move the focus. In a right-to-left page, ArrowLeft and ArrowRight go the other way.
The positioner part (or the popup part, with no positioner) goes next to the trigger of the open item, with the attributes and the variables of Position. The popup part gets the size of the open content in --popup-width and --popup-height, and the positioner in --positioner-width and --positioner-height. At rest, they are auto: the popup has the size of its content. When the open item changes, the popup goes from the old size in px to the new size in px, so a CSS transition of its width and height animates the size. The positioner gets the new size at once, so its place does not change during the transition. When the width or height transition of the popup ends, the variables go back to auto: content that changes its size in the open popup changes the size of the popup. Other animations of the popup do not delay this. Do not use the variables as a length in calc(). On the first open, the positioner (or the popup, with no positioner) gets data-instant: it goes to its place at once, and only the move from one trigger to another animates, also the first move. Turn off the transition of the position with [data-instant] { transition: none }, or the Tailwind variant data-instant:transition-none:
[data-phem-navigation-menu-target="popup"] {
width: var(--popup-width);
height: var(--popup-height);
transition: width 200ms, height 200ms;
}
[data-phem-navigation-menu-target="viewport"] {
position: relative;
overflow: hidden;
}
[data-phem-navigation-menu-target="content"] { transition: opacity 200ms, translate 200ms; }
[data-phem-navigation-menu-target="content"][data-starting-style][data-activation-direction="right"] { translate: 50% 0; opacity: 0; }
[data-phem-navigation-menu-target="content"][data-ending-style][data-activation-direction="right"] { translate: -50% 0; opacity: 0; }When the open item changes, the old and the new contents get data-activation-direction: right or left (down or up in a vertical menu), the direction of the new item from the old one. A content that leaves the viewport gets position: absolute at the start of the viewport until its animation ends, so it takes no space. The contents, the positioner, the popup and the backdrop get data-open, data-closed and the animations of the conventions.
The nav, the lists and the arrow get data-open while an item is open. The popup in a positioner gets the data-side and data-align of the positioner, also after a flip. The popup gets tabindex="-1", also with no positioner: a click on its space gives it the focus, and Tab does not stop on it. These attributes stay after the morph of an event.
A trigger gets aria-expanded, aria-controls, data-popup-open and data-pressed when its item is open, and data-disabled. A content needs no id: with no id, the controller links the trigger to it with an element reference (ariaControlsElements). An icon gets data-popup-open. A link with aria-current="page" or data-active from the server gets both. The controller sends phem-navigation-menu:change with { value } when the user opens, changes or closes an item. After the pointer on Overview and then on Guides (the contents of this page have an id), the browser shows:
<nav data-controller="phem-navigation-menu" data-phem-navigation-menu-side-offset-value="8" data-phem-navigation-menu-value-value="guides" data-open="">
<ul data-phem-navigation-menu-target="list" data-open="">
<li data-phem-navigation-menu-target="item" data-value="overview">
<button data-phem-navigation-menu-target="trigger" aria-expanded="false" aria-controls="overview-content">Overview ...</button>
<div id="overview-content" data-phem-navigation-menu-target="content" data-activation-direction="right" data-closed="" hidden="">...</div>
</li>
<li data-phem-navigation-menu-target="item" data-value="guides">
<button data-phem-navigation-menu-target="trigger" aria-expanded="true" aria-controls="guides-content"
data-popup-open="" data-pressed="">Guides</button>
</li>
<li data-phem-navigation-menu-target="item">
<a data-phem-navigation-menu-target="link" href="/blog" aria-current="page" data-active="">Blog</a>
</li>
</ul>
<div data-phem-navigation-menu-target="positioner" data-open="" data-side="bottom" data-align="center"
style="--positioner-width: auto; --positioner-height: auto; position: fixed; inset: 37px auto auto 26px; --anchor-width: 58.234375px; ...">
<div data-phem-navigation-menu-target="popup" tabindex="-1" data-open="" data-side="bottom" data-align="center"
style="--popup-width: auto; --popup-height: auto;">
<div data-phem-navigation-menu-target="arrow" data-open="" data-side="bottom" data-align="center" style="left: 96px;"></div>
<div data-phem-navigation-menu-target="viewport">
<div id="guides-content" data-phem-navigation-menu-target="content" data-activation-direction="right" data-open="">
<a data-phem-navigation-menu-target="link" href="/styling">Styling</a>
</div>
</div>
</div>
</div>
</nav>During the size transition (the first 200 ms of the move to Guides), the variables have px values: the popup goes from --popup-width: 302px; --popup-height: 122px; to 202px and 82px, and the positioner has --positioner-width: 202px; --positioner-height: 82px; at once. When the menu is closed, the nav, the list and the arrow have no data-open, the popup has no data-side and data-align, and tabindex="-1" stays.
A navigation menu in a content is a nested menu, as in Base UI. Give it its own positioner, popup and viewport: its content shows next to its trigger, and the outer menu stays open while the pointer or the focus is in the nested menu. With only a viewport in the content (no positioner and no popup), its content shows in the content of the outer menu. Escape closes the nested menu first, then the outer menu:
<div data-phem-navigation-menu-target="content" hidden>
<a data-phem-navigation-menu-target="link" href="/quick-start">Quick start</a>
<div data-controller="phem-navigation-menu" data-phem-navigation-menu-orientation-value="vertical"
data-phem-navigation-menu-side-value="right" data-phem-navigation-menu-align-value="end">
<ul data-phem-navigation-menu-target="list">
<li data-phem-navigation-menu-target="item" data-value="handbook">
<button data-phem-navigation-menu-target="trigger">Handbook</button>
<div data-phem-navigation-menu-target="content" hidden>
<a data-phem-navigation-menu-target="link" href="/styling">Styling</a>
</div>
</li>
</ul>
<div data-phem-navigation-menu-target="positioner" hidden>
<div data-phem-navigation-menu-target="popup">
<div data-phem-navigation-menu-target="viewport"></div>
</div>
</div>
</div>
</div>A nested menu gives its arrow keys to the content of the outer menu, as in Base UI. In the example, ArrowDown on Quick start moves the focus to Handbook, and ArrowRight on Handbook moves it back to Quick start. The arrow keys do not open the nested menu: open it with Enter, Space, a click or the pointer. In the content of the nested menu, the arrow keys move the focus between its own links.
Open a menu on a right click#
phem-context-menu is a phem-menu that opens at the point of the pointer, in place of the menu of the browser. Its trigger is an area of the page:
<div data-controller="phem-context-menu">
<div data-phem-context-menu-target="trigger" class="file">{{ file.name }}</div>
<div data-phem-context-menu-target="popup" hidden>
<button data-phem-context-menu-target="item" onClick={{ open(file.id) }}>Open</button>
<button data-phem-context-menu-target="item" onClick={{ rename(file.id) }}>Rename</button>
<div data-phem-context-menu-target="separator"></div>
<button data-phem-context-menu-target="item" onClick={{ delete(file.id) }}>Delete</button>
</div>
</div>A right click on the trigger opens the menu with its top left corner at the pointer. The Menu key and Shift+F10 open it below the element with the focus. On a touch screen, a long press of 500 ms opens it. A new right click on the trigger moves the open menu to the new point. Put the popup out of the trigger. After a right click, the browser shows:
<div data-phem-context-menu-target="trigger" class="file" data-popup-open data-pressed>report.pdf</div>
<div data-phem-context-menu-target="popup" role="menu" tabindex="-1" data-instant="click"
data-open data-side="bottom" data-align="start" style="position: fixed; top: 212px; left: 348px; ...">The popup gets data-instant="click" at the open, as in Base UI: a [data-instant] rule opens it with no animation. Near an edge of the viewport, the popup keeps its side, moves on the two axes to stay in the viewport, and flips its align. A submenu of the context menu goes next to its trigger, as in phem-menu.
The focus goes to the popup, so ArrowDown goes to the first item. When the popup opens under the pointer, no item gets the focus until the pointer moves. The parts, the keys, the submenus and the values are the ones of phem-menu, with data-phem-context-menu- in their names. A click on the trigger does not open the menu, and the trigger gets no ARIA attributes, because it is not a button. Escape gives the focus back to the element that had it before the menu opened. Tab and Shift+Tab close the menu and its submenus. The focus goes back to the element that had it before the menu opened, and the Tab moves on from that element to the next (or the previous) element. When no element had the focus, the Tab moves on from the place of the popup. The menu does not trap the focus. On macOS, a press of the right button, a move to an item and a release of the button click the item, when the release is more than 1 px from the point of the open, as in Base UI. The other systems send contextmenu after the release, so the release does not click an item there. A menu that opens with no point, with the open action or with data-phem-context-menu-open-value from the server, goes below the trigger.
Suggest values while the user types#
phem-combobox adds a list of suggestions to a text field. The server gives the suggestions: the input sends its text, and the render of the server gives the items. The controller adds the roles and the keyboard.
#[Component]
final class CityField
{
private const array CITIES = ['Lyon', 'Paris', 'Parma', 'Porto'];
#[State] public string $query = '';
#[State] public string $city = '';
public function choose(string $city): void
{
$this->city = $city;
}
public function render(): Node
{
$text = strtolower(trim($this->query));
$results = '' === $text || $text === strtolower($this->city) ? [] : array_values(array_filter(
self::CITIES,
static fn (string $name): bool => str_starts_with(strtolower($name), $text),
));
return (
<div data-controller="phem-combobox">
<input data-phem-combobox-target="input" phem:bind={{ query }} phem:live aria-label="City" />
<div data-phem-combobox-target="popup" hidden>
<ul data-phem-combobox-target="list">
{% for name in results %}
<li key={{ name }} id={{ 'city-' ~ name }} data-phem-combobox-target="item" onClick={{ choose(name) }}>{{ name }}</li>
{% endfor %}
</ul>
<p data-phem-combobox-target="empty">No city.</p>
</div>
</div>
);
}
}The popup opens when the user types, and on ArrowDown or ArrowUp. It shows when it has an item, an empty part or a status part that shows something: a part of its state, or a text that is not hidden. A status part with nothing to show does not open the popup, and the input keeps aria-expanded="false". The empty part is a live region that stays in the page, as the Empty part of Base UI: it gets role="status", aria-live="polite" and aria-atomic="true", and it has its content only when the list has no item. So a screen reader reads "No city." when the list becomes empty, and :empty matches the part when the list has items (.empty:empty { padding: 0; }). Do not give it hidden. The focus stays in the input. An input that the user empties closes the popup, as in Base UI, and the next ArrowDown opens it again. With data-phem-combobox-open-on-input-click-value="true", or with the input in the popup, the popup stays open. The input gets autocomplete="off", spellcheck="false", autocorrect="off" and autocapitalize="none", unless the markup gives them. With no selection (an Autocomplete), the items get no aria-selected, as in Base UI: the highlight is data-highlighted and aria-activedescendant. When the user types "pa" and presses ArrowDown two times, the browser shows:
<input data-phem-combobox-target="input" role="combobox" aria-autocomplete="list" autocomplete="off"
spellcheck="false" autocorrect="off" autocapitalize="none"
aria-haspopup="listbox" aria-controls="" aria-expanded="true" aria-activedescendant="city-Parma" data-popup-open value="pa">
<div data-phem-combobox-target="popup" data-open data-side="bottom" data-align="start" style="...">
<ul data-phem-combobox-target="list" role="listbox">
<li id="city-Paris" role="option">Paris</li>
<li id="city-Parma" role="option" data-highlighted>Parma</li>
</ul>
<p data-phem-combobox-target="empty" hidden>No city.</p>
</div>| Key | Action |
|---|---|
| ArrowDown, ArrowUp | Opens the popup. With no selection (an Autocomplete) and no auto-highlight, the key that opens the popup gives no highlight, and the next key goes to the first item (ArrowDown) or the last item (ArrowUp). Then it moves the highlight. After the last item, the highlight goes back to the input, and the next key goes to the first item (with auto-highlight, the key goes to the first item at once). With data-phem-combobox-loop-focus-value="false", the highlight stays on the last item |
| Enter | Chooses the highlighted item. With an open popup and no highlight, Enter closes the popup and keeps its default action (the submit of the form). With a closed popup, Enter has its default action. The keys of an IME composition (a Japanese or a Chinese word that the user confirms with Enter) go to the IME, also in Safari |
| Escape | Closes the popup. A second Escape, on the closed popup, clears all that the clear part clears (the text, the selected item or the chips), as in Base UI, and the popup stays closed. A shown clear part gets the click, with its handler. With multiple and a list of the server, the chipRemove of each chip gets a click. After a text with nothing to show (no item, no empty part, and no status part that shows something), the popup does not show, but the first Escape is the same: the text stays, and the second Escape clears it |
To choose an item is a click on it: its handler runs (here choose), or its link opens. The input gets the label of the item at once (its data-label, or its text), and the popup closes. The input and change events of the input go in the next task, after the event of the click: the handler of the item runs first, and then the input sends the label, as when the user types it. Here the click sends choose('Paris'), and then phem:bind sends query: "Paris", so the handler does not write the text of the input. Then the controller dispatches phem-combobox:select with the item in the detail. A click on a clear part clears the input and sends its input event, as when the user deletes the text. The popup stays closed when it was closed. A click on a trigger part opens or closes the popup. A click outside and the focus out of the combobox close the popup. A trigger part gets aria-haspopup="listbox", aria-expanded and data-popup-open, and aria-controls only while the popup is open. The input also gets aria-controls only while the popup is open, as in Base UI. An inline list (no popup part) is always open, so its input always has it. An inputGroup part gets role="group" and data-popup-open. A press on an inputGroup out of the input, the chips and the buttons gives the focus to the input, as a press on the input. The input gets aria-haspopup="listbox", and data-list-empty when the list has no visible item.
The input can be in the popup, as a search. A trigger button opens the popup, and a value part in it that is not an input shows the label of the selection, or its placeholder (with data-placeholder):
<div data-controller="phem-combobox" data-phem-combobox-filter-value="contains" data-phem-combobox-selection-mode-value="single">
<button type="button" data-phem-combobox-target="trigger"><span data-phem-combobox-target="value" placeholder="Select country"></span></button>
<div data-phem-combobox-target="popup" hidden>
<input data-phem-combobox-target="input" aria-label="Country" />
<ul data-phem-combobox-target="list">...</ul>
</div>
</div>The popup goes against the trigger, and the input gets the focus at the open. A choice, Escape and a click outside give the focus back to the trigger, and the input has no text at the next open.
As in Base UI, the popup is a dialog that has the search and the list. The popup gets role="dialog". The trigger gets aria-haspopup="dialog", aria-controls to the popup while it is open, and data-placeholder while no item is selected. The input keeps aria-haspopup="listbox" and points to the list. Give the popup an aria-label. With no item selected, the browser shows:
<button type="button" data-phem-combobox-target="trigger" aria-haspopup="dialog" aria-expanded="false" data-placeholder="">
<span data-phem-combobox-target="value" placeholder="Select country" data-placeholder="">Select country</span>
</button>
<div data-phem-combobox-target="popup" aria-label="Select country" role="dialog" hidden>
<input data-phem-combobox-target="input" aria-label="Country" role="combobox" aria-haspopup="listbox" ... />
<ul data-phem-combobox-target="list" role="listbox">...</ul>
</div>The list has no id, so the input points to it with an element reference (ariaControlsElements). An item with no id gets no id: the input points to the highlighted item with ariaActiveDescendantElement, and the browser shows aria-activedescendant="". The highlight stays on the same item through the morph of another event. When the morph replaces the item, the highlight stays only on an item with the same id, so give each item a stable id in a list that changes. A move of the pointer on an item highlights it. An item that comes under a pointer that does not move, for example when the list changes, does not get the highlight. When the pointer goes out of the items, the highlight goes. A highlight of the keyboard stays, also on an item that the pointer highlighted before: for example, with multiple and openOnInputClick, an input that the user empties gives the highlight to the first chosen item, and it stays when the list changes under the pointer. With both and inline, the input gets the text of the user again. When the user types, the highlight goes. With data-phem-combobox-auto-highlight-value="true", the first item gets the highlight when the user types a text (not an empty text) and when an arrow key opens the popup. When the pointer goes out of the items, this highlight goes too, as in Base UI: the first item does not get it again until the user types, and the next ArrowDown goes to the first item. A closed popup keeps the data-highlighted of its item until its close animation ends, and then the item loses it. The popup and the list get data-empty when there is no visible item, and the empty parts show only then. A status part gets role="status", so a screen reader reads its text, for example "3 cities". Use var(--anchor-width) to give the popup the width of the input.
Choose more than one value#
Set data-phem-combobox-multiple-value="true" to choose more than one item, for a field of tags. Show the chosen values as chip parts, each with a chipRemove part:
#[Component]
final class TagField
{
private const array TAGS = ['bug', 'docs', 'feature', 'question'];
#[State] public string $query = '';
#[State] public array $tags = [];
public function toggle(string $tag): void
{
$this->tags = \in_array($tag, $this->tags, true)
? array_values(array_diff($this->tags, [$tag]))
: [...$this->tags, $tag];
$this->query = '';
}
public function render(): Node
{
$text = strtolower(trim($this->query));
$results = array_values(array_filter(self::TAGS, static fn (string $tag): bool => str_starts_with($tag, $text)));
return (
<div data-controller="phem-combobox" data-phem-combobox-multiple-value="true">
{% for tag in tags %}
<span key={{ tag }} data-phem-combobox-target="chip">
{{ tag }}
<button data-phem-combobox-target="chipRemove" onClick={{ toggle(tag) }} aria-label={{ 'Remove ' ~ tag }}>×</button>
</span>
{% endfor %}
<input data-phem-combobox-target="input" phem:bind={{ query }} phem:live aria-label="Tags" />
<div data-phem-combobox-target="popup" hidden>
<ul data-phem-combobox-target="list">
{% for tag in results %}
<li key={{ tag }} id={{ 'tag-' ~ tag }} data-phem-combobox-target="item"
aria-selected={{ tag in tags ? 'true' : 'false' }} onClick={{ toggle(tag) }}>{{ tag }}</li>
{% endfor %}
</ul>
</div>
</div>
);
}
}The server gives the chosen items aria-selected="true", and the controller does not change it: the highlight is data-highlighted and aria-activedescendant. A choice keeps the popup open and the highlight, so the user can choose more items. A choice while the input has a text closes the popup, as in Base UI. Then the text goes, with its input event: the event of the click goes to the server first, so its item is still in the list. The input has no free text: when the popup closes, the text goes, with an input event, and the chips stay. Give each chip the data-value of its item, and the browser keeps the chosen items in the list: a server render that does not have a chosen item keeps it at the end of the list, until its chip goes. This also works when the morph of an answer changes only the chips, for example when the chips are in a component of their own and the list did not change. The status parts also work here. A popup that opens before the user types has the highlight on the first chosen item. When the user deletes all the text, the popup closes, as in Base UI. With data-phem-combobox-open-on-input-click-value="true", the popup stays open, and the first chosen item gets the highlight. Enter with no highlight closes the popup, and the form submits. When the user chose bug and highlights docs, the browser shows:
<span data-phem-combobox-target="chip" tabindex="-1">bug <button data-phem-combobox-target="chipRemove" tabindex="-1" aria-label="Remove bug">×</button></span>
<input data-phem-combobox-target="input" role="combobox" aria-activedescendant="tag-docs" aria-expanded="true" ...>
<ul data-phem-combobox-target="list" role="listbox" aria-multiselectable="true">
<li id="tag-bug" role="option" aria-selected="true" data-selected>bug</li>
<li id="tag-docs" role="option" aria-selected="false" data-highlighted>docs</li>
</ul>| Key | On | Action |
|---|---|---|
| ArrowLeft | The start of the input | Moves the focus to the last chip |
| Backspace | An empty input | Clicks the chipRemove of the last chip |
| ArrowLeft, ArrowRight | A chip | Moves the focus to the chip before or after it, or back to the input |
| Backspace, Delete | A chip | Clicks its chipRemove. The focus goes to the chip before it (Backspace) or after it (Delete), or to the input |
The chips and their chipRemove parts get tabindex="-1", so Tab goes from the input out of the combobox. The focus on a chip closes the popup, as in Base UI. A chips part (the row of the chips and the input) gets role="toolbar" while a chip shows. A press on a chipRemove keeps the focus in the input. A press in the popup also keeps the focus in the input: on an item, on the space around the items, and on a list with tabindex="-1". A field of your application in the popup (an input, a textarea, a select, editable content) gets the focus of its press.
Select one item of a list of the browser#
With a list of the browser (see the next section) and data-phem-combobox-selection-mode-value="single", the combobox has one selected item, as a select with a search:
<div data-controller="phem-combobox" data-phem-combobox-filter-value="contains"
data-phem-combobox-selection-mode-value="single" data-phem-combobox-open-on-input-click-value="true">
<div data-phem-combobox-target="inputGroup">
<input data-phem-combobox-target="input" aria-label="Fruit" />
<button type="button" data-phem-combobox-target="clear" aria-label="Clear">×</button>
</div>
<input type="hidden" name="fruit" data-phem-combobox-target="value" />
<div data-phem-combobox-target="popup" hidden>
<ul data-phem-combobox-target="list">
{% for fruit in fruits %}
<li key={{ fruit.id }} data-value={{ fruit.id }} data-phem-combobox-target="item">{{ fruit.name }}</li>
{% endfor %}
</ul>
</div>
</div>A click on an item selects it. The item gets data-selected and aria-selected="true", the input gets its label, the value part gets its data-value, and the combobox sends phem-combobox:change with the item and the value in the detail. After a click on Cherry, the browser shows:
<input data-phem-combobox-target="input" role="combobox" aria-expanded="false" value="Cherry" />
<input type="hidden" name="fruit" data-phem-combobox-target="value" value="cherry" />
<li data-value="cherry" data-phem-combobox-target="item" role="option" aria-selected="true" data-selected>Cherry</li>An inputGroup part gets data-placeholder while no item is selected. The first selection is the item that the server renders with data-selected: an input with no text gets its label, and a value part with no value gets its data-value, with no event. So <li data-value="cherry" data-selected data-phem-combobox-target="item">Cherry</li> gives <input value="Cherry"> and <input type="hidden" name="fruit" value="cherry"> in the browser. The selection of the browser stays after the morph of an event. A server render that changes the data-selected of an item sets the selection again, with the text of the input and the value part: use it to reset a form. The input has no free text: when the popup closes, the input gets the label of the selected item again, or no text. When the popup opens with the label of the selection in the input, the list shows all the items, until the user types, and the selected item has the highlight (data-highlighted) and is in view. Enter with no highlight closes the popup, and the input gets the label of the selected item again. A click on the clear part removes the text and the selection. When the user deletes all the text of the input, the selection goes too, as in Base UI: the input then stays empty when the popup closes. An input in the popup (a search) keeps the selection.
A clear part shows only when there is something to clear, and has hidden at other times: a selected item in this mode, a chip with multiple or the multiple selection mode, and else a text in the input. With data-phem-combobox-open-on-input-click-value="true", a click on the input opens the popup. With an inputGroup part (the box around the input and its buttons), the popup is positioned against this box, so var(--anchor-width) is its width.
Select one item of a list of the server#
The single selection mode also works with a list of the server, as the Async search of Base UI: the server searches, and the browser keeps the selection. Search in the onChange handler of the input, with a service, and keep the results in the state. Bind a hidden value part to the selected value:
#[Component]
final class ReviewerField
{
#[State] public string $query = '';
#[State] public string $selected = '';
#[State] public array $people = [];
public function __construct(private readonly PersonRepository $repository)
{
}
public function search(Event $e): void
{
$this->query = trim($e->value);
$this->people = $this->repository->search($this->query);
}
public function render(): Node
{
return (
<div data-controller="phem-combobox" data-phem-combobox-selection-mode-value="single" data-phem-combobox-open-on-input-click-value="true">
<input data-phem-combobox-target="input" onChange={{ search }} aria-label="Reviewer" />
<button type="button" data-phem-combobox-target="clear" aria-label="Clear">×</button>
<input type="hidden" data-phem-combobox-target="value" phem:bind={{ selected }} />
<div data-phem-combobox-target="popup" hidden>
<div data-phem-combobox-target="status">
<span data-phem-combobox-status="pending" hidden>Searching…</span>
<span data-phem-combobox-status="error" hidden>Failed to fetch people.</span>
<span data-phem-combobox-status="idle" hidden>Start typing to search people…</span>
<span data-phem-combobox-status="empty" hidden>No matches for "{{ query }}".</span>
</div>
<p data-phem-combobox-target="empty">Try a different search term.</p>
<ul data-phem-combobox-target="list">
{% for person in people %}
<li key={{ person.id }} id={{ 'person-' ~ person.id }} data-value={{ person.id }} data-label={{ person.name }} data-phem-combobox-target="item">{{ person.name }}</li>
{% endfor %}
</ul>
</div>
</div>
);
}
}A click on an item selects it in the browser, as with a list of the browser: the item gets data-selected and aria-selected="true", and the value part gets its data-value. The input gets the label of the item at once, and sends its input and change events in the next task, after the event of the click (when the item has an onClick handler): the server render of the search does not have to keep the item for its handler. The label of the selection is not a search, so the event gives no text: after a click on Lindsay Walton, the browser sends search with $e->value set to "", and selected: "lindsay". A phem:bind input gets "" too. The text that the user types goes as it is.
The browser keeps the selected item in the list: a server render that does not have it (here search('') gives no people) keeps the item at the end of the list. The text of an input with no phem:bind belongs to the browser, so the morph keeps the label. The server can also mark the selected item, for example with data-selected={{ person.id == selected ? '' : null }} on the li. A server render that marks the item that the browser already selected does not change the input: the text that the user types stays. For example, after the user deletes the last letter of Lindsay Walton, the input keeps Lindsay Walto when the answer of the server marks Lindsay as selected. A server render that removes this item does not change the selection of the browser. So a choice with the keyboard keeps the new item while the list of the search still has the old selection: after the search "Li", ArrowDown to Leslie Alexander and Enter, the answer of the server has no people. The browser keeps Leslie, with its label and its value, and the item of Lindsay leaves the list. After the click on Lindsay Walton, the browser shows:
<input data-phem-combobox-target="input" role="combobox" aria-expanded="false" value="Lindsay Walton" />
<input type="hidden" data-phem-combobox-target="value" value="lindsay" />
<ul data-phem-combobox-target="list" role="listbox">
<li id="person-lindsay" data-value="lindsay" data-label="Lindsay Walton" data-phem-combobox-target="item" role="option" aria-selected="true" data-selected>Lindsay Walton</li>
</ul>The parts of the status part (the elements with data-phem-combobox-status) show the state of the search. The part of the state shows, and the other parts get hidden:
| Part | Shows |
|---|---|
pending |
While an event of the input runs, after the loading delay (see Show a loading state) |
error |
When the last event of the input failed, for example when the handler throws. The items get hidden, and phem shows no error message for the event (it cancels phem:error, see An event fails and the page doesn’t change). The next event that the server takes hides it |
idle |
When the list has no item and the input has no text to search |
empty |
When no item matches the text. The items that the browser keeps (the selected item) do not count |
results |
When items show, for example "3 results found". The server renders the text |
more |
With results, when the limit value hides matches of the filter of the browser |
From the input of the user until the event of the input runs, the parts keep the state of a text, so they do not show a result of the old text. With no text to search, or with the filter of the browser, the state does not wait for the server. While the pending part shows, the results and more parts are hidden. The content of the status with no data-phem-combobox-status always shows. While the pending, error or idle part shows, the empty parts have no content. With the text "zzz" and no selection, the browser shows:
<div data-phem-combobox-target="status" role="status">
<span data-phem-combobox-status="pending" hidden>Searching…</span>
<span data-phem-combobox-status="error" hidden>Failed to fetch people.</span>
<span data-phem-combobox-status="idle" hidden>Start typing to search people…</span>
<span data-phem-combobox-status="empty">No matches for "zzz".</span>
</div>
<p data-phem-combobox-target="empty" role="status" aria-live="polite" aria-atomic="true">Try a different search term.</p>The rest is as with a list of the browser. The input has no free text: when the popup closes, the input gets the label of the selected item again (and sends its events), or no text. A popup that opens with the label of the selection in the input has the highlight on the selected item. The clear part shows only while an item is selected: a click on it removes the text and the selection, and the next event sends both. When the user deletes all the text, the selection goes too: the event sends "" and selected: "". When the selection goes, the item that the browser kept leaves the list at once, so the idle part shows with no wait. A clear on an open popup keeps the popup open, as in Base UI. Give an item a data-label when its text has more than its label (for example a name and an email).
Show a choice before the server answers#
With a list of the server, give an item an onClick handler that saves the choice, for example a service that assigns a reviewer. The browser shows the choice at once, before the handler answers, and shows the state of the handler:
- With
data-phem-combobox-multiple-value="true", a click on an item that is not chosen adds a chip from thechipTemplatepart, a<template>in the box of the chips. The chip goes before the template, with thedata-valueof the item and its ownid.{label}in its text and in its attributes is the label of the item (itsdata-label, or its text). The item getsaria-selected="true". An item that is chosen (a click removes it) gets no chip. - With the
singleselection mode, the browser selects the item, as with no handler.
While the handler runs, the chip, or the clear parts of the single selection mode, get data-pending. The chip stays through the morph of another event, for example a search. The answer of the handler shows the server render: its morph removes the chip of the browser, and shows the chip of the server. When the handler fails (it throws, or the server does not answer), the chip or the clear part gets data-failed in place of data-pending, and phem shows no error message for the event. After one second, the choice goes: the chip leaves and the item is not chosen, or the selection and the text of the input are cleared.
#[Component]
final class ReviewersField
{
#[State] public string $query = '';
#[State] public array $people = [];
#[State] public array $chosen = [];
public function __construct(private readonly PersonRepository $repository)
{
}
public function search(Event $e): void
{
$this->query = trim($e->value);
$this->people = $this->repository->search($this->query);
}
public function toggle(string $id): void
{
$others = array_values(array_filter($this->chosen, static fn (array $person): bool => $person['id'] !== $id));
// assign() throws when the person cannot review.
$this->chosen = $others !== $this->chosen ? $others : [...$this->chosen, $this->repository->assign($id)];
}
public function render(): Node
{
return (
<div data-controller="phem-combobox" data-phem-combobox-multiple-value="true">
<div data-phem-combobox-target="chips">
{% for person in chosen %}
<span key={{ person.id }} data-value={{ person.id }} data-phem-combobox-target="chip">
{{ person.name }}
<button type="button" data-phem-combobox-target="chipRemove" onClick={{ toggle(person.id) }} aria-label="Remove {{ person.name }}">×</button>
</span>
{% endfor %}
<template data-phem-combobox-target="chipTemplate">
<span data-phem-combobox-target="chip">{label} <button type="button" data-phem-combobox-target="chipRemove" aria-label="Remove {label}">…</button></span>
</template>
<input data-phem-combobox-target="input" onChange={{ search }} aria-label="Reviewers" />
</div>
<div data-phem-combobox-target="popup" hidden>
<ul data-phem-combobox-target="list">
{% for person in people %}
<li key={{ person.id }} id={{ 'person-' ~ person.id }} data-value={{ person.id }} data-label={{ person.name }}
aria-selected={{ person.id in chosen|column('id') ? 'true' : 'false' }}
data-phem-combobox-target="item" onClick={{ toggle(person.id) }}>{{ person.name }}</li>
{% endfor %}
</ul>
</div>
</div>
);
}
}Right after a click on Tom Cook, before the answer, the browser shows:
<div data-phem-combobox-target="chips" role="toolbar">
<span data-phem-combobox-target="chip" tabindex="-1" id="phem-combobox-choice-1" data-value="tom" data-pending="">Tom Cook <button type="button" data-phem-combobox-target="chipRemove" tabindex="-1" aria-label="Remove Tom Cook">…</button></span>
<template data-phem-combobox-target="chipTemplate">...</template>
<input data-phem-combobox-target="input" role="combobox" ... />
</div>The answer replaces the chip with the chip of the server. When assign() throws, the chip gets data-failed, and one second later it leaves, and the item gets aria-selected="false" again. phem shows no error message for the failed choice: its chip shows the failure. The failure of another event, also of the same handler, shows the message. The label of the item goes in the chip as it is, also with a $ or a {value} in it. Style the parts with these attributes, for example a spinner in the remove button of a chip with data-pending, and a warning icon with data-failed. The ComboboxChipTemplate, ComboboxChipRemove and ComboboxClear components of phem/ui-base do it.
Choose more than one item of a list of the browser#
With a list of the browser and data-phem-combobox-selection-mode-value="multiple", the browser keeps the chosen items, as the Combobox of Base UI with multiple. A choice sends no event to the server. The server renders the items and the chips one time: give each chip part the data-value of its item, and hidden when its item is not chosen. A chips part is the box of the chips and the input:
<div data-controller="phem-combobox" data-phem-combobox-filter-value="contains"
data-phem-combobox-selection-mode-value="multiple" data-phem-combobox-multiple-value="true">
<div data-phem-combobox-target="chips" aria-label="Selected languages">
{% for id, label in languages %}
<span key={{ id }} data-value={{ id }} hidden data-phem-combobox-target="chip">
{{ label }}
<button type="button" data-phem-combobox-target="chipRemove" aria-label="Remove {{ label }}">×</button>
</span>
{% endfor %}
<input data-phem-combobox-target="input" aria-label="Languages" placeholder="e.g. TypeScript"
data-phem-combobox-description="{count} selected" />
</div>
<div data-phem-combobox-target="popup" hidden>
<ul data-phem-combobox-target="list">
{% for id, label in languages %}
<li key={{ id }} data-value={{ id }} data-phem-combobox-target="item">{{ label }}</li>
{% endfor %}
</ul>
</div>
</div>A click on an item adds its data-value to the values, or removes it. The item gets data-selected and aria-selected, and the chip of the value shows. The chips show in the order of the choices. A click on a chipRemove part removes the value of its chip, and a click on the clear part removes all the values. The keys of the chips are the same as above. A choice clears the text of the input: when the input had a text, the popup closes, and else it stays open, as in Base UI. When the popup closes, the text goes too, and a popup that opens before the user types has the highlight on the first chosen item. Each change sends phem-combobox:change with the item (or null) and the value (the array of the values) in the detail. While a value is chosen, the input has no placeholder, it gets the aria-description of its data-phem-combobox-description attribute ({count} is the number of the values), and the chips part keeps its aria-label. With no value, they lose it, and an inputGroup part gets data-placeholder. After a click on TypeScript and on Go, the browser shows:
<div data-phem-combobox-target="chips" aria-label="Selected languages" role="toolbar">
<span data-value="js" hidden data-phem-combobox-target="chip" tabindex="-1">JavaScript …</span>
<span data-value="ts" data-phem-combobox-target="chip" tabindex="-1">TypeScript …</span>
<span data-value="go" data-phem-combobox-target="chip" tabindex="-1">Go …</span>
<input data-phem-combobox-target="input" role="combobox" aria-description="2 selected" ...>
</div>
<li data-value="ts" data-phem-combobox-target="item" role="option" aria-selected="true" data-selected>TypeScript</li>The first values are the items that the server renders with data-selected, and the chips with a data-value that it renders with no hidden. The values stay after the morph of an event, and a server render that changes the data-selected of an item adds or removes its value.
A form sends only the chosen values: put a hidden input with the value in each chip (<input type="hidden" name="languages" value="js">). The form fields of a hidden chip get disabled, and the fields of a chip that shows lose it.
Choose more than one item of a list of the server#
The multiple selection mode also works with a list of the server, for a form that sends the choices on submit: the server searches, and the browser keeps the chosen items. The items have no onClick handler, so a choice sends no event. A click on an item that is not chosen adds a chip from the chipTemplate part, before the template: {label} in its text and its attributes is the label of the item, and {value} in its attributes is the value of the item. A hidden input in the chip gives the value to the form:
<form onSubmit={{ save }}>
<div data-controller="phem-combobox" data-phem-combobox-selection-mode-value="multiple" data-phem-combobox-multiple-value="true">
<div data-phem-combobox-target="chips" aria-label="Selected reviewers">
<template data-phem-combobox-target="chipTemplate">
<span data-phem-combobox-target="chip" aria-label="{label}">
{label}
<input type="hidden" name="reviewers" value="{value}" />
<button type="button" data-phem-combobox-target="chipRemove" aria-label="Remove {label}">×</button>
</span>
</template>
<input data-phem-combobox-target="input" onChange={{ search }} aria-label="Reviewers" />
</div>
<div data-phem-combobox-target="popup" hidden>
<ul data-phem-combobox-target="list">
{% for person in people %}
<li key={{ person.id }} data-value={{ person.id }} data-label={{ person.name }} data-phem-combobox-target="item">{{ person.name }}</li>
{% endfor %}
</ul>
</div>
</div>
<button type="submit">Submit</button>
</form>After a click on Tom Cook, the browser shows the chip, and the item stays in the list while new results come in:
<span data-phem-combobox-target="chip" aria-label="Tom Cook" id="phem-combobox-choice-1" data-value="tom-cook" tabindex="-1">
Tom Cook
<input type="hidden" name="reviewers" value="tom-cook">
<button type="button" data-phem-combobox-target="chipRemove" aria-label="Remove Tom Cook" tabindex="-1">×</button>
</span>The chips that the browser made stay through the morph of an event, until the server renders a chip with the same data-value: the chip of the server replaces the chip of the browser. A server render that removes a chip with a data-value removes its value. A click on the chipRemove part of a chip, a second click on its item or a click on the clear part removes the value: the chip leaves, and so does an item that the browser kept in the list. The handler of the submit gets the values in $e->form['reviewers']: a string for one value, and a list for more than one. For a choice that the server saves at once, give the items an onClick handler and no multiple selection mode (see "Show a choice before the server answers").
The trigger and the inputGroup parts show the state of the popup. An Autocomplete (no selection) with a list of the browser, a trigger and an input group:
<div data-controller="phem-combobox" data-phem-combobox-filter-value="contains">
<div data-phem-combobox-target="inputGroup">
<input data-phem-combobox-target="input" aria-label="Color" />
<button type="button" data-phem-combobox-target="clear" aria-label="Clear">×</button>
<button type="button" data-phem-combobox-target="trigger" aria-label="Show the colors">▾</button>
</div>
<div data-phem-combobox-target="popup" hidden>
<ul id="colors" data-phem-combobox-target="list">
{% for name in colors %}
<li key={{ name }} id={{ 'color-' ~ name|lower }} data-phem-combobox-target="item">{{ name }}</li>
{% endfor %}
</ul>
</div>
</div>After the connect, with a closed popup, the browser shows:
<div data-phem-combobox-target="inputGroup" role="group">
<input data-phem-combobox-target="input" aria-label="Color" role="combobox" aria-autocomplete="list"
autocomplete="off" spellcheck="false" autocorrect="off" autocapitalize="none" aria-haspopup="listbox" aria-expanded="false">
<button type="button" data-phem-combobox-target="clear" aria-label="Clear" hidden>×</button>
<button type="button" data-phem-combobox-target="trigger" aria-label="Show the colors"
aria-haspopup="listbox" aria-expanded="false">▾</button>
</div>After ArrowDown in the input, the popup opens, and no item has the highlight:
<div data-phem-combobox-target="inputGroup" role="group" data-popup-open>
<input ... aria-haspopup="listbox" aria-controls="colors" aria-expanded="true" data-popup-open>
<button type="button" data-phem-combobox-target="trigger" aria-haspopup="listbox"
aria-expanded="true" aria-controls="colors" data-popup-open>▾</button>
</div>
<div data-phem-combobox-target="popup" data-open data-side="bottom" data-align="start" style="...">
<ul id="colors" data-phem-combobox-target="list" role="listbox">
<li id="color-blue" role="option">Blue</li>
<li id="color-green" role="option">Green</li>
<li id="color-red" role="option">Red</li>
</ul>
</div>A second ArrowDown gives the input aria-activedescendant="color-blue", and Blue gets data-highlighted. Enter then chooses Blue. With no highlight (for example after the user types "re" and moves the pointer out of the items), Enter closes the popup: the input and the trigger get aria-expanded="false", they lose aria-controls and data-popup-open, and the form submits. With the text "zz", the input gets data-list-empty.
Filter a list in the browser#
A short list that does not change, such as the fruits of a form, does not need a round trip for each letter. The server renders all the items one time, and the browser filters them. Set data-phem-combobox-filter-value to contains, starts-with or fuzzy:
#[Component]
final class FruitField
{
private const array FRUITS = ['Apple', 'Banana', 'Cherry', 'Éclair', 'Lemon'];
public function render(): Node
{
$fruits = self::FRUITS;
return (
<div data-controller="phem-combobox" data-phem-combobox-filter-value="contains">
<input data-phem-combobox-target="input" name="fruit" aria-label="Fruit" />
<div data-phem-combobox-target="popup" hidden>
<ul data-phem-combobox-target="list">
{% for name in fruits %}
<li key={{ name }} id={{ 'fruit-' ~ loop.index }} data-phem-combobox-target="item">{{ name }}</li>
{% endfor %}
</ul>
<p data-phem-combobox-target="empty">No fruit.</p>
</div>
</div>
);
}
}An item that does not match the text of the input gets hidden. The match ignores the case and the accents: "ecl" finds "Éclair". It uses Intl.Collator with the language of the page, or with data-phem-combobox-locale-value. The label of an item is its text, or its data-label attribute. Put the items in group parts, with a heading in a groupLabel part: the group gets role="group" and the name of its label, and a group with no visible item gets hidden too. The groupLabel part gets aria-hidden="true", so a screen reader reads the name of the group one time. data-phem-combobox-limit-value="10" shows only the first 10 matches. When the user types "ecl", the browser shows:
<input data-phem-combobox-target="input" role="combobox" aria-autocomplete="list" aria-expanded="true" ... value="ecl">
<ul data-phem-combobox-target="list" role="listbox">
<li id="fruit-1" role="option" hidden>Apple</li>
<li id="fruit-2" role="option" hidden>Banana</li>
<li id="fruit-3" role="option" hidden>Cherry</li>
<li id="fruit-4" role="option">Éclair</li>
<li id="fruit-5" role="option" hidden>Lemon</li>
</ul>With fuzzy, the characters of the text must be in the label in the same order, with other characters between them, as the MATCHES rank of match-sorter. The space is a character too. "nwy" finds "New York", and "ywn" does not. The items keep their order.
The data-keywords attribute of an item gives more keys to the filter, one on each line. Each key matches alone, in all the modes: the label and the keywords are not joined. A choice still writes the label in the input. A highlight target in an item shows the parts of its text that are the text of the user in mark elements, as the highlightText of the Base UI demos. Its data-phem-combobox-mark-class attribute gives the class of the mark elements. The match ignores the case and the accents, and uses the first 100 characters of the trimmed text. The highlight target also works with a list of the server, and the morph of an event keeps the marks:
<li id="fruit-lemon" data-phem-combobox-target="item" data-keywords="citrus
sour"><span data-phem-combobox-target="highlight" data-phem-combobox-mark-class="font-bold">Lemon</span></li>When the user types "sour", Lemon shows: the keyword matches. When the user types "em", the browser shows:
<li id="fruit-lemon" data-phem-combobox-target="item" role="option" data-keywords="citrus
sour"><span data-phem-combobox-target="highlight" data-phem-combobox-mark-class="font-bold">L<mark class="font-bold">em</mark>on</span></li>An element with a data-phem-combobox-text attribute is a text template: the browser gives it the text of the template at each render. {count} is the number of matches that the limit value hides, and {query} is the text of the input. The morph of an event keeps the text of the browser. Put a template in the more part of a status part, and in an empty part:
<div data-phem-combobox-target="status">
<span data-phem-combobox-status="more" hidden><span data-phem-combobox-text="Hiding {count} results (type a more specific query to narrow results)"></span></span>
</div>
<p data-phem-combobox-target="empty"><span data-phem-combobox-text='No results found for "{query}"'></span></p>With data-phem-combobox-limit-value="10", 25 fruits and no text, ArrowDown opens the popup, and the browser shows:
<div data-phem-combobox-target="status" role="status">
<span data-phem-combobox-status="more"><span data-phem-combobox-text="Hiding {count} results (type a more specific query to narrow results)">Hiding 15 results (type a more specific query to narrow results)</span></span>
</div>
<p data-phem-combobox-target="empty" role="status" aria-live="polite" aria-atomic="true"></p>When the user types "zzz", the more part gets hidden, and the empty part shows No results found for "zzz".
The list belongs to the client. A choice writes the label of the item in the input, and sends its input and change events, so a phem:bind or an onChange handler gets the value. The text of an input with no phem:bind stays through the morph of another event. An item can still have an onClick handler.
A popup that closes keeps its items and its highlight until its close animation ends, as in Base UI. For example, the user types "ap" and chooses Pineapple: the input shows "Pineapple", and the closing popup still shows the matches of "ap", with the highlight on Pineapple. The next open filters again: with the single selection mode, it shows all the items, with the highlight on Pineapple.
The mode value is the aria-autocomplete attribute of the input, as in the Autocomplete of Base UI:
mode |
The items | The input on ArrowDown and ArrowUp |
|---|---|---|
list (default) |
Filtered | Does not change |
both |
Filtered | Shows the label of the highlighted item |
inline |
Not filtered | Shows the label of the highlighted item |
none |
Not filtered | Does not change |
With both and inline, Escape closes the popup and gives back the text that the user typed. While the input shows the label of the highlighted item, an event sends the text that the user typed for a phem:bind input, not the label. With inline and none, the list is static: the filter value has no effect, and a choice still writes the label in the input.
While an event of the input runs (for example the search of the server), the input gets aria-busy="true" (see Show a loading state). The popup gets it too, so a screen reader waits for the new results.
Highlight, inline lists and grids#
These values change the highlight, as the props of the Autocomplete of Base UI:
| Value | Effect |
|---|---|
data-phem-combobox-auto-highlight-value="true" |
The first item gets the highlight when the user types a text, and when an arrow key opens the popup. The pointer that goes out of the items removes it |
data-phem-combobox-auto-highlight-value="always" |
An open list always has a highlight: the first item at first |
data-phem-combobox-keep-highlight-value="true" |
The highlight stays when the pointer goes out of the items |
data-phem-combobox-highlight-item-on-hover-value="false" |
The pointer does not move the highlight |
With no popup part, the list is inline: it is always open, and the input has aria-expanded="true". When the focus leaves the input, the list keeps its text, its filter and its highlight, as in Base UI. Use it for a command palette in a dialog. Give an item data-phem-dialog-target="close", and a choice (a click, or Enter) closes the dialog:
<div data-controller="phem-combobox" data-phem-combobox-filter-value="contains" data-phem-combobox-auto-highlight-value="always" data-phem-combobox-keep-highlight-value="true">
<input data-phem-combobox-target="input" aria-label="Search commands" />
<div data-phem-combobox-target="list">
<div data-phem-combobox-target="item" data-phem-dialog-target="close">New Window</div>
<div data-phem-combobox-target="item" data-phem-dialog-target="close">New Tab</div>
</div>
</div>After the connect, the first item has the highlight:
<input data-phem-combobox-target="input" role="combobox" aria-expanded="true" aria-activedescendant="..." ...>
<div data-phem-combobox-target="list" role="listbox">
<div data-phem-combobox-target="item" role="option" data-highlighted>New Window</div>
<div data-phem-combobox-target="item" role="option">New Tab</div>
</div>An inline list in a <dialog> starts again when the dialog closes, as Base UI unmounts the popup. The controller waits for the close event of the <dialog>. The phem-dialog controller calls close() at the end of its close animation, so the text, the filter and the highlight do not change while the dialog fades out, also when a close button takes the focus. Then the input gets its default value (its value attribute, or the label of the selection with a selectionMode), and it sends its input and change events when its text changes. The filter shows the items of that text, the highlight goes, and the list scrolls to its top. With autoHighlight="always", the first item gets the highlight again. A dialog that opens again before its close event keeps the state. For a phem:bind input, the server render gives the value attribute: reset the bound state in the server when the dialog closes. After the user types tab, Escape and a new open, the palette above shows:
<input data-phem-combobox-target="input" role="combobox" aria-expanded="true" aria-activedescendant="..." ...>
<div data-phem-combobox-target="list" role="listbox">
<div data-phem-combobox-target="item" role="option" data-highlighted>New Window</div>
<div data-phem-combobox-target="item" role="option">New Tab</div>
</div>With data-phem-combobox-grid-value="true", the list is a grid, for example an emoji picker. Put the items in row parts. The list gets role="grid", the input and the trigger aria-haspopup="grid", the rows role="row" and their items role="gridcell". With a highlight, ArrowLeft and ArrowRight move it to the item before or after it, and ArrowUp and ArrowDown to the item in the same column of the row above or below (the last item of a shorter row). ArrowUp on the first row gives the highlight back to the input. With no highlight, ArrowLeft and ArrowRight move the caret of the input.
With a filter of the browser (data-phem-combobox-filter-value), the visible items fill the rows again after each filter, as in Base UI: the visible items of each group part (or of the list) go into its rows in their order. Each row gets as many items as the first row of its group has in the server render. A row with no visible item gets hidden, so the grid has no empty row. An empty text puts each item back in its first row, and so does a morph, before it compares the rows with the server render. Before Turbo caches the page (turbo:before-cache), the items also go back, so the page from the cache has the rows of the server render. Put the items directly in the row parts.
Insert a choice into a text field
With data-phem-combobox-insert-into-value, a choice (a click, or Enter) inserts the data-value of the item into the text field (an input or a textarea) with this id. The value goes in at the caret of the field, in place of the selected text. The field gets its input and change events, the popup closes, and the search input gets no text. The field gets the focus, with the caret after the inserted text. The choice selects no item, and the controller still dispatches phem-combobox:select. Use it for an emoji picker, as the grid demo of Base UI:
<input id="message" aria-label="Message" />
<div data-controller="phem-combobox" data-phem-combobox-grid-value="true" data-phem-combobox-filter-value="contains" data-phem-combobox-insert-into-value="message">
<button type="button" data-phem-combobox-target="trigger" aria-label="Choose emoji">😀</button>
<div data-phem-combobox-target="popup" hidden>
<input data-phem-combobox-target="input" aria-label="Search emojis" />
<div data-phem-combobox-target="list">
<div data-phem-combobox-target="row">
<span data-phem-combobox-target="item" data-value="😀" data-label="grinning face">😀</span>
<span data-phem-combobox-target="item" data-value="😎" data-label="smiling face with sunglasses">😎</span>
</div>
</div>
</div>
</div>The user puts the caret after Hi in Hi !, searches sun, and presses ArrowDown and Enter. The message gets the emoji, and the caret is after it:
<input id="message" aria-label="Message" value="Hi 😎!">In phem/ui-base, the insertInto prop of Combobox and Autocomplete gives the value:
<Autocomplete id="emoji" grid filter="contains" insertInto="message">
<ComboboxTrigger aria-label="Choose emoji">😀</ComboboxTrigger>
<ComboboxPopup>
<ComboboxInput aria-label="Search emojis" />
<ComboboxList>
<ComboboxRow><ComboboxItem value="😀" label="grinning face">😀</ComboboxItem></ComboboxRow>
</ComboboxList>
</ComboboxPopup>
</Autocomplete>Sort a list with drag and drop#
phem-sortable moves the items of a list with the pointer (mouse, touch and pen) and with the keyboard. A drop calls the onCall handler of the list. Lists with the same group value share their items, for a board:
#[Component]
final class Board
{
#[State] public array $columns = ['todo' => ['a', 'b', 'c'], 'done' => ['d']];
public function move(Event $e): void
{
// The browser makes the detail: check it.
$card = $e->detail['id'] ?? null;
$index = $e->detail['index'] ?? null;
$to = $e->detail['list'] ?? null;
if (!\is_string($card) || !\is_int($index) || !\is_string($to) || !isset($this->columns[$to])) {
return;
}
foreach ($this->columns as $name => $cards) {
$this->columns[$name] = array_values(array_diff($cards, [$card]));
}
array_splice($this->columns[$to], max(0, min($index, \count($this->columns[$to]))), 0, [$card]);
}
public function render(): Node
{
return (
<div class="board">
{% for name, cards in columns %}
<ul key={{ name }} data-controller="phem-sortable" data-phem-sortable-group-value="board"
data-phem-sortable-list-value={{ name }} onCall={{ move }}>
{% for card in cards %}
<li key={{ card }} data-phem-sortable-target="item" data-phem-sortable-id={{ card }}>{{ card }}</li>
{% endfor %}
</ul>
{% endfor %}
</div>
);
}
}A press on an item and a move of 5 px start a drag. With handle parts in an item, only a press on a handle starts a drag. The item stays at its place while the pointer moves. A drop marker (a line) shows the drop place, in the list or in another list of the same group. On the release, the item moves one time to the drop place. When the user drops card b at the top of the done column, the browser dispatches a phem:call event on that list, and the handler gets this detail:
{"id": "b", "index": 0, "list": "done"}id is the data-phem-sortable-id of the item, index is its new position in the list (from 0), and list is the list value of the list. There is no marker and no event when the drop place is the place of the item. The click after a drop does not run the handler of the item. Escape cancels the drag: the marker goes, and the item stays at its place. During a press, the browser starts no native drag of an image or of a text in the item.
A touch on a handle starts a drag at once, and does not scroll the page. A touch on an item with no handle scrolls the page, as on a list with no drag. To drag that item, the user holds the touch for 250 ms, then moves it. Give a handle to the items when the user must scroll and drag a list often on a phone.
With the keyboard, Space or Enter on an item (or on its handle) takes it. A handle gets aria-pressed="true" while the keyboard holds its item. The arrow keys of the orientation value (vertical by default) move it in its list, and the other arrow keys move it to the list before or after in the group. Space or Enter drops it, and Escape or the blur cancels it: the item goes back. The controller gives tabindex="0" to an item (or to its handle) that cannot get the focus. A tabindex of your markup stays, also after the morph: give tabindex="-1" to an item that the keyboard must skip.
The moves use moveBefore() when the browser has it, so the item keeps its state (for example the focus and the values of its fields). The other browsers use insertBefore(), and the controller gives the focus back to the item. The new order shows at once. The morph after the event shows the order of the server render, so an item that the handler did not move goes back. A morph of a list during a drag cancels the drag.
During a drag, the item gets data-dragging, and the lists of the group get data-sorting. The list of the drop place gets data-drop. When the user drags card a between cards b and c, the browser shows:
<html data-phem-sorting="" style="--sortable-drop-x: 60px; --sortable-drop-y: 80px; --sortable-drop-width: 120px; --sortable-drop-height: 0px;">
…
<ul data-controller="phem-sortable" data-sorting="" data-drop="">
<li data-phem-sortable-target="item" data-dragging="">a</li>
<li data-phem-sortable-target="item">b</li>
<li data-phem-sortable-target="item">c</li>
</ul>phem.css draws the marker with the ::after of the data-drop list: a line with position: fixed. The --sortable-drop-* properties on the root give the center of the line in the viewport, and its length across the list (the other size is 0px). Change the color and the thickness of the line with custom properties, and style the dragged item and the lists:
:root { --sortable-drop-color: #16a34a; --sortable-drop-thickness: 3px; }
[data-dragging] { opacity: 0.6; }
[data-sorting] { outline: 2px dashed #94a3b8; }The rule of phem.css is in :where(), so a rule of your CSS on [data-drop]::after replaces it. In the forced colors mode, the line has the Highlight color. A position: fixed element uses an ancestor with a transform, a filter or a contain as its box: do not put these properties on a list or on its ancestors, or draw your own marker.
phem.css stops the text selection during a drag, and sets touch-action: none on the handles, so a touch drag on a handle does not scroll the page.
During a drag with the keyboard, a screen reader reads each step in the live region of the page. When the user takes card b, moves it to the done list and drops it, it reads:
Picked up b. Position 2 of 3 in todo.
b: position 1 of 2 in done.
Dropped b at position 1 of 2 in done.The text of a step goes to the live region 500 ms after the step, so the screen reader reads the focus of the moved item first. A new step in this time replaces the text. For a drag with the pointer, the screen reader reads the drop (only when the place changed) and the cancel.
The name of the item is its aria-label, the aria-label of its handle, or its text. The name of the list is its aria-label, or its list value, so give a list a label that a user knows, such as aria-label="To do". A list with no name has no in {list} part in the texts: "Picked up b. Position 2 of 3." Change the texts with the values of the list where the drag starts: picked-up-message, moved-message, dropped-message and cancelled-message. In a text, {item}, {position}, {count} and {list} get their values:
<ul data-controller="phem-sortable" aria-label="Tâches"
data-phem-sortable-picked-up-message-value="{item} pris, position {position} sur {count}.">Open a modal dialog#
phem-dialog opens a native <dialog> as a modal:
<div data-controller="phem-dialog">
<button data-phem-dialog-target="trigger">Edit</button>
<dialog data-phem-dialog-target="popup">
<h2 data-phem-dialog-target="title" id="edit-title">Edit the title</h2>
<input phem:bind={{ title }} />
<button onClick={{ save }} data-phem-dialog-target="close">Save</button>
<button data-phem-dialog-target="close">Cancel</button>
</dialog>
</div>A click on a trigger opens the dialog, and a click on a close part closes it. The browser gives the focus to the first element of the dialog that can have it. After an open with a touch, the focus goes to the popup, which gets tabindex="-1", so the keyboard of the device does not show for a first field. A drawer always gives the focus to its popup, as in Base UI, so no button of the drawer shows a focus ring. Put autofocus on an element of the drawer to give the focus to it. In a modal dialog (modal is true or trap-focus), Tab goes from the last element of the dialog to the first one, and Shift+Tab goes back: the focus does not go out of the dialog to the page or to the browser. This is also true from the checked radio of a last radio group, and from an element with tabindex="-1", such as a menu item. In a dialog with no element for Tab, Tab does nothing. While a modal dialog is open, the page does not scroll: phem.css gives the dialog and its ::backdrop overscroll-behavior: contain, so a scroll on them does not go to the page. The page keeps its scroll bar. Safari does not apply this rule to a dialog with no overflow, so in a browser with no closedby (Safari), the controller also sets overflow: hidden on <html>, with the width of the scroll bar as a padding, so the page does not move. Chrome 134 to 143 and Firefox 141 to 149 have closedby but do not apply the rule to a dialog with no overflow: in these old versions, the page can scroll under a modal dialog. Add phem:shortcut="mod+k" to a trigger to open the dialog with a key (see Add keyboard shortcuts). The open and close actions do the same from another element. The dialog gets aria-labelledby (the title part) and aria-describedby (a description part), and data-open or data-closed. A close waits for the animations of [data-ending-style], then calls close(). The trigger gets aria-haspopup="dialog", aria-expanded and data-popup-open, and aria-controls while the dialog is open. An aria-controls of the server render stays.
Escape and a tap on the backdrop close the dialog. Set data-phem-dialog-dismiss-value="false" to keep it open on Escape and on a tap on the backdrop. For an alert dialog, set data-phem-dialog-disable-pointer-dismissal-value="true": only Escape and the close parts close it. A modal dialog gets the closedby attribute from the dismiss value: closerequest, or none with dismiss false. With none, the browser ignores Escape and the back gesture of Android. A dialog that is not a modal gets none. In a browser with no closedby (Safari), the controller cancels the cancel event of Escape. A browser does not let a page cancel a second Escape with no user action between them (a click or a key): the dialog closes at once, with no close animation, and the state is false. The server can open it with data-phem-dialog-open-value={{ editing }}. A dialog that closes in another way, such as a <form method="dialog">, also sets the state.
A tap on the backdrop is a press and a click on the dialog itself, at a point outside the box of the dialog. The press does not move the focus, so the trigger gets the focus back after the close, with its focus ring after an open with the keyboard. A click on the padding of the dialog is inside the box, so the dialog stays open. A text selection that starts in the dialog and ends on the backdrop also keeps the dialog open. The controller listens for the tap itself, in all the browsers: Safari does not support closedby="any", and with any the browser and the controller would both close the dialog (a confirmClose dialog would open two times). So a modal never gets closedby="any".
A dialog in an open dialog gets data-nested. Each dialog around it gets data-nested-dialog-open and the --nested-dialogs CSS variable (the number of open dialogs in it, at all levels), for a stack of dialogs. With three open dialogs, the first one gets 2 and the second one gets 1. The count changes when the close of a dialog starts, so the dialogs behind change with its close animation. The dialog in it gets its own --nested-dialogs (0 when no dialog is open in it), so it does not inherit the value of the dialog around it:
dialog[data-nested-dialog-open] { scale: calc(1 - 0.05 * var(--nested-dialogs)); }| Value | Default | Effect |
|---|---|---|
open |
false |
The state: the dialog is open |
dismiss |
true |
Escape and a tap on the backdrop close the dialog |
disable-pointer-dismissal |
false |
A tap on the backdrop, or a press outside a dialog that is not a modal, does not close the dialog |
modal |
true |
true: showModal(), the page is inert. false: show(), the page stays active, and Escape and a press outside close the dialog. trap-focus: as false, and Tab stays in the dialog |
swipe-direction |
none | The dialog is a drawer: up, down, left or right |
snap-points |
none | The heights where a drawer stops (see Stop a drawer at snap points) |
snap-point |
the first one | The state: the snap point of the drawer |
snap-to-sequential-points |
false |
A fast swipe does not skip a snap point |
Open a dialog from several triggers#
A dialog can have several triggers, as a popover (see Open one popover from several triggers): the trigger parts, and the elements anywhere in the page with data-phem-dialog-for and the id of the controller element. An item of a menu can be a detached trigger: the click closes the menu and opens the dialog. The trigger that opened the dialog gets aria-expanded="true", the others get "false", and the trigger-id value gets its id. The payload part shows the data-phem-dialog-payload text of this trigger:
<button data-phem-dialog-for="details" data-phem-dialog-payload="Dialog 1">Open 1</button>
<button data-phem-dialog-for="details" data-phem-dialog-payload="Dialog 2">Open 2</button>
<div id="details" data-controller="phem-dialog">
<dialog data-phem-dialog-target="popup">
<h2 data-phem-dialog-target="title"><span data-phem-dialog-target="payload"></span></h2>
<button data-phem-dialog-target="close">Close</button>
</dialog>
</div>After a click on Open 2, the title is Dialog 2. The server opens the dialog for a trigger with data-phem-dialog-open-value="true" and data-phem-dialog-trigger-id-value, and the open action takes a trigger-id param.
Ask before the close#
Set the confirm-close value to the id of another phem-dialog element in the popup, for example an alert dialog. When the user changed a field of the popup (its value is not its value of the server render), a request to close opens this dialog, and the first dialog stays open. A request to close is Escape, a tap on the backdrop, a close part, and the close action. A click on a [data-phem-dialog-discard] element of the second dialog closes the two dialogs, and the fields get the values of the server render again:
<div data-controller="phem-dialog" data-phem-dialog-confirm-close-value="discard">
<button data-phem-dialog-target="trigger">Tweet</button>
<dialog data-phem-dialog-target="popup">
<form method="dialog">
<textarea required></textarea>
<button data-phem-dialog-target="close" type="button">Cancel</button>
<button>Tweet</button>
</form>
<div id="discard" data-controller="phem-dialog" data-phem-dialog-disable-pointer-dismissal-value="true">
<dialog data-phem-dialog-target="popup" role="alertdialog">
<button data-phem-dialog-target="close">Go back</button>
<button data-phem-dialog-target="close" data-phem-dialog-discard>Discard</button>
</dialog>
</div>
</dialog>
</div>With no changed field, the dialog closes at once. A <form method="dialog"> closes the dialog with no question.
Choose the focus#
The browser gives the first focus to the [autofocus] element of the dialog, or else to its first element that can have the focus. As in Base UI, this first focus does not scroll: the dialog and the content of the popup start at the top, also when the element is lower than the screen or when a start transition moves the popup out of the screen. After the close, the browser gives the focus back to the element that had it before the open. Set the final-focus value to a CSS selector to give it to another element: data-phem-dialog-final-focus-value="#next-step".
Add a backdrop and a viewport#
The ::backdrop of a modal <dialog> covers the page. For a backdrop with its own animation, add a backdrop part: an element outside the dialog. It gets hidden, data-open or data-closed, and the animations of the conventions, at the same time as the dialog.
For a popup that a box places on the screen (Base UI Dialog.Viewport), make the <dialog> the viewport part, and put the popup part in it. The controller opens the viewport, and the viewport and the popup get the open state. A press on the viewport outside the popup is a tap on the backdrop. The close waits for the animations of the popup:
<div data-controller="phem-dialog">
<button data-phem-dialog-target="trigger">Edit</button>
<div class="backdrop" data-phem-dialog-target="backdrop" hidden></div>
<dialog class="viewport" data-phem-dialog-target="viewport">
<div class="popup" data-phem-dialog-target="popup">
<h2 data-phem-dialog-target="title">Edit</h2>
...
</div>
</dialog>
</div>After a click on Edit, the browser shows:
<div class="backdrop" data-phem-dialog-target="backdrop" data-open></div>
<dialog class="viewport" data-phem-dialog-target="viewport" open data-open>
<div class="popup" data-phem-dialog-target="popup" data-open>...</div>
</dialog>A title or a description with no id gets its link with the element references of ARIA (ariaLabelledByElements and ariaDescribedByElements), so a screen reader names the dialog.
Close a drawer with a swipe#
Set the swipe-direction value (up, down, left or right) to make the dialog a drawer that closes with a swipe in that direction:
<div data-controller="phem-dialog" data-phem-dialog-swipe-direction-value="down">
<button data-phem-dialog-target="trigger">Filters</button>
<dialog data-phem-dialog-target="popup" class="sheet">...</dialog>
</div>A press on the popup and a move of 5 px in the direction start a swipe. The popup gets data-swipe-direction (the direction of the value), data-swiping during the swipe, the distance in --drawer-swipe-movement-x or --drawer-swipe-movement-y, and the part of the way to the close in --drawer-swipe-progress (from 0 to 1). A move in the other direction has a resistance. A release after half the size of the popup, or after a fast swipe, closes it. A shorter swipe gives the popup its place again. After the release, --drawer-swipe-strength (from 0.1 to 1) is smaller for a faster swipe: multiply the duration of the transition with it. Use the variables in your CSS:
.sheet {
margin: auto 0 0;
width: 100%;
transform: translateY(var(--drawer-swipe-movement-y, 0px));
transition: transform calc(var(--drawer-swipe-strength, 1) * 300ms);
}
.sheet[data-swiping] { transition: none; }
.sheet[data-starting-style], .sheet[data-ending-style] { transform: translateY(100%); }
.sheet::backdrop { opacity: calc(1 - var(--drawer-swipe-progress, 0)); }A mouse press in a content part (data-phem-dialog-target="content", around the text of the drawer) selects its text and does not start a swipe. A press on an element with data-phem-swipe-ignore never starts a swipe, also in a toast. It also does not start the drag of a phem-sortable item.
The popup that a swipe closed gets data-swipe-dismiss until the end of its [data-ending-style] transition, so the transition goes on from its place. A drawer in an open drawer makes the drawer around it get data-nested-drawer-open and --nested-drawers, as data-nested-dialog-open and --nested-dialogs. During the swipe of that drawer, the drawer around it also gets data-nested-drawer-swiping, and only the drawer in it follows the pointer. The popup of each drawer gets --drawer-frontmost-height: the height of the last open drawer in it, or else its own height. Use it to give the drawer behind the size of the drawer in front, as the nested drawers of Base UI: .drawer[data-nested-drawer-open] { height: var(--drawer-frontmost-height); }. While a drawer in it is open or closes, and while it closes, the popup of a drawer also gets --drawer-height: its own height in px. A transition of height cannot start or end at auto, so use height: var(--drawer-height, auto): the drawer behind gets its own height back with an animation when the drawer in front closes. A drawer in a drawer gets --drawer-height: initial when it keeps no height, so it does not inherit the height of the drawer around it. Put each popup in a viewport (viewport target), so that auto is the height of its content. A press on a field, a button, a link or a label does not start a swipe. In content that scrolls (overflow: auto or scroll), the swipe starts only when the content is at its start: the list of a bottom sheet scrolls up first. A touch needs no touch-action: a touch that moves in the direction of the swipe swipes the dialog, and a touch that moves the other way scrolls the content of the dialog.
Stop a drawer at snap points#
A bottom sheet can stop at more than one height. Set the snap-points value to a list of heights: a number from 0 to 1 is a part of the height of the viewport, a larger number is px, and a string can have px or rem. The drawer opens at the first snap point:
<div data-controller="phem-dialog" data-phem-dialog-swipe-direction-value="down" data-phem-dialog-snap-points-value={{ ['148px', 1] }}>
<button data-phem-dialog-target="trigger">Details</button>
<dialog class="sheet" data-phem-dialog-target="popup">...</dialog>
</div>The popup gets --drawer-snap-point-offset: the distance from its full height to the snap point. It gets --drawer-height (its height), and data-expanded at its full height. Add the offset to the transform:
.sheet { transform: translateY(calc(var(--drawer-snap-point-offset, 0px) + var(--drawer-swipe-movement-y, 0px))); }A swipe moves the drawer up and down from its snap point. The release stops it at the nearest snap point, or closes it when the close is nearer. A fast swipe goes to the next snap point in its direction, unless data-phem-dialog-snap-to-sequential-points-value="true". The snap-point value is the state: it keeps the snap point through the morph, and a new snap point dispatches phem-dialog:snap with the snapPoint in the detail. Snap points apply to a drawer with the up or down direction. A resize of the viewport (a rotation, the keyboard of a phone) places the snap point again. Below its full height, a swipe in the other direction opens the drawer. At its full height, or on content that can scroll in that direction, a touch scrolls the content. After a swipe up to the full height of a sheet of 400 px, the browser shows:
<div data-controller="phem-dialog" data-phem-dialog-snap-point-value="1" ...>
<dialog class="sheet" data-phem-dialog-target="popup" open data-open data-swipe-direction="down" data-expanded
style="--drawer-height: 400px; --drawer-snap-point-offset: 0px;">...</dialog>
</div>Open a drawer with a swipe from the edge#
A swipeArea part is an area at the edge of the screen: a swipe away from the edge opens the drawer, and the popup follows the pointer.
<div data-controller="phem-dialog" data-phem-dialog-swipe-direction-value="right" data-phem-dialog-modal-value="false">
<div class="swipe-area" data-phem-dialog-target="swipeArea"></div>
<dialog class="drawer" data-phem-dialog-target="popup">...</dialog>
</div>A press on the area and a move of 5 px away from the edge (to the left for a drawer on the right) open the drawer. During the swipe, the popup gets data-swiping, the distance to its open place in --drawer-swipe-movement-x or --drawer-swipe-movement-y, and --drawer-swipe-progress (1 at the edge, 0 when it is open). A release after half the size of the popup, or after a fast swipe, leaves it open. A shorter swipe closes it again. The same CSS as for a swipe that closes the drawer animates it.
During a swipe, the controller dispatches phem-dialog:swipe with the progress in the detail. The viewport and the backdrop parts also get --drawer-swipe-progress, so the ::backdrop of a viewport can fade with the swipe. The controller dispatches phem-dialog:open when the dialog opens and phem-dialog:close when it closes.
Indent the page behind a drawer#
phem-drawer-provider makes the page smaller while a drawer in it is open, as the Provider, the Indent and the IndentBackground of the Base UI drawer:
<div data-controller="phem-drawer-provider">
<div class="indent-background" data-phem-drawer-provider-target="background"></div>
<div class="indent" data-phem-drawer-provider-target="indent">
<!-- the page, with drawers -->
</div>
</div>While a drawer (a phem-dialog with a swipe-direction value) in the element is open, the indent and background parts get data-active, else data-inactive. They get --drawer-swipe-progress: 0, and during the swipe of a drawer the part of the way to its close (from 0 to 1). A dialog that is not a drawer does not change them. After a click on a trigger, the browser shows:
<div class="indent" data-phem-drawer-provider-target="indent" data-active style="--drawer-swipe-progress: 0;">...</div>Use them in the CSS:
.indent { transition: transform 0.4s; transform-origin: center top; }
.indent[data-active] { transform: scale(calc(0.98 + 0.02 * var(--drawer-swipe-progress))) translateY(calc(0.5rem * (1 - var(--drawer-swipe-progress)))); }Lift the fields above the keyboard of a phone#
Set data-phem-dialog-virtual-keyboard-value="true" on a bottom sheet with fields, as the VirtualKeyboardProvider of the Base UI drawer. While the dialog is open and the keyboard of the device covers the bottom of the screen (window.visualViewport), its popup gets the height of the keyboard in --drawer-keyboard-inset, and the field with the focus scrolls into view. The variable goes when the keyboard closes, so always give it a fallback:
.footer:focus-within { padding-bottom: calc(1rem + var(--drawer-keyboard-inset, 0px)); }When a drawer opens, the browser gives the first focus to an element of the drawer, and scrolls the viewport and the content to it. A drawer then gives the focus to its popup, and scrolls them back to the top, so a long bottom sheet opens at its start.
Switch between tabs#
phem-tabs shows the panel of the selected tab:
<div data-controller="phem-tabs" data-phem-tabs-active-value="profile">
<div data-phem-tabs-target="list" aria-label="Settings">
<button data-phem-tabs-target="tab" data-value="profile">Profile</button>
<button data-phem-tabs-target="tab" data-value="security">Security</button>
<span data-phem-tabs-target="indicator"></span>
</div>
<div data-phem-tabs-target="panel" data-value="profile">...</div>
<div data-phem-tabs-target="panel" data-value="security" hidden>...</div>
</div>The value of a tab and of a panel is its data-value attribute, its key (key={{ ... }}), or its position. Each tab shows the panel with the same value. The active value is the state: the value of the selected tab. Set it to select a tab on load, and a handler can set it in a new render. With no active value, or a value that no tab has, the first tab that is not disabled is selected. A click selects a tab, and the active value gets its value. The arrow keys move the focus between the tabs, and Enter or Space selects the tab with the focus, also on a tab that is not a <button>. With data-phem-tabs-activate-on-focus-value="true", the focus selects the tab at once. Home and End go to the first and to the last tab, and the arrow keys skip a disabled tab. From the last tab, the arrow key goes to the first tab, unless data-phem-tabs-loop-focus-value="false". With data-phem-tabs-orientation-value="vertical", the Up and Down keys move between the tabs in place of Left and Right. An arrow key with Shift does not move the focus. One tab is in the Tab order: the tab with the last focus, or the selected tab. It stays after the morph of an event.
A tab is not a link. Tabs show panels on the same page. For a bar of links to other pages, do not use phem-tabs or the tab roles: use a <nav> with links, and give the link of the current page aria-current="page". This needs no controller. For a bar of links with menus, use phem-navigation-menu:
<nav aria-label="Settings">
<a href="/settings/profile" aria-current="page">Profile</a>
<a href="/settings/security">Security</a>
</nav>Style the current link with [aria-current="page"].
The controller sets the ARIA roles and attributes. A tab and its panel need no id: the controller links them with the element references of ARIA (ariaControlsElements and ariaLabelledByElements), so a screen reader names the panel with its tab. It gives them no id, because the morph must match each element to the server render. When the tab and the panel have an id, they get aria-controls and aria-labelledby. After a click on Security, the browser shows:
<div data-controller="phem-tabs" data-phem-tabs-active-value="security" data-orientation="horizontal" data-activation-direction="right">
<div data-phem-tabs-target="list" role="tablist" ...>
<button data-value="profile" role="tab" aria-selected="false" tabindex="-1" ...>Profile</button>
<button data-value="security" role="tab" aria-selected="true" tabindex="0" data-active ...>Security</button>
<span data-phem-tabs-target="indicator" style="--active-tab-left: ...; --active-tab-width: ...; ..."></span>
</div>
<div data-value="profile" role="tabpanel" data-index="0" data-hidden hidden tabindex="0" ...>...</div>
<div data-value="security" role="tabpanel" data-index="1" tabindex="0" ...>...</div>
</div>A panel with no element that gets the focus (a link, a button, a field) gets tabindex="0", so Tab goes from the tab list to the text of the panel. A panel with such an element gets no tabindex: Tab goes to the first element in it. The controller checks the panels again after the morph of an event. A tabindex of the markup stays.
With data-phem-tabs-hidden-until-found-value="true", a hidden panel has hidden="until-found": the search of the browser (Ctrl+F) finds its text and selects its tab (the beforematch event). The found panel shows with no animation and gets data-instant, because the browser scrolls to the text. To do this for one panel only, add data-phem-tabs-hidden-until-found to the panel. A browser with no support for hidden="until-found" hides the panel as hidden does, and the search does not find its text:
<div data-value="security" role="tabpanel" data-index="1" data-hidden hidden="until-found" ...>...</div>All the parts get data-orientation and data-activation-direction (the direction of the last change: left, right, up, down or none). The selected tab gets data-active, and a hidden panel gets data-hidden. A panel also gets data-index, and the animations of the conventions.
The indicator part gets the box of the selected tab in the list, for a line under the selected tab that moves:
[data-phem-tabs-target="indicator"] {
position: absolute;
left: var(--active-tab-left);
width: var(--active-tab-width);
bottom: 0;
height: 2px;
transition: left 200ms, width 200ms;
}The variables are --active-tab-left, --active-tab-top, --active-tab-right, --active-tab-bottom, --active-tab-width and --active-tab-height. The values keep the fractions of a pixel. They start inside the border of the list, so a border on the list does not move the line. The controller reads the border width from clientLeft and clientTop, which are whole pixels. In a list with a border of 2px, a selected tab that starts 60.5px from the outer edge of the list and is 80.5px wide gives:
<span data-phem-tabs-target="indicator" style="--active-tab-left: 58.5px; --active-tab-width: 80.5px; ..."></span>After a click on Profile and ArrowRight, the focus is on Security and Profile stays selected. The tab stop is on Security, also after the morph of an event:
<div data-phem-tabs-target="list" role="tablist" ...>
<button data-value="profile" role="tab" aria-selected="true" tabindex="-1" data-active ...>Profile</button>
<button data-value="security" role="tab" aria-selected="false" tabindex="0" ...>Security</button>
...
</div>Toggle a button, a switch or a checkbox#
phem-toggle gives a pressed or checked state to a toggle button, a switch or a checkbox:
<button type="button" data-controller="phem-toggle" aria-label="Bold">B</button>
<label>
<span role="switch" data-controller="phem-toggle">
<span data-phem-toggle-target="indicator"></span>
<input type="checkbox" hidden phem:bind={{ alerts }} data-phem-toggle-target="input" />
</span>
Alerts
</label>
<label>
<span role="checkbox" data-controller="phem-toggle" data-phem-toggle-name-value="terms">
<span data-phem-toggle-target="indicator">✓</span>
<input type="checkbox" hidden data-phem-toggle-target="input" />
</span>
I accept the terms
</label>The role of the element gives its kind. An element with role="switch" or role="checkbox" is a switch or a checkbox: it gets aria-checked, and data-checked or data-unchecked. Another element is a toggle button: it gets aria-pressed, and data-pressed when it is pressed. An element that is not a <button> gets role="button" when it has no role, and tabindex="0" when it has no tabindex. Space and Enter toggle a toggle button and a switch, as a button. Only Space toggles a checkbox.
The checked value is the state (a pressed toggle button is checked). The server render gives the first value, and a server render with a new value sets it. With no checked value, the first state comes from the checked attribute of the input, or from aria-checked or aria-pressed. The state belongs to the client, so it stays after the morph of an event.
| Value | Default | Effect |
|---|---|---|
checked |
false |
The state. Base UI checked/defaultChecked, pressed/defaultPressed |
indeterminate |
false |
A checkbox in a mixed state: aria-checked="mixed", data-indeterminate. A click checks it |
disabled |
false |
Stops the changes by the user. Gives data-disabled and aria-disabled="true". The disabled attribute of a <button> also gives data-disabled |
read-only |
false |
Stops the changes by the user. Gives data-readonly and aria-readonly="true" |
required |
false |
Gives data-required, aria-required="true", and required on the input. A required checkbox input that is not checked stops the submit of its form |
name, form |
The name and the form of the input |
|
value |
on |
The value that the input sends when it is checked |
unchecked-value |
The value that an <input type="hidden"> sends when it is not checked |
|
keep-mounted |
false |
The indicator of a checkbox does not get hidden when the checkbox is not checked |
| Key | Effect |
|---|---|
| Space | Toggles the state |
| Enter | Toggles a toggle button (a <button> clicks on Enter). It does not toggle a switch or a checkbox |
A click toggles the state. A click on a <label> around the element, or on a <label for> the input, also toggles it, one time. The element gets the focus.
A <label> names only a native control, so a <span role="checkbox"> in a label has no name of its own. The controller links the element to the <label> around it with aria-labelledby: with the id of the label, or with an element reference (ariaLabelledByElements) when the label has no id. Then a screen reader reads "I accept the terms, checkbox". An aria-label or an aria-labelledby of the markup stays. A <button> gets no link: a label names a button. The indicator parts of a switch and of a checkbox get aria-hidden="true" (when they have no aria-hidden), so the check mark is not in the name: aria-checked gives the state.
The input target (or the first checkbox in the element) is the field of a form:
- A checkbox input gets the state in
checked. Hide it withhiddenor a visually hidden class. - An
<input type="hidden">gets thevaluevalue when it is checked, and theunchecked-valuevalue when it is not checked. With nounchecked-value, it is disabled when it is not checked, so a form does not send it, as a native checkbox. - The input gets
tabindex="-1"andaria-hidden="true"when it has none. - A checkbox input with
requiredthat is not checked is invalid, so it stops the submit of its form. The browser does not validate an<input type="hidden">: use a checkbox input for a required switch or checkbox. phem-formandphem-fieldgive the focus to the element, not to the hidden input. See Validate a switch, a checkbox or a radio group.- When the user changes the state, the input dispatches
inputandchange, sophem:bind,phem:liveand the other controllers see the change. - An input with
phem:bindbelongs to phem: after the morph of an event, its value (thecheckedof a checkbox, thevalueof a hidden input) gives the state.
The indicator targets (the check mark of a checkbox, the thumb of a switch) get the same data attributes as the element. The indicator of a checkbox shows only when the checkbox is checked or indeterminate, with the animations of the conventions (data-open, data-closed, data-starting-style, data-ending-style). The indicator of a switch and of a toggle button always shows.
Each change by the user dispatches phem-toggle:change with { checked, indeterminate, value }. After a click on the checkbox and on the toggle button, the browser shows:
<label>
<span role="checkbox" data-controller="phem-toggle" data-phem-toggle-name-value="terms" data-phem-toggle-checked-value="true" tabindex="0" aria-labelledby="" aria-checked="true" data-phem-toggle-indeterminate-value="false" data-checked="">
<span data-phem-toggle-target="indicator" data-checked="" aria-hidden="true" data-open="" data-starting-style="">✓</span>
<input type="checkbox" hidden="" data-phem-toggle-target="input" name="terms" tabindex="-1" aria-hidden="true">
</span>
I accept the terms
</label>
<button type="button" data-controller="phem-toggle" aria-label="Bold" data-phem-toggle-checked-value="true" aria-pressed="true" data-phem-toggle-indeterminate-value="false" data-pressed="">B</button>A phem-checkbox-group and a phem-toggle-group set the state of their phem-toggle items. See Select all the rows and Press one button of a group.
Press one button of a group#
phem-toggle-group gives one state to a group of toggle buttons, for example the alignment buttons of a text editor:
<div data-controller="phem-toggle-group" data-phem-toggle-group-value-value='["left"]' aria-label="Alignment">
<button type="button" data-phem-toggle-group-target="item" data-value="left">Left</button>
<button type="button" data-phem-toggle-group-target="item" data-value="center">Center</button>
<button type="button" data-phem-toggle-group-target="item" data-value="right">Right</button>
</div>The value of an item is its data-value attribute, the value value of a phem-toggle item, its key (key={{ ... }}), or its position. The value value is the state: the values of the pressed items. The server render gives the first value, and a server render with a new value sets it. The state belongs to the client, so it stays after the morph of an event.
| Value | Default | Effect |
|---|---|---|
value |
[] |
The values of the pressed items |
multiple |
false |
More than one item can be pressed. With no multiple, a pressed item releases the other items |
orientation |
horizontal |
horizontal (Left and Right move the focus) or vertical (Up and Down) |
loop-focus |
true |
The arrow key on the last item goes to the first item |
disabled |
false |
No item changes. Gives data-disabled and aria-disabled="true" |
| Key | Effect |
|---|---|
| ArrowLeft, ArrowRight (or ArrowUp, ArrowDown when vertical) | Moves the focus to the previous or the next item, and skips a disabled item |
| Home, End | Moves the focus to the first or the last item |
| Space, Enter | Presses or releases the item with the focus (a <button> clicks on them) |
A click on an item presses it, and a click on a pressed item releases it. The items have a roving focus: one item is in the tab order (the item with the last focus, the first pressed item, or the first item that is not disabled).
In a phem-toolbar, make the group a group part of the toolbar, and its items also item parts of the toolbar. Then the toolbar moves the focus with the arrow keys, and gives the tab order and data-orientation (the orientation value of the toolbar, not of the group). The focus goes from the last item of the group to the next item of the toolbar. The group keeps the pressed state:
<div data-controller="phem-toolbar" aria-label="Editor">
<div data-controller="phem-toggle-group" data-phem-toolbar-target="group" data-phem-toggle-group-value-value='["start"]' aria-label="Justify">
<button type="button" data-phem-toggle-group-target="item" data-phem-toolbar-target="item" data-value="start">Start</button>
<button type="button" data-phem-toggle-group-target="item" data-phem-toolbar-target="item" data-value="end">End</button>
</div>
<button type="button" data-phem-toolbar-target="item">Bold</button>
</div>ArrowRight on End moves the focus to Bold, and ArrowRight on Bold goes back to Start. One item of the toolbar is in the tab order: the item with the last focus, or the first item. In a toolbar, Home and End do not move the focus.
An item can be a phem-toggle button, as a Base UI Toggle in a ToggleGroup:
<div data-controller="phem-toggle-group" data-phem-toggle-group-multiple-value="true" aria-label="Style">
<button type="button" data-controller="phem-toggle" data-phem-toggle-group-target="item" data-value="bold">Bold</button>
<button type="button" data-controller="phem-toggle" data-phem-toggle-group-target="item" data-value="italic">Italic</button>
</div>The group owns the state of a phem-toggle item: it sets the checked value of the toggle, and the toggle gives it aria-pressed and data-pressed. Do not give a checked value to an item. A click on the item changes its state and dispatches phem-toggle:change, then the group changes the state of the other items. A disabled group stops the click before the toggle gets it. A phem-toggle item with the disabled value is a disabled item. The group has no form field: use the input of a phem-toggle item, or phem:bind on a hidden input.
The group gets role="group" when it has no role, data-orientation and data-multiple. The items get aria-pressed, data-pressed, data-orientation, tabindex, and data-disabled when the group or the item is disabled. Each change by the user dispatches phem-toggle-group:change with { value }. After a click on Center, the browser shows:
<div data-controller="phem-toggle-group" data-phem-toggle-group-value-value="["center"]" aria-label="Alignment" role="group" data-orientation="horizontal">
<button type="button" data-phem-toggle-group-target="item" data-value="left" aria-pressed="false" data-orientation="horizontal" tabindex="-1">Left</button>
<button type="button" data-phem-toggle-group-target="item" data-value="center" aria-pressed="true" data-orientation="horizontal" tabindex="0" data-pressed="">Center</button>
<button type="button" data-phem-toggle-group-target="item" data-value="right" aria-pressed="false" data-orientation="horizontal" tabindex="-1">Right</button>
</div>Group controls in a toolbar#
phem-toolbar gives a row of controls one tab stop. The arrow keys move the focus between the items:
<div data-controller="phem-toolbar" aria-label="Format">
<div data-phem-toolbar-target="group" aria-label="Alignment">
<button type="button" data-phem-toolbar-target="item">Left</button>
<button type="button" data-phem-toolbar-target="item">Center</button>
</div>
<div data-phem-toolbar-target="separator"></div>
<button type="button" data-phem-toolbar-target="item" aria-disabled="true" onClick={{ bold }}>Bold</button>
<a href="/help" data-phem-toolbar-target="item">Help</a>
<input data-phem-toolbar-target="item" aria-label="Zoom" value="100" />
</div>An item is a button, a link, an input, or another element that can get the focus. A group part holds some of the items, and a separator part divides them. A group can be a phem-toggle-group: see Press one button of a group. Only one item is in the tab order: the item that had the focus last, else the first item. The tab stop stays after the morph of an event.
| Value | Default | Description |
|---|---|---|
orientation |
horizontal |
horizontal or vertical: the arrow keys that move the focus. |
loopFocus |
true |
From the last item, the arrow key goes to the first item. |
disabled |
false |
Disables all the items. |
| Key | Action |
|---|---|
| ArrowRight, ArrowLeft | Move the focus to the next or to the previous item (horizontal). In a right-to-left page, the keys change places. |
| ArrowDown, ArrowUp | Move the focus to the next or to the previous item (vertical). |
Home and End do not move the focus, as in Base UI. In a text input, Home, End, ArrowLeft and ArrowRight move the caret. ArrowLeft and ArrowRight move the focus only when the caret is at the start or at the end of the text.
A disabled item stays in the focus order, so a keyboard user can find it. A click on it does nothing, and an input takes no text. Give the item aria-disabled="true", or give its group aria-disabled="true", or set the disabled value. An item with the native disabled attribute does not get the focus.
The controller sets the ARIA roles. The toolbar gets role="toolbar" and aria-orientation, a group gets role="group", and a separator gets role="separator" and the other orientation in aria-orientation. All the parts get data-orientation (a separator gets the other orientation). A disabled item gets data-disabled, and data-focusable when it stays in the focus order. The toolbar and a disabled group get data-disabled.
After the arrow keys move the focus to the input, the browser shows:
<div data-controller="phem-toolbar" aria-label="Format" role="toolbar" aria-orientation="horizontal" data-orientation="horizontal">
<div data-phem-toolbar-target="group" aria-label="Alignment" role="group" data-orientation="horizontal">
<button type="button" data-phem-toolbar-target="item" data-orientation="horizontal" tabindex="-1">Left</button>
<button type="button" data-phem-toolbar-target="item" data-orientation="horizontal" tabindex="-1">Center</button>
</div>
<div data-phem-toolbar-target="separator" role="separator" aria-orientation="vertical" data-orientation="vertical"></div>
<button type="button" data-phem-toolbar-target="item" aria-disabled="true" data-orientation="horizontal" data-disabled data-focusable tabindex="-1" ...>Bold</button>
<a href="/help" data-phem-toolbar-target="item" data-orientation="horizontal" tabindex="-1">Help</a>
<input data-phem-toolbar-target="item" aria-label="Zoom" value="100" data-orientation="horizontal" tabindex="0">
</div>Choose one option in a radio group#
phem-radio-group makes a group of radio buttons from elements that are not native inputs, for a radio with its own style:
<div data-controller="phem-radio-group" data-phem-radio-group-value-value={{ apple }} aria-label="Apple">
<label>
<span data-phem-radio-group-target="radio" data-value="fuji">
<span data-phem-radio-group-target="indicator"></span>
</span>
Fuji
</label>
<label>
<span data-phem-radio-group-target="radio" data-value="gala">
<span data-phem-radio-group-target="indicator"></span>
</span>
Gala
</label>
<input type="hidden" name="apple" data-phem-radio-group-target="input" phem:bind={{ apple }} phem:live />
</div>The value of a radio is its data-value attribute, its key (key={{ ... }}), or its position. The value value is the state: the value of the checked radio. With no value value, no radio is checked. The server render gives its first value, and a handler can set a new value in a new render. The checked radio stays after the morph of an event.
| Value | Default | Description |
|---|---|---|
value |
none | The value of the checked radio. |
disabled |
false |
Disables all the radios. |
readOnly |
false |
The arrow keys move the focus, but the user cannot check a radio. |
required |
false |
Gives the group aria-required, and required to the inputs. |
| Key | Action |
|---|---|
| ArrowDown, ArrowRight | Move the focus to the next radio and check it. From the last radio, go to the first radio. |
| ArrowUp, ArrowLeft | Move the focus to the previous radio and check it. |
| Space | Check the radio with the focus. |
A click on a radio, or on a <label> around it, checks it. The label also names the radio: the controller links them with ariaLabelledByElements, and gives them no id. The arrow keys skip a disabled radio: a radio with disabled or aria-disabled="true". Only one radio is in the tab order: the checked radio, else the first radio that is not disabled.
The input part keeps the value for a form. A hidden input gets the value of the checked radio. A native radio input (type="radio") is checked when its value is the value of the checked radio. When the user checks a radio, the input that changes sends input and change, so phem:bind and phem:live see the change. The controller also dispatches phem-radio-group:change, with the value in its detail.
The browser does not validate an <input type="hidden">. For a required group, use an input that the browser validates and that the user does not see, such as <input hidden>. With no checked radio, its value is empty, so it stops the submit of its form. phem-form and phem-field give the focus to the radio of the tab stop, not to the input:
<div data-controller="phem-radio-group" data-phem-radio-group-required-value="true" aria-label="Apple">
...
<input name="apple" hidden data-phem-radio-group-target="input" />
</div>The group gets role="radiogroup", and aria-disabled, aria-readonly and aria-required for the values. A radio gets role="radio", aria-checked and tabindex. The group, the radios and the indicators get data-disabled, data-readonly and data-required. A radio and its indicator get data-checked or data-unchecked. The indicator shows only when its radio is checked, with the animations of the conventions.
After a click on Gala, the browser shows:
<div data-controller="phem-radio-group" data-phem-radio-group-value-value="gala" aria-label="Apple" role="radiogroup">
<label>
<span data-phem-radio-group-target="radio" data-value="fuji" role="radio" aria-checked="false" tabindex="-1" data-unchecked aria-labelledby="">
<span data-phem-radio-group-target="indicator" data-unchecked data-closed hidden></span>
</span>
Fuji
</label>
<label>
<span data-phem-radio-group-target="radio" data-value="gala" role="radio" aria-checked="true" tabindex="0" data-checked aria-labelledby="">
<span data-phem-radio-group-target="indicator" data-checked data-open></span>
</span>
Gala
</label>
<input type="hidden" name="apple" data-phem-radio-group-target="input" value="gala" ...>
</div>Give an element the keyboard of a button#
A <button> is the best element for a button. When a button must be another element (a <div> or a <span>, as a Base UI button with nativeButton={false}), phem-button gives it the keyboard of a button:
<div data-controller="phem-button" onClick={{ save }}>Save</div>Enter clicks the element when the key goes down, and Space clicks it when the key goes up. The click runs the handler of the element, as a click of the pointer. The element gets role="button" and tabindex="0", unless your markup gives them. The browser shows:
<div data-controller="phem-button" data-phem-on-click="..." role="button" tabindex="0">Save</div>A button with aria-disabled="true" keeps the focus, so a keyboard user can find it, but it does nothing: the keys do not click it, and a click of the pointer or of a script does not run its handler.
<div data-controller="phem-button" aria-disabled={{ saving ? 'true' : null }} onClick={{ save }}>Save</div>The controller also works on a native <button> that must keep the focus while it is disabled (aria-disabled="true" in place of disabled): it stops the click and the keys. A native button gets no role and no tabindex.
Show an avatar with a fallback#
phem-avatar shows the image of a user when it loads, else a fallback (for example the initials):
<span data-controller="phem-avatar" data-phem-avatar-delay-value="600">
<img data-phem-avatar-target="image" src={{ user.avatarUrl }} alt={{ user.name }} />
<span data-phem-avatar-target="fallback">{{ user.initials }}</span>
</span>The image is hidden until it loads. An image that the browser has already (in its cache) shows at once. The fallback shows while the image is not loaded, after the delay value: a fast image does not show the fallback for a short time. A new src or srcset, for example in the server render of an event, loads the image again.
| Value | Default | Description |
|---|---|---|
delay |
0 |
The time in milliseconds from the load of the page to the fallback. |
A fallback can have its own delay: give it data-phem-avatar-delay="600", and render it with hidden, so it does not show before the controller connects:
<span data-controller="phem-avatar">
<img data-phem-avatar-target="image" src={{ user.avatarUrl }} alt={{ user.name }} />
<span data-phem-avatar-target="fallback" data-phem-avatar-delay="600" hidden>{{ user.initials }}</span>
</span>The load status of the image is idle (no image part), loading, loaded, or error (an image that does not load, or an image with no src). Each change of the status dispatches phem-avatar:status, with the status in its detail:
<span data-controller="phem-avatar" data-action="phem-avatar:status->stats#avatar">...</span>The image gets data-loading while it loads, and data-error when it does not load. When it loads, it gets data-open, with the animations of the conventions. A hidden image or fallback gets hidden. The status stays after the morph of an event.
The image loads while it is hidden. Do not give it loading="lazy": the browser does not load a lazy image that it does not show.
After an event gives the image a src that does not load, the browser shows:
<span data-controller="phem-avatar">
<img data-phem-avatar-target="image" src="..." alt="Ada Lovelace" data-error data-closed hidden>
<span data-phem-avatar-target="fallback" data-open>AL</span>
</span>Validate a field in the browser#
phem-field gives a form control a label, a description and errors, with the constraint validation of the browser (required, type="email", pattern, minlength…). phem-form validates all the fields of a form before its submit:
<form onSubmit={{ save }} data-controller="phem-form">
<div data-controller="phem-field">
<label data-phem-field-target="label">Name</label>
<input id="name" name="name" required data-phem-field-target="control" />
<p data-phem-field-target="description">Visible on your profile</p>
<p data-phem-field-target="error" data-match="valueMissing" hidden>Enter your name.</p>
</div>
<button type="submit">Save</button>
</form>The parts of a field are Stimulus targets:
| Part | Element |
|---|---|
label |
A <label>, or another element that names the control |
control |
An <input>, a <select>, a <textarea> or a custom control. A field can have more than one control, for example the slots of a one-time code |
description |
A text that describes the control. Zero or more |
error |
An error message. Zero or more |
The values of phem-field:
| Value | Default | Description |
|---|---|---|
data-phem-field-validation-mode-value |
the mode of the form, else onSubmit |
onSubmit: the field validates on the submit of the form, then on each change. onBlur: the field validates when the control loses the focus after a change by the user. onChange: the field validates on each change |
data-phem-field-validation-debounce-time-value |
0 |
The time in milliseconds after the last change before the validation, in the onChange mode |
data-phem-field-disabled-value |
false |
Disables the controls |
In all the modes, an invalid field validates again on each change, so its error goes when the user corrects the value. Enter in an <input> control also validates the field in all the modes. In a phem-form, Enter submits the form, and the submit validates the field.
In the onBlur mode, a blur with no change does not validate: the user can go through an empty required field with Tab, and no error shows. An invalid field, and a field after a submit of its form, also validate on a blur with no change.
The values of phem-form:
| Value | Default | Description |
|---|---|---|
data-phem-form-validation-mode-value |
onSubmit |
The validation mode of the fields that have no mode of their own |
Show the errors#
An error part with data-match shows for one key of the ValidityState of the control: valueMissing, typeMismatch, patternMismatch, tooShort, tooLong, rangeUnderflow, rangeOverflow, stepMismatch, badInput or customError. With data-match="valid", it shows when the field is valid. With data-match="true", it always shows. An error part with no data-match shows for all the errors. When it has no text in the markup, it gets the message of the browser (validationMessage):
<div data-controller="phem-field" data-phem-field-validation-mode-value="onBlur">
<label data-phem-field-target="label">Email</label>
<input name="email" type="email" data-phem-field-target="control" />
<p data-phem-field-target="error" hidden></p>
</div>Give the errors hidden in the markup, so they do not show before the controller starts. An error shows and hides with the animations of the conventions (data-open, data-closed, data-starting-style, data-ending-style).
An error that shows on a blur, on a change or on Enter goes to the polite live region of the page (the region of $reply->announce()), 500 milliseconds later. Thus a screen reader first reads the control that gets the focus, and then the error. An error that hides before this time is not announced, and an error that stays shown is not announced again. A submit announces nothing: it focuses the first invalid control, and the screen reader reads its error from aria-describedby.
Submit the form#
The form gets novalidate: the fields show their errors in place of the messages of the browser. On a submit, each field validates. When a field is invalid, phem-form stops the submit before the onSubmit handler sends its event, and before the browser sends the form. It focuses the first invalid control, selects its text, and sends phem-form:invalid, with the invalid fields in detail.fields. A control of the form that is not in a field shows the message of the browser (reportValidity()). The hidden input of a switch, a checkbox or a radio group gives the focus to the element that the user sees (see Validate a switch, a checkbox or a radio group). A submit button with formnovalidate submits with no validation.
After a submit with an empty name, the browser shows:
<div id="name-field" data-controller="phem-field" data-invalid="" data-focused="">
<label id="name-label" data-phem-field-target="label" for="name" data-invalid="" data-focused="">Name</label>
<input id="name" name="name" required="" value="" data-phem-field-target="control" aria-describedby="" data-invalid="" aria-invalid="true" data-focused="">
<p class="description" data-phem-field-target="description" data-invalid="" data-focused="">Visible on your profile</p>
<p id="name-missing" data-phem-field-target="error" data-match="valueMissing" data-invalid="" data-open="" data-focused="">Enter your name.</p>
</div>The aria-describedby="" is empty because the description has no id: the control gets the element references of ARIA (ariaDescribedByElements) for the description and the error. When all the parts have an id, the control gets the ids in aria-describedby.
Keep the server errors#
A server error is a control that the server renders with aria-invalid="true": a field with phem:bind and an error (see Show the error of a bound field), or a Symfony form with form_row(). The field is invalid until the user changes the value of the control. While the error stays, the form does not submit the same value again, and the control gets the message of the server as its custom validity. After the submit event of the form (its onSubmit handler) ends with a new render, the first control with a server error gets the focus and its text is selected, as in Base UI. An error of the browser, a hidden or disabled control, another event of the form (an onChange of the form), and a submit that ends with a redirect do not move the focus. When the user moved the focus out of the form while the request ran, the focus stays there.
An error part whose id is in the aria-describedby of the server render is the message of the server, for example the element of phem:error. It shows while the server error stays. After the user changes the value, it shows the errors of the browser instead, as the Field.Error of Base UI: for example the message of valueMissing when the user clears the control.
The server sends the HTML of a component only when it changes. When the user submits the same value again, the answer has no new HTML for the field, so the field shows the server error of the last render again. This applies to each event that sends the control: the events of its form and of the control, and each event for a control with phem:bind. A change of the value while the request runs, a redirect, and an event that sends no request keep the field with no server error:
<div data-controller="phem-field">
<label data-phem-field-target="label">Name</label>
<input name="name" phem:bind={{ name }} data-phem-field-target="control" />
<p phem:error={{ name }} data-phem-field-target="error" />
</div>After a submit of a name that the server refuses, #name has the focus and its text ("Shoes") is selected. The browser shows:
<div id="name-field" data-controller="phem-field" data-filled="" data-touched="" data-invalid="" data-focused="">
<label id="name-label" data-phem-field-target="label" data-filled="" data-touched="" data-invalid="" data-focused="" for="name">Name</label>
<input id="name" name="name" required="" value="Shoes" data-phem-field-target="control" aria-describedby="" data-filled="" data-touched="" aria-invalid="true" data-invalid="" data-focused="">
<p class="description" data-phem-field-target="description" data-filled="" data-touched="" data-invalid="" data-focused="">Visible on your profile</p>
<p id="name-missing" data-phem-field-target="error" data-match="valueMissing" hidden="" data-closed="" data-filled="" data-touched="" data-invalid="" data-focused="">Enter your name.</p>
<p id="name-error" data-phem-field-target="error" data-invalid="" data-touched="" data-filled="" data-open="" data-focused="">This name is taken.</p>
</div>In a field with a part of the server, an error part with no data-match does not show: the part of the server shows these errors, so a message does not show two times. An error part keeps the elements of its text, for example a link. Do not write aria-invalid on a control yourself: the controller sets it.
Data attributes and ARIA#
All the parts (the field, the labels, the controls, the descriptions and the errors) get these attributes:
| Attribute | When |
|---|---|
data-valid |
The last validation found no error |
data-invalid |
The last validation found an error, or the control has a server error |
data-dirty |
The value is not the value of the server render |
data-touched |
The control lost the focus one time |
data-filled |
The control has a value |
data-focused |
The control has the focus |
data-disabled |
The field is disabled: the disabled value, or disabled controls (also in a disabled <fieldset>) |
Before the first validation, a field has no data-valid and no data-invalid. The label links to the control: a <label> gets for when the control has an id, else the control gets aria-labelledby (an element reference when the label has no id). A bound control with no id gets aria-labelledby, also after the morph of an event: the temporary id that the morph gives it is not an id to link to. A click on a label that is not a native label of the control focuses the control. The control gets aria-invalid="true" when the field is invalid, else aria-invalid="false".
The state stays after the morph of an event: the visible errors, aria-invalid, novalidate and the data attributes.
Validate a switch, a checkbox or a radio group#
A switch or a checkbox and a radio group keep their value in an input that the user does not see. The browser validates this input, but it cannot focus a hidden input or show its message. The input is linked to the element that the user sees (the custom control):
- When the input is the first invalid control of a submit,
phem-formfocuses the custom control: the switch, the checkbox, or the radio of the tab stop (the checked radio, else the first radio). The browser shows no message for a control that is not in a field. - Make the input the
controlpart of aphem-fieldto show an error. The field reads the validity of the input. The custom control getsaria-invalidandaria-describedby, and its focus is the focus of the field.
<form onSubmit={{ save }} data-controller="phem-form">
<div data-controller="phem-field">
<label>
<span role="switch" data-controller="phem-toggle" data-phem-toggle-required-value="true">
<input type="checkbox" name="terms" hidden data-phem-toggle-target="input" data-phem-field-target="control" />
</span>
I accept the terms
</label>
<p data-phem-field-target="error" data-match="valueMissing" hidden>Accept the terms.</p>
</div>
<button type="submit">Save</button>
</form>After a submit with the switch off, the switch has the focus, and the browser shows:
<span role="switch" data-controller="phem-toggle" data-phem-toggle-required-value="true" tabindex="0" aria-checked="false" aria-required="true" aria-invalid="true" aria-describedby="" ...>
<input type="checkbox" name="terms" hidden="" required="" tabindex="-1" aria-hidden="true" aria-invalid="true" aria-describedby="" data-invalid="" data-focused="" ...>
</span>
...
<p data-phem-field-target="error" data-match="valueMissing" data-invalid="" data-open="" ...>Accept the terms.</p>A switch that is a native <button> (a button cannot hold a checkbox), or a switch with an unchecked-value, keeps its value in an <input type="hidden">. The browser does not validate this input. So phem-toggle gives phem-form and phem-field the validity of the input: a required switch that is not checked has the valueMissing error, with the message of the browser for a required checkbox. A disabled switch is valid. phem-form and phem-field then stop the submit and show the error as for a checkbox input. A form with no phem-form sends the form, because the browser does not check the hidden input:
<form onSubmit={{ save }} data-controller="phem-form">
<button type="button" role="switch" data-controller="phem-toggle" data-phem-toggle-required-value="true"
data-phem-toggle-name-value="newsletter" data-phem-toggle-unchecked-value-value="no" aria-label="Newsletter">
<input type="hidden" data-phem-toggle-target="input" />
</button>
<button type="submit">Save</button>
</form>Group the fields#
A Base UI Fieldset needs no controller. Use a <fieldset> with a <legend>. A disabled fieldset disables its controls, and the fields in it get data-disabled:
<fieldset disabled>
<legend>Address</legend>
<div data-controller="phem-field">...</div>
</fieldset>Style the legend of a disabled fieldset with fieldset:disabled > legend.
Edit a number with steps#
phem-number-field is a text input for a number. It has increment and decrement buttons, keys, the mouse wheel, a scrub area and the number format of the locale:
<div data-controller="phem-number-field" data-phem-number-field-value-value="10"
data-phem-number-field-min-value="0" data-phem-number-field-max-value="5000"
data-phem-number-field-locale-value="de-DE">
<span data-phem-number-field-target="scrubArea">
<label for="amount">Amount</label>
<span data-phem-number-field-target="scrubAreaCursor" hidden>↔</span>
</span>
<div data-phem-number-field-target="group">
<button data-phem-number-field-target="decrement">−</button>
<input id="amount" data-phem-number-field-target="input">
<button data-phem-number-field-target="increment">+</button>
</div>
<input type="hidden" name="amount" data-phem-number-field-target="hiddenInput">
</div>The value value is the state: the raw number (for example 1234.5), or an empty string for no value. Set it to give a value on load. A handler can set it in a new render. The value stays after the morph of an event. The input part shows the value in the format of the locale. The hiddenInput part gets the raw value for the form. With no hiddenInput, give the name to the input: the form then gets the formatted text.
The user types a number in the format of the locale. The controller reads the group and decimal separators of the locale, so 1.234,5 is 1234.5 in de-DE. The value changes while the user types. On blur, the input shows the formatted value, and a value out of the limits goes to the nearest limit. Enter does not format the text: it puts the value of the hidden input in the limits, so a form submit gets a valid value, and it ends the change (phem-number-field:commit with the reason keyboard). A text that is not a number gives the last value again.
A character that no number of the format has does not go in the input. A space goes in only when the format has one, for example the group separator of fr-FR: in en-US, "1 2" gives "12". The input takes one sign, one decimal separator, one currency sign and one percent (or permille) sign, unless the new character replaces the selected one. A minus sign goes in only when the value can be negative (no min, a negative min, or allowOutOfRange). The digits of other scripts (Arabic-Indic, Persian, fullwidth and Han) are the same as 0-9: ٤٢ is 42. The Arabic decimal separator (٫) is the decimal separator of the locale, and the Arabic group separator (٬) is its group separator. The fullwidth . and , are . and ,: 1,5 is 1.5 in de-DE.
| Value | Default | Description |
|---|---|---|
value |
empty | The state: the raw number, or empty for no value. |
min, max |
none | The limits. A step always stays in the limits. |
step |
1 |
The step of the arrow keys, the buttons, the wheel and the scrub area. |
smallStep |
0.1 |
The step with Alt. |
largeStep |
10 |
The step with Shift. |
allowOutOfRange |
false |
A typed value out of the limits stays on blur. |
snapOnStep |
false |
A step goes to a multiple of the step, from min (or 0). |
allowWheelScrub |
false |
The mouse wheel changes the value while the input has the focus. |
format |
none | The options of Intl.NumberFormat, as JSON: {"style":"currency","currency":"EUR"}. |
locale |
the browser | The locale of the format and of the typed text. |
disabled, readOnly, required |
false |
The state of the inputs. |
pixelSensitivity |
2 |
The pixels of a movement in the scrub area for one step. |
direction |
horizontal |
The movement of the scrub area: horizontal or vertical. |
| Key (on the input) | Action |
|---|---|
| ArrowUp, ArrowDown | Add or remove one step. With Alt, the small step. With Shift, the large step. |
| Home, End | Go to min or to max. |
The first step from no value goes to 0, in the limits (min when min is more than 0, max when max is less than 0). This is one step, also for a wheel step or a scrub of many steps. PageUp and PageDown are keys of the browser, as in Base UI: use Shift with an arrow key for the large step.
A press on a button changes the value one time. When the user holds the button, the value changes again after 400 ms, and then each 60 ms. The changes stop at the limit, and while the pointer is out of the button: they start again when the pointer comes back. The focus stays in the input. Alt and Shift give the small and the large steps. A touch or a pen press waits 50 ms. A movement (3 moves, or 8 px) before then is a scroll: the press does nothing, and the click does one step. A button gets disabled at its limit.
A press in the scrubArea part locks the pointer on the body (the Pointer Lock API), except on WebKit (Safari), as in Base UI. The movement changes the value as in Base UI: after a movement of pixelSensitivity pixels (2 by default), the value changes by one step for each pixel of the last movement. While the pointer is locked, the scrubAreaCursor part shows with its center at the position of the pointer, as a virtual cursor, and goes to the opposite edge at an edge of the viewport. With data-phem-number-field-teleport-distance-value="40", the edges are those of the scrub area plus 20 px on each side, so the cursor stays near the label. With no pointer lock, the native pointer stays. A press with no movement is a click: a click on the label focuses the input. While the pointer is locked, the browser sends this click to the body, so the controller sends it to the target. With no lock (a touch, WebKit), the browser sends it. The scrub area gets role="presentation", and phem.css gives it touch-action: none and user-select: none: a touch scrubs and does not scroll the page.
All the parts get data-disabled, data-readonly, data-required, and data-scrubbing while the user scrubs. The group part gets role="group". The input gets inputmode="numeric" (on iOS decimal when min is 0 or more, else text: the numeric keyboard of iOS has no minus and no decimal key), aria-roledescription="Number field", autocomplete="off" and spellcheck="false", as Base UI does, unless the markup gives them. The buttons get type="button", tabindex="-1", aria-label ("Increase" or "Decrease") and aria-controls. The controller links a button to an input with no id with an element reference.
A change of the value dispatches phem-number-field:change (detail: value, reason) and sends input on the inputs. The end of a change (a blur, a key, the release of a button or of the scrub area, a wheel step) dispatches phem-number-field:commit and sends change on the inputs. The controller sets the value of the hidden input before it sends the events, so a bound hidden input sends its value. The reason is input-change, input-clear, input-blur, keyboard, increment-press, decrement-press, wheel or scrub.
After Shift+ArrowUp and an event, the browser shows:
<div id="field" data-controller="phem-number-field" data-phem-number-field-value-value="20" data-phem-number-field-min-value="0" data-phem-number-field-max-value="5000" data-phem-number-field-locale-value="de-DE" data-phem-number-field-allow-wheel-scrub-value="">
<span id="area" data-phem-number-field-target="scrubArea" style="...">
<label for="amount">Amount</label>
<span id="cursor" data-phem-number-field-target="scrubAreaCursor" hidden="">↔</span>
</span>
<div id="group" data-phem-number-field-target="group" role="group">
<button id="dec" data-phem-number-field-target="decrement" type="button" tabindex="-1" aria-label="Decrease" aria-controls="amount">−</button>
<input id="amount" data-phem-number-field-target="input" aria-roledescription="Number field" autocomplete="off" autocorrect="off" spellcheck="false" inputmode="numeric" value="20">
<button id="inc" data-phem-number-field-target="increment" type="button" tabindex="-1" aria-label="Increase" aria-controls="amount">+</button>
</div>
<input id="raw" type="hidden" name="amount" data-phem-number-field-target="hiddenInput" value="20">
</div>Enter a one-time code#
phem-otp-field shows a code with one input for each character (a slot), or one input for the whole code:
<label id="otp-label" for="otp-1">Verification code</label>
<div data-controller="phem-otp-field" data-phem-otp-field-auto-submit-value="true">
<input id="otp-1" data-phem-otp-field-target="input" />
<input data-phem-otp-field-target="input" />
<input data-phem-otp-field-target="input" />
<span data-phem-otp-field-target="separator">-</span>
<input data-phem-otp-field-target="input" />
<input data-phem-otp-field-target="input" />
<input data-phem-otp-field-target="input" />
<input type="hidden" name="code" data-phem-otp-field-target="hiddenInput" />
</div>The input parts are the slots. The hiddenInput part gets the full value, so a form submit and a phem event send it with its name. When the user changes the value, the hidden input gets an input and a change event. The separator part gets role="separator" and data-orientation="horizontal".
| Value | Default | Description |
|---|---|---|
data-phem-otp-field-value-value |
"" |
The state: the characters of the slots. A server render with a new value sets it |
data-phem-otp-field-length-value |
the number of input parts |
The number of slots |
data-phem-otp-field-validation-type-value |
numeric |
The characters: numeric (digits), alpha (the letters A to Z), alphanumeric, or none (all the characters except spaces) |
data-phem-otp-field-normalize-value |
uppercase or lowercase: changes the accepted characters, for example a recovery code in capital letters. The validation type filters the result again |
|
data-phem-otp-field-input-mode-value |
numeric for numeric, else text |
The inputmode of the slots: the keyboard of a phone |
data-phem-otp-field-mask-value |
false |
The slots are type="password". Else they are type="text": the controller replaces a type of the markup. A change of the value changes the type |
data-phem-otp-field-auto-submit-value |
false |
Submits the form (requestSubmit()) when all the slots have a value |
data-phem-otp-field-autocomplete-value |
one-time-code |
The autocomplete of the first slot. The others get off |
data-phem-otp-field-disabled-value |
false |
Disables the slots and the hidden input |
data-phem-otp-field-read-only-value |
false |
The user cannot change the value |
data-phem-otp-field-required-value |
false |
The slots are required |
A character that the user types goes in its slot, and the focus goes to the next slot. When a slot has two characters (its character and a new one), the two characters go in the slots from it on, in the order of the text of the slot, as in Base UI: with the caret before the 3 of 123456, a 9 gives 129356. During an IME composition, the slot keeps the text of the IME, and the keys are for the IME. The end of the composition puts the text in the slots one time. A paste fills the slots from the slot with the focus, without the spaces. The browser can fill the code from a message (autocomplete="one-time-code"): all the characters go in the slots. A character that the validation type refuses does not go in the slot. The value has no gaps: a click on a slot after the first empty slot focuses the first empty slot.
| Key | Action |
|---|---|
| Backspace | Removes the character of the slot, and focuses the slot before. On an empty slot, removes the character of the slot before |
| Ctrl+Backspace (Command on a Mac) | Removes all the characters, and focuses the first slot |
| Delete | Removes the character of the slot |
| ArrowLeft / ArrowRight | Focuses the slot before / after (up to the first empty slot). With Ctrl or Command: the first slot / the first empty slot |
| Home, ArrowUp / End, ArrowDown | Focuses the first slot / the first empty slot (or the last slot) |
A press of the mouse on a slot selects its character, so the next character replaces it.
The slots have one tab stop: the slot with the focus, or else the first empty slot (the last slot when the code is complete). So Tab and Shift+Tab go out of the field from each slot. Each slot gets inputmode, a pattern of one character ([0-9] for numeric), spellcheck="false", autocorrect="off" and enterkeyhint (done on the last slot, else next). The first slot gets maxlength (the number of slots), so the browser can fill all the code. The root gets role="group" and aria-labelledby to the label of the first slot, unless it has an aria-label. The first slot uses the label of the field. A slot after the first one with no aria-label gets aria-label="Character 2 of 6". An attribute that the markup gives stays.
The events:
| Event | Detail |
|---|---|
phem-otp-field:change |
value, and reason: input-change, input-clear, input-paste or keyboard |
phem-otp-field:complete |
value, when a typed character fills the last empty slot, or at each paste after which all the slots have a value (also the same code again, or one character in a complete code), as in Base UI. A typed character in a complete code does not send it |
phem-otp-field:invalid |
value: the text with a character that the validation type refuses |
After the user types 123, the browser shows:
<div id="otp" data-controller="phem-otp-field" data-phem-otp-field-auto-submit-value="true" role="group" aria-labelledby="otp-label" data-focused="" data-phem-otp-field-value-value="123" data-filled="">
<input data-phem-key="1" id="otp-1" data-phem-otp-field-target="input" tabindex="-1" inputmode="numeric" autocomplete="one-time-code" spellcheck="false" type="text" autocorrect="off" enterkeyhint="next" maxlength="6" pattern="[0-9]" data-focused="" data-filled="">
<input data-phem-key="2" id="otp-2" data-phem-otp-field-target="input" tabindex="-1" inputmode="numeric" autocomplete="off" spellcheck="false" type="text" autocorrect="off" enterkeyhint="next" pattern="[0-9]" aria-label="Character 2 of 6" data-focused="" data-filled="">
<input data-phem-key="3" id="otp-3" data-phem-otp-field-target="input" tabindex="-1" inputmode="numeric" autocomplete="off" spellcheck="false" type="text" autocorrect="off" enterkeyhint="next" pattern="[0-9]" aria-label="Character 3 of 6" data-focused="" data-filled="">
<span id="dash" data-phem-otp-field-target="separator" role="separator" data-orientation="horizontal">-</span>
<input data-phem-key="4" id="otp-4" data-phem-otp-field-target="input" tabindex="0" inputmode="numeric" autocomplete="off" spellcheck="false" type="text" autocorrect="off" enterkeyhint="next" pattern="[0-9]" aria-label="Character 4 of 6" data-focused="">
<input data-phem-key="5" id="otp-5" data-phem-otp-field-target="input" tabindex="-1" inputmode="numeric" autocomplete="off" spellcheck="false" type="text" autocorrect="off" enterkeyhint="next" pattern="[0-9]" aria-label="Character 5 of 6" data-focused="">
<input data-phem-key="6" id="otp-6" data-phem-otp-field-target="input" tabindex="-1" inputmode="numeric" autocomplete="off" spellcheck="false" type="text" autocorrect="off" enterkeyhint="done" pattern="[0-9]" aria-label="Character 6 of 6" data-focused="">
<input id="code" type="hidden" name="code" data-phem-otp-field-target="hiddenInput" value="123" data-phem-touched="">
</div>The controller element and the slots get data-disabled, data-readonly, data-required, data-complete (all the slots have a value) and data-focused (a slot has the focus). The controller element gets data-filled when the value is not empty, and a slot gets it when it has a character. The value stays after the morph of an event.
To validate the code and show its errors, give the slots the control target of a phem-field around it, with data-phem-otp-field-required-value="true".
One input for the whole code#
Some browsers do not fill a code from a message into a set of inputs, and a screen reader reads each slot as a different field. For the best autofill and accessibility, use one input for the whole code: a codeInput part in place of the input parts. The slot parts are optional. They only show the characters, so a design with boxes keeps working:
<label for="otp">Verification code</label>
<div data-controller="phem-otp-field" data-phem-otp-field-auto-submit-value="true">
<input id="otp" name="code" data-phem-otp-field-target="codeInput" />
<span data-phem-otp-field-target="slot"></span>
<span data-phem-otp-field-target="slot"></span>
<span data-phem-otp-field-target="slot"></span>
<span data-phem-otp-field-target="separator">-</span>
<span data-phem-otp-field-target="slot"></span>
<span data-phem-otp-field-target="slot"></span>
<span data-phem-otp-field-target="slot"></span>
</div>The browser edits the text of the input. The controller keeps the characters that the validation type accepts, and the caret stays after the same characters. The values, the events, the auto submit and the hiddenInput part are the same as with the slots. The input can have the name itself, because its text is the value. Some differences:
- The
lengthvalue is by default the number ofslotparts, or 6 when there is noslotpart. - The input gets
autocomplete="one-time-code"(theautocompletevalue),inputmode,maxlength(the length), apatternof the full code ([0-9]{6}fornumeric),spellcheck="false",autocorrect="off"andenterkeyhint="done". It istype="text", ortype="password"withmask. - A paste removes the spaces, and the dashes when the validation type refuses them:
123-456gives123456and does not sendphem-otp-field:invalid. A pasted full code replaces the value. A shorter text goes in the place of the selection. - The keys of a text input stay: the controller does not change Backspace, Delete and the arrows.
- During an IME composition, the input keeps the text of the IME. The end of the composition puts the text in the value one time.
- The controller element does not get
role="group": the input uses the label of the field. - Each
slotpart getsaria-hidden="true"and shows one character (•withmask). It getsdata-filledwhen it has a character, anddata-activewhen the input has the focus and the caret or the selection is on it (the caret after the last character is on the last slot). A press of the mouse on a slot puts the caret before its character, and selects it. Aseparatorpart getsaria-hidden="true". - The input and the slots get
data-disabled,data-readonly,data-required,data-completeanddata-focused.
Put the input over the slots with CSS (for example position: absolute, a transparent text color and caret-color: transparent), so a press goes to the input and the slots show the characters. Or show only the input, with a monospace font and letter-spacing.
After the user types 123, the browser shows:
<div data-controller="phem-otp-field" data-phem-otp-field-auto-submit-value="true" data-focused="" data-phem-otp-field-value-value="123" data-filled="">
<input id="otp" name="code" data-phem-otp-field-target="codeInput" inputmode="numeric" autocomplete="one-time-code" spellcheck="false" type="text" autocorrect="off" enterkeyhint="done" maxlength="6" pattern="[0-9]{6}" data-focused="" data-filled="">
<span data-phem-otp-field-target="slot" aria-hidden="true" data-focused="" data-filled="">1</span>
<span data-phem-otp-field-target="slot" aria-hidden="true" data-focused="" data-filled="">2</span>
<span data-phem-otp-field-target="slot" aria-hidden="true" data-focused="" data-filled="">3</span>
<span data-phem-otp-field-target="separator" aria-hidden="true">-</span>
<span data-phem-otp-field-target="slot" aria-hidden="true" data-focused="" data-active=""></span>
<span data-phem-otp-field-target="slot" aria-hidden="true" data-focused=""></span>
<span data-phem-otp-field-target="slot" aria-hidden="true" data-focused=""></span>
</div>To validate the code with a phem-field, give the control target to the input.
Select a value on a slider#
phem-slider moves one thumb, or more thumbs for a range, on a track, with the pointer and the keys. Each thumb has a native <input type="range">. It gives the ARIA and the value of the form:
<div data-controller="phem-slider" data-phem-slider-value-value="[20, 60]"
data-phem-slider-min-steps-between-values-value="10">
<div id="price-label" data-phem-slider-target="label">Price</div>
<output data-phem-slider-target="value"></output>
<div data-phem-slider-target="control">
<div data-phem-slider-target="track">
<div data-phem-slider-target="indicator"></div>
<div data-phem-slider-target="thumb">
<input type="range" name="min" aria-label="Minimum price" class="sr-only">
</div>
<div data-phem-slider-target="thumb">
<input type="range" name="max" aria-label="Maximum price" class="sr-only">
</div>
</div>
</div>
</div>The value value is the state: one number (25) for one thumb, or a JSON list with one number for each thumb ([20, 60]). Set it to give the values on load. A handler can set it in a new render. The values stay after the morph of an event. Hide the range inputs visually (for example with sr-only): the controller moves the thumbs.
A press on the control part moves the nearest thumb to the point and starts a drag. The pointer capture keeps the drag when the pointer goes out of the control. A press on a thumb does not move it: the drag moves it from there. Two thumbs stay at least minStepsBetweenValues steps apart, and they do not cross.
The press gives the focus to the thumb input with no focus ring (focusVisible: false), as in Base UI. A focus with the keyboard (Tab) shows the ring.
| Value | Default | Description |
|---|---|---|
value |
min |
The state: a number, or a JSON list of numbers. |
min, max |
0, 100 |
The limits. |
step |
1 |
The step of the values, from min. |
largeStep |
10 |
The step with Shift, and of PageUp and PageDown. |
minStepsBetweenValues |
0 |
The minimum steps between two thumbs of a range. |
thumbCollisionBehavior |
push |
In a drag, a thumb that meets another thumb: push pushes it, swap changes places with it, none stops. |
orientation |
horizontal |
horizontal or vertical. |
thumbAlignment |
center |
center: the center of the thumb is at the edge of the control at min and max. edge: the thumb stays in the control. |
disabled |
false |
The slider ignores the user. |
format |
none | The options of Intl.NumberFormat, as JSON, for the value part and aria-valuetext. |
locale |
the browser | The locale of the format. |
| Key (on a thumb input) | Action |
|---|---|
| ArrowRight, ArrowUp | Add one step. With Shift, the large step. |
| ArrowLeft, ArrowDown | Remove one step. With Shift, the large step. |
| PageUp, PageDown | Add or remove the large step. |
| Home, End | Go to the lowest or to the highest value that the thumb can have. |
In a right-to-left page, ArrowLeft adds and ArrowRight removes. The keys stop a thumb at the other thumb.
The controller gives the positions as inline styles, as Base UI does. The track gets position: relative, and the control gets touch-action: none. In the horizontal orientation, a thumb gets position: absolute, inset-inline-start (the percent of its value), top: 50% and translate: -50% -50%. The indicator gets inset-inline-start and width, from the first to the last value (from the start to the value with one thumb). In the vertical orientation, they get bottom and height. While the user presses a thumb, it gets z-index: 1. In a range, the thumb of the press gets z-index: 2, and the thumb of the last change gets z-index: 1.
The thumb inputs get min, max, step, value, aria-valuenow, aria-orientation and disabled. With two thumbs, they get aria-valuetext ("20 start range" and "60 end range"). With one thumb and a format, they get the formatted value. An aria-valuetext that the markup gives stays. The root gets role="group". The root and the inputs with no aria-label get a link to the label part (aria-labelledby). The value part shows the formatted values, separated by " – ", and gets aria-live="off". All the parts get data-orientation, data-disabled, and data-dragging while the user drags. The thumbs get data-index.
A change of a value dispatches phem-slider:change (detail: value, values, index, reason) and sends input on the inputs that change. The end of a change (the release of the pointer, a key) dispatches phem-slider:commit (Base UI onValueCommitted) and sends change. The value of the detail is a number for one thumb, or the list. The reason is track-press, drag, keyboard or input-change.
After ArrowRight on the second thumb and an event, the browser shows:
<div id="price" data-controller="phem-slider" data-phem-slider-value-value="[20,61]" data-phem-slider-min-steps-between-values-value="10" data-orientation="horizontal" role="group" aria-labelledby="price-label">
<div id="price-label" data-phem-slider-target="label">Price</div>
<output id="price-value" data-phem-slider-target="value" data-orientation="horizontal" aria-live="off">20 – 61</output>
<div id="price-control" class="control" data-phem-slider-target="control" data-orientation="horizontal" style="touch-action: none;">
<div class="track" data-phem-slider-target="track" data-orientation="horizontal" style="position: relative;">
<div id="price-indicator" class="indicator" data-phem-slider-target="indicator" data-orientation="horizontal" style="position: relative; width: 41%; inset-inline-start: 20%; height: inherit;"></div>
<div id="price-min" class="thumb" data-phem-slider-target="thumb" data-orientation="horizontal" data-index="0" style="position: absolute; inset-inline-start: 20%; top: 50%; translate: -50% -50%;">
<input id="price-min-input" type="range" name="min" aria-label="Minimum price" class="sr-only" value="20" aria-valuenow="20" aria-valuetext="20 start range" min="0" max="100" step="1" aria-orientation="horizontal">
</div>
<div id="price-max" class="thumb" data-phem-slider-target="thumb" data-orientation="horizontal" data-index="1" style="position: absolute; z-index: 1; inset-inline-start: 61%; top: 50%; translate: -50% -50%;">
<input id="price-max-input" type="range" name="max" aria-label="Maximum price" class="sr-only" value="61" aria-valuenow="61" aria-valuetext="61 end range" min="0" max="100" step="1" aria-orientation="horizontal">
</div>
</div>
</div>
</div>Show custom scrollbars#
phem-scroll-area puts custom scrollbars over a native scroll container:
<div data-controller="phem-scroll-area" class="h-32 w-96">
<div data-phem-scroll-area-target="viewport" class="h-full">
<div data-phem-scroll-area-target="content">...</div>
</div>
<div data-phem-scroll-area-target="scrollbar" data-orientation="vertical" class="flex w-2">
<div data-phem-scroll-area-target="thumb" class="w-full bg-neutral-900"></div>
</div>
<div data-phem-scroll-area-target="scrollbar" data-orientation="horizontal" class="flex h-2">
<div data-phem-scroll-area-target="thumb" class="h-full bg-neutral-900"></div>
</div>
<div data-phem-scroll-area-target="corner"></div>
</div>The viewport part is the element that scrolls. The controller gives it overflow: scroll and removes its native scrollbar (scrollbar-width: none), so the wheel, a touch and the keyboard scroll it as usual. The content part gets min-width: fit-content, so a wide content overflows on the horizontal axis. A scrollbar part has the data-orientation of the markup: vertical (the default) or horizontal. Its thumb part shows the visible part of the content, with a minimum size of 16px.
A drag of a thumb scrolls the viewport. The pointer capture keeps the drag when the pointer goes out of the scroll area. A press on a scrollbar out of its thumb scrolls the viewport, so that the center of the thumb goes to the pointer, and the drag continues from there. A scrollbar gets hidden when its axis has no overflow, unless it has the data-keep-mounted attribute. The corner part is the square between the two scrollbars. It gets hidden when one axis has no overflow.
| Value | Default | Description |
|---|---|---|
overflowEdgeThreshold |
0 |
The distance (px) from an edge before its data-overflow-* attribute shows. A number for all the edges, or a JSON object with xStart, xEnd, yStart and yEnd, for example data-phem-scroll-area-overflow-edge-threshold-value='{"yEnd": 20}'. |
The scroll area has no keys of its own: the viewport is a native scroll container, and the browser scrolls it with the arrow keys, Page Up, Page Down, Home and End. The viewport gets tabindex="0" when its content is wider or taller than it, so Tab goes to it. A viewport with no overflow gets tabindex="-1", as in Base UI: Tab does not stop on an area that does not scroll.
The root, the viewport, the content and the scrollbars get these attributes:
| Attribute | Description |
|---|---|
data-has-overflow-x, data-has-overflow-y |
The content is wider or taller than the viewport. |
data-overflow-x-start, data-overflow-x-end |
The content continues past the start or the end edge on the horizontal axis. |
data-overflow-y-start, data-overflow-y-end |
The content continues past the top or the bottom edge. |
data-scrolling |
The user scrolls. It goes at the scrollend event of the viewport. A browser with no scrollend event removes it 500 ms after the last scroll. A scrollbar and its thumb get it only for a scroll on their axis. The end of a drag of a thumb removes it at once. |
data-hovering |
On the scrollbars only: the pointer is over the scroll area. |
The thumbs get data-orientation and data-scrolling. A scroll changes the attributes and the CSS variables once per frame, in the next animation frame, for all the scroll events of that frame. The controller reads all the sizes before it writes, so a scroll does not force a layout between two writes. The CSS variables are:
| Variable | Part | Description |
|---|---|---|
--scroll-area-corner-width, --scroll-area-corner-height |
root | The size of the corner: the width of the vertical scrollbar and the height of the horizontal scrollbar, or 0px when one axis has no overflow. |
--scroll-area-overflow-x-start, --scroll-area-overflow-x-end, --scroll-area-overflow-y-start, --scroll-area-overflow-y-end |
viewport | The distance to each edge, in px. Use it for a fade at the edges. |
--scroll-area-thumb-height, --scroll-area-thumb-width |
scrollbar | The size of the thumb of a vertical or a horizontal scrollbar. |
The size and the offset of a thumb keep the fractions of a pixel. In a viewport of 100px with a content of 300px, scrolled to 100px, the thumb gets --scroll-area-thumb-height: 33.333333333333336px and transform: translate3d(0px, 33.333333333333336px, 0px).
As in Base UI, the controller gives the parts their layout styles: the root gets position: relative, a scrollbar position: absolute on its edge (it stops at the corner), a thumb its size and a transform to its place, and the corner its size. The style attribute of the parts belongs to the client, so the morph does not change it. The scroll position, the attributes and the styles stay after the morph of an event. The controller renders them again after a scroll, a resize of the viewport, of the content or of a scrollbar, and the morph.
To show the scrollbars only while the pointer is over the area or while the user scrolls:
[data-phem-scroll-area-target="scrollbar"] {
opacity: 0;
transition: opacity 150ms;
}
[data-phem-scroll-area-target="scrollbar"]:is([data-hovering], [data-scrolling]) {
opacity: 1;
}After a scroll of 200px in a viewport of 100px with a content of 500px, the browser shows:
<div data-controller="phem-scroll-area" style="...; position: relative; --scroll-area-corner-width: 0px; --scroll-area-corner-height: 0px;" data-has-overflow-y="" data-overflow-y-end="" data-overflow-y-start="" data-scrolling="">
<div data-phem-scroll-area-target="viewport" style="...; overflow: scroll; scrollbar-width: none; --scroll-area-overflow-x-start: 0px; --scroll-area-overflow-x-end: 0px; --scroll-area-overflow-y-start: 200px; --scroll-area-overflow-y-end: 200px;" data-has-overflow-y="" data-overflow-y-end="" data-overflow-y-start="" data-scrolling="">
<div data-phem-scroll-area-target="content" style="...; min-width: fit-content;" data-has-overflow-y="" data-overflow-y-end="" data-overflow-y-start="" data-scrolling="">...</div>
</div>
<div data-phem-scroll-area-target="scrollbar" data-orientation="vertical" style="...; position: absolute; touch-action: none; user-select: none; top: 0px; bottom: var(--scroll-area-corner-height); inset-inline-end: 0px; --scroll-area-thumb-height: 20px;" data-has-overflow-y="" data-overflow-y-end="" data-overflow-y-start="" data-scrolling="">
<div data-phem-scroll-area-target="thumb" style="...; height: var(--scroll-area-thumb-height); transform: translate3d(0px, 40px, 0px);" data-orientation="vertical" data-scrolling=""></div>
</div>
<div data-phem-scroll-area-target="scrollbar" data-orientation="horizontal" style="..." hidden="" ...>
<div data-phem-scroll-area-target="thumb" data-orientation="horizontal" style="width: var(--scroll-area-thumb-width); transform: translate3d(0px, 0px, 0px);"></div>
</div>
<div data-phem-scroll-area-target="corner" hidden="" style="position: absolute; bottom: 0px; inset-inline-end: 0px; width: var(--scroll-area-corner-width); height: var(--scroll-area-corner-height);"></div>
</div>Select a value from a list#
phem-select is a button that opens a list of options, as a native <select> that you can style:
<div data-controller="phem-select" data-phem-select-value-value="fuji">
<button data-phem-select-target="trigger" aria-label="Apple">
<span data-phem-select-target="value" placeholder="Select apple"></span>
<span data-phem-select-target="icon">▾</span>
</button>
<input data-phem-select-target="input" name="apple" hidden />
<div data-phem-select-target="popup" popover="manual" hidden>
<div data-phem-select-target="scrollUpArrow">▴</div>
<div data-phem-select-target="list">
<div data-phem-select-target="group">
<div data-phem-select-target="groupLabel">Red</div>
<div data-phem-select-target="item" data-value="gala">
<span data-phem-select-target="itemIndicator">✓</span>
<span data-phem-select-target="itemText">Gala</span>
</div>
<div data-phem-select-target="item" data-value="fuji">...</div>
</div>
<div data-phem-select-target="separator"></div>
<div data-phem-select-target="item" data-value="granny" aria-disabled="true">...</div>
</div>
<div data-phem-select-target="scrollDownArrow">▾</div>
</div>
</div>The value of an item is its data-value attribute, its key (key={{ ... }}), or its position. The value value is the state: the value of the selected item. With the multiple value, it is a JSON array of the selected values, for example data-phem-select-value-value='["gala","fuji"]'. Set it to select an item on load, and a handler can set it in a new render. The value and the open popup belong to the client, so they stay after the morph of an event.
A label part (data-phem-select-target="label") names the trigger, with aria-labelledby or with an element reference when the label has no id. A click on it gives the focus to the trigger, and does not open the popup.
The value part shows the label of the selected item: its data-label attribute, the text of its itemText part, or its text. With multiple, it shows the labels separated by a comma (Gala, Fuji). With no value, it shows its placeholder attribute, and it gets data-placeholder. The controller writes the text of the value part: put no other content in it.
Two attributes of the value part change what it shows:
data-phem-select-template: the text for two values or more.{first}is the first label,{others}the number of the other values,{count}the number of values and{labels}the labels. Withdata-phem-select-template="{first} (+{others} more)", the part showsGala (+2 more).data-phem-select-markup: the part shows a copy of the markup of theitemTextpart of the selected item, with no ids, for an item with more than a text (a name and a price, for example).
| Value | Default | Description |
|---|---|---|
value |
The selected value, or a JSON array with multiple. |
|
multiple |
false |
The user selects more than one item. A click on an item adds or removes its value, and the popup stays open. |
alignItemWithTrigger |
true |
The popup goes over the trigger: the text of the selected item (or of the first item) is on the text of the value part, and the popup gets data-side="none". When the popup is too tall for the viewport, it gets smaller (10 px from the top and the bottom of the viewport) and its list scrolls. The popup goes next to the trigger when the trigger is less than 20 px from the top or less than 40 px from the bottom of the viewport, when the popup would be less than 100 px high (or less than the full height of a list that is shorter than 100 px), when its list does not scroll, or after a touch. The popup over the trigger stays 5 px from the left and the right of the viewport: the collisionPadding value applies only to a popup next to the trigger. |
modal |
true |
While the popup is open, the page does not scroll and the elements out of the popup get no pointer events. |
disabled |
false |
The select does not open. The trigger gets disabled (or aria-disabled when it is not a button), and the input gets disabled. |
readOnly |
false |
The select does not open and the value does not change. |
required |
false |
The input gets required. |
loopFocus |
true |
The arrow keys go from the last item to the first item. |
highlightItemOnHover |
true |
A move of the pointer on an item gives it the focus. An item that comes under a pointer that does not move (the popup opens over the trigger) does not get it. A press of the mouse on the trigger of a select with no value gives the focus to the first item. When the pointer leaves the items, the highlight goes away and the popup gets the focus. |
open |
false |
The popup is open on load. A script can change it. |
side, align, sideOffset, alignOffset, collisionPadding, arrowPadding |
bottom, center, 0, 0, 5, 5 |
The place of the popup next to the trigger, as a popover. |
A press on the trigger opens the popup. The press does not select text, and it gives the trigger a focus of the mouse, with no focus ring, as in Base UI. When the user drags the pointer from the trigger and releases it on an item, the item is selected. A click on an item selects it and closes the popup, and the focus goes back to the trigger with no focus ring. A press of the mouse outside closes the popup at the press, and the focus goes back to the trigger, unless the press gives the focus to another element, for example a text field. A touch outside closes the popup at the end of the touch, or when the touch moves (a scroll). The focus out of the select closes the popup.
| Key | Where | Action |
|---|---|---|
| ArrowDown, Enter, Space | trigger | Opens the popup. The focus goes to the selected item, or to the first item. |
| ArrowUp | trigger | Opens the popup. The focus goes to the selected item, or to the last item. |
| A letter | trigger | Selects the next item that starts with it, with no popup (not with multiple). |
| ArrowDown, ArrowUp | popup | Moves the focus to the next or the previous item. |
| Home, End | popup | Moves the focus to the first or the last item. |
| A letter | popup | Moves the focus to the next item that starts with the letters. The typed text stays for 750 ms. A new text starts after the selected item. A letter that matches no item starts a new text. |
| Enter, Space | popup | Selects the item with the focus. |
| Escape | popup | Closes the popup. The focus goes back to the trigger. |
| Tab | popup | Closes the popup. The focus goes to the trigger, and then to the next element. |
The keys skip a disabled item (disabled or aria-disabled="true"). The focus goes to the items: the controller does not use aria-activedescendant.
The input part is the field of the form. An <input> gets the value, or the values separated by a comma with multiple. A <select> (for example <select multiple name="apples[]" hidden> with an <option> for each item) gets its options selected. When the user changes the value, the input sends its input and change events, and the controller dispatches phem-select:change with the value in its detail (an array with multiple). A focus on the input goes to the trigger. For required, use an input that is visually hidden (class="sr-only"), not hidden: the browser does not validate a field that it does not show.
The scroll arrows scroll the list one item each 40 ms while the pointer is on them. Each one shows only when the list can scroll in its direction, unless it has data-keep-mounted. The item with the focus does not go under a visible arrow: the list keeps the larger of its scroll-padding-top (or scroll-padding-bottom) and the height of the arrow free. The list part, or the popup with no list, is the element that scrolls: give it a max-height, for example max-height: var(--available-height).
A popup over the trigger gets smaller in a short viewport, and its list must then scroll. The controller sets --available-height of the popup to the height of its content box (in its border and its padding), so a list with max-height: var(--available-height) and overflow-y: auto fits. You can also make the open popup a flex column, and let the list scroll. Do not put display in the style attribute of the popup: it shows the popup when it has hidden.
[data-phem-select-target="popup"]:not([hidden]) {
display: flex;
flex-direction: column;
}
[data-phem-select-target="list"] {
overflow-y: auto;
min-height: 0;
}A margin or a transform of the popup (for example translate: 0 1px over the trigger) does not move the text of the selected item from the text of the value: the controller measures the popup with them.
The parts get these attributes:
| Part | Attributes |
|---|---|
| trigger | role="combobox", aria-haspopup="listbox", aria-expanded, aria-controls, data-popup-open and data-pressed while the popup is open, data-popup-side (bottom when the popup is over the trigger), data-placeholder, data-disabled, data-readonly, data-required |
| value | data-placeholder |
| icon | data-popup-open |
| popup | data-open, data-closed, data-starting-style, data-ending-style, data-side (none over the trigger), data-align, and the CSS variables of a popover (--anchor-width, --available-height, --transform-origin...) |
| list (or popup) | role="listbox", aria-multiselectable with multiple |
| item | role="option", aria-selected, data-selected, data-highlighted, data-disabled, tabindex="0" on the highlighted item and tabindex="-1" on the others |
| itemIndicator | hidden when its item is not selected (unless data-keep-mounted), and the animation attributes |
| group, groupLabel | role="group" with the name of its label |
| separator | role="separator", data-orientation |
| scrollUpArrow, scrollDownArrow | data-direction (up or down), data-side, data-visible, position: absolute |
| arrow | data-side, data-align, data-uncentered |
| backdrop | data-open, data-closed, and the animation attributes |
A popup over the trigger does not animate from the trigger in Base UI. Use [data-side="none"] to remove the scale animation there:
[data-phem-select-target="popup"][data-starting-style]:not([data-side="none"]) {
opacity: 0;
transform: scale(0.98);
}After a click on the trigger of a select with the value honeycrisp, the browser shows:
<div data-controller="phem-select" data-phem-select-value-value="honeycrisp" data-phem-select-open-value="true">
<button data-phem-select-target="trigger" aria-label="Apple" role="combobox" aria-haspopup="listbox" aria-expanded="true" aria-controls="list" data-popup-open="" data-pressed="" data-popup-side="bottom">
<span data-phem-select-target="value" placeholder="Select apple">Honeycrisp</span>
<span data-phem-select-target="icon" data-popup-open="">▾</span>
</button>
<input data-phem-select-target="input" name="apple" hidden="" tabindex="-1" aria-hidden="true">
<div data-phem-select-target="popup" popover="manual" style="pointer-events: auto; position: fixed; inset: 142px auto auto 27px; --anchor-width: 160px; --anchor-height: 22px; --available-width: 1270px; --available-height: 710px; --transform-origin: 62px 70px;" tabindex="-1" data-open="" data-side="none" data-align="center">
<div id="list" data-phem-select-target="list" role="listbox">
<div data-phem-select-target="item" data-value="gala" role="option" tabindex="-1" aria-selected="false">
<span data-phem-select-target="itemIndicator" data-closed="" hidden="">✓</span>
<span data-phem-select-target="itemText">Gala</span>
</div>
<div data-phem-select-target="item" data-value="honeycrisp" role="option" tabindex="0" aria-selected="true" data-selected="" data-highlighted="">
<span data-phem-select-target="itemIndicator" data-open="">✓</span>
<span data-phem-select-target="itemText">Honeycrisp</span>
</div>
...
</div>
</div>
</div>After ArrowDown and Enter, the value is granny, the popup is closed, and the input has the new value:
<div data-controller="phem-select" data-phem-select-value-value="granny" data-phem-select-open-value="false">
<button data-phem-select-target="trigger" ... aria-expanded="false" aria-controls="list">
<span data-phem-select-target="value" placeholder="Select apple">Granny Smith</span>
...
</button>
<input data-phem-select-target="input" name="apple" hidden="" tabindex="-1" aria-hidden="true">
<div data-phem-select-target="popup" popover="manual" tabindex="-1" data-closed="" hidden="">...</div>
</div>The value property of the input is granny.
Mark the section in view#
phem-scrollspy marks the link of the section in view, for a table of contents:
<nav data-controller="phem-scrollspy" aria-label="On this page">
<a href="#install" data-phem-scrollspy-target="link">Install</a>
<a href="#configure" data-phem-scrollspy-target="link">Configure</a>
</nav>
<h2 id="install">Install</h2>
...
<h2 id="configure">Configure</h2>
...The section of a link is the element of the page with the id of its hash. A section is current when its top is at its scroll-margin-top or above it, and the current link is the link of the last current section. A click on a link scrolls its section to this place, so a click always marks the clicked link. When the page has a sticky header, give the sections a scroll-margin-top (the header can then cover no heading after a click):
h2, h3 {
scroll-margin-top: 80px;
}The controller uses an IntersectionObserver to see when the top of a section goes past the line, so a scroll reads no layout. During a scroll, the current link gets data-active at once. It gets aria-current="true" when the scroll stops (the scrollend event, or 100 ms after the last scroll in a browser with no scrollend event), so a screen reader does not read each section that the scroll passes. The CSS scroll-target-group property does not replace the controller: it gives no aria-current, no event and no rule for the end of the scroll, and only Chromium has it.
When the scroll stops on "Configure", the browser shows:
<a href="#configure" data-phem-scrollspy-target="link" aria-current="true" data-active>Configure</a>Above the first section, no link is current. At the end of the scroll, the last section is current, also when it is too short to get to the line. A section whose link the user clicked stays current at the end of the scroll while it is in view, until the user scrolls. When two links point to the same section, both get the marks. A change of the height of the content (an image that loads, a section that opens) marks the link again with no scroll. When the sections are in an element that scrolls (overflow: auto or scroll), the line starts at the top of this element. A link to another page, or to an id that the page does not have, gets no mark.
When the current section changes, the controller dispatches phem-scrollspy:change. Its detail has the link and the section, or null for the two above the first section. Use it, for example, to keep the current link in view in a long table of contents:
<nav data-controller="phem-scrollspy" data-action="phem-scrollspy:change->toc#follow">...</nav>The marks belong to the client, so they stay after the morph of an event.
Switch between a light and a dark theme#
phem-theme keeps the choice of the user between a light and a dark theme. The choice goes in localStorage, and <html> gets the theme. Put theme_script() in the <head> of the document. This inline script sets the theme before the first paint, so a dark page does not flash white on load:
#[Component]
function Document(string $title, Html $children): Node
{
return (
<html lang="en">
<head>
<title>{{ title }}</title>
{{ theme_script() }}
{{ importmap('app') }}
</head>
<body><Root>{{ children }}</Root></body>
</html>
);
}Then put the switch in the page. The choose action stores the theme param of the button: light, dark or system:
<div data-controller="phem-theme" role="group" aria-label="Theme">
<button data-phem-theme-target="option" data-action="phem-theme#choose" data-phem-theme-theme-param="light">Light</button>
<button data-phem-theme-target="option" data-action="phem-theme#choose" data-phem-theme-theme-param="dark">Dark</button>
<button data-phem-theme-target="option" data-action="phem-theme#choose" data-phem-theme-theme-param="system">System</button>
</div>After a click on "Dark", the browser shows:
<html lang="en" data-theme="dark" style="color-scheme: dark;">
...
<div data-controller="phem-theme" role="group" aria-label="Theme" data-theme="dark" data-preference="dark">
<button ... data-phem-theme-theme-param="light" aria-pressed="false">Light</button>
<button ... data-phem-theme-theme-param="dark" aria-pressed="true">Dark</button>
<button ... data-phem-theme-theme-param="system" aria-pressed="false">System</button>
</div>data-theme on <html> is always light or dark, so your CSS needs one rule for each theme:
:root { --bg: white; --text: #111; }
:root[data-theme="dark"] { --bg: #111; --text: #eee; }With Tailwind, make the dark: variant use the attribute:
@custom-variant dark (&:where([data-theme="dark"], [data-theme="dark"] *));With system, or with no choice, the theme follows the prefers-color-scheme of the system, also when the system changes it. color-scheme gives the native controls and the scroll bars the same theme.
Other markup for the switch:
- One button: the
toggleaction stores the opposite of the current theme:<button data-controller="phem-theme" data-action="phem-theme#toggle">Theme</button>. - A
<select>or radio buttons: with nothemeparam,choosestores the value of the element. Make each<option>or radio button anoptiontarget, and the controller selects or checks the option of the choice:
<select data-controller="phem-theme" data-action="phem-theme#choose" aria-label="Theme">
<option data-phem-theme-target="option" value="system">System</option>
<option data-phem-theme-target="option" value="light">Light</option>
<option data-phem-theme-target="option" value="dark">Dark</option>
</select>The controller element gets data-theme (the current theme) and data-preference (the choice), for example to show a sun or a moon icon. An option target of the choice gets aria-pressed="true" (a button), checked (a radio button) or selected (an <option>), and the other options lose it.
The theme with no choice is system. To use another default, give it to the script. The script gives it to <html> (data-theme-default), and the switches take it from there:
{{ theme_script(default: 'light') }}When your Content Security Policy blocks inline scripts, give the nonce of the response: {{ theme_script(nonce: csp_nonce('script')) }} (NelmioSecurityBundle).
A choice changes the theme in the other tabs of the site, and in the other switches of the page. A choice that another tab removes (also with localStorage.clear()) gives the default theme again. When the theme or the choice changes, <html> dispatches one phem-theme:change event, also when the page has more than one switch. Its detail has the theme and the preference. Listen to it on the document:
<div data-controller="chart" data-action="phem-theme:change@document->chart#redraw">...</div>The marks belong to the client, so they stay after the morph of an event.
Let the user pick a palette#
phem-palette keeps the choice of the user between named palettes: sets of colors, next to the light and the dark theme of phem-theme. The choice goes in localStorage, and <html> gets data-palette. Put palette_script() in the <head> of the document, with the names of your palettes. This inline script sets the palette before the first paint. The first name is the default:
<head>
{{ theme_script() }}
{{ palette_script(['base', 'contrast']) }}
</head>Then put a picker in the page. The choose action stores the palette param of a button, or the value of a <select> or a radio button:
<select data-controller="phem-palette" data-action="phem-palette#choose" aria-label="Palette">
<option data-phem-palette-target="option" value="base">Base</option>
<option data-phem-palette-target="option" value="contrast">Contrast</option>
</select>The rendered <html> after a choice:
<html lang="en" data-theme="light" data-palettes="base contrast" data-palette-default="base" data-palette="contrast">Give each palette its CSS variables. A palette rule needs a higher specificity than the rules of the light and the dark theme, so the two settings combine:
:root[data-palette="contrast"] { --text: #000; --link: #00e; }
:root[data-palette="contrast"][data-theme="dark"] { --text: #fff; --link: #9cf; }Only a name of palette_script() is a palette: a choice of another name does nothing. A name has only lowercase letters, digits and dashes. To use another default, give it as the second argument: {{ palette_script(['base', 'contrast'], 'contrast') }}. The nonce argument works as in theme_script().
The controller element gets data-palette (the current palette). An option target of the choice gets aria-pressed="true" (a button), checked (a radio button) or selected (an <option>). A choice changes the palette in the other tabs and in the other pickers of the page. <html> dispatches phem-palette:change with the palette after each change. The marks belong to the client, so they stay after the morph of an event.
Show toast messages#
phem-toast shows short messages that disappear after a few seconds. Put one element in the layout:
<div data-controller="phem-toast"></div>It shows the toasts from handlers, failed events (phem:error) and rate limits (phem:rate-limited). A handler shows a toast with $reply->toast():
use Phem\Reply;
use Phem\ToastType;
public function save(Reply $reply): void
{
$this->orders->save($this->order);
$reply->toast('Saved', ToastType::Success);
}The types are ToastType::Info, Success, Warning and Error. The browser also knows the loading type (see Update a toast with its id). With no type (the default), the toast gets no data-type, as in Base UI. A third argument sets the duration of this toast in milliseconds. 0 keeps the toast until the user closes it:
$reply->toast('The order has no stock.', ToastType::Error, 0);A component or a service that the handler calls injects Phem\Toasts. It has a method for each type:
use Phem\Toasts;
public function __construct(
private readonly Toasts $toasts,
) {
}
$this->toasts->error('The schedule starts before it ends.');
$this->toasts->success('Saved', 8000);An empty message throws an InvalidArgumentException. So does a duration below 0 or above Toasts::MAX_DURATION (2147483647 ms, the longest timer of a browser).
A toast shows only with the response of an event. A handler that fails or redirects shows no toast. A toast of a page load doesn't show.
While a modal <dialog> is open, the page under it is inert: the user cannot get to the toasts. A new toast still shows at once, so the user sees the result of an action in the dialog, for example the error of its form. The time of the toasts stops while the modal dialog is open, in a browser that sends the toggle event of a <dialog>: after the close, each toast stays for the rest of its time, and the user can get to it. A phem-toast element in the modal dialog is not inert, and its time runs.
A toast is a phem:toast browser event with the detail {"message": "Saved", "type": "success"}.
Change the default duration and the texts with the controller values: data-phem-toast-duration-value={{ 8000 }}, data-phem-toast-error-message-value, data-phem-toast-rate-limited-message-value and data-phem-toast-close-label-value (the label of the close button, "Close" by default).
The show action shows the detail of another browser event as a toast. A browser event bubbles up to window, so listen there: data-action="phem:saved@window->phem-toast#show".
Each toast has these parts: the root (data-phem-toast), the content ([data-phem-toast-content]), the title ([data-phem-toast-title]), the description ([data-phem-toast-description], the message) and the close button ([data-phem-toast-close]). A toast with no title has no title part. The newest toast is first in the DOM, as in Base UI, so Tab goes to the newest toast first. phem.css shows the newest toast at the bottom (flex-direction: column-reverse). The root gets data-type (the type of the toast) and --toast-index (0 for the newest toast). A toast of a phem:toast event with no type, or with a type that is not info, success, warning or error, has no data-type. A toast gets data-limited and inert when more than the limit value (3) of newer toasts are on the screen: phem.css hides it (a toast with no class). The title, the description and the close button also get data-type. The close button gets aria-hidden="true" while the stack is not expanded and the button has no focus, as in Base UI. A new toast has the animations of the conventions, and a toast that goes stays until the end of its [data-ending-style] transition.
A toast with the same type, title and message as a toast on the screen does not show again: the toast on the screen starts its full time again. A toast with no type and a toast with the info type are not the same. This is different from Base UI, which shows a new toast for each add call unless the id is the same. So two $reply->toast('Saved') in a short time show one "Saved" toast, not two. A toast with the same message as a toast that goes shows as a new toast.
The element is a landmark and a live region, as the viewport of Base UI: it gets role="region", aria-label ("Notifications", or data-phem-toast-label-value), tabindex="-1", aria-live="polite", aria-atomic="false" and aria-relevant="additions text". An attribute that your markup gives stays. Each toast is a dialog that can have the focus: it gets role="dialog", tabindex="0", aria-modal="false", and aria-labelledby and aria-describedby for its title and its description:
<div data-controller="phem-toast" role="region" aria-label="Notifications" tabindex="-1" aria-live="polite" aria-atomic="false" aria-relevant="additions text"
style="--toast-frontmost-height: 42px;">
<div id="phem-toast-2" data-phem-toast role="dialog" tabindex="0" aria-modal="false" aria-describedby="phem-toast-2-description"
style="--toast-swipe-movement-x: 0px; --toast-swipe-movement-y: 0px; --toast-index: 0; --toast-height: 42px; --toast-offset-y: 0px;">
<div data-phem-toast-content>
<span data-phem-toast-description id="phem-toast-2-description">Copied</span>
</div>
<button type="button" data-phem-toast-close aria-label="Close" aria-hidden="true">×</button>
</div>
<div id="phem-toast-1" data-phem-toast data-type="success" role="dialog" tabindex="0" aria-modal="false" aria-describedby="phem-toast-1-description"
style="--toast-swipe-movement-x: 0px; --toast-swipe-movement-y: 0px; --toast-index: 1; --toast-height: 62px; --toast-offset-y: 42px;">
<div data-phem-toast-content data-behind>
<span data-phem-toast-description data-type="success" id="phem-toast-1-description">Saved</span>
</div>
<button type="button" data-phem-toast-close aria-label="Close" data-type="success" aria-hidden="true">×</button>
</div>
</div>The first toast above is the newest: a phem:toast event of your JavaScript with no type, window.dispatchEvent(new CustomEvent("phem:toast", { detail: "Copied" })).
| Key | Action |
|---|---|
| F6 | While there is a toast, moves the focus to the phem-toast element, and expands the stack |
| Tab | Moves the focus to the toasts, from the newest to the oldest, and to their buttons. After the last one, gives the focus back to the element that had it before F6 |
| Shift+Tab | On the phem-toast element, gives the focus back to the element that had it before F6 |
| Escape | Closes the toast with the focus. The focus goes to the next (older) toast, or else to the newer toast, or else back to the element that had it before F6. This is also true after a mouse click on the toast. After a mouse click on the close button, the focus does not move |
To use your own markup, put a <template> part in the element, with the same parts:
<div data-controller="phem-toast">
<template data-phem-toast-target="template">
<div class="toast">
<div class="toast-content" data-phem-toast-content>
<strong data-phem-toast-title></strong>
<p data-phem-toast-description></p>
</div>
<button data-phem-toast-close aria-label="Close">×</button>
</div>
</template>
</div>The content and the title are optional parts of a template. The controller removes the title part from a toast with no title. A template with no title part does not show the title. The content part gets data-behind when a newer toast is in front of its toast, and data-expanded with the stack. Use them to hide the text of the toasts behind the last one in a stack: .toast-content[data-behind]:not([data-expanded]) { opacity: 0; }.
The default styles in phem.css use :where(), so a rule of your app replaces them. A part with a class attribute gets no default style: its classes give all the look. So Tailwind utilities work on the parts of a template, also in their cascade layer. Then hide the toasts with data-limited yourself, for example with data-limited:hidden, and move a toast with the swipe variables (see Close a toast with a swipe).
Add a title to a toast#
Give the title argument for a short title above the message:
$reply->toast('The file is in your downloads.', ToastType::Success, title: 'Export complete');
$this->toasts->error('The card was declined.', title: 'Payment failed');The browser shows the title in the title part of the toast:
<div data-phem-toast-content>
<strong data-phem-toast-title>Export complete</strong>
<span data-phem-toast-description>The file is in your downloads.</span>
</div>The detail of the browser event has the title: {"message": "The file is in your downloads.", "type": "success", "title": "Export complete"}. An empty title throws an InvalidArgumentException. In the detail of an event of your own, a title that is empty or not a text is no title. A toast with the same type, title and message as a toast on the screen does not show again.
Show the stack on hover#
The pointer or the focus of the keyboard on the phem-toast element expands the stack (the focus of a click on a toast does not): the element and each toast get data-expanded, and the time of all the toasts stops. The time that remains goes on when the stack closes: the time does not start again. The time also stops while the browser window does not have the focus. The stack closes when the pointer and the focus go, and when its last toast goes. The state stays after the morph of an event. Each toast gets --toast-height (its height) and --toast-offset-y (the heights of the newer toasts), and the element gets --toast-frontmost-height (the height of the newest toast). The height of a toast is the height of its content (its height with height: auto), so the CSS can give each toast the height of the newest toast: height: var(--toast-frontmost-height, var(--toast-height)). With no toast, the element has no --toast-frontmost-height. Use them for a stack that shows only the newest toast until the pointer comes:
[data-controller~="phem-toast"] { display: block; height: var(--toast-frontmost-height); }
[data-phem-toast] {
position: absolute;
inset: auto 0 0;
transform: translateY(calc(var(--toast-index) * -10px)) scale(calc(1 - var(--toast-index) * 0.05));
transition: transform 200ms;
}
[data-phem-toast][data-expanded] {
transform: translateY(calc(-1 * var(--toast-offset-y) - var(--toast-index) * 8px));
}Close a toast with a swipe#
A swipe of a toast to the right or down closes it. Set data-phem-toast-swipe-direction-value to other directions (up, down, left and right, with a space between them), or to an empty text to turn off the swipe. During the swipe, the toast gets data-swiping, transition: none in its style (so it follows the pointer with no delay), and the distance in --toast-swipe-movement-x and --toast-swipe-movement-y. A toast with no swipe has 0px in the two variables, so a style can use them with no fallback, as the classes of Base UI do. A move in another direction has a resistance. When the value has a horizontal and a vertical direction, the swipe keeps the axis of its first move: a diagonal swipe moves on one axis only. The toast gets data-swipe-direction at its first move in a direction of the value. A swipe of 40 px closes the toast, and its [data-ending-style] transition can go on in that direction:
[data-phem-toast][data-ending-style][data-swipe-direction="right"] {
transform: translateX(calc(var(--toast-swipe-movement-x) + 100%));
opacity: 0;
}During a swipe to the right (a diagonal move of +60, +50):
<div id="phem-toast-2" data-phem-toast data-swiping data-swipe-direction="right" …
style="--toast-swipe-movement-x: 60px; --toast-swipe-movement-y: 0px; …; transition: none;">A shorter swipe removes data-swipe-direction, gives the toast its place again, and the time that remained goes on. A press on the close button, a link or a field of the toast does not start a swipe. phem.css moves a toast with the swipe variables. A touch that moves in a direction of the swipe does not scroll the page, and a touch that moves in another direction scrolls it.
Update a toast with its id#
Give a toast an id. A toast with the id of a toast on the screen updates it: its message, its title, its type, its action and its data change, and its full time starts again. The controller makes the parts again from the template, so an update can add a title or an action that the first toast did not have, and an update with no title removes the title. This is the id of toastManager.add() of Base UI:
$reply->toast('Click again while it is visible to replay the pulse.', title: 'Draft saved', id: 'save-status');A toast with an id is not the same as a toast with the same text and no id. The root gets data-update-key (1 after the first update, then 2, …) and data-updated. The controller removes data-updated and adds it again for each update, so a CSS animation on it plays again:
@keyframes pulse { 50% { scale: 1.04; } }
[data-phem-toast][data-updated] { animation: pulse 0.28s ease; }The loading type is the type of a toast that waits for the answer of the server, as the promise() of Base UI. Show it at the click (see Show a toast at the click), and give the toast of the handler the same id:
<button onClick={{ run }} data-phem-toast-show={{ {message: 'Loading data…', type: 'loading', id: 'job', duration: 0}|json_encode }}>Run</button>public function run(Reply $reply): void
{
$this->jobs->run();
$reply->toast('Success: operation completed', ToastType::Success, id: 'job');
}The browser shows the loading toast at the click, and the toast of the answer replaces its text and its type:
<div id="phem-toast-3" data-phem-toast data-phem-toast-id="job" data-type="success" data-update-key="1" data-updated ...>Show a toast at the click#
A click on an element with data-phem-toast-show shows its toast at once, before the server answers the event of the same click. The value is the detail of a phem:toast event, in JSON: a message, or an object with the options of a toast.
Add an action to a toast#
The action option gives the label of an action button, as the actionProps of Base UI. Put a [data-phem-toast-action] part in the template: the controller removes it from a toast with no action. A click on the button closes the toast, and dispatches a phem:call event on it, so the onCall handler around the phem-toast element runs. $e->detail has the action, the id and the data of the toast:
public function delete(Reply $reply): void
{
$this->items->delete($this->id);
$reply->toast('You can undo this action.', ToastType::Success, 10000, 'Action performed', id: 'delete-'.$this->id, action: 'Undo', data: ['item' => $this->id]);
}
public function undo(Event $e, Reply $reply): void
{
$this->items->restore($e->detail['data']['item']);
$reply->toast('Action undone');
}<div data-controller="phem-toast" onCall={{ undo }}>
<template data-phem-toast-target="template">
<div>
<p data-phem-toast-description></p>
<button data-phem-toast-action></button>
</div>
</template>
</div>The browser dispatches phem:call with this detail: {"action": "Undo", "id": "delete-7", "data": {"item": 7}}. Check it in the handler, as you check $e->value.
Show data in a toast#
The data option is an array of names and values, as the data of Base UI. A value is a text, a number, a boolean or null: Toasts::show() and $reply->toast() throw an InvalidArgumentException for a list or for a value that is an array or an object. Each [data-phem-toast-data="name"] part of the template shows the value of its name. The controller removes a part with no value:
$reply->toast('Toast with custom data', data: ['userId' => '123']);<p>data.userId is <span data-phem-toast-data="userId"></span></p>Anchor a toast to an element#
The anchor option is the id of an element: the toast shows next to it, as the positionerProps of Base UI, for feedback such as "Copied" next to a copy button. The toast gets data-anchored, and the position of a popover (see Position): position: fixed, data-side and the CSS variables. The side value of the element (top by default) and its side-offset value place it. An [data-phem-toast-arrow] part of the template points to the anchor:
$reply->toast('Copied', duration: 1500, anchor: 'copy-button', viewport: 'anchored');<div data-controller="phem-toast" data-phem-toast-name-value="anchored" data-phem-toast-side-offset-value="10">
<template data-phem-toast-target="template">
<div class="tip"><span data-phem-toast-arrow></span><span data-phem-toast-description></span></div>
</template>
</div>Show the toasts in more than one place#
A page can have more than one phem-toast element: for example a stack at the bottom, and the anchored toasts. Give an element a data-phem-toast-name-value, and give a toast the viewport option with this name. An element with a name shows only the toasts with its name. The element with no name shows the toasts with no viewport, and the toasts of phem:error and phem:rate-limited.
Copy text to the clipboard#
phem-clipboard copies text to the clipboard and shows brief feedback:
<div data-controller="phem-clipboard">
<code data-phem-clipboard-target="source">{{ token }}</code>
<button data-action="phem-clipboard#copy">Copy</button>
<span data-phem-clipboard-target="feedback"></span>
</div>It copies the text value, or the text of the source target when there’s no value. The feedback targets show the label value (“Copied” by default) for the duration value (2000 ms by default), and the controller element gets data-copied for the same time. The controller dispatches phem-clipboard:copied (detail text), or phem-clipboard:error (detail error) when the browser does not copy. Browsers allow a page to copy only on HTTPS or localhost.
Count the characters of a field#
phem-count shows the number of characters of a text field while the user types:
<div data-controller="phem-count">
<textarea phem:bind={{ bio }} maxlength="160" aria-label="Bio" data-phem-count-target="field"></textarea>
<span data-phem-count-target="output">{{ bio|length }}/160</span>
</div>After the user types "Hello", the browser shows:
<span data-phem-count-target="output" data-count="5" data-max="160">5/160</span>The maximum is the maxlength of the field, or the max value (data-phem-count-max-value={{ 140 }}). The max value is a soft limit: the user can type more, and the controller element and the outputs get data-over. Each output shows the data-phem-count-template attribute, where {count}, {max} and {remaining} get their numbers. The default is {count}/{max}, or {count} with no maximum:
<span data-phem-count-target="output" data-phem-count-template="{remaining} characters left"></span>When the field targets are checkboxes, the count is the number of checked boxes, for a "3 selected" text. Give the output a server render of the count, for the time before the controller connects. The count belongs to the client, so it stays right after the morph of an event. The output has no aria-live: a screen reader that reads each key is noisy. Add aria-live="polite" to the output when your users need it.
Select images with a preview#
phem-image-input shows the images of a file input before the upload, and makes the large images smaller in the browser:
<div data-controller="phem-image-input"
data-phem-image-input-max-width-value={{ 1600 }}
data-phem-image-input-preview-class="h-20 w-20 object-cover">
<input type="file" name="photos[]" multiple accept="image/*" data-phem-image-input-target="input" />
<div data-phem-image-input-target="preview"></div>
</div>After the user selects beach.jpg, the preview has:
<div data-phem-image-input-target="preview">
<img src="blob:https://app.example/3f6c…" alt="beach.jpg" class="h-20 w-20 object-cover">
</div>| Part | Attribute | Effect |
|---|---|---|
| Input | data-phem-image-input-target="input" |
The file input |
| Preview | data-phem-image-input-target="preview" |
Gets an <img> for each image. An <img> target shows the first image, and its own src with no file |
| Controller | data-phem-image-input-preview-class |
The classes of each <img> of a preview |
| Controller | data-phem-image-input-max-width-value, -max-height-value |
The maximum size in pixels of an image (0, the default, is no limit) |
| Controller | data-phem-image-input-type-value |
The media type of the new images, such as image/webp |
| Controller | data-phem-image-input-quality-value |
The quality of a JPEG or a WebP, from 0 to 1 (0.85) |
The controller element has data-resizing while the browser resizes, and a submit waits for the end. The previews belong to the client: the morph of an event keeps them, and they go when the handler calls $reply->clearFiles(). The controller dispatches phem-image-input:resized with the new files. For the rules of the resize, see Make the images smaller before the upload.
Show the text of a password#
phem-reveal shows and hides the text of a password field:
<div data-controller="phem-reveal">
<input type="password" name="password" aria-label="Password" data-phem-reveal-target="field" />
<button type="button" data-phem-reveal-target="trigger">Show</button>
</div>A click on a trigger shows the text (type="text"), and a second click hides it again. The toggle, show and hide actions do the same from another element. The field keeps its value. A click on the trigger gives the focus to the trigger. When the field has the focus (for example, a keyboard shortcut calls toggle), it keeps the focus and its selection. After a click, the browser shows:
<div data-controller="phem-reveal" data-phem-reveal-revealed-value="true" data-revealed>
<input type="text" name="password" ... data-revealed />
<button type="button" data-phem-reveal-target="trigger" aria-pressed="true" data-revealed>Show</button>
</div>A trigger gets aria-controls when the field has an id. Use data-revealed to change the label or the icon in CSS. The server can hide the text again with data-phem-reveal-revealed-value={{ false }}. A shown password stays a shown password after the morph of an event. In a Symfony form with live validation, the morph also keeps the value of a shown password, as it keeps the value of a hidden one.
Select all the rows#
phem-checkbox-group gives one state to a group of checkboxes, and adds a parent checkbox that checks and unchecks the items, for a "select all" box of a list or of a table:
<table data-controller="phem-checkbox-group phem-count">
<tr>
<th><input type="checkbox" aria-label="All" data-phem-checkbox-group-target="parent" /></th>
<th><span data-phem-count-target="output" data-phem-count-template="{count} selected"></span></th>
</tr>
{% for row in rows %}
<tr key={{ row.id }}>
<td><input type="checkbox" phem:bind={{ selected }} value={{ row.id }} aria-label={{ row.name }}
data-phem-checkbox-group-target="item" data-phem-count-target="field" /></td>
<td>{{ row.name }}</td>
</tr>
{% endfor %}
</table>An item and the parent are native checkboxes, or phem-toggle checkboxes:
<div data-controller="phem-checkbox-group" data-phem-checkbox-group-value-value='["pear"]'>
<label>
<span role="checkbox" data-controller="phem-toggle" data-phem-checkbox-group-target="parent"></span>
All
</label>
<label>
<span role="checkbox" data-controller="phem-toggle" data-value="apple" data-phem-checkbox-group-target="item">
<input type="checkbox" name="fruits[]" value="apple" hidden data-phem-toggle-target="input">
</span>
Apple
</label>
...
</div>The value of an item is its data-value attribute, its value (the value attribute of a native checkbox, the value value of a phem-toggle element), its key (key={{ ... }}), or its position.
| Value | Default | Effect |
|---|---|---|
value |
The values of the checked items. The controller writes it after each change. When the server render gives it, the group owns the state of the items: the value sets them on load and after the morph of an event, and a server render with a new value sets it | |
all-values |
all the items | The values of the items that the parent checks and unchecks |
With no value in the server render, the state of the items belongs to the server render and to the user, for example with phem:bind.
A click on the parent checks all its items, or unchecks all of them when all are checked. When some items are checked, the parent remembers them: the next clicks go to all the items, to no item, and back to the items that were checked. A change of an item by the user is the new mixed state. A disabled item, and a read-only phem-toggle item, keeps its state. Each item that changes dispatches input and change (on a phem-toggle item, on its input), so phem:bind, phem:live and phem-count see the change. With phem:live, the changes of the fields of one phem:bind property go in one event.
The parent shows the state of its items: it is checked when all the items are checked, and indeterminate when some of them are checked. It gets data-checked, data-unchecked or data-indeterminate, and aria-controls with the items that have an id. A phem-toggle parent also gets aria-checked="mixed". The parent shows the state again after the morph of an event.
A group can be an item of the group around it, for a tree of permissions. Give the element of the inner group a data-value:
<div data-controller="phem-checkbox-group">
<label><input type="checkbox" data-phem-checkbox-group-target="parent"> User permissions</label>
<label><input type="checkbox" value="view-dashboard" data-phem-checkbox-group-target="item"> View dashboard</label>
<div data-controller="phem-checkbox-group" data-value="manage-users">
<label><input type="checkbox" data-phem-checkbox-group-target="parent"> Manage users</label>
<label><input type="checkbox" value="create-user" data-phem-checkbox-group-target="item"> Create user</label>
<label><input type="checkbox" value="edit-user" data-phem-checkbox-group-target="item"> Edit user</label>
</div>
</div>The inner group is checked when all its items are checked: its data-value is then in the value of the outer group. When some of its items are checked, the outer parent is indeterminate. The outer parent checks and unchecks all the items of the inner group.
Each change by the user dispatches phem-checkbox-group:change with { value }. After a click on the native row 1 of three, the parent is:
<input type="checkbox" aria-label="All" data-phem-checkbox-group-target="parent" data-indeterminate />After a click on Apple in the group of phem-toggle checkboxes, the browser shows:
<div data-controller="phem-checkbox-group" data-phem-checkbox-group-value-value="["apple","pear"]">
<label>
<span role="checkbox" data-controller="phem-toggle" data-phem-checkbox-group-target="parent" data-phem-toggle-checked-value="false" data-phem-toggle-indeterminate-value="true" tabindex="0" aria-checked="mixed" data-indeterminate=""></span>
All
</label>
<label>
<span role="checkbox" data-controller="phem-toggle" data-value="apple" data-phem-checkbox-group-target="item" data-phem-toggle-checked-value="true" tabindex="0" aria-checked="true" data-phem-toggle-indeterminate-value="false" data-checked="">
<input type="checkbox" name="fruits[]" value="apple" hidden="" data-phem-toggle-target="input" tabindex="-1" aria-hidden="true">
</span>
Apple
</label>
...
<label>
<span role="checkbox" data-controller="phem-toggle" data-value="plum" data-phem-checkbox-group-target="item" data-phem-toggle-checked-value="false" tabindex="0" aria-checked="false" data-unchecked="">
<input type="checkbox" name="fruits[]" value="plum" hidden="" data-phem-toggle-target="input" tabindex="-1" aria-hidden="true">
</span>
Plum
</label>
</div>Show a time in the time zone of the user#
The server has no session, so it does not know the time zone of the user. phem-time shows the datetime of a <time> element in the time zone and the language of the browser:
<time datetime={{ comment.createdAt|date('c') }} data-controller="phem-time">{{ comment.createdAt|date('Y-m-d H:i') }} UTC</time>The browser shows a relative time, and the full date in the title:
<time datetime="2026-11-11T13:57:00+00:00" data-controller="phem-time" data-past title="November 11, 2026 at 2:57 PM">3 minutes ago</time>| Value | Default | Effect |
|---|---|---|
format |
relative |
relative ("3 minutes ago", "in 30 seconds"), date, time or datetime |
date-style, time-style |
The style of date, time and datetime: short, medium, long or full |
The language is the lang attribute of the element or of the page. A Symfony locale such as fr_FR works as the tag fr-FR, and a language that the browser does not know gives the language of the browser. A relative time updates while the page shows. The element gets data-past or data-future, and a title when the server render gives none. When a future time comes, the element gets data-past and dispatches phem-time:reached, which bubbles to the controllers around it. Use data-future for a countdown:
<p class="resend">
<span>You can ask for a new code <time datetime={{ resendAt|date('c') }} data-controller="phem-time">soon</time>.</span>
<button onClick={{ resend }}>Send a new code</button>
</p>.resend:has(time[data-future]) button,
.resend:not(:has(time[data-future])) span { display: none; }The text before the controller connects is the server render, so give it a text that is correct in all time zones, such as the UTC time. The text belongs to the client: after the morph of an event, the controller shows it again, also for a new datetime.
Focus a field after an event#
phem-autofocus focuses its element when the element connects. The native autofocus attribute works only on page load, but this controller also works when an event adds the element:
{% if editing %}
<input name="title" data-controller="phem-autofocus" />
{% endif %}To focus a child, put the controller on a parent and add data-phem-autofocus-target="focus" to the child.
The element gets the focus on page load and in the morph of an event that the user started. A push or a refresh of the server doesn’t move the focus. It doesn’t steal the focus from a field the user is editing.
To scroll to a new element with no focus, see Scroll to a new item after an event.
Open the iOS keyboard after a round trip#
iOS opens the keyboard only for a focus inside a tap. After a server round trip, such as a Turbo visit or the morph of an event, the field gets the focus but no keyboard opens. Add data-phem-autofocus-keyboard to the link or to the event element that shows the field:
<a href="/search" data-phem-autofocus-keyboard>Search</a>
<button onClick={{ edit }} data-phem-autofocus-keyboard>Edit</button>During the tap, a hidden field takes the focus and the keyboard opens. The hidden field has the name of the link or the button (its aria-label, its aria-labelledby or its text) and aria-busy="true", so a screen reader reads a field that loads, for example "Search, busy". The keyboard stays open when the phem-autofocus field takes the focus, also inside an open <dialog>. These edge cases apply:
- No field takes the focus: the keyboard closes, and the element that had the focus before the tap gets it back
- The visit is cancelled or fails: when a listener cancels the visit or its fetch fails, the keyboard closes right away; a visit that takes more than 30s closes it too
- Slow network: the keyboard opens before the new page shows
- The user is editing a field: the attribute does nothing
- Turbo Frames: the link must start a Turbo Drive visit; a link that a
<turbo-frame>loads doesn’t open the keyboard
Ask for confirmation before an event#
phem-confirm asks the user to confirm before an event goes to the server:
<button onClick={{ delete }} data-controller="phem-confirm"
data-phem-confirm-message-value="Delete this item?">Delete</button>With no message value, the question is “Are you sure?”. When the user cancels, the browser sends no event.
Ask in a dialog of the page#
By default, window.confirm() asks the question. To ask it in a <dialog> of your page, give the selector of the dialog in the dialog value:
<button onClick={{ clear }} data-controller="phem-confirm"
data-phem-confirm-message-value="Empty the bag?"
data-phem-confirm-accept-value="Empty bag"
data-phem-confirm-dialog-value="#confirm">Empty bag</button>
<dialog id="confirm" aria-describedby="confirm-message">
<form method="dialog">
<p id="confirm-message" data-phem-confirm-message></p>
<button value="cancel">Back</button>
<button value="confirm" data-phem-confirm-accept>Yes</button>
</form>
</dialog>When the user clicks Empty bag, the controller does these steps:
- It holds the event: no event goes to the server yet.
- It puts the
messagevalue in the[data-phem-confirm-message]elements of the dialog, and theacceptvalue in its[data-phem-confirm-accept]elements. With noacceptvalue, the accept button keeps the label of the page. - It opens the dialog with
showModal(). - When the dialog closes with the return value
confirm, the held event goes, with the values of the click. Any other close, such as Back, Escape or a tap on the backdrop, sends no event.
A tap on the backdrop has the same rule as in phem-dialog.
When the question ends, the dialog gets the texts of the page again. So one dialog can serve all the phem-confirm elements of the page, and the data-phem-guard elements too (see Unsaved changes): put it once in the layout. Give the dialog an accessible description, such as aria-describedby on the message, so a screen reader reads the question.
These edge cases apply:
- A morph while the dialog asks: the morph does not change the dialog, its texts or its
openattribute - A morph or a removal of the element of the event: the event drops and the dialog closes. After the morph, the handler can belong to another component, so the event could run on the wrong item. The user clicks again
- A new question while the dialog asks: the new question replaces the open question
- A Turbo visit while the dialog asks: the event drops when Turbo caches the page (
turbo:before-cache). The dialog closes and gets the texts of the page again, so a visit back shows no old question - The dialog cannot ask: when the selector is not valid, finds no
<dialog>, or finds a dialog that is open with no question, the browser logs an error andwindow.confirm()asks
A field with a phem-confirm gets the value of the server render while the dialog is open. When the user confirms, the event sends the value that the user chose, and the server render shows it.