Skip to content

Commit

Permalink
Updates composed-counter demo to leverage the on-demand definitions…
Browse files Browse the repository at this point in the history
… community protocol.

This demo shows how one component can be defined in the top-level scope (`CounterController`) and then implicitly define its dependency when used (`CounterDisplay`). This allows the dependency (`CounterDisplay`) to be defined in a pure fashion which is eligible for tree-shaking.
  • Loading branch information
dgp1130 committed Dec 4, 2024
1 parent f335ebb commit ae1a107
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/demo/composition/composed-counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export const CounterDisplay = defineSignalComponent(
},
);

// No need to call `CounterDisplay.define()` here, the component is defined
// automatically by usage in `CounterController`.
// `CounterDisplay` definition is pure and tree-shakable!

declare global {
interface HTMLElementTagNameMap {
'counter-display': InstanceType<typeof CounterDisplay>;
Expand All @@ -38,6 +42,7 @@ export const CounterController = defineSignalComponent(
'counter-controller',
(host) => {
// Get a reference to the underlying `counter-display` element.
// Automatically defines `CounterDisplay` if it isn't already defined.
const inner = host.query('counter-display').access(CounterDisplay).element;

// Bind the button clicks to modifying the counter.
Expand All @@ -50,6 +55,8 @@ export const CounterController = defineSignalComponent(
},
);

CounterController.define();

declare global {
interface HTMLElementTagNameMap {
'counter-controller': InstanceType<typeof CounterController>;
Expand Down

0 comments on commit ae1a107

Please sign in to comment.