@lexical/react
Version:
This package provides Lexical components and hooks for React applications.
34 lines (33 loc) • 1.5 kB
TypeScript
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import { type FocusTrapInitialFocus } from '@lexical/a11y';
import { type RefCallback } from 'react';
export type { FocusTrapInitialFocus } from '@lexical/a11y';
/**
* Returns a `RefCallback` that registers the attached DOM element as a
* focus-trap container with {@link FocusTrapExtension}. The trap
* activates when `isActive` is `true` and the element is mounted, and is
* released when the element detaches or `isActive` becomes `false`.
*
* ```tsx
* const trapRef = useLexicalFocusTrapRef(true, 'container');
* return <div ref={trapRef} tabIndex={-1}>…</div>;
* ```
*
* `allowOutside` is the escape hatch for descendants that portal outside the
* container (autocomplete popups, tooltips): return `true` for those targets so
* the trap lets them keep focus instead of pulling it back. It is held in a ref
* and read at event time, so passing an inline lambda is fine — it does not
* re-create the trap on every render.
*
* Multiple elements can use this hook simultaneously — each gets its
* own independent focus trap.
*
* Requires `FocusTrapExtension` in the editor's extension tree.
*/
export declare function useLexicalFocusTrapRef(isActive: boolean, initialFocus?: FocusTrapInitialFocus, allowOutside?: (target: HTMLElement) => boolean): RefCallback<HTMLElement>;