@wordpress/compose
Version:
WordPress higher-order components (HOCs).
77 lines (67 loc) • 2.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _element = require("@wordpress/element");
/**
* WordPress dependencies
*/
/**
* When opening modals/sidebars/dialogs, the focus
* must move to the opened area and return to the
* previously focused element when closed.
* The current hook implements the returning behavior.
*
* @param {Function?} onFocusReturn Overrides the default return behavior.
* @return {Function} Element Ref.
*
* @example
* ```js
* import { useFocusReturn } from '@wordpress/compose';
*
* const WithFocusReturn = () => {
* const ref = useFocusReturn()
* return (
* <div ref={ ref }>
* <Button />
* <Button />
* </div>
* );
* }
* ```
*/
function useFocusReturn(onFocusReturn) {
const ref = (0, _element.useRef)();
const focusedBeforeMount = (0, _element.useRef)();
const onFocusReturnRef = (0, _element.useRef)(onFocusReturn);
(0, _element.useEffect)(() => {
onFocusReturnRef.current = onFocusReturn;
}, [onFocusReturn]);
return (0, _element.useCallback)(node => {
if (node) {
// Set ref to be used when unmounting.
ref.current = node; // Only set when the node mounts.
if (focusedBeforeMount.current) {
return;
}
focusedBeforeMount.current = node.ownerDocument.activeElement;
} else if (focusedBeforeMount.current) {
const isFocused = ref.current.contains(ref.current.ownerDocument.activeElement);
if (ref.current.isConnected && !isFocused) {
return;
} // Defer to the component's own explicit focus return behavior, if
// specified. This allows for support that the `onFocusReturn`
// decides to allow the default behavior to occur under some
// conditions.
if (onFocusReturnRef.current) {
onFocusReturnRef.current();
} else {
focusedBeforeMount.current.focus();
}
}
}, []);
}
var _default = useFocusReturn;
exports.default = _default;
//# sourceMappingURL=index.js.map