Collections, geometry, and localization utilities
These utilities give interaction machines shared models for ordered data, tree data, selection, screen space, dates, colors, files, strings, and locale-aware output. They turn browser values and user input into stable values that machines can search, move through, validate, display, and return to the application.
Utility relationships
The collection utilities model the data that machines navigate. Geometry utilities relate that data to the viewport. Date, color, file, string, and internationalization utilities turn machine state into valid input or user-facing output. The interaction utilities page gives the wider utility map, while selection and navigation machines, form and input machines, and specialized interaction machines show the main consumers.
Shared value flow
The utilities support a common flow from raw values to machine decisions and then to display or browser actions. A machine keeps its own state, but these shared value models keep lookup, movement, validation, and formatting consistent.
flowchart LR
A[Raw data or browser value] --> B[Shared value model]
B --> C[Search, selection, or layout decision]
C --> D[Machine state]
D --> E[Formatted text or browser action]
References: packages/…/index.ts• L1-26 packages/…/index.ts• L1-21 packages/…/index.ts• L1-13
Collection models
Collections give machines a stable view of items without forcing each machine to know the shape of application data. Each collection maps an item to a value, a label, and a disabled state so that navigation and selection can work with simple identifiers.
List and grid collections
The list model supports lookup, ordering, grouping, text search, insertion, removal, and reordering. The grid model adds row and column movement while preserving disabled-item rules, so listbox, select, combobox, and similar machines can share the same navigation contract. packages/…/list-collection.ts and grid-collection.ts define these models.
Important behavior includes:
- Values identify items. Labels provide searchable and readable text.
- Disabled items remain in the model. Navigation skips them when it looks for the next enabled value.
- Grouping changes the visible order used by index and value lookups.
- Search keeps a short sequence of typed characters and resolves it to the next matching enabled item.
- Grid movement keeps the current column when possible and uses the nearest enabled cell when a row is shorter.
References: packages/…/types.ts• L5-46 packages/…/list-collection.ts• L21-31 packages/…/list-collection.ts• L196-233 packages/…/list-collection.ts• L256-352 packages/…/grid-collection.ts• L9-17 packages/…/grid-collection.ts• L87-161
Tree collections
The tree model represents nested data through a root node and child paths. It lets machines find nodes by value, move through visible nodes, resolve parents and descendants, preserve depth, and apply structural changes without losing the tree shape. packages/…/tree-collection.ts is the main model.
Index paths describe a node by its position under the root. Value paths describe the same route through node values. This gives a machine both a stable identity for state and a structural route for expansion, focus, filtering, and updates. The model can also build trees from file-like paths or from a flat node list.
References: packages/…/types.ts• L52-92 packages/…/tree-collection.ts• L29-73 packages/…/tree-collection.ts• L106-205 packages/…/tree-collection.ts• L221-328 packages/…/tree-collection.ts• L398-469 packages/…/tree-collection.ts• L472-545
Selection state
Selection is a value set with an explicit mode. It supports no selection, one selected value, multiple independent values, or range selection based on an anchor and target. packages/…/selection.ts keeps this behavior separate from the data model, so a machine can change the collection without changing the meaning of selection.
Selection operations respect item order and the selected mode. A separate selected-item map retains selected objects when the current collection no longer contains them, then resolves them again when the collection is restored. This matters for async options and controlled machines that receive values before their items.
References: packages/…/selection.ts• L4-12 packages/…/selection.ts• L14-31 packages/…/selection.ts• L68-177 packages/…/selection-map.ts• L8-23 packages/…/selection-map.ts• L25-82
Geometry and scroll positioning
Geometry utilities give machines a common language for points, sizes, rectangles, bounds, and alignment. They support collision checks, containment, resizing, and placement decisions without tying those decisions to one browser element or one interaction.
Rectangle calculations
The rectangle model stores position and size together with derived edges, midpoints, and center. It can be created from elements, the viewport, points, ranges, or rotations. Operations then compare, move, grow, shrink, align, constrain, combine, and intersect rectangles.
These calculations support menus, floating panels, tours, sliders, and pointer-driven tools. The result is a new geometric value rather than a change to the source element, which lets a machine compare a proposed state with its boundary before it applies a browser action.
References: packages/…/types.ts• L5-30 packages/…/rect.ts• L24-69 packages/…/from-element.ts• L14-56 packages/…/from-window.ts• L14-33 packages/…/align.ts• L23-26 packages/…/constrain.ts• L3-14 packages/…/intersection.ts• L7-31
Scroll snap positions
The scroll-snap utility turns CSS snap alignment into usable scroll targets. It reads the scroll container, its children, scroll padding, reading direction, and current layout scale. It then produces start, center, and end positions for horizontal and vertical movement.
The calculation treats right-to-left horizontal scrolling as a separate coordinate case and clamps targets to the scrollable range. Machines can use the full target list, find the target for a matching child, or resolve a scroll position back to its child. The carousel machine uses this contract to connect page changes to actual scroll movement.
References: packages/…/index.ts• L6-10 packages/…/index.ts• L25-46 packages/…/index.ts• L67-155 packages/…/index.ts• L158-253 packages/…/carousel.machine.ts• L1-3
Date and color values
Date and color utilities provide structured values for machines that expose rich input. They separate parsing and arithmetic from display, so a date picker or color picker can move through valid values while formatting them for the active locale.
Date values
The date model supports calendar dates, date-times, and zoned date-times. The utilities parse locale-shaped input, constrain values to allowed ranges, calculate month and week views, move through visible pages, and format selected or visible ranges. packages/…/index.ts is the public entry point.
Date navigation treats the visible range as a unit. When focus moves outside that range, the utility adjusts the start and end dates so the focused value is visible and still respects minimum and maximum limits. Locale data also controls the first day of the week, weekday names, month names, separators, and accepted native digits.
References: packages/…/types.ts• L1-31 packages/…/parse-date.ts• L9-37 packages/…/date-month.ts• L16-65 packages/…/format.ts• L6-65 packages/…/pagination.ts• L16-57 packages/…/locale.ts• L3-40
Color values
The color model accepts named colors and common RGB, HSL, and HSB forms. It keeps color channels and alpha together, supports conversion between color spaces, clamps channel changes to valid ranges, and returns CSS or serialized values for the application.
Color channels also have ranges, steps, page sizes, and locale-aware labels. This lets a color picker use the same value model for keyboard changes, percentage positions, text fields, equality checks, and the two-dimensional gradient used to show a channel area.
References: packages/…/parse-color.ts• L7-24 packages/…/types.ts• L1-27 packages/…/types.ts• L29-94 packages/…/color.ts• L18-89 packages/…/area-gradient.ts• L26-94 packages/…/color-picker.connect.ts• L1-4
File and string utilities
These utilities turn browser file objects and text into values that machines can validate, compare, search, serialize, or display. They keep browser-specific concerns out of file-upload, filtering, and diagnostic flows.
File acceptance and transfer
File utilities define the contract for accepted file types, file sizes, MIME inference, duplicate checks, directory traversal, data URLs, downloads, and total size. File type checks accept exact MIME types, MIME groups, and extensions. The result includes a stable error code so a file-upload machine can report why a file was rejected.
Data transfer can preserve relative paths when a user drops a directory. The same utility family converts files to data URLs, converts data URLs back to blobs, and creates browser downloads. File-upload machines use these rules for input attributes, dropped files, validation, deduplication, and size labels.
References: packages/…/types.ts• L3-10 packages/…/index.ts• L1-11 packages/…/is-valid-file-type.ts• L10-56 packages/…/is-valid-file-size.ts• L5-17 packages/…/data-transfer.ts• L17-79 packages/…/is-file-equal.ts• L1-8 packages/…/file-upload.utils.ts• L1-3 packages/…/file-upload.connect.ts• L1-4
String matching and highlighting
String helpers support locale-aware prefix, suffix, and contains checks. They normalize Unicode text and use an internationalized collator, so machine search does not depend only on raw code-point comparison. packages/…/filter.ts provides this search contract.
The highlight utility returns alternating matched and unmatched text chunks. It can highlight the first match or all matches, use more than one query, ignore case, and require whole words. Queries are treated as text rather than regular-expression code, which keeps user search input safe.
References: packages/…/filter.ts• L3-25 packages/…/filter.ts• L27-60 packages/…/types.ts• L1-35 packages/…/highlight.ts• L5-15 packages/…/highlight-multiple.ts• L5-33
Machine state serialization
State stringification creates a readable JSON view of machine state for diagnostics and tools. It reduces browser objects, events, files, sets, dates, and nested machine references to safe display values, and it can omit selected keys. Highlighting then adds display markup without changing the serialized state.
References: packages/…/index.ts• L4-16 packages/…/index.ts• L16-80 packages/…/index.ts• L83-85
Internationalized formatting
Internationalized formatting gives machines locale, direction, number, date, time, list, relative-time, and byte output through the platform Intl APIs. The formatters share one locale input, so a component can change language and direction without rewriting its machine logic.
Locale and direction
The locale utility resolves a usable browser locale and pairs it with a text direction. RTL detection uses the locale script when the platform supports it and falls back to known language codes when it does not. trackLocale reports the initial value and listens for browser language changes.
Direction is part of behavior, not only presentation. It affects horizontal geometry, scroll snap, and control layout, so consumers should use the locale result instead of inferring direction from a label or a CSS class.
References: packages/…/locale.ts• L3-29 packages/…/is-rtl.ts• L1-50 packages/…/track-locale.ts• L4-25 packages/…/index.ts• L67-75
Number, date, time, and list formats
The formatting helpers wrap locale rules for numbers, dates, times, relative dates, lists, and byte sizes. They accept display options instead of asking machines to assemble separators, units, day periods, or list words. Formatter instances are cached by locale and options to keep repeated machine output efficient.
Date formatting supports Unicode date field tokens. Time formatting accepts validated HH:mm or HH:mm:ss input and can use 12-hour or 24-hour output with custom day-period labels. Byte formatting supports decimal or binary scales, bit or byte units, precision, and unit display width.
References: packages/…/format-number.ts• L1-8 packages/…/format-date.ts• L207-217 packages/…/format-time.ts• L27-75 packages/…/format-relative-time.ts• L5-19 packages/…/format-list.ts• L1-8 packages/…/format-bytes.ts• L6-69 packages/…/cache.ts• L15-35
Change checklist
When these files or sections are changed, remember to consider these:
- Keep collection value, label, disabled-state, and ordering rules aligned with the machines that store selected or focused values.
- When changing tree paths or flattening rules, check expansion, descendant selection, and parent lookup together.
- When changing rectangle coordinates or scroll snap targets, check both left-to-right and right-to-left horizontal behavior.
- When changing date parsing or locale separators, check typed input and the date machine's displayed segments as one contract.
- When changing color channel ranges or formats, check keyboard increments, text values, and gradient axes together.
- When changing file acceptance, preserve agreement between the
acceptattribute, validator, MIME fallback, and file error codes. - When changing locale or formatter options, check cached formatter keys and every output that uses the same locale.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A collection maps an item to a value that is not stable or unique | Focus and selection can resolve to the wrong item | Treat the item-to-value mapping as the machine identity contract |
| A selected value is removed from an async collection without retaining its item | Controlled selection loses the object needed for display or callbacks | Keep the selected-item map in sync with selected values |
| Scroll snap math ignores RTL or uses visual scale as layout scale | Carousel movement lands on the wrong item | Use the direction-aware and scale-normalized snap calculations |
| A rectangle is constrained without reducing an oversized dimension | A popup or panel still extends beyond its boundary | Constrain position and size against the same boundary |
| Date input accepts only ASCII digits or a fixed separator | Valid input fails in locales with native digits or another separator | Use locale digits and the locale date separator |
| A file has no MIME type and validation does not inspect its extension | A valid file is rejected or an invalid file is accepted | Use MIME data first and the known extension map as fallback |
| A user query is inserted into a highlight regular expression without escaping | Search text changes matching behavior | Escape query terms before building a highlight pattern |
| A formatter is created with a locale that does not match the component direction | Text and horizontal behavior disagree | Resolve locale and direction as one value |
Key files
| File | Why to read it |
|---|---|
| packages/…/list-collection.ts | Main ordered-item model for lookup, search, grouping, disabled items, and reordering |
| packages/…/grid-collection.ts | Grid row and column navigation built on the list model |
| packages/…/tree-collection.ts | Nested data model used by tree and cascade navigation |
| packages/…/selection.ts | Selection modes and range behavior shared by selectable machines |
| packages/…/rect.ts | Core point and rectangle value model |
| packages/…/align.ts | Placement relationship between a target rectangle and a reference rectangle |
| packages/…/index.ts | Browser scroll alignment and snap target calculations |
| packages/…/pagination.ts | Visible date range movement and focus correction |
| packages/…/parse-date.ts | Locale-shaped date input parsing |
| packages/…/color.ts | Shared color channel, range, conversion, and adjustment behavior |
| packages/…/is-valid-file-type.ts | File type acceptance and error contract |
| packages/…/data-transfer.ts | Dropped-file and directory traversal behavior |
| packages/…/highlight.ts | Public text highlighting behavior |
| packages/…/index.ts | Diagnostic serialization of machine state |
| packages/…/locale.ts | Default locale and direction resolution |
| packages/…/format-bytes.ts | Locale-aware file size output used by file upload |
| packages/…/cache.ts | Shared formatter reuse rules |