The toolkit handed to an enhancer's setup. Everything registered through on/observe/add is torn down automatically when the instance is destroyed.

interface EnhancerContext<Options extends object> {
    add: (disposer: Disposer) => void;
    container: HTMLElement | Document;
    emit: (type: string, detail?: unknown, options?: EmitOptions) => boolean;
    observe: (
        target: Node,
        callback: (records: MutationRecord[]) => void,
        init?: MutationObserverInit,
    ) => void;
    on: <E extends Event = Event>(
        target: EventTarget,
        type: string,
        handler: (event: E) => void,
        options?: boolean | AddEventListenerOptions,
    ) => void;
    options: Options;
    root: HTMLElement;
    uid: (prefix?: string) => string;
}

Type Parameters

  • Options extends object

Properties

add: (disposer: Disposer) => void

Register an arbitrary disposer to run on teardown.

container: HTMLElement | Document

The container the enhancer was invoked with (often document).

emit: (type: string, detail?: unknown, options?: EmitOptions) => boolean

Dispatch a bubbling hl:* CustomEvent from the root. Returns false when a cancelable event was prevented.

observe: (
    target: Node,
    callback: (records: MutationRecord[]) => void,
    init?: MutationObserverInit,
) => void

Watch target with a MutationObserver that is disconnected on destroy. Defaults to { childList: true, subtree: true }, which is what enhancers need to pick up items added or removed after they ran. No-op where MutationObserver is unavailable.

on: <E extends Event = Event>(
    target: EventTarget,
    type: string,
    handler: (event: E) => void,
    options?: boolean | AddEventListenerOptions,
) => void

Attach an event listener that is auto-removed on destroy.

options: Options

Options merged from the definition's defaults, the root's data-hl-* attributes, and the caller.

root: HTMLElement

The matched component root.

uid: (prefix?: string) => string

Generate a unique id namespaced to this enhancer.