Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix focus trapping to respect the currently focused element #1583

Merged
merged 9 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,8 @@
"./dist/react/*": "./dist/react/*",
"./dist/translations/*": "./dist/translations/*"
},
"files": [
"dist",
"cdn"
],
"keywords": [
"web components",
"custom elements",
"components"
],
"files": ["dist", "cdn"],
"keywords": ["web components", "custom elements", "components"],
"repository": {
"type": "git",
"url": "git+https://github.com/shoelace-style/shoelace.git"
Expand Down Expand Up @@ -140,9 +133,6 @@
"user-agent-data-types": "^0.3.0"
},
"lint-staged": {
"*.{ts,js}": [
"eslint --max-warnings 0 --cache --fix",
"prettier --write"
]
"*.{ts,js}": ["eslint --max-warnings 0 --cache --fix", "prettier --write"]
}
}
10 changes: 10 additions & 0 deletions src/internal/active-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ export function* activeElements(activeElement: Element | null = document.activeE
yield* activeElements(activeElement.shadowRoot.activeElement);
}
}

export function getDeepestActiveElement() {
let activeElement;

for (const element of activeElements()) {
activeElement = element;
}

return activeElement;
KonnorRogers marked this conversation as resolved.
Show resolved Hide resolved
}
43 changes: 14 additions & 29 deletions src/internal/modal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { activeElements } from './active-elements.js';
import { getDeepestActiveElement } from './active-elements.js';
import { getTabbableElements } from './tabbable.js';

let activeModals: HTMLElement[] = [];
Expand Down Expand Up @@ -66,22 +66,6 @@ export default class Modal {
this.checkFocus();
};

get currentFocusIndex() {
return getTabbableElements(this.element).findIndex(el => el === this.currentFocus);
}

// Checks if the `startElement` is already focused. This is important if the modal already has an existing focus prior
// to the first tab key.
private startElementAlreadyFocused(startElement: HTMLElement) {
for (const activeElement of activeElements()) {
if (startElement === activeElement) {
return true;
}
}

return false;
}

private handleKeyDown = (event: KeyboardEvent) => {
if (event.key !== 'Tab' || this.isExternalActivated) return;

Expand All @@ -94,29 +78,30 @@ export default class Modal {
event.preventDefault();

const tabbableElements = getTabbableElements(this.element);
const start = tabbableElements[0];

// Sometimes we programmatically focus the first element in a modal.
// Lets make sure the start element isn't already focused.
let focusIndex = this.startElementAlreadyFocused(start) ? 0 : this.currentFocusIndex;
// Because sometimes focus can actually be taken over from outside sources,
// we don't want to rely on `this.currentFocus`. Instead we check the actual `activeElement` and
// recurse through shadowRoots.
const currentActiveElement = getDeepestActiveElement();
let currentFocusIndex = tabbableElements.findIndex(el => el === currentActiveElement);

if (focusIndex === -1) {
this.currentFocus = start;
if (currentFocusIndex === -1) {
this.currentFocus = tabbableElements[0];
this.currentFocus.focus({ preventScroll: true });
return;
}

const addition = this.tabDirection === 'forward' ? 1 : -1;

if (focusIndex + addition >= tabbableElements.length) {
focusIndex = 0;
} else if (this.currentFocusIndex + addition < 0) {
focusIndex = tabbableElements.length - 1;
if (currentFocusIndex + addition >= tabbableElements.length) {
currentFocusIndex = 0;
} else if (currentFocusIndex + addition < 0) {
currentFocusIndex = tabbableElements.length - 1;
} else {
focusIndex += addition;
currentFocusIndex += addition;
}

this.currentFocus = tabbableElements[focusIndex];
this.currentFocus = tabbableElements[currentFocusIndex];
this.currentFocus?.focus({ preventScroll: true });

setTimeout(() => this.checkFocus());
Expand Down
39 changes: 34 additions & 5 deletions src/internal/tabbable.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { elementUpdated, expect, fixture } from '@open-wc/testing';

import '../../dist/shoelace.js';
import { activeElements } from './active-elements.js';
import { activeElements, getDeepestActiveElement } from './active-elements.js';
import { html } from 'lit';
import { sendKeys } from '@web/test-runner-commands';

Expand All @@ -19,10 +19,6 @@ function activeElementsArray() {
return [...activeElements()];
}

function getDeepestActiveElement() {
return activeElementsArray().pop();
}

window.customElements.define(
'tab-test-1',
class extends HTMLElement {
Expand Down Expand Up @@ -145,3 +141,36 @@ it('Should allow tabbing to slotted elements', async () => {
await holdShiftKey(async () => await sendKeys({ press: tabKey }));
expect(activeElementsArray()).to.include(focusSix);
});

it('Should account for when focus is changed from outside sources (like clicking)', async () => {
const dialog = await fixture(html`
<sl-dialog open="" label="Dialog" class="dialog-overview">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
<sl-input placeholder="tab to me"></sl-input>
<sl-button slot="footer" variant="primary">Close</sl-button>
</sl-dialog>
`);

const inputEl = dialog.querySelector('sl-input')!;
const closeButton = dialog.shadowRoot!.querySelector('sl-icon-button')!;
const footerButton = dialog.querySelector('sl-button')!;

expect(activeElementsArray()).to.not.include(inputEl);

// Sets focus to the input element
inputEl.focus();

expect(activeElementsArray()).to.include(inputEl);

await sendKeys({ press: tabKey });

expect(activeElementsArray()).not.to.include(inputEl);
expect(activeElementsArray()).to.include(footerButton);
KonnorRogers marked this conversation as resolved.
Show resolved Hide resolved

// Reset focus back to input el
inputEl.focus();
expect(activeElementsArray()).to.include(inputEl);

await holdShiftKey(async () => await sendKeys({ press: tabKey }));
expect(activeElementsArray()).to.include(closeButton);
});
19 changes: 6 additions & 13 deletions src/internal/tabbable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ function isTabbable(el: HTMLElement) {
return false;
}

// Elements with aria-disabled are not tabbable
if (el.hasAttribute('aria-disabled') && el.getAttribute('aria-disabled') !== 'false') {
return false;
}

// Radios without a checked attribute are not tabbable
if (tag === 'input' && el.getAttribute('type') === 'radio' && !el.hasAttribute('checked')) {
return false;
Expand Down Expand Up @@ -107,14 +102,12 @@ export function getTabbableElements(root: HTMLElement | ShadowRoot) {
// Collect all elements including the root
walk(root);

return tabbableElements;

// Is this worth having? Most sorts will always add increased overhead. And positive tabindexes shouldn't really be used.
// So is it worth being right? Or fast?
// return tabbableElements.filter(isTabbable).sort((a, b) => {
// // Make sure we sort by tabindex.
// const aTabindex = Number(a.getAttribute('tabindex')) || 0;
// const bTabindex = Number(b.getAttribute('tabindex')) || 0;
// return bTabindex - aTabindex;
// });
return tabbableElements.sort((a, b) => {
// Make sure we sort by tabindex.
const aTabindex = Number(a.getAttribute('tabindex')) || 0;
const bTabindex = Number(b.getAttribute('tabindex')) || 0;
return bTabindex - aTabindex;
});
}