-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
ToggleGroupControl: Don't autoselect option on first group focus #65892
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,7 +75,6 @@ function ToggleGroupControlOptionBase( | |
value, | ||
children, | ||
showTooltip = false, | ||
onFocus: onFocusProp, | ||
disabled, | ||
...otherButtonProps | ||
} = buttonProps; | ||
|
@@ -132,7 +131,6 @@ function ToggleGroupControlOptionBase( | |
<button | ||
{ ...commonProps } | ||
disabled={ disabled } | ||
onFocus={ onFocusProp } | ||
aria-pressed={ isPressed } | ||
type="button" | ||
onClick={ buttonOnClick } | ||
|
@@ -142,19 +140,17 @@ function ToggleGroupControlOptionBase( | |
) : ( | ||
<Ariakit.Radio | ||
disabled={ disabled } | ||
render={ | ||
<button | ||
type="button" | ||
{ ...commonProps } | ||
onFocus={ ( event ) => { | ||
onFocusProp?.( event ); | ||
if ( event.defaultPrevented ) { | ||
return; | ||
} | ||
toggleGroupControlContext.setValue( value ); | ||
} } | ||
/> | ||
} | ||
onFocusVisible={ () => { | ||
// Conditions ensure that the first visible focus to a radio group | ||
// without a selected option will not automatically select the option. | ||
if ( | ||
toggleGroupControlContext.value !== null || | ||
toggleGroupControlContext.activeItemIsNotFirstItem?.() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand the logic here — if the current There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic in trunk was that, whenever an option is focused (i.e. becomes the active item), set that value in the Ariakit store explicitly (because we're managing this component in controlled mode). We want to prevent this behavior when there is no value selected yet and the active item is the first item. Once the active item is the second item, that means the user not only "focused on the ToggleGroupControl" but also actively selected an option, so we are safe to start setting values. This is how native radios behave. Hope that made better sense, but also I'd like your input on how to improve the code comment so it is easier to understand. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. See for example the default example for Ariakit Radio. When the focus first enters the radio group, the active item is the first item ("apple"). It's only when the active item becomes something else ("orange") that we want to start setting values. CleanShot.2024-10-08.at.05.04.46.mp4Once we start setting values (i.e. the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. I wish we could rely on the built-in behavior of the ariakit Also, if we even want to tweak the initial position of the active element before a selection is made, this solution won't cut it - but it's unlikely, so that doesn't seem like a big issue There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, I experimented a bit, and we can't rely on the built-in behavior if we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good — we may want to consider a v2 with such changes when we have the new design spec |
||
) { | ||
toggleGroupControlContext.setValue( value ); | ||
} | ||
} } | ||
render={ <button type="button" { ...commonProps } /> } | ||
value={ value } | ||
> | ||
<ButtonContentView>{ children }</ButtonContentView> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We used to check if the event was
defaultPrevented
or not - is this something we want to keep, and if not, why?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't see why we would need it, especially now that we're using
onFocusVisible
instead ofonFocus
. @ciampo Do you remember why you initially prevented the default (#50278)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't find a specific reason, I believe it was just to respect a consumer wanting to
preventDefault
. I don't think we need it, as you say, since we're not usingonFocus
anymoreThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for confirming, folks 👍