Repository navigation
[toggle-group][toggle] Improve type safety and inference - #3173
Conversation
commit: |
Bundle size report
Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Signed-off-by: Michael Hazan <[email protected]>
Signed-off-by: Michael Hazan <[email protected]>
|
it seems like this is failing over something i have not affected... would appreciate help |
|
@michaelhazan My bad, I remembered we actually do this intentionally:
Accordion is the same and the value is always an array |
|
@michaldudak Hey! should be good now.. |
…oggleGroup` values
LukasTy
left a comment
There was a problem hiding this comment.
Thank you for your effort. 🙏
Leaving a few suggestions and one discussion point.
| export namespace ToggleGroup { | ||
| export type State = ToggleGroupState; | ||
| export type Props = ToggleGroupProps; | ||
| export type Props<Value extends string> = ToggleGroupProps<Value>; |
There was a problem hiding this comment.
Correct me if I'm wrong, but is there a use case for why we still need the generic here? 🤔
As far as I can see, it only helps with type inference, like if you have the following:
type Alignment = 'horizontal' | 'vertical';
const alignments: Alignment[] = ['horizontal', 'vertical'];
<ToggleGroup
multiple
defaultValue={alignments}
// this is going to know that the type is `Alignment` instead of plain string as it is now
value={}
>
But, the current types don't seem to block the above usage as well.
If we go with this, then we should probably adjust other components (i.e., RadioGroup, CheckboxGroup) accordingly.
WDYT @michaldudak?
There was a problem hiding this comment.
I think this is useful and addresses #3756
(it should also be able to infer the value arg in onValueChange I assume)
There was a problem hiding this comment.
Gotcha. Makes sense, especially given we already have others noticing this shortcoming. 👍
I think we need to do a follow-up and address this issue for all relevant components.
There was a problem hiding this comment.
@LukasTy would you like me to fix it in this pr? or leave it for now?
There was a problem hiding this comment.
I think we can keep this one isolated to these specific components.
WDYT?
…o check for it also made the generic type in `ToggleGroup` & `Toggle` default to `string` to match the behavior in `SliderRootProps`.
|
@LukasTy fixed! did i get that right? 😅 please note i've also set the props's generic type to equal |
| useIsoLayoutEffect(() => { | ||
| if (groupContext && valueProp === undefined) { | ||
| warn( | ||
| 'A `<Toggle>` component rendered in a `<ToggleGroup>` has no explicit `value` prop. This can cause type issues between the Toggle Group and Toggle values. Provide the `<Toggle>` with a `value` prop matching the `<ToggleGroup>` values prop type.', |
There was a problem hiding this comment.
What type issue does this cause?
This seems like a clumsy but legit way to use toggle group without values:
<ToggleGroup>
<Toggle onPressedChange={setState1} />
<Toggle onPressedChange={setState2} />
</ToggleGroup>
There was a problem hiding this comment.
I think Michal raised this point that in case the <ToggleGroup> defaultValue would have a type of Alignment[], then you'd end up in a slight type discrepancy, because internally a Toggle without a value would have generated an internal value type of string.
However, this is a very edge case condition. 🤔
Would it make more sense if the warning would be shown only in your case (no value on the Toggle and no default and no direct value on the Toggle Group)?
There was a problem hiding this comment.
i guess it should be more like
if(groupContext &&
groupContext.valueProp /* somehow the `Toggle` needs to know the prop given to the `ToggleGroup` */ &&
valueProp === undefined)
but this is problematic obviously... adding a value to the context simply for debugging doesnt seem right...
and there isn't really a way to check for it otherwise..
There was a problem hiding this comment.
There is value in the context already, or am I missing something?
I played around with the PR, I hope you don't mind that I hijacked it a bit and pushed a few changes. 🙈
Indeed, I don't see the "typing" as an issue, the real issue is the value problem.
Try this example, for instance:
'use client';
import * as React from 'react';
import { ToggleGroup } from '@base-ui/react/toggle-group';
import { Toggle } from '@base-ui/react/toggle';
const alignmentOptions = ['start'] as const;
export default function ToggleGroupExperiment() {
return (
<React.Fragment>
<ToggleGroup
multiple
defaultValue={alignmentOptions}
onValueChange={(value, eventDetails) => console.info(value, eventDetails)}
>
<Toggle>Start</Toggle>
<Toggle>Center</Toggle>
<Toggle>End</Toggle>
</ToggleGroup>
</React.Fragment>
);
}
The onValueChange will always have a ghost start value, that can not be unchecked ("unpressed").
It looks like the bigger problem is a case, where the Group has a value or defaultValue (both map to groupContext.value).
I feel like we should better warn about these cases instead.
WDYT @michaldudak, @mj12albert?
I leave the approval to anyone of you, given that I modified the approach.
| if (process.env.NODE_ENV !== 'production') { | ||
| // eslint-disable-next-line react-hooks/rules-of-hooks | ||
| useIsoLayoutEffect(() => { | ||
| if (groupContext && valueProp === undefined && groupContext.isValueInitialized) { |
There was a problem hiding this comment.
feels weird having a context value only for debugging, but it is more accurate now.
maybe write a comment on it in the context?
nonetheless LGTM 👍
There was a problem hiding this comment.
IMHO, we can even consider "upgrading" this message to an error, given that it can/will cause actual usage problems.
WDYT?
There was a problem hiding this comment.
I'm wondering if we even need to check if groupContext's value is initialized. I can't think of a use case where users would want to have value-less Toggles within a ToggleGroup
| if (process.env.NODE_ENV !== 'production') { | ||
| // eslint-disable-next-line react-hooks/rules-of-hooks | ||
| useIsoLayoutEffect(() => { | ||
| if (groupContext && valueProp === undefined && groupContext.isValueInitialized) { |
There was a problem hiding this comment.
I'm wondering if we even need to check if groupContext's value is initialized. I can't think of a use case where users would want to have value-less Toggles within a ToggleGroup
LukasTy
left a comment
There was a problem hiding this comment.
Sorry for taking so long with this PR; the company retreat delayed IC work. 🙈
Thank you for your contribution. 🙏
Looks good to merge.
Closes #3142.
Closes #3756.
Note
Originally this was meant to only introduce generic types to the
ToggleGroup, but i realized while working on this thatToggletoo had to be changed to handle a generic value.