@atlaskit/navigation-system
Version:
The latest navigation system for Atlassian apps.
56 lines (55 loc) • 1.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.bindOpenLayerChange = bindOpenLayerChange;
var _bindEventListener = require("bind-event-listener");
/**
* Calls `onChange` after any popover or dialog inside `within` opens or closes.
* Read the new state with `hasOpenDialogOrPopover`.
*
* `beforetoggle`, `toggle` and `close` do not bubble, so they are observed in the capture phase.
*
* `toggle` and `close` are queued tasks. When a surface closes because it unmounts,
* they fire after the element has left the DOM, so they never reach `within`.
* `beforetoggle` fires synchronously while the element is still inside `within`,
* so it also schedules `onChange` in a microtask, after the state has changed.
*/
function bindOpenLayerChange(_ref) {
var within = _ref.within,
onChange = _ref.onChange;
var isBound = true;
function listener() {
onChange();
}
function deferredListener() {
queueMicrotask(function () {
if (isBound) {
onChange();
}
});
}
var unbind = (0, _bindEventListener.bindAll)(within, [{
type: 'beforetoggle',
listener: deferredListener,
options: {
capture: true
}
}, {
type: 'toggle',
listener: listener,
options: {
capture: true
}
}, {
type: 'close',
listener: listener,
options: {
capture: true
}
}]);
return function cleanup() {
isBound = false;
unbind();
};
}