Skip to content

Commit

Permalink
fix: update when events are added to manage overlays
Browse files Browse the repository at this point in the history
  • Loading branch information
Westbrook committed Aug 9, 2021
1 parent 4efe767 commit 60cddac
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
22 changes: 18 additions & 4 deletions packages/overlay/src/overlay-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ export class OverlayStack {
private handlingResize = false;
private overlayTimer = new OverlayTimer();

public constructor() {
this.addEventListeners();
}

private canTabTrap = true;
private trappingInited = false;
private tabTrapper!: HTMLElement;
Expand Down Expand Up @@ -158,13 +154,27 @@ export class OverlayStack {
window.addEventListener('resize', this.handleResize);
}

private removeEventListeners(): void {
this.document.removeEventListener(
'click',
this.handleMouseCapture,
true
);
this.document.removeEventListener('click', this.handleMouse);
this.document.removeEventListener('keyup', this.handleKeyUp);
window.removeEventListener('resize', this.handleResize);
}

private isClickOverlayActiveForTrigger(trigger: HTMLElement): boolean {
return this.overlays.some(
(item) => trigger === item.trigger && item.interaction === 'click'
);
}

public async openOverlay(details: OverlayOpenDetail): Promise<boolean> {
if (!this.overlays.length) {
this.addEventListeners();
}
if (this.findOverlayForContent(details.content)) {
return false;
}
Expand Down Expand Up @@ -423,6 +433,10 @@ export class OverlayStack {
},
})
);

if (!this.overlays.length) {
this.removeEventListeners();
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions packages/overlay/test/benchmark/basic-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2020 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

import '@spectrum-web-components/overlay/overlay-trigger.js';
import '@spectrum-web-components/button/sp-button.js';
import '@spectrum-web-components/popover/sp-popover.js';
import { html } from '@spectrum-web-components/base';
import { measureFixtureCreation } from '../../../../test/benchmark/helpers.js';

measureFixtureCreation(
html`
<overlay-trigger>
<sp-button slot="trigger">Trigger</sp-button>
<sp-popover slot="content">
<p>This is the content.</p>
</sp-popover>
</overlay-trigger>
`
);

0 comments on commit 60cddac

Please sign in to comment.