sdxc

Type to search, or start from one of these:

[ @sdxc/ui/mixins ]

trackHostNode

The insert/remove lifecycle bookkeeping a mixin repeats on its own before it can read or write anything on its own host element from outside its render callback.

import { trackHostNode } from "@sdxc/ui/mixins";

Usage

Caches the DOM node behind a mixin's own host element for as long as it stays mounted, since no host node exists while setup runs; insert/remove keep the returned getter's node current, or undefined while unmounted.

Signature

trackHostNode<node extends EventTarget = Element>(handle: MixinHandle<node>): () => node | undefined

Returns a getter returning the currently mounted host node, or undefined while unmounted.

Parameters

ParameterTypeDescription
handleMixinHandle<node>Mixin handle to track the host node of.

Examples

export const example = createMixin<HTMLElement>((handle) => {
	let hostNode = trackHostNode(handle);

	addEventListeners(document, handle.signal, {
		keydown() {
			let node = hostNode();
			if (node === undefined) return;
			// ...
		},
	});

	return () => {};
});

Written by Sergio Xalambrí. Follow @sergiodxa for new packages, or sponsor the work.