Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

554 B
Raw Blame
import { useEffect, useRef } from "react";

export function useFocusHeadingOnChange<T extends HTMLElement>(key: string) {
  const containerRef = useRef<T>(null);
  const previousKey = useRef(key);

  useEffect(() => {
    if (previousKey.current === key) return;
    previousKey.current = key;

    const heading = containerRef.current?.querySelector<HTMLElement>("h1");
    if (!heading) return;
    if (!heading.hasAttribute("tabindex")) {
      heading.setAttribute("tabindex", "-1");
    }
    heading.focus();
  }, [key]);

  return containerRef;
}