Skip to content

DOM Lifecycles

Elements managed by Croft receive lifecycle events dispatched as native custom events.

Available Lifecycles

AttributeEventWhen
onCreate@createWhen the DocumentFragment is created
onMount / onLoad@mountWhen attached to the document body
onUnmount@unmountWhen removed from its parent
onDestroy@destroyWhen detached from the document

Usage

typescript
html`
  <div
    onCreate=${() => console.log("Element created")}
    onMount=${() => console.log("Element mounted to DOM")}
    onUnmount=${() => console.log("Element removed from DOM")}
    onDestroy=${() => console.log("Element destroyed")}
  >
    Lifecycle Demo
  </div>
`;

onMount / onLoad

Fires when an element is added to the document body. Useful for:

  • Starting animations
  • Fetching data
  • Focusing inputs
  • Integrating with third-party libraries
typescript
html`
  <input
    onMount=${(e: Event) => (e.target as HTMLInputElement).focus()}
  />
`;

onDestroy

Fires when an element is removed from the DOM and is no longer in the document. Useful for:

  • Canceling timers/intervals
  • Removing event listeners
  • Freeing resources
typescript
const state = createHook({ mounted: true });

const timer = setInterval(() => console.log("tick"), 1000);

html`
  <div onDestroy=${() => clearInterval(timer)}>
    This div cleans up its timer when removed
  </div>
`;

How It Works

Lifecycles are powered by a MutationObserver on document.body. When elements are added or removed, the observer dispatches the corresponding events on the element and all its descendants.

Disabling Lifecycles

typescript
import Croft from "croft";

Croft.disableLifecycle();

// Lifecycles can be re-enabled:
Croft.enableLifecycle();

Lifecycle observation is automatically disabled in non-browser environments (SSR, testing).