Skip to content

fix(picker): should have focused state on click #5566

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions packages/picker/src/InteractionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ export class InteractionController implements ReactiveController {
public handlePointerdown(_event: PointerEvent): void {}

public handleButtonFocus(event: FocusEvent): void {
// Set focused to true when button receives focus (including from clicks)
// But only if the picker is not pending, to maintain existing behavior
if (!this.host.pending && !this.host.focused) {
this.host.focused = true;
}

// When focus comes from a pointer event, and the related target is the Menu,
// we don't want to reopen the Menu.
if (
Expand Down
23 changes: 19 additions & 4 deletions packages/picker/src/Picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,16 @@ export class PickerBase extends SizedMixin(SpectrumElement, {
this.toggle();
}

public handleButtonBlur(): void {
this.focused = false;
public handleButtonBlur(event?: FocusEvent): void {
// Only set focused to false if focus is moving outside the picker entirely
// If focus is moving to a menu item within the picker, keep focused true
if (
!event ||
!event.relatedTarget ||
!this.contains(event.relatedTarget as Node)
) {
this.focused = false;
}
}

public override focus(options?: FocusOptions): void {
Expand Down Expand Up @@ -586,8 +594,10 @@ export class PickerBase extends SizedMixin(SpectrumElement, {
id="button"
class=${ifDefined(
this.labelAlignment
? `label-${this.labelAlignment}`
: undefined
? `label-${this.labelAlignment} ${this.focused ? 'focus-visible is-keyboardFocused' : ''}`
: this.focused
? 'focus-visible is-keyboardFocused'
: undefined
)}
@focus=${this.handleButtonFocus}
@blur=${this.handleButtonBlur}
Expand Down Expand Up @@ -916,6 +926,11 @@ export class PickerBase extends SizedMixin(SpectrumElement, {

this.recentlyConnected = this.hasUpdated;
this.addEventListener('focus', this.handleFocus);

// Add sp-closed listener to set focused to false when picker closes
this.addEventListener('sp-closed', () => {
this.focused = false;
});
}

public override disconnectedCallback(): void {
Expand Down
Loading