@yamada-ui/focus-lock
Version:
Yamada UI focus lock component
58 lines (56 loc) • 1.59 kB
JavaScript
"use client"
// src/focus-lock.tsx
import { getAllFocusable, interopDefault } from "@yamada-ui/utils";
import { useCallback } from "react";
import ReactFocusLock from "react-focus-lock";
import { jsx } from "react/jsx-runtime";
var InternalFocusLock = interopDefault(ReactFocusLock);
var FocusLock = ({
autoFocus,
children,
contentRef,
isDisabled,
disabled = isDisabled,
finalFocusRef,
initialFocusRef,
lockFocusAcrossFrames,
persistentFocus,
restoreFocus
}) => {
const returnFocus = restoreFocus && !finalFocusRef;
const onActivation = useCallback(() => {
if (initialFocusRef == null ? void 0 : initialFocusRef.current) {
initialFocusRef.current.focus();
} else if (contentRef == null ? void 0 : contentRef.current) {
const focusables = getAllFocusable(contentRef.current);
if (focusables.length === 0)
requestAnimationFrame(() => {
var _a;
(_a = contentRef.current) == null ? void 0 : _a.focus();
});
}
}, [initialFocusRef, contentRef]);
const onDeactivation = useCallback(() => {
var _a;
(_a = finalFocusRef == null ? void 0 : finalFocusRef.current) == null ? void 0 : _a.focus();
}, [finalFocusRef]);
return /* @__PURE__ */ jsx(
InternalFocusLock,
{
autoFocus,
crossFrame: lockFocusAcrossFrames,
disabled,
persistentFocus,
returnFocus,
onActivation,
onDeactivation,
children
}
);
};
FocusLock.displayName = "FocusLock";
FocusLock.__ui__ = "FocusLock";
export {
FocusLock
};
//# sourceMappingURL=chunk-MVPR4RH5.mjs.map