Skip to content

Commit

Permalink
streamline active option handling
Browse files Browse the repository at this point in the history
  • Loading branch information
radium-v committed Dec 20, 2024
1 parent b08f465 commit 1a1bb2e
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions packages/web-components/src/dropdown/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,17 @@ export class BaseDropdown extends FASTElement {
* @internal
*/
public activeIndexChanged(prev: number | undefined, next: number | undefined): void {
this.setActiveOption();
if (typeof next === 'number') {
const optionIndex = this.matches(':has(:focus-visible)') ? next : -1;

this.enabledOptions.forEach((option, index) => {
option.active = index === optionIndex;
});

if (this.open) {
this.enabledOptions[optionIndex]?.scrollIntoView({ block: 'nearest' });
}
}
}

/**
Expand Down Expand Up @@ -694,8 +704,6 @@ export class BaseDropdown extends FASTElement {

this.activeIndex = indexInBounds;

this.setActiveOption(true);

return true;
}

Expand All @@ -714,23 +722,6 @@ export class BaseDropdown extends FASTElement {
return !isOption(e.target as HTMLElement);
}

/**
* Sets the `active` state on the currently focused option.
*
* @internal
*/
public setActiveOption(force?: boolean): void {
const optionIndex = this.matches(':has(:focus-visible)') || force ? this.activeIndex : -1;

this.enabledOptions.forEach((option, index) => {
option.active = index === optionIndex;
});

if (this.open) {
this.enabledOptions[optionIndex]?.scrollIntoView({ block: 'nearest' });
}
}

/**
* Selects an option by index.
*
Expand Down

0 comments on commit 1a1bb2e

Please sign in to comment.