Skip to main content

Accessibility

Accessibility principles, keyboard interaction models, and ARIA contracts in Moraine.

View as Markdown

Moraine implements keyboard navigation, focus management, and ARIA relationships based on the W3C WAI-ARIA Authoring Practices. Applications still need accessible names, readable color contrast, and appropriate semantics for custom renderers and polymorphic elements.

Keyboard Interaction Models#

Moraine components follow established UI patterns for keyboard interaction:

Collection Navigation#

Tabs uses roving tabindex: the highlighted tab has tabindex="0", and other tab triggers have tabindex="-1". Select-family controls keep DOM focus on their trigger or input and identify the highlighted option through aria-activedescendant. Menus move focus between their items. These models support arrow navigation without adding every option to the page’s tab order:

Key Action
↓ / → Moves to the next item along the component’s navigation axis.
↑ / ← Moves focus to the previous item.
↖ Moves focus directly to the first enabled item.
↘ Moves focus directly to the last enabled item.
↵ / Space Activates or selects where supported; editable inputs retain normal text entry.

Looping and activation depend on the component and its options. See each component’s keyboard documentation for orientation, disabled items, typeahead, and selection.

Overlay Dismissal#

Dismissible overlays (Dialog, Sheet, Drawer, Popover, Tooltip, HoverCard, DropdownMenu, ContextMenu, and select popups) close on Esc:

  • Only the topmost active overlay handles Esc, so closing a nested overlay leaves its parent open.
  • Esc is ignored while an IME composition is active.
  • On overlays with closeOnEscape, pass closeOnEscape={false} to block Esc dismissal; onClosePrevented reports the blocked attempt.

Focus Management#

Focus Trapping in Modals#

Modal surfaces (Dialog, Sheet, Drawer, and BaseDialog) trap keyboard focus inside their content while open:

  • Tabbing past the last focusable element wraps back to the first.
  • Outside content is hidden from assistive technology and page scroll is locked while open.
  • Popover is non-modal and does not trap focus.
  • Passing modal={false} on a modal surface disables focus trapping, initial focus movement, and focus restoration.

Focus Restoration#

  • Modal overlays: Restore focus to their trigger after closing (or to the previously focused element). Pass finalFocus to override the target element.
  • Non-modal floating overlays: Restore focus after keyboard dismissal, while allowing outside pointer presses to move focus naturally to the clicked target.
  • Tooltips: Never move focus away from their trigger.

Initial Auto-Focus#

When a modal surface opens:

  • It focuses the first [autofocus] or [data-autofocus] descendant, falling back to the first focusable child, and otherwise the content container itself.
  • When opened by touch, it focuses the content container directly.
  • Pass initialFocus to target a specific element, or initialFocus={false} to skip moving focus.
  • Menus and select-family controls manage their own highlighted item; tooltips never move focus into their content.

ARIA Attribute Contracts#

Moraine sets and updates these ARIA attributes from component state:

  • State indication: Components maintain aria-expanded="true|false" on triggers, aria-selected="true|false" on selection items, and aria-checked="true|false|mixed" on checkboxes.
  • Relationship wiring: Triggers and contents are linked through aria-controls with deterministic element IDs generated by createId.
  • Overlay names: Dialog.Content and Sheet.Content use title as aria-labelledby (unless aria-label is set) and description as aria-describedby. BaseDialog.Content renders role="dialog" and needs an aria-label or aria-labelledby.
  • Form association: Field binds its label to the registered control through aria-labelledby, and its hint, description, error, and help text through aria-describedby. hiddenLabel supplies a visually hidden label when label is empty.
  • Validation: When a field has an error, Moraine sets aria-invalid="true" on the control and adds the visible error message to aria-describedby.
import { Field, Input } from 'moraine'

export function AccountField() {
  return (
    <Field label="Email Address" description="We will never share your email.">
      {/* Automatically wires id, aria-labelledby, and aria-describedby */}
      <Input type="email" placeholder="[email protected]" />
    </Field>
  )
}

Screen Reader Considerations#

  • Semantic HTML: Moraine uses native controls such as <button> and <input> alongside ARIA roles for custom widgets and overlay surfaces. When you use the as prop, the rendered element must provide the same semantics.

  • Icon-only buttons: Provide an accessible name with aria-label:

    import { Button } from 'moraine'
    
    export function CloseSettingsButton() {
      return (
        <Button aria-label="Close settings" variant="ghost" size="icon-md" leading="icon-close" />
      )
    }
  • Built-in labels: Accessible names that Moraine supplies, such as the Dialog close button, come from locale messages. Override them with a component’s translations prop or MoraineProvider messages; see the Localization guide.