дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-11-08 19:02:17 +02:00
21 рядки
780 B
TypeScript
21 рядки
780 B
TypeScript
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]);
|
|
}
|