Brain headless primitives
Brain is the headless Angular behavior layer for Spartan components. It supplies interaction state, keyboard and focus behavior, form links, overlay life cycles, and ARIA relationships without visual styling, so Helm or another design system can control the final appearance.
Product role
Brain separates accessible interaction behavior from visual presentation. An Angular application composes Brain primitives in its templates and uses their state and attributes to apply its own design. The same behavior can support Helm or a custom design system.
The relationship between the application, Brain, and the visual layer is:
flowchart LR
A[Angular application markup] --> B[Brain primitive]
B --> C[Interaction state]
B --> D[Keyboard and focus behavior]
B --> E[ARIA semantics]
C --> F[Helm or custom styles]
Headless behavior layer
The Brain layer owns behavior and accessibility. It does not own colors, spacing, borders, motion, or other visual treatment. The package README describes it as the behavior half of Spartan UI and supports pairing it with Helm or another design system. libs/…/README.md
References: libs/…/README.md• L10-14
Public package surface
Brain exposes feature-specific Angular APIs through secondary entry points. An application imports the primitive family it needs and composes its public directives, components, and shared import bundle.
Secondary entry points
Each feature is imported from @spartan-ng/brain/<name>. The current public surface covers these groups:
| Area | Entry points |
|---|---|
| Disclosure and structure | accordion, collapsible, tabs |
| Forms and selection | autocomplete, calendar, checkbox, combobox, date-time, date-time-luxon, field, forms, input, input-otp, label, radio-group, select, slider, switch, textarea, toggle, toggle-group |
| Overlays and transient content | alert-dialog, dialog, hover-card, popover, sheet, tooltip |
| Navigation and commands | command, navigation-menu |
| Status and presentation | avatar, button, message-scroller, progress, separator, sonner |
| Layout and shared behavior | resizable, core |
The package also provides hlm-tailwind-preset.css for the styled layer. The preset supplies shared state variants and overlay styling hooks that let a design layer style Brain state without adding visual rules to the primitives. Brain supports Angular 21 through 22, Angular CDK, Angular forms, RxJS, and Tailwind CSS as peer contracts. Luxon is optional for date-time support. libs/…/package.json libs/…/README.md
References: libs/…/README.md• L61-84 libs/…/package.json• L10-27
Import bundles
Most component entry points provide an *Imports array that contains the complete public pieces for one primitive. Applications can use that bundle for simple composition or import individual public pieces when they need a smaller surface. libs/…/README.md
References: libs/…/README.md• L34-68 libs/…/index.ts• L1-20 libs/…/index.ts• L1-31
Shared core infrastructure
The shared core gives primitive families common rules for state exposure, element measurement, delayed interaction, class handling, and animation-aware cleanup. It supports consistent behavior across components without becoming a visual component layer.
State and interaction helpers
Core helpers expose primitive state to host elements and to the styling layer. They also support shared needs such as element size, content dimensions, side and position data, delayed hover behavior, and safe cleanup after animations. The class helpers accept common class-value shapes and merge several reactive sources on one element while preserving classes managed by the host or another system. libs/…/index.ts libs/…/brn-classes.ts
References: libs/…/index.ts• L1-18 libs/…/brn-class-value.ts• L1-13 libs/…/brn-classes.ts• L17-25 libs/…/brn-classes.ts• L67-78 libs/…/exposes-state.ts• L1-80 libs/…/wait-for-element-animations.ts• L1-80
Form state infrastructure
The forms and field layers connect controls to Angular forms and make validation state available to the surrounding field. A field can then expose invalid, dirty, touched, and error state through host attributes and accessible descriptions. The state trackers also refresh matcher results when a parent form is submitted or reset, so submit-based validation does not remain stale. libs/…/index.ts
References: libs/…/index.ts• L1-7 libs/…/parent-form-events.ts• L4-18 libs/…/reactive-state-tracker.ts• L14-65 libs/…/signal-state-tracker.ts• L21-65 libs/…/brn-field.ts• L7-82
Overlay option infrastructure
Overlay defaults define the shared contract for placement, focus, dismissal, backdrop, scroll response, and ARIA role. Applications or design layers can provide these defaults through Angular injection, while each overlay can still supply its own ID and local context. libs/…/brn-overlay-options.ts libs/…/brn-overlay-token.ts
References: libs/…/brn-overlay-options.ts• L10-27 libs/…/brn-overlay-token.ts• L4-48
Calendar coordination
Calendar variants share a calendar contract so cells and navigation controls can work with single, multiple, or range selection. The contract covers selection, disabled dates, focused-date movement, range state, and registered cells. Calendar navigation keeps the focused day near its prior day number and skips disabled days. It disables a month button when the target month has no enabled day. Calendar text, formats, week start, and year ranges can be replaced through the calendar internationalization layer. libs/…/brn-calendar.token.ts
References: libs/…/brn-calendar.token.ts• L5-27 libs/…/brn-calendar.ts• L80-107 libs/…/brn-calendar-next-button.ts• L29-51 libs/…/brn-calendar-previous-button.ts• L29-51 libs/…/find-enabled-date.ts• L8-42 libs/…/nearest-enabled-date-in-month.ts• L4-32 libs/…/calendar-i18n.ts• L20-39
Accessibility behavior
Accessibility is part of the Brain contract. The primitives provide semantic links, state attributes, keyboard paths, focus movement, and form feedback so a visual layer can change without rebuilding the interaction model.
Semantic state and relationships
Primitives publish state through native semantics, ARIA attributes, and data attributes. Composite controls link triggers, lists, active options, labels, descriptions, and errors so assistive technology can follow the same state that the visual layer presents.
Fields merge their own description and error IDs with a control's manually supplied aria-describedby value. This keeps custom descriptions while allowing field feedback to reach the control. Select, autocomplete, and combobox options also inherit their parent disabled state, including while a list is open. libs/…/brn-field-aria.service.ts libs/…/brn-select-item.ts libs/…/brn-autocomplete-item.ts
Tabs use the same relationship model: a tab trigger exposes its selected and disabled state, points to its tab panel, and stays in the tab order only when it is the selected enabled tab. libs/…/brn-tabs-trigger.ts
References: libs/…/brn-accordion-trigger.ts• L7-22 libs/…/brn-select-trigger.ts• L7-28 libs/…/brn-select-item.ts• L9-24 libs/…/brn-autocomplete-input.ts• L8-31 libs/…/brn-autocomplete-item.ts• L9-24 libs/…/brn-field-aria.service.ts• L4-29 libs/…/brn-field-control-described-by.ts• L4-23 libs/…/brn-tabs-trigger.ts• L15-30
Keyboard and focus behavior
Grouped controls use shared focus models to move through enabled items with arrow keys and related navigation keys. Selects, autocomplete, and comboboxes expose the active option through aria-activedescendant; their inputs open the list from navigation keys and commit the active option with Enter. Tab closes an open list without replacing uncommitted text. libs/…/brn-select-trigger.ts libs/…/brn-autocomplete-input.ts libs/…/brn-combobox-input.ts
Calendars move focus to an enabled date and keep month navigation usable under date limits and disabled-date rules. Combobox chip inputs add the same keyboard flow to chip removal: Backspace removes the last chip when the search is empty. Sliders keep the changed thumb focused after value changes. libs/…/find-enabled-date.ts libs/…/brn-combobox-chip-input.ts libs/…/brn-slider.ts
Tabs support automatic or manual activation. Their list keeps keyboard focus aligned with the trigger that owns DOM focus, even after a click or programmatic selection. Dynamic triggers follow their current DOM order, and Home, End, wrap, and page navigation work through the same focus model. libs/…/brn-tabs-list.ts
When a tab header is wider than its viewport, the paginated list keeps the focused or selected tab visible. It updates its controls after resize, direction changes, and tab changes, and supports pointer-held pagination. libs/…/brn-tabs-paginated-list.ts
References: libs/…/brn-select-trigger.ts• L101-135 libs/…/brn-autocomplete-input.ts• L102-134 libs/…/brn-combobox-input.ts• L104-155 libs/…/brn-calendar.ts• L223-248 libs/…/brn-calendar-next-button.ts• L29-51 libs/…/brn-calendar-previous-button.ts• L29-51 libs/…/brn-combobox-chip-input.ts• L93-123 libs/…/brn-slider.ts• L288-337 libs/…/brn-tabs-list.ts• L24-57 libs/…/brn-tabs-paginated-list.ts• L221-259 libs/…/brn-tabs-paginated-list.ts• L326-355
Pointer and overlay behavior
The overlay system coordinates attached or centered content, dismissal, focus, scroll response, and close state. It supports outside-pointer and Escape dismissal, unique overlay IDs, optional auto-focus, and scroll strategies that block, reposition, or close the content.
Dialogs use the same overlay foundation for centered or attached content. Backdrop clicks close the matching dialog, while outside clicks and Escape dismiss only the top eligible dialog. A modal overlay above another dialog blocks dismissal of the lower dialog, and nested attached content can remain related to its parent. Dialogs created through the directive and service share one ID sequence, and an active ID cannot be reused. libs/…/brn-dialog.service.ts libs/…/brn-dialog-id.ts
Attached overlays also follow enclosing scroll containers that are not registered with the Angular CDK. Repositioning overlays update their position and close-on-scroll overlays detach when those containers move. The listeners stop when the overlay closes, while CDK-registered containers remain under the CDK strategy. libs/…/brn-overlay.service.ts
Tooltips are supplementary labels for an existing trigger. They open for mouse or pen hover and keyboard focus, not for touch, and connect the trigger with aria-describedby. A tooltip has a preferred side and a viewport-aware fallback order, so its active side can change when space is limited. The position model also accounts for text direction and mobile viewport changes. libs/…/brn-tooltip.ts
Tooltip defaults can be layered by injector, so a local design layer can change delays, placement, or arrow classes without replacing unrelated defaults. Popovers are the suitable choice when touch users need interactive or discoverable content. libs/…/brn-tooltip.token.ts
Toasts provide temporary status messages in a notification list. Each toast uses a live region with polite or assertive announcement priority, can expose a close or action control, and supports keyboard focus through a configurable hotkey. The toaster pauses timed dismissal while the user interacts with it and supports updates such as changing a loading message into a result. libs/…/brn-toaster.ts libs/…/brn-toast.ts
Dialog and overlay close operations expose an open, closing, and closed life cycle. They keep content mounted until its exit animation finishes, and dialog descriptions remain linked unless the caller supplies an explicit description value. libs/…/brn-overlay-ref.ts
References: libs/…/brn-overlay.service.ts• L52-135 libs/…/brn-overlay.service.ts• L138-182 libs/…/brn-overlay.service.ts• L243-273 libs/…/brn-overlay-scroll-containers.ts• L9-35 libs/…/brn-overlay-ref.ts• L47-79 libs/…/brn-overlay-ref.ts• L124-163 libs/…/brn-dialog-id.ts• L1-9 libs/…/brn-dialog.service.ts• L34-161 libs/…/brn-tooltip.ts• L149-183 libs/…/brn-tooltip.ts• L269-303 libs/…/brn-tooltip.token.ts• L31-49 libs/…/brn-dialog-ref.ts• L60-105 libs/…/brn-dialog-ref.ts• L126-167 libs/…/brn-toaster.ts• L43-66 libs/…/brn-toast.ts• L35-62
Accessibility checks
Brain exposes an axe-core helper for browser-based accessibility checks. Primitive tests can inspect rendered DOM and verify the semantic relationships that users and assistive tools receive. libs/…/axe.ts
References: libs/…/axe.ts• L1-19 libs/…/index.ts• L1
Primitive families
The primitive families share the same headless model but solve different interaction problems. The two broad feature groups below have their own pages with more detail.
Form and selection controls
Form and selection primitives manage values, disabled state, validation feedback, labels, and keyboard selection without deciding how fields or options look. They include inputs, fields, selects, comboboxes, calendars, date pickers, sliders, toggles, and related controls. See Form and selection controls.
References: libs/…/index.ts• L1-7 libs/…/brn-field-control.ts• L23-40 libs/…/brn-select.ts• L41-81 libs/…/index.ts• L22-45 libs/…/brn-slider.ts• L33-81
Status and code-entry primitives
Status and code-entry primitives expose small, stateful interaction surfaces without imposing a visual layout. Avatar reports idle, loading, loaded, and error states so an image can reveal itself only when ready and a fallback can remain available when it is not. Input OTP keeps one form value behind a slot-based view, accepts constrained input and paste text, and exposes focus, validation, masking, and completion state for the consuming design. libs/…/brn-avatar.ts libs/…/brn-input-otp.ts
References: libs/…/brn-avatar.ts• L3-25 libs/…/brn-avatar-fallback.ts• L4-14 libs/…/brn-avatar-image.ts• L4-51 libs/…/brn-input-otp.ts• L30-137 libs/…/brn-input-otp-input.ts• L9-87 libs/…/brn-input-otp-slot.ts• L5-48
Overlay, navigation, and feedback primitives
Overlay, navigation, and feedback primitives coordinate content that opens from a trigger, moves through a menu, or reports application state. Dialogs, popovers, sheets, drawers, tooltips, navigation menus, progress indicators, and toasts provide interaction rules while leaving panel and message styling to the consuming layer. See Overlay, navigation, and feedback.
References: libs/…/brn-overlay.ts• L39-109 libs/…/brn-navigation-menu.ts• L29-76 libs/…/brn-tooltip.ts• L269-303 libs/…/brn-toaster.ts• L34-66 libs/…/brn-toast.ts• L29-62
Change checklist
When these files or sections are changed, remember to consider these:
- When a secondary entry point changes, update the matching public import path and any Helm composition that consumes its
Brn*Importsbundle. - When a state or ARIA contract changes, check the related host attributes and styles that use
data-state,data-disabled,data-invalid,aria-describedby, or focus state. - When field descriptions or errors change, check the merge of manual and field-generated
aria-describedbyvalues. - When form state tracking changes, check both reactive and signal controls after parent form submit and reset events.
- When autocomplete, select, or combobox disabled state changes, check options that are already rendered in an open list.
- When calendar selection, date limits, disabled dates, or internationalization changes, check month/year navigation, range and multiple modes, and date-picker composition together.
- When month navigation changes, check day clamping, nearest enabled dates, whole-month disabled states, focused-date updates, and the button disabled state.
- When slider value normalization or form behavior changes, check thumb focus, range constraints, and Angular control updates as one contract.
- When tab registration or focus behavior changes, check dynamic add, remove, reorder, automatic activation, manual activation, and programmatic selection together.
- When tab pagination changes, check selected-tab visibility, RTL layout, resize handling, and the enabled state of both pagination controls.
- When overlay scroll or position behavior changes, check both custom enclosing scroll containers and containers registered as
cdkScrollable. - When overlay close, focus, or dismissal behavior changes, review every primitive that builds on
BrnOverlay, including popover and select. - When dialog IDs or dismissal changes, check directive-created dialogs, service-created dialogs, backdrop clicks, topmost stacked dialogs, nested attached content, outside clicks, and Escape.
- When tooltip trigger, placement, or close behavior changes, check touch behavior, RTL placement, viewport changes, and the shared overlay styling contract.
- When toast announcement, focus, timer, or dismissal behavior changes, check live-region priority, hotkey focus, action handling, pointer interaction, and updated toast durations together.
- When dialog phase or description handling changes, check exit-animation timing, reopen and force-close paths, dismiss rules, and the dialog container's
aria-describedbylink. - When a core helper changes, review its cross-cutting consumers instead of only the helper's direct tests.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Importing a component from the package root instead of its secondary entry point | The intended primitive API is not available through the root entry point | Import from @spartan-ng/brain/<name> and use the matching Brn*Imports bundle when provided |
| Placing a trigger, item, or content piece outside its owning primitive | The piece cannot find its parent behavior or complete its state and ARIA links | Keep each composed piece inside the required Brain root and item hierarchy |
| Changing select or autocomplete disabled state while its list is open | A visible option can still accept input after its parent becomes disabled | Let the parent disabled state reach every rendered option |
| Selecting a month or year without valid date bounds or enabled-day rules | Focus can move to an unavailable date or the control can appear out of sync with the calendar | Keep min, max, disabled-date, and internationalization rules aligned |
| Navigating to a month with no enabled day | The user reaches a month with no usable focus target | Disable the navigation button when the target month has no enabled date |
| Giving a range slider values that violate its spacing limits | A thumb move is rejected or the range cannot move as the user expects | Keep minStepsBetweenThumbs, maxStepsBetweenThumbs, and initial values compatible |
| Letting a tab focus model retain a removed or stale trigger | Arrow-key navigation can skip the current DOM order or focus a trigger that no longer exists | Keep the focus model synchronized with current triggers and with the trigger that owns DOM focus |
| Choosing pagination without handling changing tab widths or direction | The selected tab can move out of view or the controls can report the wrong scroll state | Let pagination react to resize, item changes, and LTR or RTL direction |
| Choosing a block scroll strategy for an attached popup that must track its anchor | The popup stays in place while its anchor moves inside a scrolling panel | Use reposition or close behavior when the popup must respond to scroll |
| Clicking the backdrop of a stacked dialog without topmost handling | A lower dialog can close while a higher dialog remains active | Dismiss only the top eligible dialog and let a modal overlay block lower dismissal |
| Reusing an active dialog ID across the directive and service paths | The dialog service rejects the second instance | Use the shared generated ID or provide a unique ID for each open dialog |
| Expecting a tooltip to open from a touch tap | No tooltip appears because touch is not treated as hover input | Use a popover when touch users need interactive or discoverable content |
| Assuming a tooltip's preferred side is always its active side | The content or arrow can use the wrong side after a viewport flip | Use the resolved tooltip position when applying placement-dependent behavior |
| Opening overlay content before it is registered or reusing an active overlay ID | The content cannot attach or the overlay service rejects the second instance | Register the matching content before opening and use a unique ID for each open overlay |
Supplying an explicit dialog aria-describedby value that omits the dialog description | Assistive technology does not receive the omitted description | Leave the value unset for automatic description links or include every required description ID |
| Giving a toast a dismiss path without preserving its live-region or focus contract | Users can miss the status message or lose keyboard access to its controls | Keep the toast announcement, focus hotkey, and action or close behavior aligned |
Key files
| File | Why to read it |
|---|---|
| libs/…/README.md | Defines Brain's product role, installation path, import model, and public entry points |
| libs/…/package.json | Defines package metadata, peer version ranges, optional date-time support, and the Tailwind preset export |
| libs/…/index.ts | Shows the shared helper surface used across primitives |
| libs/…/brn-classes.ts | Shows how reactive class sources share one host element without taking ownership of visual design |
| libs/…/brn-overlay-options.ts | Defines the common overlay option contract |
| libs/…/brn-overlay-token.ts | Defines injectable overlay defaults and overlay context |
| libs/…/index.ts | Defines shared form state and value-change contracts |
| libs/…/parent-form-events.ts | Defines form submit and reset events that refresh validation state |
| libs/…/brn-field.ts | Shows how field state and validation reach the host field |
| libs/…/brn-field-control-described-by.ts | Shows how manual and field-generated descriptions reach a control |
| libs/…/brn-calendar.token.ts | Defines the common calendar behavior contract |
| libs/…/brn-calendar.ts | Defines focused-date selection, limits, disabled dates, and the calendar day set |
| libs/…/find-enabled-date.ts | Defines how calendar focus finds an enabled date within bounds |
| libs/…/nearest-enabled-date-in-month.ts | Defines the usable target for month navigation |
| libs/…/brn-select-trigger.ts | Illustrates value selection, active-item management, ARIA state, and keyboard integration |
| libs/…/brn-select-item.ts | Shows option semantics and inherited disabled state |
| libs/…/brn-autocomplete-input.ts | Shows autocomplete input semantics and keyboard paths |
| libs/…/brn-slider.ts | Illustrates normalized values, range movement, constraints, and form integration |
| libs/…/brn-tabs-trigger.ts | Defines tab semantics, selection state, focus order, and trigger registration |
| libs/…/brn-tabs-list.ts | Defines tab-list keyboard movement and focus synchronization |
| libs/…/brn-tabs-paginated-list.ts | Defines overflow handling, selected-tab visibility, and pagination controls |
| libs/…/brn-overlay.service.ts | Defines overlay creation, dismissal, focus, position, and scroll behavior |
| libs/…/brn-overlay-ref.ts | Defines overlay phases, dismissal, animation-aware close, and disposal |
| libs/…/brn-dialog-id.ts | Defines the shared dialog ID sequence |
| libs/…/brn-dialog.service.ts | Defines dialog positioning, scroll behavior, duplicate-ID checks, and stacked dismissal |
| libs/…/brn-tooltip.ts | Illustrates lazy overlay creation, trigger rules, placement, ARIA description, and close behavior |
| libs/…/brn-tooltip.token.ts | Defines layered defaults for tooltip timing, placement, and visual hook values |
| libs/…/brn-toaster.ts | Defines notification grouping, live-region structure, focus entry, and toaster state |
| libs/…/brn-toast.ts | Defines toast announcements, actions, timed dismissal, and pointer interaction |
| libs/…/axe.ts | Defines the shared accessibility check used against rendered primitive output |