UNPKG

rsuite

Version:

A suite of react components

36 lines (33 loc) 1.19 kB
'use client'; "use strict"; exports.__esModule = true; exports.default = void 0; exports.useFocus = useFocus; var _react = require("react"); function useFocus(elementRef) { // When grabbing focus, keep track of previous activeElement // so that we can return focus later const previousActiveElementRef = (0, _react.useRef)(null); // Focus the element itself const grab = (0, _react.useCallback)(() => { requestAnimationFrame(() => { if (document.activeElement !== elementRef.current) { var _elementRef$current; previousActiveElementRef.current = document.activeElement; (_elementRef$current = elementRef.current) === null || _elementRef$current === void 0 || _elementRef$current.focus(); } }); }, [elementRef]); // Return focus to previous active element const release = (0, _react.useCallback)(options => { requestAnimationFrame(() => { var _previousActiveElemen; (_previousActiveElemen = previousActiveElementRef.current) === null || _previousActiveElemen === void 0 || _previousActiveElemen.focus(options); }); }, []); return { grab, release }; } var _default = exports.default = useFocus;