ComponentHydrator
View as MarkdownFinds 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');
ComponentHydrator
Permalink to heading "ComponentHydrator"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
Constructor
Permalink to heading "Constructor"| Name | Description | Required | Default |
|---|---|---|---|
componentName | stringThe name of the component, in camelCase. | Yes | - |
rootId | string | undefinedThe rootId the component was rendered with. Throws if it is missing. | Yes | - |
doc | DocumentThe document the parts are searched in. | No | document |
Properties
Permalink to heading "Properties"| Name | Description |
|---|---|
componentName | stringThe name of the component, in camelCase. |
clientComponentName | stringThe kebab-case form of componentName, as it appears in attribute names. |
doc | DocumentThe document the parts are searched in. |
rootId | stringThe 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
| Name | Description | Required | Default |
|---|---|---|---|
part | string | Yes | - |
selector()
The CSS selector of every element of part that belongs to this instance.
selector(part: string): string
| Name | Description | Required | Default |
|---|---|---|---|
part | string | Yes | - |
scopeKey()
The key that identifies part, and optionally one value of it, across this instance.
scopeKey(part: string, value?: string): string
| Name | Description | Required | Default |
|---|---|---|---|
part | string | Yes | - |
value | string | No | - |
query()
Finds the first element of part.
query<T extends Element = HTMLElement>(part: string, parent?: Element | Document): T | null
| Name | Description | Required | Default |
|---|---|---|---|
part | stringThe name of the part, in camelCase. | Yes | - |
parent | Element | DocumentWhere 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[]
| Name | Description | Required | Default |
|---|---|---|---|
part | stringThe name of the part, in camelCase. | Yes | - |
parent | Element | DocumentWhere 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
| Name | Description | Required | Default |
|---|---|---|---|
element | ElementThe element to mark. | Yes | - |
part | stringThe name of the part, in camelCase. | Yes | - |
value | stringSets 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