@uspk-ui/focus-lock
Version:
Focus lock utils for all the components
54 lines (52 loc) • 1.49 kB
JavaScript
// ../../../react-shim.js
import React from "react";
// src/focus-lock.tsx
import ReactFocusLock from "react-focus-lock";
import { getAllFocusable } from "@uspk-ui/dom-utils";
import { useCallback } from "react";
import { jsx } from "react/jsx-runtime";
var FocusLock = (props) => {
const {
initialFocusRef,
finalFocusRef,
contentRef,
restoreFocus,
children,
isDisabled,
autoFocus,
persistentFocus,
lockFocusAcrossFrames
} = props;
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]);
const returnFocus = restoreFocus && !finalFocusRef;
return /* @__PURE__ */ jsx(ReactFocusLock, {
crossFrame: lockFocusAcrossFrames,
persistentFocus,
autoFocus,
disabled: isDisabled,
onActivation,
onDeactivation,
returnFocus,
children
});
};
FocusLock.displayName = "FocusLock";
export {
FocusLock
};