rsuite
Version:
A suite of react components
35 lines (28 loc) • 1.16 kB
JavaScript
;
exports.__esModule = true;
exports.default = useFocus;
var _react = require("react");
function useFocus(elementRef) {
// When grabbing focus, keep track of previous activeElement
// so that we can return focus later
var previousActiveElementRef = (0, _react.useRef)(null); // Focus the element itself
var grab = (0, _react.useCallback)(function () {
requestAnimationFrame(function () {
if (document.activeElement !== elementRef.current) {
var _elementRef$current;
previousActiveElementRef.current = document.activeElement;
(_elementRef$current = elementRef.current) === null || _elementRef$current === void 0 ? void 0 : _elementRef$current.focus();
}
});
}, [elementRef]); // Return focus to previous active element
var release = (0, _react.useCallback)(function (options) {
requestAnimationFrame(function () {
var _previousActiveElemen;
(_previousActiveElemen = previousActiveElementRef.current) === null || _previousActiveElemen === void 0 ? void 0 : _previousActiveElemen.focus(options);
});
}, []);
return {
grab: grab,
release: release
};
}