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(list-item): focus on the first focusable element within the component when using arrow keys #8291

Merged
merged 3 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
35 changes: 22 additions & 13 deletions packages/calcite-components/src/components/list-item/list-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import {
VNode,
Watch,
} from "@stencil/core";
import { getElementDir, slotChangeHasAssignedElement, toAriaBoolean } from "../../utils/dom";
import {
getElementDir,
getFirstTabbable,
slotChangeHasAssignedElement,
toAriaBoolean,
} from "../../utils/dom";
import {
connectInteractive,
disconnectInteractive,
Expand Down Expand Up @@ -344,7 +349,7 @@ export class ListItem
const focusIndex = focusMap.get(parentListEl);

if (typeof focusIndex === "number") {
const cells = [actionsStartEl, contentEl, actionsEndEl].filter(Boolean);
const cells = [actionsStartEl, contentEl, actionsEndEl].filter((el) => el && !el.hidden);
if (cells[focusIndex]) {
this.focusCell(cells[focusIndex]);
} else {
Expand Down Expand Up @@ -737,7 +742,7 @@ export class ListItem
const composedPath = event.composedPath();
const { containerEl, contentEl, actionsStartEl, actionsEndEl, open, openable } = this;

const cells = [actionsStartEl, contentEl, actionsEndEl].filter(Boolean);
const cells = [actionsStartEl, contentEl, actionsEndEl].filter((el) => el && !el.hidden);
const currentIndex = cells.findIndex((cell) => composedPath.includes(cell));

if (
Expand Down Expand Up @@ -790,16 +795,20 @@ export class ListItem
focusMap.set(parentListEl, null);
}

[actionsStartEl, contentEl, actionsEndEl].filter(Boolean).forEach((tableCell, cellIndex) => {
const tabIndexAttr = "tabindex";
if (tableCell === focusEl) {
tableCell.setAttribute(tabIndexAttr, "0");
saveFocusIndex && focusMap.set(parentListEl, cellIndex);
} else {
tableCell.removeAttribute(tabIndexAttr);
}
});
const focusedEl = getFirstTabbable(focusEl);

[actionsStartEl, contentEl, actionsEndEl]
.filter((el) => el && !el.hidden)
.forEach((tableCell, cellIndex) => {
const tabIndexAttr = "tabindex";
if (tableCell === focusEl) {
focusEl === focusedEl && tableCell.setAttribute(tabIndexAttr, "0");
saveFocusIndex && focusMap.set(parentListEl, cellIndex);
} else {
tableCell.removeAttribute(tabIndexAttr);
}
});

focusEl?.focus();
focusedEl?.focus();
};
}
16 changes: 13 additions & 3 deletions packages/calcite-components/src/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,26 @@ export async function focusElement(el: FocusableElement): Promise<void> {
}

/**
* Helper to focus the first tabbable element.
* Helper to get the first tabbable element.
*
* @param {HTMLElement} element The html element containing tabbable elements.
* @returns the first tabbable element.
*/
export function focusFirstTabbable(element: HTMLElement): void {
export function getFirstTabbable(element: HTMLElement): HTMLElement {
if (!element) {
return;
}

(tabbable(element, tabbableOptions)[0] || element).focus();
return (tabbable(element, tabbableOptions)[0] ?? element) as HTMLElement;
}

/**
* Helper to focus the first tabbable element.
*
* @param {HTMLElement} element The html element containing tabbable elements.
*/
export function focusFirstTabbable(element: HTMLElement): void {
getFirstTabbable(element)?.focus();
}

interface GetSlottedOptions {
Expand Down
Loading