Skip to content

Commit

Permalink
Migrates deferred-composition-child to HydroActive.
Browse files Browse the repository at this point in the history
Now that HydroActive supports exposing public properties on the custom element, there is no need for a native custom element.
  • Loading branch information
dgp1130 committed Dec 2, 2024
1 parent dc957fc commit f1c0ffd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 36 deletions.
51 changes: 17 additions & 34 deletions src/demo/hydration/deferred-composition-child.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,25 @@
/**
* An implementation of the `defer-hydration` community protocol which provides
* the name of the speaker in the underlying hydrated DOM.
*/
export class DeferredCompositionChild extends HTMLElement {
// Implement `defer-hydration` community protocol.
#hydrated = false;
public static observedAttributes = ['defer-hydration'];
public connectedCallback(): void {
if (!this.hasAttribute('defer-hydration')) this.#hydrate();
}
public attributeChangedCallback(
name: string, _oldValue: string | null, newValue: string | null): void {
if (name === 'defer-hydration' && newValue === null) {
this.#hydrate();
}
}

// Hydrate some state.
#speaker!: string;
#hydrate(): void {
if (this.#hydrated) return;
this.#hydrated = true;
import { defineBaseComponent } from 'hydroactive';

this.#speaker = this.querySelector('span#subject')!.textContent!;
console.log(`Hydrating ${this.#speaker}!`);
this.querySelector('span#target')!.textContent = 'HydroActive';
}
/** Hydrates by reading the speaker's name from the DOM and exposing it. */
export const DeferredCompositionChild = defineBaseComponent(
'deferred-composition-child',
(host) => {
const speaker = host.query('span#subject').access().read(String);
console.log(`Hydrating ${speaker}!`);

// Return hydrated state.
public getSpeakerName(): string {
return this.#speaker;
}
}
host.query('span#target').access().write('HydroActive', String);

customElements.define('deferred-composition-child', DeferredCompositionChild);
return {
/** Provides hydrated speaker name. */
getSpeakerName(): string {
return speaker;
}
};
},
);

declare global {
interface HTMLElementTagNameMap {
'deferred-composition-child': DeferredCompositionChild;
'deferred-composition-child': InstanceType<typeof DeferredCompositionChild>;
}
}
4 changes: 2 additions & 2 deletions src/demo/hydration/deferred-composition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('deferred-composition', () => {
document.body.append(el);

const firstChild = el.querySelector('#first')! as
DeferredCompositionChild;
InstanceType<typeof DeferredCompositionChild>;
expect(isHydrated(firstChild)).toBeTrue();

expect(firstChild.querySelector('#target')!.textContent!)
Expand All @@ -52,7 +52,7 @@ describe('deferred-composition', () => {
hydrate(el, DeferredComposition);

const secondChild = el.querySelector('#second')! as
DeferredCompositionChild;
InstanceType<typeof DeferredCompositionChild>;
expect(isHydrated(secondChild)).toBeTrue();

expect(secondChild.querySelector('#target')!.textContent!)
Expand Down

0 comments on commit f1c0ffd

Please sign in to comment.