fix: fix backdrop teardown/init bugs and align APIs WIP

This commit is contained in:
jorenbroekema 2020-10-07 18:08:27 +02:00
parent 8301092d9d
commit b5e7df3ed9

View file

@ -160,6 +160,8 @@ export class OverlayController extends EventTargetShim {
this.updateConfig(config); this.updateConfig(config);
this.__hasActiveTrapsKeyboardFocus = false; this.__hasActiveTrapsKeyboardFocus = false;
this.__hasActiveBackdrop = true; this.__hasActiveBackdrop = true;
/** @type {HTMLElement | undefined} */
this.__backdropNodeToBeTornDown = undefined;
this.__escKeyHandler = this.__escKeyHandler.bind(this); this.__escKeyHandler = this.__escKeyHandler.bind(this);
} }
@ -418,7 +420,7 @@ export class OverlayController extends EventTargetShim {
*/ */
updateConfig(cfgToAdd) { updateConfig(cfgToAdd) {
// Teardown all previous configs // Teardown all previous configs
this._handleFeatures({ phase: 'teardown' }); this.teardown();
/** @type {OverlayConfig} */ /** @type {OverlayConfig} */
this.__prevConfig = this.config || {}; this.__prevConfig = this.config || {};
@ -896,19 +898,31 @@ export class OverlayController extends EventTargetShim {
* @param {{ animation?: boolean, phase: OverlayPhase }} config * @param {{ animation?: boolean, phase: OverlayPhase }} config
*/ */
_handleBackdrop({ animation = true, phase }) { _handleBackdrop({ animation = true, phase }) {
if (this.placementMode === 'local') {
switch (phase) { switch (phase) {
case 'init': case 'init': {
if (!this.backdropNode) { if (!this.backdropNode) {
this.__backdropNode = document.createElement('div'); this.__backdropNode = document.createElement('div');
/** @type {HTMLElement} */ /** @type {HTMLElement} */
(this.backdropNode).classList.add('local-overlays__backdrop'); (this.backdropNode).classList.add(`${this.placementMode}-overlays__backdrop`);
} }
this.backdropNode.slot = '_overlay-shadow-outlet'; this.backdropNode.slot = '_overlay-shadow-outlet';
/** @type {HTMLElement} */
(this.contentNode.parentNode).insertBefore(this.backdropNode, this.contentNode); let insertionAnchor = /** @type {HTMLElement} */ (this.contentNode.parentNode);
let insertionBefore = this.contentNode;
if (this.placementMode === 'global') {
insertionAnchor = /** @type {HTMLElement} */ (this.contentWrapperNode.parentElement);
insertionBefore = this.contentWrapperNode;
}
insertionAnchor.insertBefore(this.backdropNode, insertionBefore);
break; break;
}
case 'show': case 'show':
if (this.placementMode === 'global') {
this.backdropNode.classList.add('global-overlays__backdrop--visible');
if (animation === true) {
this.backdropNode.classList.add('global-overlays__backdrop--fade-in');
}
}
this.__hasActiveBackdrop = true; this.__hasActiveBackdrop = true;
break; break;
case 'hide': case 'hide':
@ -916,50 +930,20 @@ export class OverlayController extends EventTargetShim {
return; return;
} }
this.__hasActiveBackdrop = false; this.__hasActiveBackdrop = false;
break;
case 'teardown':
if (!this.backdropNode || !this.backdropNode.parentNode) {
return;
}
this.backdropNode.parentNode.removeChild(this.backdropNode);
this.__backdropNode = undefined;
break;
/* no default */
}
return;
}
switch (phase) {
case 'init':
this.__backdropNode = document.createElement('div');
this.backdropNode.classList.add('global-overlays__backdrop');
/** @type {HTMLElement} */
(this.contentWrapperNode.parentElement).insertBefore(
this.backdropNode,
this.contentWrapperNode,
);
break;
case 'show':
this.backdropNode.classList.add('global-overlays__backdrop--visible');
if (animation === true) {
this.backdropNode.classList.add('global-overlays__backdrop--fade-in');
}
this.__hasActiveBackdrop = true;
break;
case 'hide':
if (!this.backdropNode) {
return;
}
this.backdropNode.classList.remove('global-overlays__backdrop--fade-in');
if (this.placementMode === 'global') {
this.backdropNode.classList.remove('global-overlays__backdrop--fade-in');
if (animation) { if (animation) {
/** @type {(ev:AnimationEvent) => void} */ /** @type {(ev:AnimationEvent) => void} */
let afterFadeOut; let afterFadeOut;
this.backdropNode.classList.add('global-overlays__backdrop--fade-out'); this.backdropNode.classList.add('global-overlays__backdrop--fade-out');
this.__backDropAnimation = new Promise(resolve => { this.__backDropAnimation = new Promise(resolve => {
afterFadeOut = () => { afterFadeOut = () => {
if (this.backdropNode) {
this.backdropNode.classList.remove('global-overlays__backdrop--fade-out'); this.backdropNode.classList.remove('global-overlays__backdrop--fade-out');
this.backdropNode.classList.remove('global-overlays__backdrop--visible'); this.backdropNode.classList.remove('global-overlays__backdrop--visible');
this.backdropNode.removeEventListener('animationend', afterFadeOut); this.backdropNode.removeEventListener('animationend', afterFadeOut);
}
resolve(); resolve();
}; };
}); });
@ -968,20 +952,27 @@ export class OverlayController extends EventTargetShim {
} else { } else {
this.backdropNode.classList.remove('global-overlays__backdrop--visible'); this.backdropNode.classList.remove('global-overlays__backdrop--visible');
} }
this.__hasActiveBackdrop = false; }
break; break;
case 'teardown': case 'teardown':
if (!this.backdropNode || !this.backdropNode.parentNode) { if (!this.backdropNode || !this.backdropNode.parentNode) {
return; return;
} }
if (animation && this.__backDropAnimation) { if (animation && this.__backDropAnimation) {
this.__backdropNodeToBeTornDown = this.backdropNode;
this.__backDropAnimation.then(() => { this.__backDropAnimation.then(() => {
/** @type {HTMLElement} */ if (this.__backdropNodeToBeTornDown) {
(this.backdropNode.parentNode).removeChild(this.backdropNode); /** @type {HTMLElement} */ (this.__backdropNodeToBeTornDown.parentNode).removeChild(
this.__backdropNodeToBeTornDown,
);
}
}); });
} else { } else {
this.backdropNode.parentNode.removeChild(this.backdropNode); this.backdropNode.parentNode.removeChild(this.backdropNode);
} }
this.__backdropNode = undefined;
break; break;
/* no default */ /* no default */
} }