Skip to content

The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.

780 B
Raw Blame
import { useEffect, type RefObject } from "react";

/**
 * The editor area focuses an editor kind's host element (`[data-editor-focus]`);
 * hand that focus on to `target` so the viewer's keyboard handling and context
 * keys apply right away.
 */
export function useForwardFocus(target: RefObject<HTMLElement | null>) {
  useEffect(() => {
    const el = target.current;
    const host = el?.closest<HTMLElement>("[data-editor-focus]");
    if (!el || !host) return;
    const onFocus = (e: FocusEvent) => {
      if (e.target === host) el.focus({ preventScroll: true });
    };
    host.addEventListener("focus", onFocus);
    if (document.activeElement === host) el.focus({ preventScroll: true });
    return () => host.removeEventListener("focus", onFocus);
  }, [target]);
}