@wordpress/compose
Version:
WordPress higher-order components (HOCs).
53 lines (49 loc) • 1.27 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = useFocusableIframe;
var _useRefEffect = _interopRequireDefault(require("../use-ref-effect"));
/**
* External dependencies
*/
/**
* Internal dependencies
*/
/**
* Dispatches a bubbling focus event when the iframe receives focus. Use
* `onFocus` as usual on the iframe or a parent element.
*
* @return Ref to pass to the iframe.
*/
function useFocusableIframe() {
return (0, _useRefEffect.default)(element => {
const {
ownerDocument
} = element;
if (!ownerDocument) {
return;
}
const {
defaultView
} = ownerDocument;
if (!defaultView) {
return;
}
/**
* Checks whether the iframe is the activeElement, inferring that it has
* then received focus, and dispatches a focus event.
*/
function checkFocus() {
if (ownerDocument && ownerDocument.activeElement === element) {
element.focus();
}
}
defaultView.addEventListener('blur', checkFocus);
return () => {
defaultView.removeEventListener('blur', checkFocus);
};
}, []);
}
//# sourceMappingURL=index.js.map
;