Controllers
A controller is a small class that adds one behavior to an element: listening, dismissing, positioning, moving focus, dragging. Radiant UI components are assembled from them, and so can yours be.
Controllers need a browser. Pure logic with no DOM (dates, masks, numbers, ordering math) is in Helpers. Both entries are tree-shakeable: a bundle contains only what it imports.
import { createSwipeController } from '@ecopages/radiant-ui/controllers';The shape of a controller
Every controller is a plain class with attach() and destroy(). The constructor does nothing; attach() starts work and destroy() ends all of it, and both are safe to call again. Each class has a createX(host, config) factory that attaches on connect and destroys on disconnect, where host is a RadiantElement or anything with registerConnectedCallback and registerCleanupCallback. Without a host, construct the class and call attach() and destroy(), as the examples on these pages do. See bindController.
Config is getters (getItems, getTarget) and callbacks (onChange), so the element keeps its own state and the controller never reaches into it. Where a decision may need to differ, a controller documents a protected hook; see Composing a component.
Controllers
| Controller | What it does | Reach for it when |
|---|---|---|
| bindController | The contract and the connect and cleanup wiring behind every factory. | You write a new controller, or hold one without a factory. |
| EventListenerController | Listens to event types on a target while attached; typed from target and type. | Listeners on document or window, added and removed at runtime, or with once. |
| MutationController | A MutationObserver on a node. | You react to children or attributes changing. |
| ResizeController | A ResizeObserver on elements. | You react to an element changing size. |
| DismissController | Reports a pointer press outside a surface, focus leaving it, or Escape. | A dialog, drawer or popup that closes itself. |
| PopoverController | Portal mount, floating position, dismissal and teardown of a popup. | A popup, hover card or menu panel beside an anchor. |
| PortalController | Moves a node into document.body (or a container) and carries the origin theme, color-scheme and direction. | A surface that must escape clipping or stacking contexts. |
| FocusTrapController | Keeps Tab inside a modal surface and returns focus on destroy. | A modal dialog. |
| FocusWithinController | One enter when focus comes into an element and one leave when it goes outside; a popup portaled out of it counts as inside. | A field or a card that reacts to focus anywhere inside it. |
| ScrollLockController | Locks scroll on the page or a scroll container, counted across controllers. | A modal that must stop the page or a container scrolling. |
| RovingController | Roving tabindex with arrows, Home and End over the items you return. | Tabs, toolbars, lists and grids with one tab stop. |
| SwipeController | Reports a left, right, up or down swipe on release. | A carousel or pager. |
| PointerDragController | Pointer capture, move, end and cancel, with position, delta and the element under the pointer. | A slider, splitter, resize handle or free-form drag. |
| SortableController | Reorders items, or moves them between containers, by pointer or keyboard with announcements. It never moves a node: onChange does. | A reorderable list or board, including drag between lists. |
| MenuTreeController | Nested submenu coordination: timers, keyboard, popovers. | A menu button or menubar with submenus. |
| CaptionsController | Captions and subtitles for a media element from cues, WebVTT text or a URL, one shown at a time, kept above your controls. | A video player with your own captions button and language menu. |
| MediaController | Playback core of an audio or video element: one state snapshot, per-frame progress, seek, scrub, volume, rate. | You build an audio or video player with custom controls. |
| AudioVisualizerController | Feeds a preset (bars, wave or your own) the sound of an audio or video element while it plays, and swaps presets at runtime. | A level meter or waveform next to a player. |
| CopyController | Copies text to the clipboard on a click, with a state attribute, a fallback and an announcement. | A copy button on a code sample, link or token. |
| MediaRecorderController | Records the microphone or camera: permission states, pause, time limit, result URL cleanup and a stream other controllers can read. | Voice notes or a camera capture. |
| HotkeyController | Runs a handler for a key string such as mod+k or ArrowLeft, skipping text fields and keys another handler took. | Keyboard shortcuts for a widget, a player or the page. |
| StickToBottomController | Keeps a scroller pinned to new content while the reader is at the bottom, and counts what arrives after they scroll up. | A chat, agent log or live feed. |
| VirtualizerController | Works out which rows of a long list are in view, with measured sizes, scroll to index and find in page. | A list, table or feed with thousands of rows. |
| SelectionController | Single and multiple selection: click chooses or toggles, Shift extends a range from an anchor, Ctrl/Cmd+A selects all. Writes no attribute. | A listbox, table, tag group or grid with a value of selected items. |
| HoldController | Reports a press held for a delay, cancels on movement, scroll or Escape, and sets data-holding and --hold-delay for a progress fill. | A long-press menu, hold to confirm. |
| RepeatController | Steps on press and keeps stepping while held, with Enter, Space and an assistive-technology click stepping too. | A stepper, a scroll arrow. |
| AnnouncerController | Announces messages through a polite and an assertive live region it inserts, so a repeat is heard again. | A sortable list, a filter count, an async status. |
| PreferenceController | One value from a fixed list, written as an attribute, saved across visits and kept in step across tabs. | A saved choice such as density, a layout or a mode. |
| ColorSchemeController | Saves system, light or dark and applies it as color-scheme plus a class or attribute, following the system while system is chosen. | A light, dark and system switch. |
DOM utilities
The same entry exports functions that touch elements. They have no page of their own; the TSDoc of each says how to use it.
| Export | What it does | Reach for it when |
|---|---|---|
applyFloatingPosition, attachFloating | Floating placement with flip and clamping, applied to an element and kept on scroll and resize. | You position a surface without a popover. |
queryFocusableCandidates, getFocusableElements, findFirstFocusableCandidate, queryRovingTabindexItems | Query focusable descendants; getFocusableElements also skips hidden ones (needs layout). | Custom focus loops and initial focus. |
applyRovingTabindex, focusRovingItem, navigateRovingTabindex | The roving tabindex functions RovingController composes. | One-off roving navigation without a controller. |
getVisibleCollectionOptions, setCollectionActive, clearCollectionActive | aria-activedescendant and data-active bookkeeping for option collections. | Active-descendant lists, such as a combobox you build yourself. |
SORTABLE_*_ATTRIBUTE, SORTABLE_OFFSET_PROPERTIES | The data-sortable-* attributes and --sortable-dx and --sortable-dy the sortable sets. | Styling a sortable. |
Accessibility of the utilities
queryFocusableCandidates matches links, buttons, form controls except input[type=hidden], summary, [contenteditable], audio and video with controls, iframe and [tabindex] other than -1; getFocusableElements also drops nodes that are not rendered, disabled (a <fieldset disabled> ancestor included) or inside [hidden] or [inert]. setCollectionActive gives an option without an id a document-unique one and sets aria-activedescendant on the host and data-active on the option; the caller provides role="option", aria-selected, aria-expanded and the host's role. isRovingKeyIgnored tells a key press that belongs to the browser or to a text field (a textarea, contenteditable element or text-entry input) from one for roving navigation.
How the examples run
Each example on these pages is a mount(root) function in the page's script. It receives one [data-example="<name>"] element, wires the controllers to that element and returns the function that undoes it, which is where destroy() goes. A controller that listens on document or window must be destroyed there, or it keeps listening after the example is gone.
The page script ends with a call to mountExample, which the code blocks leave out. It mounts each matching element once when the page loads and again for the new elements the router swaps in (eco:page-load and eco:after-swap), and it runs the returned function for an element that left the page. A script is a module and runs once per tab, so the page cannot rely on it running again after a navigation.