Skip to Content
CodeUI

States

@codeui/styles v0.3.0

The state attributes a behavior layer sets, and what the CSS does with them.

The stylesheets style state but never change it. Native elements report some state through pseudo-classes such as :disabled, :checked, and :focus-visible. Everything else comes from attributes that your code sets.

states/field.html
<div class="cui-stack" data-gap="5" style="max-width: 360px; margin-inline: auto">  <div role="group" class="cui-textfield cui-field" data-cui="textfield" data-field-size="sm">    <label for="name-invalid" class="cui-field-label" data-cui="textfield-label">File name</label>    <input      id="name-invalid"      type="text"      value="my snippet?.png"      aria-invalid="true"      aria-describedby="name-invalid-error"      class="cui-field-input cui-textfield-input"      data-cui="textfield-input"      data-size="sm"      data-variant="filled"      data-invalid    />    <div id="name-invalid-error" class="cui-field-error" data-cui="textfield-error-message">      Use letters, numbers, and dashes.    </div>  </div>  <div role="group" class="cui-textfield cui-field" data-cui="textfield" data-field-size="sm">    <label for="name-disabled" class="cui-field-label" data-cui="textfield-label">Export folder</label>    <input      id="name-disabled"      type="text"      value="~/Pictures/CodeImage"      disabled      class="cui-field-input cui-textfield-input"      data-cui="textfield-input"      data-size="sm"      data-variant="filled"      data-disabled    />  </div></div>

The invalid field above is static markup: data-invalid on the input and an error message element. A real form toggles both when validation runs, and sets aria-invalid and aria-describedby to match.

State attributes

AttributeMeaning
data-checkedA checkbox, switch, radio, or toggle is on.
data-indeterminateA checkbox is mixed. Progress has no known value.
data-selectedAn option or tab is selected.
data-highlightedAn option has virtual focus in a list or menu.
data-disabledThe element is disabled. Set it with native disabled on form controls.
data-invalidThe value failed validation.
data-focus, data-hoverFocus or hover reported by a behavior layer, for elements that cannot use the pseudo-classes.
data-expandedA trigger's popup or an accordion item is open.
data-open, data-opened, data-closed, data-visibleOverlay lifecycle. They start and end enter and exit animations.
data-loadingWork is pending.
data-motion="none"Suppresses overlay motion.
data-swipeToast swipe phase: move, cancel, or end.

Presence is enough: data-checked and data-checked="" select the same rules. Remove the attribute to clear the state.

Each component page lists the states its parts read.

Input modality

Some focus and motion rules depend on the last input. Set data-cui-input on the root element to keyboard or pointer:

const root = document.documentElement;
addEventListener("keydown", () => root.setAttribute("data-cui-input", "keyboard"), true);
addEventListener("pointerdown", () => root.setAttribute("data-cui-input", "pointer"), true);

With keyboard, tab and segmented-control indicators and accordion content change without animation. With pointer, the switch hides its focus ring after a click.

Geometry variables

Overlays and progress bars read a few custom properties that only code can know.

VariableValue
--cui-popper-anchor-widthWidth of the trigger an overlay is attached to.
--cui-popper-content-available-heightHeight available to an overlay before it hits the viewport.
--cui-popper-content-transform-originOrigin for the overlay's scale animation.
--cui-progress-fill-widthProgress value as a width, such as 50%.
--cui-toast-progress-fill-widthRemaining toast time as a width.
--cui-toast-swipe-move-x, --cui-toast-swipe-end-xCurrent and final horizontal swipe distance.

Static demos set these inline, for example style="--cui-progress-fill-width: 40%".

Accessibility is yours

CSS cannot add roles, names, or keyboard support. The markup on the component pages includes the ARIA attributes a working widget needs, but the attributes describe one frozen moment. Keep them in sync with the data-* state, move focus as the WAI-ARIA patterns describe, and return focus when an overlay closes.

On this page