From 1a1bb2e7e661c7cb42dd03a8c5e3e38e2f6659fb Mon Sep 17 00:00:00 2001 From: John Kreitlow <863023+radium-v@users.noreply.github.com> Date: Fri, 20 Dec 2024 14:10:47 -0800 Subject: [PATCH] streamline active option handling --- .../web-components/src/dropdown/dropdown.ts | 31 +++++++------------ 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/packages/web-components/src/dropdown/dropdown.ts b/packages/web-components/src/dropdown/dropdown.ts index 34888fd652ed69..a595f4d583203f 100644 --- a/packages/web-components/src/dropdown/dropdown.ts +++ b/packages/web-components/src/dropdown/dropdown.ts @@ -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' }); + } + } } /** @@ -694,8 +704,6 @@ export class BaseDropdown extends FASTElement { this.activeIndex = indexInBounds; - this.setActiveOption(true); - return true; } @@ -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. *