• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

adobe / spectrum-web-components / 13322647394

14 Feb 2025 04:40AM UTC coverage: 98.189%. First build
13322647394

Pull #5082

github

web-flow
Merge 88793c34c into ff7318295
Pull Request #5082: fix(menu): make submenu scrollable

5187 of 5464 branches covered (94.93%)

Branch coverage included in aggregate %.

33099 of 33528 relevant lines covered (98.72%)

386.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.01
/packages/overlay/src/OverlayStack.ts
1
/*
41✔
2
Copyright 2023 Adobe. All rights reserved.
41✔
3
This file is licensed to you under the Apache License, Version 2.0 (the "License");
41✔
4
you may not use this file except in compliance with the License. You may obtain a copy
41✔
5
of the License at http://www.apache.org/licenses/LICENSE-2.0
41✔
6
Unless required by applicable law or agreed to in writing, software distributed under
41✔
7
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
41✔
8
OF ANY KIND, either express or implied. See the License for the specific language
41✔
9
governing permissions and limitations under the License.
41✔
10
*/
41✔
11

41✔
12
import { Overlay } from './Overlay.js';
41✔
13

41✔
14
const supportsPopover = 'showPopover' in document.createElement('div');
41✔
15

41✔
16
class OverlayStack {
41✔
17
    private get document(): Document {
41✔
18
        return this.root.ownerDocument /* c8 ignore next */ || document;
41✔
19
    }
41✔
20

41✔
21
    private pointerdownPath?: EventTarget[];
41✔
22

41✔
23
    private lastOverlay?: Overlay;
41✔
24

41✔
25
    private root: HTMLElement = document.body;
41✔
26

41✔
27
    stack: Overlay[] = [];
41✔
28

41✔
29
    constructor() {
41✔
30
        this.bindEvents();
41✔
31
    }
41✔
32

41✔
33
    bindEvents(): void {
41✔
34
        this.document.addEventListener('pointerdown', this.handlePointerdown);
41✔
35
        this.document.addEventListener('pointerup', this.handlePointerup);
41✔
36
        this.document.addEventListener('keydown', this.handleKeydown);
41✔
37
    }
41✔
38

41✔
39
    private closeOverlay(overlay: Overlay): void {
41✔
40
        const overlayIndex = this.stack.indexOf(overlay);
522✔
41
        if (overlayIndex > -1) {
522✔
42
            this.stack.splice(overlayIndex, 1);
443✔
43
        }
443✔
44
        overlay.open = false;
522✔
45
    }
522✔
46

41✔
47
    /**
41✔
48
     * Cach the `pointerdownTarget` for later testing
41✔
49
     *
41✔
50
     * @param event {ClickEvent}
41✔
51
     */
41✔
52
    handlePointerdown = (event: Event): void => {
41✔
53
        this.pointerdownPath = event.composedPath();
172✔
54
        this.lastOverlay = this.stack[this.stack.length - 1];
172✔
55
    };
172✔
56

41✔
57
    /**
41✔
58
     * Close all overlays that are not ancestors of this click event
41✔
59
     *
41✔
60
     * @param event {ClickEvent}
41✔
61
     */
41✔
62
    handlePointerup = (): void => {
41✔
63
        // Test against the composed path in `pointerdown` in case the visitor moved their
507✔
64
        // pointer during the course of the interaction.
507✔
65
        // Ensure that this value is cleared even if the work in this method goes undone.
507✔
66
        const composedPath = this.pointerdownPath;
507✔
67
        this.pointerdownPath = undefined;
507✔
68
        if (!this.stack.length) return;
507✔
69
        if (!composedPath?.length) return;
507✔
70
        const lastOverlay = this.lastOverlay;
53✔
71
        this.lastOverlay = undefined;
53✔
72

53✔
73
        const lastIndex = this.stack.length - 1;
53✔
74
        const nonAncestorOverlays = this.stack.filter((overlay, i) => {
53✔
75
            const inStack = composedPath.find(
53✔
76
                (el) =>
53✔
77
                    // The Overlay is in the stack
671✔
78
                    el === overlay ||
671✔
79
                    // The Overlay trigger is in the stack and the Overlay is a "hint"
628✔
80
                    (el === overlay?.triggerElement &&
628!
81
                        'hint' === overlay?.type) ||
6!
82
                    // The last Overlay in the stack is not the last Overlay at `pointerdown` time and has a
624✔
83
                    // `triggerInteraction` of "longpress", meaning it was opened by this poitner interaction
624✔
84
                    (i === lastIndex &&
624✔
85
                        overlay !== lastOverlay &&
624✔
86
                        overlay.triggerInteraction === 'longpress')
13✔
87
            );
53✔
88
            return (
53✔
89
                !inStack &&
53✔
90
                !overlay.shouldPreventClose() &&
4✔
91
                overlay.type !== 'manual' &&
3✔
92
                // Don't close if this overlay is modal and not on top of the overlay stack.
2✔
93
                !(overlay.type === 'modal' && lastOverlay !== overlay)
2✔
94
            );
53✔
95
        }) as Overlay[];
53✔
96
        nonAncestorOverlays.reverse();
53✔
97
        nonAncestorOverlays.forEach((overlay) => {
53✔
98
            this.closeOverlay(overlay);
2✔
99
            let parentToClose = overlay.parentOverlayToForceClose;
2✔
100
            while (parentToClose) {
2✔
101
                this.closeOverlay(parentToClose);
×
102
                parentToClose = parentToClose.parentOverlayToForceClose;
×
103
            }
×
104
        });
53✔
105
    };
507✔
106

41✔
107
    handleBeforetoggle = (event: Event): void => {
41✔
108
        const { target, newState: open } = event as Event & {
442✔
109
            newState: string;
442✔
110
        };
442✔
111
        if (open === 'open') return;
442✔
112
        this.closeOverlay(target as Overlay);
33✔
113
    };
442✔
114

41✔
115
    private handleKeydown = (event: KeyboardEvent): void => {
41✔
116
        if (event.code !== 'Escape') return;
285✔
117
        if (!this.stack.length) return;
18✔
118
        const last = this.stack[this.stack.length - 1];
17✔
119
        if (last?.type === 'page') {
285!
120
            event.preventDefault();
1✔
121
            return;
1✔
122
        }
1✔
123
        if (supportsPopover) return;
16!
124
        if (last?.type === 'manual') {
285!
125
            // Manual Overlays should not close on "light dismiss".
×
126
            return;
×
127
        }
×
128

×
129
        if (!last) return;
×
130
        this.closeOverlay(last);
×
131
    };
285✔
132

41✔
133
    /**
41✔
134
     * Get an array of Overlays that all share the same trigger element.
41✔
135
     *
41✔
136
     * @param triggerElement {HTMLELement}
41✔
137
     * @returns {Overlay[]}
41✔
138
     */
41✔
139
    overlaysByTriggerElement(triggerElement: HTMLElement): Overlay[] {
41✔
140
        return this.stack.filter(
×
141
            (overlay) => overlay.triggerElement === triggerElement
×
142
        );
×
143
    }
×
144

41✔
145
    /**
41✔
146
     * When overlays are added manage the open state of exisiting overlays appropriately:
41✔
147
     * - 'modal': should close other non-'modal' and non-'manual' overlays
41✔
148
     * - 'page': should close other non-'modal' and non-'manual' overlays
41✔
149
     * - 'auto': should close other 'auto' overlays and other 'hint' overlays, but not 'manual' overlays
41✔
150
     * - 'manual': shouldn't close other overlays
41✔
151
     * - 'hint': shouldn't close other overlays and give way to all other overlays on a trigger
41✔
152
     */
41✔
153
    add(overlay: Overlay): void {
41✔
154
        if (this.stack.includes(overlay)) {
449✔
155
            const overlayIndex = this.stack.indexOf(overlay);
3✔
156
            if (overlayIndex > -1) {
3✔
157
                this.stack.splice(overlayIndex, 1);
3✔
158
                this.stack.push(overlay);
3✔
159
            }
3✔
160
            return;
3✔
161
        }
3✔
162
        if (
446✔
163
            overlay.type === 'auto' ||
446✔
164
            overlay.type === 'modal' ||
141✔
165
            overlay.type === 'page'
100✔
166
        ) {
449✔
167
            // manage closing open overlays
355✔
168
            const queryPathEventName = 'sp-overlay-query-path';
355✔
169
            const queryPathEvent = new Event(queryPathEventName, {
355✔
170
                composed: true,
355✔
171
                bubbles: true,
355✔
172
            });
355✔
173
            overlay.addEventListener(
355✔
174
                queryPathEventName,
355✔
175
                (event: Event) => {
355✔
176
                    const path = event.composedPath();
355✔
177
                    this.stack.forEach((overlayEl) => {
355✔
178
                        const inPath = path.find((el) => el === overlayEl);
61✔
179
                        if (
61✔
180
                            !inPath &&
61✔
181
                            overlayEl.type !== 'manual' &&
36✔
182
                            overlayEl.type !== 'modal'
34✔
183
                        ) {
61✔
184
                            this.closeOverlay(overlayEl);
33✔
185
                        }
33✔
186
                    });
355✔
187
                },
355✔
188
                { once: true }
355✔
189
            );
355✔
190
            overlay.dispatchEvent(queryPathEvent);
355✔
191
        } else if (overlay.type === 'hint') {
449✔
192
            const hasPrevious = this.stack.some((overlayEl) => {
81✔
193
                return (
12✔
194
                    overlayEl.type !== 'manual' &&
12✔
195
                    overlayEl.triggerElement &&
12✔
196
                    overlayEl.triggerElement === overlay.triggerElement
10✔
197
                );
12✔
198
            });
81✔
199
            if (hasPrevious) {
81!
200
                overlay.open = false;
×
201
                return;
×
202
            }
×
203
            this.stack.forEach((overlayEl) => {
81✔
204
                if (overlayEl.type === 'hint') {
12✔
205
                    this.closeOverlay(overlayEl);
6✔
206
                }
6✔
207
            });
81✔
208
        }
81✔
209
        requestAnimationFrame(() => {
446✔
210
            this.stack.push(overlay);
446✔
211
            overlay.addEventListener('beforetoggle', this.handleBeforetoggle, {
446✔
212
                once: true,
446✔
213
            });
446✔
214
        });
446✔
215
    }
449✔
216

41✔
217
    remove(overlay: Overlay): void {
41✔
218
        this.closeOverlay(overlay);
448✔
219
    }
448✔
220
}
41✔
221

41✔
222
export const overlayStack = new OverlayStack();
41✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc