All files / src/dom render.ts

100% Statements 16/16
100% Branches 3/3
100% Functions 1/1
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 311x 1x                   1x 8x   4x 4x 4x 4x 4x       4x 4x 4x 4x 4x   4x 4x  
import { AidedError } from '../error';
import { createRoot } from '../lifecycle/lifecycle';
import type { Disposer } from '../types';
 
/**
 * Renders a component into a DOM node.
 *
 * @param component A function that returns a DOM element.
 * @param mountNode The DOM node to mount the component into.
 * @returns A disposer function to unmount the component and clean up all resources.
 */
export function render(component: () => Element, mountNode: Element): Disposer {
  if (!mountNode) {
    // Use the new custom error with a more descriptive message
    throw new AidedError(
      'render(...): The provided mount node is null or undefined. Aided requires a valid DOM element to render into.',
      'INVALID_MOUNT_NODE'
    );
  }
 
  // createRoot establishes the lifecycle for the entire application.
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const dispose = createRoot((disposeRoot) => {
    const appElement = component();
    mountNode.innerHTML = ''; // Clear the mount node
    mountNode.appendChild(appElement);
  });
 
  return dispose;
}