Skip to main content

ComponentHydrator

View as Markdown

Finds the parts of one component instance in the DOM.

A component without a state machine finds its parts with a ComponentHydrator. The server marks the parts with ui:ref, the hydrator queries them by name.

The mountAll callback hands you createHydrator, which returns a hydrator scoped to the current instance:

import { mountAll } from 'fluid-primitives';

mountAll('ui:productGallery', ({ createHydrator }) => {
    const hydrator = createHydrator();

    const thumbnails = hydrator.queryAll<HTMLButtonElement>('thumbnail');
    const image = hydrator.query<HTMLImageElement>('image');
    if (!image) return;

    thumbnails.forEach(thumbnail => {
        thumbnail.addEventListener('click', () => {
            image.src = thumbnail.dataset.src ?? image.src;
        });
    });
});

query() returns the first matching element or null, queryAll() every match. Both search the whole document, so content that was portaled elsewhere is found too. Pass an element as the second argument to narrow the search to it.

Every Component instance exposes its own hydrator as instance.hydrator, for example combobox.hydrator.query('list'). Mark an element you create on the client like ui:ref does with hydrator.stamp(el, 'part', value?).

Outside a mountAll callback, create a hydrator with the component name and the root id. It takes the bare component name, without a namespace:

import { ComponentHydrator } from 'fluid-primitives';

const hydrator = new ComponentHydrator('productGallery', 'root-id-123');

Finds the parts of one component instance. Every part that ui:ref renders carries a data-<component>-<part>="<rootId>" attribute, so a lookup needs no id and searches the whole document by default. That is also what finds content that was portaled away from the root.

class ComponentHydrator
NameDescriptionRequiredDefault
componentNamestring
The name of the component, in camelCase.
Yes
-
rootIdstring | undefined
The rootId the component was rendered with. Throws if it is missing.
Yes
-
docDocument
The document the parts are searched in.
No
document
NameDescription
componentNamestring
The name of the component, in camelCase.
clientComponentNamestring
The kebab-case form of componentName, as it appears in attribute names.
docDocument
The document the parts are searched in.
rootIdstring
The rootId the component was rendered with.

attr()

The name of the attribute that marks part, e.g. data-accordion-item-trigger.

attr(part: string): string
NameDescriptionRequiredDefault
partstringYes
-

selector()

The CSS selector of every element of part that belongs to this instance.

selector(part: string): string
NameDescriptionRequiredDefault
partstringYes
-

scopeKey()

The key that identifies part, and optionally one value of it, across this instance.

scopeKey(part: string, value?: string): string
NameDescriptionRequiredDefault
partstringYes
-
valuestringNo
-

query()

Finds the first element of part.

query<T extends Element = HTMLElement>(part: string, parent?: Element | Document): T | null
NameDescriptionRequiredDefault
partstring
The name of the part, in camelCase.
Yes
-
parentElement | Document
Where to search, the whole document by default.
No
this.doc

Returns T | null. The element, or null if there is none.

queryAll()

Finds every element of part.

queryAll<T extends Element = HTMLElement>(part: string, parent?: Element | Document): T[]
NameDescriptionRequiredDefault
partstring
The name of the part, in camelCase.
Yes
-
parentElement | Document
Where to search, the whole document by default.
No
this.doc

stamp()

Marks an element as part of this instance, like ui:ref does on the server. For elements you create after hydration.

stamp(element: Element, part: string, value?: string): void
NameDescriptionRequiredDefault
elementElement
The element to mark.
Yes
-
partstring
The name of the part, in camelCase.
Yes
-
valuestring
Sets data-value, for a part that exists once per item.
No
-

destroy()

Nothing to release yet, it exists so every instance can be destroyed alike.

destroy(): void