-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrates
deferred-composition-child
to HydroActive.
Now that HydroActive supports exposing public properties on the custom element, there is no need for a native custom element.
- Loading branch information
Showing
2 changed files
with
19 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters