Core modules
Core modules
Section titled “Core modules”The src/core directory hosts the primitives that every pattern, context, and plugin shares. Use this page as a map when you need to wire a bespoke behavior or understand how higher-level features stay in sync.
Attributes (@core/attributes)
Section titled “Attributes (@core/attributes)”ensureId(element, prefix)guarantees repeatable ARIA wiring by stamping IDs when authors omit them.setAriaExpanded,setAriaHidden,appendToken, andremoveTokenkeep boolean state and token lists synchronized across trigger and target elements.- Prefer these helpers over manual attribute juggling so you inherit the library’s whitespace and UUID safeguards.
import { ensureId, setAriaExpanded } from 'automagica11y/core';
const trigger = document.querySelector<HTMLButtonElement>('[data-automagica11y-toggle]');if (trigger) { ensureId(trigger, 'docs-trigger'); setAriaExpanded(trigger, false);}ARIA (@core/aria)
Section titled “ARIA (@core/aria)”setIfAbsent(element, name, value)writes defaults without clobbering author overrides. Contexts use it to respect custom roles and labels.
Classes (@core/classes)
Section titled “Classes (@core/classes)”- Truthiness-aware parsing lets authors describe class hooks using any of the reserved state keywords (
open,shown,true, etc.). createClassToggler(trigger, options)returns a function that applies the correct class set to the trigger and target for each boolean state.getClassConfigandapplyClassesare available when you need to inspect or reuse the parsed configuration directly.
Share configuration
Reuse the toggler returned from
createClassToggler whenever multiple surfaces mirror the same state so you avoid re-parsing author attributes. Contexts (@core/context/*)
Section titled “Contexts (@core/context/*)”Context logic allows toggle-based patterns to layer richer semantics and behaviors:
context/aliases.tsmaps friendly names (modal,tabs,treeview) to canonical registry keys.normalizeContext(value)accepts any alias and returns the canonical key registered inContexts.applyContext(trigger, target, contextName, mode)orchestrates semantics (aria-*, roles) and behaviors (focus traps, hover intent, inert background) based on the selected mode (all,semantics-only, orbehaviors-only).helpers.tsexposes a toolbox of reusable behaviors: focus traps, Escape-to-close, hover intent, inert siblings, anchor-follow notifications, and more.registry.tsenumerates the built-in contexts (dialog,tooltip,menu,accordion,disclosure,listbox,tablist,tree) and records which capabilities each one enables today. Dialog and tooltip ship with complete behaviors; the remaining contexts currently provide semantic scaffolding so authors can build progressively.
Capability reference
Section titled “Capability reference”| Capability | Helper | Purpose |
|---|---|---|
semantics | helpers.link, helpers.setRole, helpers.setAria | Applies ARIA relationships and roles safely. |
focusTrap | helpers.focusTrap | Traps focus inside a dialog surface. |
escToClose | helpers.closeOnEscape | Wires Escape to the owning toggle controller. |
inertSiblings | helpers.inertSiblingsWhileOpen | Locks background content while a modal dialog is active. |
restoreFocus | helpers.restoreFocusOnClose | Returns focus to the opener after dismissal. |
hoverIntent | helpers.hoverIntent | Provides pointer/focus/touch hover intent for tooltip-style surfaces. |
anchorFollow | helpers.anchorFollow | Emits automagica11y:context:anchor so positioning plugins can react. |
rovingTabindex, typeahead, arrowNav | Planned | Reserved for future interactive list patterns. |
Use the capabilities array exposed on each context to detect what semantics and behaviors are currently automated and where you may need to augment with custom logic.
Events (@core/events)
Section titled “Events (@core/events)”dispatch(target, type, detail, options?)wrapsCustomEventcreation with the library defaults (bubbling + composed events) and enforces theautomagica11y:<pattern>:<action>naming convention.- Pass
{ cancelable: true }when plugins should be able to veto a close or open transition.
Focus (@core/focus)
Section titled “Focus (@core/focus)”Focus utilities power both the dialog context and dedicated focus patterns:
isFocusable(element)andgetFocusableIn(root)filter nodes against visibility,hidden,aria-hidden, andtabindexrules.focusElement(element, { preventScroll, preserveTabIndex })safely focuses while preserving authortabindexvalues.focusFirst(root)moves focus to the first focusable descendant, falling back to the root element.enableFocusTrap(container, options)activates a managed focus loop and returns a disposer.applyFocusOrder(elements, options)assigns a deterministic tab order and returns a controller to restore the previous state.
Keyboard (@core/keyboard)
Section titled “Keyboard (@core/keyboard)”isActivateKey(event)checks for Space/Enter activation. Toggle uses it to give non-button triggers button semantics.
Placement (@core/placement)
Section titled “Placement (@core/placement)”resolveAnchoredPlacement(trigger, surface, preferred, measure?)decides where to position anchored surfaces (tooltips, popovers) while respecting viewport constraints. Provide a custommeasureobject when rendering inside a scroller or virtual viewport.
Registry (@core/registry)
Section titled “Registry (@core/registry)”registerPattern(name, selector, init)registers a pattern initializer along with its selector.initPattern(name, root?),initPatterns(names, root?),initNode(node), andinitAllPatterns(root?)hydrate patterns individually, in bulk, or against injected DOM fragments.- Patterns automatically mark hydrated elements with a
WeakSetguard, so re-running an initializer is safe.
Styles (@core/styles)
Section titled “Styles (@core/styles)”setHiddenState(element, hidden)keepshiddenandaria-hiddensynchronized.setInert(element, true/false)applies or removes theinertattribute while preserving author intent.
Example & documentation coverage plan
Section titled “Example & documentation coverage plan”To keep the docs actionable, every core subsystem will receive the following coverage over time:
- Narrative guide – deepen this overview with end-to-end recipes (focus traps inside dialogs, hover intent customization, etc.).
- Interactive example – each capability should have a live playground demonstrating the helper in isolation.
- API reference – expand the existing tables with parameter/return type callouts and cross-links to patterns that consume the helper.
Tracking progress
As you create new guides, reference this plan and tick off the relevant bullet so contributors know which areas still need hands-on demos.