Skip to content

Commit

Permalink
feat(list-item): Added isActive() method to check active status and m…
Browse files Browse the repository at this point in the history
…inor fixes to keyboard navigation

PiperOrigin-RevId: 467690622
  • Loading branch information
material-web-copybara authored and copybara-github committed Aug 15, 2022
1 parent 6546316 commit 9f410f6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
16 changes: 9 additions & 7 deletions list/lib/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ export class List extends LitElement {
handleKeydown(event: KeyboardEvent) {
if (Object.values(NAVIGATABLE_KEYS).indexOf(event.key) === -1) return;

for (const item of this.items) {
if (item.isActive()) {
this.activeListItem = item;
}

item.deactivate();
}

if (event.key === NAVIGATABLE_KEYS.ArrowDown) {
event.preventDefault();
if (this.activeListItem) {
Expand Down Expand Up @@ -97,13 +105,7 @@ export class List extends LitElement {
this.activeListItem = this.getLastItem();
}

if (!this.activeListItem) return;

for (const item of this.items) {
item.deactivate();
}

this.activeListItem.activate();
this.activeListItem?.activate();
}

handleItemInteraction(event: ListItemInteractionEvent) {
Expand Down
15 changes: 15 additions & 0 deletions list/lib/listitem/list-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export class ListItem extends ActionElement {
this.ripple.endHover();
}

/** @bubbleWizEvent */
protected handleKeyDown(e: KeyboardEvent) {
if (e.key !== ' ' && e.key !== 'Enter') return;

Expand All @@ -204,11 +205,25 @@ export class ListItem extends ActionElement {
this.ripple.endPress();
}

/**
* Focuses list item and makes list item focusable via keyboard.
*/
activate() {
this.itemTabIndex = 0;
this.listItemRoot.focus();
this.showFocusRing = true;
}

/**
* Returns true if list item is currently focused and is focusable.
*/
isActive() {
return this.itemTabIndex === 0 && this.showFocusRing;
}

/**
* Removes list item from sequential keyboard navigation.
*/
deactivate() {
this.itemTabIndex = -1;
}
Expand Down

0 comments on commit 9f410f6

Please sign in to comment.