GitHub v1.3.1

client

Stable

The browser runtime execution block. Handles DOM lifecycle, event listeners, and form association.

TypeScript Signature

The CoraliteClientContext parameter signature:

typescript
Code copied!
  export async function client (context: {
    state: Record<string, any>;
    refs: (name: string) => HTMLElement | null;
    signal: AbortSignal;
    emit: (name: string, detail?: any) => boolean;
    observe: (key: string, cb: (newVal: any, oldVal: any) => void) => () => void;
    updateComplete: Promise<boolean>;
    setFormValue?: (value: any) => void;
    setValidity?: (flags: ValidityStateFlags, message: string, anchor: HTMLElement) => void;
  }): Promise<void>
          </void></boolean></string,>

Lifecycle & Cleanup

The signal provided in the context is an AbortSignal that fires when the component disconnects from the DOM. Passing it to addEventListener ensures zero memory leaks.

javascript
Code copied!
  export async function client ({ refs, signal }) {
    refs('btn').addEventListener('click', () => {
      console.log('clicked')
    }, { signal }) // Auto-cleanup on disconnect
  }

Observing State

Use observe() to react to specific state changes. This is exclusively for side-effects (like third-party canvas rendering). Never mutate state inside the observer.

javascript
Code copied!
  export async function client ({ state, observe }) {
    observe('count', (newVal) => {
      console.log('Count changed to:', newVal)
    })
  }

Form-Associated Custom Elements (FACE)

If the component exports export const formControl = true, the client() context provides form helpers to integrate with native <form> elements.

javascript
Code copied!
  export async function client ({ setFormValue, setValidity }) {
    setFormValue('initial-value')
    setValidity({ valueMissing: true }, 'This field is required', anchorEl)
  }