@vaadin/popover 25.0.11 → 25.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +11 -11
- package/src/vaadin-popover-focus-controller.d.ts +35 -0
- package/src/vaadin-popover-focus-controller.js +226 -0
- package/src/vaadin-popover.js +5 -234
- package/web-types.json +1 -1
- package/web-types.lit.json +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vaadin/popover",
|
|
3
|
-
"version": "25.0.
|
|
3
|
+
"version": "25.0.12",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -36,24 +36,24 @@
|
|
|
36
36
|
],
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@open-wc/dedupe-mixin": "^1.3.0",
|
|
39
|
-
"@vaadin/a11y-base": "~25.0.
|
|
40
|
-
"@vaadin/component-base": "~25.0.
|
|
41
|
-
"@vaadin/lit-renderer": "~25.0.
|
|
42
|
-
"@vaadin/overlay": "~25.0.
|
|
43
|
-
"@vaadin/vaadin-themable-mixin": "~25.0.
|
|
39
|
+
"@vaadin/a11y-base": "~25.0.12",
|
|
40
|
+
"@vaadin/component-base": "~25.0.12",
|
|
41
|
+
"@vaadin/lit-renderer": "~25.0.12",
|
|
42
|
+
"@vaadin/overlay": "~25.0.12",
|
|
43
|
+
"@vaadin/vaadin-themable-mixin": "~25.0.12",
|
|
44
44
|
"lit": "^3.0.0"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@vaadin/aura": "~25.0.
|
|
48
|
-
"@vaadin/chai-plugins": "~25.0.
|
|
49
|
-
"@vaadin/test-runner-commands": "~25.0.
|
|
47
|
+
"@vaadin/aura": "~25.0.12",
|
|
48
|
+
"@vaadin/chai-plugins": "~25.0.12",
|
|
49
|
+
"@vaadin/test-runner-commands": "~25.0.12",
|
|
50
50
|
"@vaadin/testing-helpers": "^2.0.0",
|
|
51
|
-
"@vaadin/vaadin-lumo-styles": "~25.0.
|
|
51
|
+
"@vaadin/vaadin-lumo-styles": "~25.0.12",
|
|
52
52
|
"sinon": "^21.0.0"
|
|
53
53
|
},
|
|
54
54
|
"web-types": [
|
|
55
55
|
"web-types.json",
|
|
56
56
|
"web-types.lit.json"
|
|
57
57
|
],
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "4da2fac35ba4a02254c6e18c8392fa445d873d13"
|
|
59
59
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2024 - 2026 Vaadin Ltd.
|
|
4
|
+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
|
+
*/
|
|
6
|
+
import type { Popover } from './vaadin-popover.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Controller that routes Tab and Shift+Tab when a non-modal popover is opened.
|
|
10
|
+
* The controller's host element is the popover itself.
|
|
11
|
+
*
|
|
12
|
+
* The popover is reachable via Tab only from its target, and its content comes
|
|
13
|
+
* logically right after the target — regardless of the popover's DOM position.
|
|
14
|
+
* When the popover lives inside a focus trap (e.g. a dialog), the controller
|
|
15
|
+
* cooperates with the active `FocusTrapController` so the trap never lands
|
|
16
|
+
* focus on the popover itself.
|
|
17
|
+
*
|
|
18
|
+
* Modal popovers rely on the overlay's own focus trap; this controller bails
|
|
19
|
+
* out early in that case.
|
|
20
|
+
*/
|
|
21
|
+
export class PopoverFocusController {
|
|
22
|
+
host: Popover;
|
|
23
|
+
|
|
24
|
+
constructor(host: Popover);
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Starts listening for Tab keystrokes. Called when the popover opens.
|
|
28
|
+
*/
|
|
29
|
+
activate(): void;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Stops listening for Tab keystrokes. Called when the popover closes.
|
|
33
|
+
*/
|
|
34
|
+
deactivate(): void;
|
|
35
|
+
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2024 - 2026 Vaadin Ltd.
|
|
4
|
+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
|
+
*/
|
|
6
|
+
import { getActiveTrappingNode } from '@vaadin/a11y-base/src/focus-trap-controller.js';
|
|
7
|
+
import { getDeepActiveElement, getFocusableElements, isElementFocused } from '@vaadin/a11y-base/src/focus-utils.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Controller that routes Tab and Shift+Tab when a non-modal popover is opened.
|
|
11
|
+
* The controller's host element is the popover itself.
|
|
12
|
+
*
|
|
13
|
+
* The popover is reachable via Tab only from its target, and its content comes
|
|
14
|
+
* logically right after the target — regardless of the popover's DOM position.
|
|
15
|
+
* When the popover lives inside a focus trap (e.g. a dialog), the controller
|
|
16
|
+
* cooperates with the active `FocusTrapController` so the trap never lands
|
|
17
|
+
* focus on the popover itself.
|
|
18
|
+
*
|
|
19
|
+
* Modal popovers rely on the overlay's own focus trap; this controller bails
|
|
20
|
+
* out early in that case.
|
|
21
|
+
*/
|
|
22
|
+
export class PopoverFocusController {
|
|
23
|
+
constructor(host) {
|
|
24
|
+
this.host = host;
|
|
25
|
+
this.__onKeyDown = this.__onKeyDown.bind(this);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
activate() {
|
|
29
|
+
document.addEventListener('keydown', this.__onKeyDown, true);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
deactivate() {
|
|
33
|
+
document.removeEventListener('keydown', this.__onKeyDown, true);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** @private */
|
|
37
|
+
__handleTab(event) {
|
|
38
|
+
const host = this.host;
|
|
39
|
+
const targetFocusable = this.__getTargetFocusable();
|
|
40
|
+
|
|
41
|
+
if (targetFocusable && isElementFocused(targetFocusable)) {
|
|
42
|
+
event.preventDefault();
|
|
43
|
+
host.focus();
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const lastPopoverFocusable = this.__getLastPopoverFocusable();
|
|
48
|
+
if (isElementFocused(lastPopoverFocusable)) {
|
|
49
|
+
this.__moveLogicalNext(event, host);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Native Tab would land on the popover when DOM order places it right
|
|
54
|
+
// after the current element. Skip past it via the logical list.
|
|
55
|
+
const activeElement = getDeepActiveElement();
|
|
56
|
+
const scopeFocusables = this.__getScopeFocusables();
|
|
57
|
+
const activeIdx = scopeFocusables.indexOf(activeElement);
|
|
58
|
+
if (activeIdx >= 0 && scopeFocusables[activeIdx + 1] === host) {
|
|
59
|
+
this.__moveLogicalNext(event, activeElement, scopeFocusables);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** @private */
|
|
64
|
+
__handleShiftTab(event) {
|
|
65
|
+
const host = this.host;
|
|
66
|
+
const targetFocusable = this.__getTargetFocusable();
|
|
67
|
+
|
|
68
|
+
// Just clear the flag so native Shift+Tab from the target doesn't reopen.
|
|
69
|
+
if (targetFocusable && isElementFocused(targetFocusable) && host.__shouldRestoreFocus) {
|
|
70
|
+
host.__shouldRestoreFocus = false;
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (isElementFocused(host)) {
|
|
75
|
+
event.preventDefault();
|
|
76
|
+
targetFocusable.focus();
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Browser handles Shift+Tab inside popover content.
|
|
81
|
+
const activeElement = getDeepActiveElement();
|
|
82
|
+
if (host.contains(activeElement)) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const scopeFocusables = this.__getScopeFocusables();
|
|
87
|
+
const logicalFocusables = this.__buildLogicalList(scopeFocusables);
|
|
88
|
+
const activeLogicalIdx = logicalFocusables.indexOf(activeElement);
|
|
89
|
+
const prevFocusable = activeLogicalIdx > 0 ? logicalFocusables[activeLogicalIdx - 1] : null;
|
|
90
|
+
|
|
91
|
+
// When the logical previous is the popover, move focus into the popover tail.
|
|
92
|
+
if (prevFocusable === host) {
|
|
93
|
+
event.preventDefault();
|
|
94
|
+
this.__getLastPopoverFocusable().focus();
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Native Shift+Tab would land on the popover: skip it and redirect to the
|
|
99
|
+
// true logical previous, or wrap when at the logical start inside a trap.
|
|
100
|
+
const activeScopeIdx = scopeFocusables.indexOf(activeElement);
|
|
101
|
+
if (activeScopeIdx > 0 && scopeFocusables[activeScopeIdx - 1] === host) {
|
|
102
|
+
if (prevFocusable) {
|
|
103
|
+
event.preventDefault();
|
|
104
|
+
prevFocusable.focus();
|
|
105
|
+
} else if (getActiveTrappingNode(host)) {
|
|
106
|
+
this.__wrapToLogicalLast(event, logicalFocusables);
|
|
107
|
+
}
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// At the logical start of a trap: wrap to the logical last. When the
|
|
112
|
+
// popover is the logical last (target is last), this lands on the popover tail.
|
|
113
|
+
if (!prevFocusable && getActiveTrappingNode(host)) {
|
|
114
|
+
this.__wrapToLogicalLast(event, logicalFocusables);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** @private */
|
|
119
|
+
__onKeyDown(event) {
|
|
120
|
+
// Modal popovers rely on the overlay's focus trap.
|
|
121
|
+
if (this.host.modal) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (event.key !== 'Tab') {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (event.shiftKey) {
|
|
128
|
+
this.__handleShiftTab(event);
|
|
129
|
+
} else {
|
|
130
|
+
this.__handleTab(event);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** @private */
|
|
135
|
+
__getTargetFocusable() {
|
|
136
|
+
const target = this.host.target;
|
|
137
|
+
if (!target) {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
return target.focusElement || target;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The popover's tail element: the last focusable inside the popover's content
|
|
145
|
+
* area, or the popover itself when it has no focusable content.
|
|
146
|
+
* @private
|
|
147
|
+
*/
|
|
148
|
+
__getLastPopoverFocusable() {
|
|
149
|
+
const lastContent = getFocusableElements(this.host._overlayElement.$.content).pop();
|
|
150
|
+
return lastContent || this.host;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* DOM-ordered focusables in the current scope (active focus trap, or document
|
|
155
|
+
* body), with popover light-DOM descendants excluded but the popover itself
|
|
156
|
+
* retained. Used to detect DOM adjacency to the popover.
|
|
157
|
+
* @private
|
|
158
|
+
*/
|
|
159
|
+
__getScopeFocusables() {
|
|
160
|
+
const host = this.host;
|
|
161
|
+
const scope = getActiveTrappingNode(host) || document.body;
|
|
162
|
+
return getFocusableElements(scope).filter((el) => el === host || !host.contains(el));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Scope focusables in *logical* tab order: the popover is moved from its DOM
|
|
167
|
+
* position to right after the target focusable. The popover is left out of
|
|
168
|
+
* the list entirely when there is no target.
|
|
169
|
+
* @private
|
|
170
|
+
*/
|
|
171
|
+
__buildLogicalList(scopeFocusables = this.__getScopeFocusables()) {
|
|
172
|
+
const host = this.host;
|
|
173
|
+
const targetFocusable = this.__getTargetFocusable();
|
|
174
|
+
const logicalFocusables = scopeFocusables.filter((el) => el !== host);
|
|
175
|
+
|
|
176
|
+
if (targetFocusable && targetFocusable !== host) {
|
|
177
|
+
const targetIdx = logicalFocusables.indexOf(targetFocusable);
|
|
178
|
+
if (targetIdx >= 0) {
|
|
179
|
+
logicalFocusables.splice(targetIdx + 1, 0, host);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return logicalFocusables;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** @private */
|
|
186
|
+
__moveLogicalNext(event, from, scopeFocusables) {
|
|
187
|
+
const host = this.host;
|
|
188
|
+
const logicalFocusables = this.__buildLogicalList(scopeFocusables);
|
|
189
|
+
const fromIdx = logicalFocusables.indexOf(from);
|
|
190
|
+
if (fromIdx < 0) {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// The popover sits right after the target in the logical list, so it can
|
|
195
|
+
// be the logical next only when `from` is the target. Skip it: the popover
|
|
196
|
+
// is Tab-reachable only from its target (handled in __handleTab case 1).
|
|
197
|
+
let nextIdx = fromIdx + 1;
|
|
198
|
+
if (logicalFocusables[nextIdx] === host) {
|
|
199
|
+
nextIdx += 1;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Past the end inside a trap: wrap to the first element. The popover
|
|
203
|
+
// never sits at position 0 (it only follows a target), so list[0] is real.
|
|
204
|
+
let focusable = logicalFocusables[nextIdx];
|
|
205
|
+
if (!focusable && getActiveTrappingNode(host)) {
|
|
206
|
+
focusable = logicalFocusables[0];
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (focusable) {
|
|
210
|
+
event.preventDefault();
|
|
211
|
+
focusable.focus();
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** @private */
|
|
216
|
+
__wrapToLogicalLast(event, logicalFocusables) {
|
|
217
|
+
const logicalLast = logicalFocusables.at(-1);
|
|
218
|
+
if (!logicalLast) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
// When the popover is the logical last, land on the popover tail instead.
|
|
222
|
+
const focusable = logicalLast === this.host ? this.__getLastPopoverFocusable() : logicalLast;
|
|
223
|
+
event.preventDefault();
|
|
224
|
+
focusable.focus();
|
|
225
|
+
}
|
|
226
|
+
}
|
package/src/vaadin-popover.js
CHANGED
|
@@ -6,13 +6,7 @@
|
|
|
6
6
|
import './vaadin-popover-overlay.js';
|
|
7
7
|
import { css, html, LitElement } from 'lit';
|
|
8
8
|
import { ifDefined } from 'lit/directives/if-defined.js';
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
getDeepActiveElement,
|
|
12
|
-
getFocusableElements,
|
|
13
|
-
isElementFocused,
|
|
14
|
-
isKeyboardActive,
|
|
15
|
-
} from '@vaadin/a11y-base/src/focus-utils.js';
|
|
9
|
+
import { isKeyboardActive } from '@vaadin/a11y-base/src/focus-utils.js';
|
|
16
10
|
import { defineCustomElement } from '@vaadin/component-base/src/define.js';
|
|
17
11
|
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
|
|
18
12
|
import { PolylitMixin } from '@vaadin/component-base/src/polylit-mixin.js';
|
|
@@ -22,6 +16,7 @@ import {
|
|
|
22
16
|
isLastOverlay as isLastOverlayBase,
|
|
23
17
|
} from '@vaadin/overlay/src/vaadin-overlay-stack-mixin.js';
|
|
24
18
|
import { ThemePropertyMixin } from '@vaadin/vaadin-themable-mixin/vaadin-theme-property-mixin.js';
|
|
19
|
+
import { PopoverFocusController } from './vaadin-popover-focus-controller.js';
|
|
25
20
|
import { PopoverPositionMixin } from './vaadin-popover-position-mixin.js';
|
|
26
21
|
import { PopoverTargetMixin } from './vaadin-popover-target-mixin.js';
|
|
27
22
|
|
|
@@ -484,7 +479,6 @@ class Popover extends PopoverPositionMixin(
|
|
|
484
479
|
|
|
485
480
|
this.__generatedId = `vaadin-popover-${generateUniqueId()}`;
|
|
486
481
|
|
|
487
|
-
this.__onGlobalKeyDown = this.__onGlobalKeyDown.bind(this);
|
|
488
482
|
this.__onTargetClick = this.__onTargetClick.bind(this);
|
|
489
483
|
this.__onTargetFocusIn = this.__onTargetFocusIn.bind(this);
|
|
490
484
|
this.__onTargetFocusOut = this.__onTargetFocusOut.bind(this);
|
|
@@ -492,6 +486,7 @@ class Popover extends PopoverPositionMixin(
|
|
|
492
486
|
this.__onTargetMouseLeave = this.__onTargetMouseLeave.bind(this);
|
|
493
487
|
|
|
494
488
|
this._openedStateController = new PopoverOpenedStateController(this);
|
|
489
|
+
this._focusController = new PopoverFocusController(this);
|
|
495
490
|
}
|
|
496
491
|
|
|
497
492
|
/** @protected */
|
|
@@ -665,9 +660,9 @@ class Popover extends PopoverPositionMixin(
|
|
|
665
660
|
/** @private */
|
|
666
661
|
__openedChanged(opened, oldOpened) {
|
|
667
662
|
if (opened) {
|
|
668
|
-
|
|
663
|
+
this._focusController.activate();
|
|
669
664
|
} else if (oldOpened) {
|
|
670
|
-
|
|
665
|
+
this._focusController.deactivate();
|
|
671
666
|
}
|
|
672
667
|
}
|
|
673
668
|
|
|
@@ -712,230 +707,6 @@ class Popover extends PopoverPositionMixin(
|
|
|
712
707
|
}
|
|
713
708
|
}
|
|
714
709
|
|
|
715
|
-
/**
|
|
716
|
-
* Overlay's global Escape press listener doesn't work when
|
|
717
|
-
* the overlay is modeless, so we use a separate listener.
|
|
718
|
-
* @private
|
|
719
|
-
*/
|
|
720
|
-
__onGlobalKeyDown(event) {
|
|
721
|
-
// Modal popover uses overlay logic focus trap.
|
|
722
|
-
if (this.modal) {
|
|
723
|
-
return;
|
|
724
|
-
}
|
|
725
|
-
|
|
726
|
-
// Include popover content in the Tab order after the target.
|
|
727
|
-
if (event.key === 'Tab') {
|
|
728
|
-
if (event.shiftKey) {
|
|
729
|
-
this.__onGlobalShiftTab(event);
|
|
730
|
-
} else {
|
|
731
|
-
this.__onGlobalTab(event);
|
|
732
|
-
}
|
|
733
|
-
}
|
|
734
|
-
}
|
|
735
|
-
|
|
736
|
-
/** @private */
|
|
737
|
-
__onGlobalTab(event) {
|
|
738
|
-
// Move focus to the popover on target element Tab
|
|
739
|
-
if (this.target && isElementFocused(this.__getTargetFocusable())) {
|
|
740
|
-
event.preventDefault();
|
|
741
|
-
this.focus();
|
|
742
|
-
return;
|
|
743
|
-
}
|
|
744
|
-
|
|
745
|
-
// Cache filtered focusable list for this keystroke to avoid redundant DOM traversals
|
|
746
|
-
const focusables = this.__getScopeFocusables();
|
|
747
|
-
|
|
748
|
-
// Move focus to the next element after target on last content Tab,
|
|
749
|
-
// or when popover itself is focused and has no focusable content
|
|
750
|
-
const lastFocusable = this.__getLastFocusable();
|
|
751
|
-
const isFocusOut = lastFocusable ? isElementFocused(lastFocusable) : isElementFocused(this);
|
|
752
|
-
if (isFocusOut) {
|
|
753
|
-
let focusable = this.__getNextScopeFocusable(this.__getTargetFocusable(), focusables);
|
|
754
|
-
// If the next element after the target is the popover itself (DOM position
|
|
755
|
-
// differs from logical position), skip past it to the actual next element.
|
|
756
|
-
if (focusable === this) {
|
|
757
|
-
focusable = this.__getNextScopeFocusable(this, focusables);
|
|
758
|
-
}
|
|
759
|
-
if (focusable) {
|
|
760
|
-
event.preventDefault();
|
|
761
|
-
focusable.focus();
|
|
762
|
-
return;
|
|
763
|
-
}
|
|
764
|
-
// No next element after the target in the scope. When inside a focus trap,
|
|
765
|
-
// wrap explicitly to the first focusable. Don't fall through - the
|
|
766
|
-
// FocusTrapController uses DOM order which may differ from the popover's
|
|
767
|
-
// logical tab position.
|
|
768
|
-
if (getActiveTrappingNode(this) && focusables[0]) {
|
|
769
|
-
event.preventDefault();
|
|
770
|
-
focusables[0].focus();
|
|
771
|
-
return;
|
|
772
|
-
}
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
// Handle cases where Tab from the current element would land on the popover
|
|
776
|
-
const activeElement = getDeepActiveElement();
|
|
777
|
-
const nextFocusable = this.__getNextScopeFocusable(activeElement, focusables);
|
|
778
|
-
if (nextFocusable === this) {
|
|
779
|
-
// The popover should only be Tab-reachable from its target (handled above).
|
|
780
|
-
// Skip the popover when Tab from any other element would land on it
|
|
781
|
-
// due to its DOM position.
|
|
782
|
-
const focusableAfterPopover = this.__getNextScopeFocusable(this, focusables);
|
|
783
|
-
if (focusableAfterPopover) {
|
|
784
|
-
event.preventDefault();
|
|
785
|
-
focusableAfterPopover.focus();
|
|
786
|
-
} else if (getActiveTrappingNode(this) && focusables[0]) {
|
|
787
|
-
// Popover is last in DOM scope but shouldn't be Tab-reachable from
|
|
788
|
-
// non-target elements. Wrap to first focusable in focus trap.
|
|
789
|
-
event.preventDefault();
|
|
790
|
-
focusables[0].focus();
|
|
791
|
-
}
|
|
792
|
-
}
|
|
793
|
-
}
|
|
794
|
-
|
|
795
|
-
/** @private */
|
|
796
|
-
__onGlobalShiftTab(event) {
|
|
797
|
-
// Prevent restoring focus after target blur on Shift + Tab
|
|
798
|
-
if (this.target && isElementFocused(this.__getTargetFocusable()) && this.__shouldRestoreFocus) {
|
|
799
|
-
this.__shouldRestoreFocus = false;
|
|
800
|
-
return;
|
|
801
|
-
}
|
|
802
|
-
|
|
803
|
-
// Move focus back to the target on popover Shift + Tab
|
|
804
|
-
if (this.target && isElementFocused(this)) {
|
|
805
|
-
event.preventDefault();
|
|
806
|
-
this.__getTargetFocusable().focus();
|
|
807
|
-
return;
|
|
808
|
-
}
|
|
809
|
-
|
|
810
|
-
// Don't intercept if focus is inside the popover content.
|
|
811
|
-
// The browser's native Shift+Tab handles navigation within
|
|
812
|
-
// the overlay (e.g. between focusable content and the popover element itself).
|
|
813
|
-
const activeElement = getDeepActiveElement();
|
|
814
|
-
if (this.contains(activeElement)) {
|
|
815
|
-
return;
|
|
816
|
-
}
|
|
817
|
-
|
|
818
|
-
// Cache filtered focusable list for this keystroke to avoid redundant DOM traversals
|
|
819
|
-
const focusables = this.__getScopeFocusables();
|
|
820
|
-
|
|
821
|
-
// Get previous focusable element excluding the popover
|
|
822
|
-
const prevFocusable = this.__getPrevScopeFocusable(activeElement, focusables);
|
|
823
|
-
const targetFocusable = this.__getTargetFocusable();
|
|
824
|
-
|
|
825
|
-
// Intercept Shift+Tab when the previous focusable (excluding the popover)
|
|
826
|
-
// is the target. Instead of moving to the target, redirect focus into
|
|
827
|
-
// the popover's last focusable content (or the popover itself).
|
|
828
|
-
if (prevFocusable === targetFocusable) {
|
|
829
|
-
event.preventDefault();
|
|
830
|
-
this.__focusLastOrSelf();
|
|
831
|
-
return;
|
|
832
|
-
}
|
|
833
|
-
|
|
834
|
-
// Move focus into the popover when:
|
|
835
|
-
// 1. There is no previous focusable element in the focus trap (at the
|
|
836
|
-
// beginning, would wrap around), and
|
|
837
|
-
// 2. The target is the last focusable in the focus trap (making the
|
|
838
|
-
// popover logically last).
|
|
839
|
-
// Don't fall through - the FocusTrapController uses DOM order which
|
|
840
|
-
// may differ from the popover's logical tab position.
|
|
841
|
-
if (!prevFocusable && getActiveTrappingNode(this)) {
|
|
842
|
-
const list = focusables.filter((el) => el !== this);
|
|
843
|
-
if (list.at(-1) === targetFocusable) {
|
|
844
|
-
event.preventDefault();
|
|
845
|
-
this.__focusLastOrSelf();
|
|
846
|
-
return;
|
|
847
|
-
}
|
|
848
|
-
// Popover is last in DOM but target is not the last focusable.
|
|
849
|
-
// Wrap to last non-popover focusable to prevent FocusTrapController
|
|
850
|
-
// from landing on the popover.
|
|
851
|
-
const last = list.at(-1);
|
|
852
|
-
if (last) {
|
|
853
|
-
event.preventDefault();
|
|
854
|
-
last.focus();
|
|
855
|
-
return;
|
|
856
|
-
}
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
// Get previous focusable element including the popover (simulates native Tab order)
|
|
860
|
-
const prevFocusableNative = this.__getPrevScopeFocusable(activeElement, focusables, true);
|
|
861
|
-
// Skip the popover when native Shift+Tab would land on it
|
|
862
|
-
// and redirect to the actual previous element
|
|
863
|
-
if (prevFocusableNative === this) {
|
|
864
|
-
if (prevFocusable) {
|
|
865
|
-
event.preventDefault();
|
|
866
|
-
prevFocusable.focus();
|
|
867
|
-
} else if (getActiveTrappingNode(this)) {
|
|
868
|
-
// Popover is first in DOM scope but shouldn't be Shift+Tab-reachable
|
|
869
|
-
// from non-target elements. Wrap to last non-popover focusable.
|
|
870
|
-
const list = focusables.filter((el) => el !== this);
|
|
871
|
-
const last = list.at(-1);
|
|
872
|
-
if (last) {
|
|
873
|
-
event.preventDefault();
|
|
874
|
-
last.focus();
|
|
875
|
-
}
|
|
876
|
-
}
|
|
877
|
-
}
|
|
878
|
-
}
|
|
879
|
-
|
|
880
|
-
/**
|
|
881
|
-
* Returns whether the element is a light DOM child of this popover
|
|
882
|
-
* (i.e. slotted popover content, excluding the popover element itself).
|
|
883
|
-
* @param {Element} el
|
|
884
|
-
* @return {boolean}
|
|
885
|
-
* @private
|
|
886
|
-
*/
|
|
887
|
-
__isPopoverContent(el) {
|
|
888
|
-
return el !== this && this.contains(el);
|
|
889
|
-
}
|
|
890
|
-
|
|
891
|
-
/**
|
|
892
|
-
* Returns focusable elements within the current scope (active focus trap or
|
|
893
|
-
* document body) with popover light DOM children filtered out.
|
|
894
|
-
* @return {Element[]}
|
|
895
|
-
* @private
|
|
896
|
-
*/
|
|
897
|
-
__getScopeFocusables() {
|
|
898
|
-
const scope = getActiveTrappingNode(this) || document.body;
|
|
899
|
-
return getFocusableElements(scope).filter((el) => !this.__isPopoverContent(el));
|
|
900
|
-
}
|
|
901
|
-
|
|
902
|
-
/** @private */
|
|
903
|
-
__getNextScopeFocusable(target, focusables = this.__getScopeFocusables()) {
|
|
904
|
-
const idx = focusables.findIndex((el) => el === target);
|
|
905
|
-
return idx >= 0 ? focusables[idx + 1] : undefined;
|
|
906
|
-
}
|
|
907
|
-
|
|
908
|
-
/** @private */
|
|
909
|
-
__getPrevScopeFocusable(target, focusables = this.__getScopeFocusables(), includePopover = false) {
|
|
910
|
-
const list = includePopover ? focusables : focusables.filter((el) => el !== this);
|
|
911
|
-
const idx = list.findIndex((el) => el === target);
|
|
912
|
-
// Returns null both when target is the first element (idx === 0)
|
|
913
|
-
// and when target is not found in the list (idx === -1)
|
|
914
|
-
return idx > 0 ? list[idx - 1] : null;
|
|
915
|
-
}
|
|
916
|
-
|
|
917
|
-
/** @private */
|
|
918
|
-
__getLastFocusable() {
|
|
919
|
-
// Search within the overlay's content area to avoid returning the popover element itself
|
|
920
|
-
const focusables = getFocusableElements(this._overlayElement.$.content);
|
|
921
|
-
return focusables.pop();
|
|
922
|
-
}
|
|
923
|
-
|
|
924
|
-
/** @private */
|
|
925
|
-
__focusLastOrSelf() {
|
|
926
|
-
(this.__getLastFocusable() || this).focus();
|
|
927
|
-
}
|
|
928
|
-
|
|
929
|
-
/** @private */
|
|
930
|
-
__getTargetFocusable() {
|
|
931
|
-
if (!this.target) {
|
|
932
|
-
return null;
|
|
933
|
-
}
|
|
934
|
-
|
|
935
|
-
// If target has `focusElement`, check if that one is focused.
|
|
936
|
-
return this.target.focusElement || this.target;
|
|
937
|
-
}
|
|
938
|
-
|
|
939
710
|
/** @private */
|
|
940
711
|
__onTargetFocusIn() {
|
|
941
712
|
this.__focusInside = true;
|
package/web-types.json
CHANGED