import type { JSX } from "solid-js";
import { createUniqueId, onCleanup, onMount, splitProps } from "solid-js";
import { useInternalDrawerContext } from "./drawer-context";

/** Props for Drawer.Title. */
export interface DrawerTitleProps extends JSX.HTMLAttributes<HTMLHeadingElement> {
  children?: JSX.Element;
}

/**
 * Title for the Drawer. Registers its ID for aria-labelledby.
 */
export function DrawerTitle(props: DrawerTitleProps): JSX.Element {
  const [local, rest] = splitProps(props, ["children"]);
  const ctx = useInternalDrawerContext();
  const id = createUniqueId();
  onMount(() => ctx.setTitleId(id));
  onCleanup(() => ctx.setTitleId(undefined));
  return (
    <h2 id={id} {...rest}>
      {local.children}
    </h2>
  );
}
