/** * Context Bridge Utility * * Generalizes the "parent publishes state onto its host element + child * subscribes via closest()" pattern used by compound components (select, * toggle-group, combobox, dialog, popover, ...). * * Parent (provider) contract: * - call publishContext() from componentWillLoad(), so the context exists * before any child renders for the first time, and * - call it again on every state change, including changes that arrive as a * @Prop assignment (@Watch) rather than through a @Method or user input. * * Child (consumer) contract — use a ContextConsumer: * - call connect(this.el) from connectedCallback() (runs before the first * render, and again after the element is moved to a new parent), and * - call disconnect() from disconnectedCallback(). * connect() invokes the callback once immediately, so a child that * upgrades after its parent already published still starts from the current * state; a child that upgrades before its parent (one-module-per-tag * loading, #314) hears the parent's first publish from componentWillLoad(). * * subscribeToContextChange() is the older listener-only form (no immediate * read, light-DOM closest() only); prefer ContextConsumer for new code. * * Extracted after the same bug (child freezes at mount-time state because * nothing told it the parent changed) was independently fixed three times * for select (#244/#253), toggle-group (#255/#257), and combobox (#258/#259). */ /** * One event name shared by every compound-component family. Stencil's * @Event() emits with { bubbles: true, composed: true } by default, so a * nested compound (e.g. a toggle-group rendered inside a select) can cause an * inner parent's event to bubble past an outer parent's own children. This is * harmless today only because subscribeToContextChange()'s callback takes no * payload and every handler re-reads its own closest() parent's live context * rather than trusting event.detail — do not add a payload to this event or * that bubbling becomes a real cross-family bug. */ export declare const SEKI_CONTEXT_CHANGE_EVENT = "sekiContextChange"; /** * Parent-side: publish a fresh context object onto the host element and * notify subscribed children. Must assign a plain property, not a class * accessor of the same name — in the dist-custom-elements build the class * IS the host element, so an accessor collision throws (#254). */ export declare function publishContext(el: HTMLElement, propertyName: string, context: T, emitter: { emit: (value?: void) => void; }): void; /** * Child-side: find the nearest ancestor matching parentSelector and listen * for context-change notifications. onChange is called with no payload by * design — children re-read the live context property via closest() rather * than trusting a stale event.detail snapshot. No-ops safely (null parent, * no-op unsubscribe) if no matching parent is found. */ export declare function subscribeToContextChange(el: HTMLElement, parentSelector: string, onChange: () => void): { parent: HTMLElement | null; unsubscribe: () => void; }; /** * Nearest ancestor matching `selector`, continuing past shadow roots: when the * element's own tree has no match, the search resumes from the shadow host. * This lets a part rendered inside another component's shadow tree still find * the provider that wraps that component. */ export declare function findContextProvider(el: Element, selector: string): HTMLElement | null; /** * Child-side: tracks one provider (see findContextProvider) for a part. * * private dialog = new ContextConsumer('seki-dialog', '_dialogContext', () => this.sync()); * connectedCallback() { this.dialog.connect(this.el); } * disconnectedCallback() { this.dialog.disconnect(); } * * connect() finds the provider, starts listening for its context-change * notifications and calls `onChange` once right away, so the part starts from * whatever is already published. `parent` and `context` are already set when * that first call happens. Calling connect() from connectedCallback runs it * before the first render and again after the part is moved to another * provider. * * `onChange` gets no event payload by design (see SEKI_CONTEXT_CHANGE_EVENT): * it reads the live `context`. Notifications bubbling up from a nested * provider are ignored; only events dispatched by this part's own provider * host (or retargeted to it from that host's shadow tree) count. * * Works whether or not the provider element has been upgraded yet: the * listener sits on the element itself, so the provider's first * publishContext() from componentWillLoad() reaches it. */ export declare class ContextConsumer { private readonly parentSelector; private readonly propertyName; private readonly onChange; private provider; constructor(parentSelector: string, propertyName: string, onChange: (context: T | null) => void); /** The provider element found by the last connect(), or null. */ get parent(): HTMLElement | null; /** The provider's current context, or null when there is none yet. */ get context(): T | null; connect(host: HTMLElement): void; disconnect(): void; private handleEvent; } /** * Read a context object a provider published with publishContext(). Returns * null when there is no provider or it has not published yet. */ export declare function readContext(parent: Element | null | undefined, propertyName: string): T | null;