import { children, createRenderEffect, onCleanup, type JSX } from "solid-js";

/**
 * Document title.
 *
 * The `children` must be plain text.
 */
export default function (props: { children?: JSX.Element }) {
  let otitle: string | undefined;

  createRenderEffect(() => (otitle = document.title));

  const title = children(() => props.children);

  createRenderEffect(
    () => (document.title = (title.toArray() as string[]).join("")),
  );

  onCleanup(() => (document.title = otitle!));

  return <></>;
}
