Skip to main content

Patterns

The bridge between declarative schemas and UI components


Overview

The Pattern System connects declarative schemas to actual UI components. When a trait's render-ui effect specifies a pattern type, the system uses three key mechanisms to:

  1. Validate the pattern props against the schema
  2. Map the pattern to a concrete component
  3. Enforce the event contract for closed-circuit compliance
Schema (render-ui)  →  Pattern Registry  →  Component Mapping  →  Shell Component

Event Contract

Closed Circuit Validation

Pattern Registry

The pattern registry is the source of truth for all available patterns. Each pattern defines:

{
"entity-table": {
"type": "entity-table",
"category": "display",
"description": "Data table with columns and sorting",
"suggestedFor": ["data-dense views", "comparisons", "admin panels"],
"typicalSize": "medium",
"componentHints": ["row-action:*", "table-cell", "sort-header"],
"implements": "EntityBoundPatternProps",
"propsSchema": {
"columns": {
"required": true,
"types": ["array"],
"description": "Columns can be Column objects or simple string field names"
},
"entity": {
"types": ["string", "array"],
"description": "Entity name for auto-fetch OR data array"
},
"itemActions": {
"types": ["array"],
"description": "Item actions from generated code - maps to rowActions"
}
},
"componentMapping": {
"component": "DataTable",
"eventContract": { }
}
}
}

Pattern Properties

PropertyDescription
typeUnique pattern identifier (used in render-ui)
categoryGrouping: display, form, header, filter, navigation, layout, game, state
descriptionHuman-readable description
suggestedForUse case hints for LLM generation
typicalSizeUI footprint: tiny, small, medium, large
componentHintsSub-component patterns this pattern may use
implementsInterface the component implements (e.g., EntityBoundPatternProps)
propsSchemaProp definitions with types and requirements
componentMappingMaps to shell component and event contract

Pattern Categories

CategoryExamplesPurpose
displayentity-table, entity-list, entity-cards, statsData presentation
formform, form-section, form-fieldsData input
headerpage-header, title-onlyPage titles and actions
filtersearch-bar, filter-group, search-inputData filtering
navigationtabs, breadcrumb, wizard-progress, paginationNavigation controls
layoutmodal, drawer, master-detail, dashboard-gridPage structure
gamegame-canvas, game-hud, game-controlsGame UI elements
stateempty-state, loading-state, error-stateState feedback

Component Mapping

The component mapping connects pattern types to shell components:

{
"mappings": {
"entity-table": {
"component": "DataTable",
"category": "display"
},
"form": {
"component": "Form",
"category": "form"
},
"page-header": {
"component": "PageHeader",
"category": "header"
}
}
}

Mapping Properties

PropertyDescription
componentComponent name in the shell
categorySame as pattern category
clientOptional - client-specific component
deprecatedOptional - marks pattern as deprecated
replacedByOptional - replacement pattern for deprecated ones

Event Contracts

Event contracts define what events a component emits and requires. This is critical for closed-circuit validation - ensuring every UI interaction has a corresponding state machine transition.

{
"contracts": {
"form": {
"emits": [
{
"event": "SAVE",
"trigger": "submit",
"payload": { "type": "FormData" }
},
{
"event": "CANCEL",
"trigger": "click",
"payload": { "type": "void" }
}
],
"requires": ["SAVE", "CANCEL"],
"entityAware": true
},
"entity-table": {
"emits": [
{
"event": "VIEW",
"trigger": "action",
"payload": { "type": "EntityRow" },
"optional": true
},
{
"event": "SELECT",
"trigger": "select",
"payload": { "type": "EntityRow" },
"optional": true
},
{
"event": "EDIT",
"trigger": "action",
"payload": { "type": "EntityRow" },
"optional": true
},
{
"event": "DELETE",
"trigger": "action",
"payload": { "type": "EntityRow" },
"optional": true
}
],
"requires": [],
"entityAware": true,
"configDriven": true
}
}
}

Contract Properties

PropertyDescription
emitsEvents the component can emit
requiresEvents that MUST have transitions (closed circuit)
entityAwareComponent receives entity data
configDrivenEvents are determined by config (e.g., itemActions)

Event Definition

PropertyDescription
eventEvent name (e.g., SAVE, CANCEL, SELECT)
triggerWhat triggers the event: click, submit, change, action, close
payloadPayload type: void, FormData, EntityRow, or custom shape
optionalIf true, transition is not required

Closed Circuit Integration

Event contracts power the Closed Circuit validation:

  1. Required Events: If requires: ["SAVE", "CANCEL"], the validator ensures transitions exist for both events
  2. Overlay Patterns: modal and drawer require CLOSE transitions to prevent stuck UI states
  3. Config-Driven Events: For entity-table with itemActions: [{ event: "DELETE" }], the validator checks for a DELETE transition

Component Interface Requirements

Components mapped to patterns must implement specific interfaces to participate in the closed circuit.

EntityBoundPatternProps

For data-bound components (entity-table, entity-list, form, etc.):

interface EntityBoundPatternProps {
entity?: string; // Entity type name
data?: unknown[]; // Data array
isLoading?: boolean; // Loading state
error?: Error | null; // Error state
}

Event Bus Integration

All interactive components must emit events via the Event Bus, not internal callbacks:

// CORRECT - uses event bus
const handleRowClick = (row: EntityRow) => {
eventBus.emit('UI:SELECT', { row });
};

// WRONG - internal state management
const handleRowClick = (row: EntityRow) => {
setSelectedRow(row); // Breaks the circuit!
};

Action Props Pattern

Components with configurable actions receive them as props:

interface ActionablePatternProps {
actions?: Array<{
label: string;
event: string; // Event to emit
variant?: 'primary' | 'secondary' | 'danger';
icon?: string;
}>;
itemActions?: Array<{ // For row-level actions
label: string;
event: string;
icon?: string;
}>;
}

The component emits UI:{event} when the action is triggered, completing the circuit back to the state machine.


Design System

The design system contains the actual component implementations that patterns map to.

Component Hierarchy

LevelPurposeExamples
AtomsIndivisible UI elementsButton, Input, Badge, Icon, Spinner
MoleculesSimple compositionsSearchInput, Tabs, Breadcrumb, FilterGroup
OrganismsComplex, self-containedDataTable, Form, PageHeader, ModalSlot
TemplatesPage-level layoutsClient-specific full-page components

Using Patterns in Schemas

render-ui Effect

Patterns are used via the render-ui effect in trait transitions:

{
"from": "viewing",
"to": "viewing",
"event": "INIT",
"effects": [
["render-ui", "main", {
"type": "page-header",
"title": "Tasks",
"actions": [
{ "label": "Create Task", "event": "CREATE", "variant": "primary" }
]
}],
["render-ui", "main", {
"type": "entity-table",
"entity": "Task",
"columns": ["title", "status", "assignee"],
"itemActions": [
{ "label": "Edit", "event": "EDIT" },
{ "label": "Delete", "event": "DELETE", "variant": "danger" }
]
}]
]
}

Prop Validation

The compiler validates props against propsSchema:

  1. Required props must be present
  2. Prop types must match allowed types
  3. Unknown props generate warnings

Event Wiring

For each action/itemAction event:

  1. Component emits UI:{EVENT} via event bus
  2. useUIEvents hook catches and dispatches to trait
  3. State machine processes the event
  4. Effects execute, potentially re-rendering

Available Patterns

The following patterns are available out of the box:

Display Patterns

PatternDescriptionCommon Props
entity-tableData table with columns and sortingentity, columns, itemActions
entity-listList view of entity itemsentity, itemActions
entity-cardsCard grid layout for entitiesentity, columns, itemActions
statsStatistics display with cardsitems
detail-viewSingle entity detail displayentity, fields

Form Patterns

PatternDescriptionCommon Props
formComplete form with validationentity, fields, layout
form-sectionGrouped form fieldstitle, fields
form-fieldsInline form fieldsfields

Header Patterns

PatternDescriptionCommon Props
page-headerPage title with actionstitle, subtitle, actions
title-onlySimple title displaytitle

Filter Patterns

PatternDescriptionCommon Props
search-barGlobal search inputplaceholder, entity
filter-groupFilter chips/buttonsfilters
search-inputStandalone search fieldplaceholder
PatternDescriptionCommon Props
tabsTab navigationitems, activeTab
breadcrumbBreadcrumb trailitems
wizard-progressStep indicator for wizardssteps, currentStep
paginationPage navigationpage, totalPages

Layout Patterns

PatternDescriptionCommon Props
modalModal dialog overlaytitle, children
drawerSide panel overlaytitle, position
master-detailSplit view layoutmaster, detail
dashboard-gridGrid layout for dashboardsitems

State Patterns

PatternDescriptionCommon Props
empty-stateEmpty data placeholdertitle, description, action
loading-stateLoading indicatormessage
error-stateError displayerror, onRetry

Summary

The Pattern System provides:

  1. Pattern Registry - Defines available patterns with props, categories, and metadata
  2. Component Mapping - Connects pattern types to shell components
  3. Event Contracts - Specifies what events components emit and require
  4. Closed Circuit Validation - Ensures all UI interactions have state machine handlers
  5. Design System - Contains actual component implementations

This architecture ensures that schemas remain declarative while the compiler handles the complexity of wiring components to the event-driven state machine system.


For more details on related concepts, see Traits and Closed Circuit.