Skip to content

[toggle-group][toggle] Improve type safety and inference - #3173

Merged
LukasTy merged 32 commits into
mui:masterfrom
michaelhazan:toggle-group-type-safety
Feb 9, 2026
Merged

LukasTy merged 32 commits into
mui:masterfrom
michaelhazan:toggle-group-type-safety

Conversation

@michaelhazan

@michaelhazan michaelhazan commented Nov 7, 2025 •

Copy link
Copy Markdown
Contributor

Closes #3142.
Closes #3756.

Note

Originally this was meant to only introduce generic types to the ToggleGroup, but i realized while working on this that Toggle too had to be changed to handle a generic value.

@pkg-pr-new

pkg-pr-new Bot commented Nov 7, 2025 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@3173
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@3173
    

commit: eea77b3

@mui-bot

mui-bot commented Nov 7, 2025 •

Copy link
Copy Markdown

Bundle size report

Bundle Parsed size Gzip size
@base-ui/react 🔺+69B(+0.02%) 🔺+34B(+0.03%)

Details of bundle changes


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Nov 7, 2025 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit eea77b3
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/697b854d423507000831968e
😎 Deploy Preview https://deploy-preview-3173--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Nov 7, 2025
@github-actions github-actions Bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Nov 7, 2025
@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Nov 10, 2025
@github-actions github-actions Bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Nov 10, 2025
@LukasTy LukasTy added type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature. component: toggle Changes related to the toggle component. typescript component: toggle group Changes related to the toggle group component. labels Nov 10, 2025
@michaelhazan

Copy link
Copy Markdown
Contributor Author

it seems like this is failing over something i have not affected...

would appreciate help

@mj12albert

Copy link
Copy Markdown
Member

@michaelhazan My bad, I remembered we actually do this intentionally:

the value prop and onValueChange don't depend on if the multiple is true or not causing the component to always expect and array

Accordion is the same and the value is always an array

@michaelhazan

Copy link
Copy Markdown
Contributor Author

@michaldudak Hey! should be good now..
if you have anymore suggestions you want me to implement let me know!

Comment thread packages/react/src/toggle/Toggle.tsx Resolved

@LukasTy LukasTy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your effort. 🙏
Leaving a few suggestions and one discussion point.

Comment thread packages/react/src/toggle/Toggle.tsx Resolved
export namespace ToggleGroup {
export type State = ToggleGroupState;
export type Props = ToggleGroupProps;
export type Props<Value extends string> = ToggleGroupProps<Value>;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is useful and addresses #3756

(it should also be able to infer the value arg in onValueChange I assume)

@LukasTy LukasTy Jan 27, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LukasTy would you like me to fix it in this pr? or leave it for now?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can keep this one isolated to these specific components.
WDYT?

@michaelhazan michaelhazan Jan 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for sure

…o check for it

also made the generic type in `ToggleGroup` & `Toggle` default to `string` to match the behavior in `SliderRootProps`.
@michaelhazan

michaelhazan commented Jan 27, 2026 •

Copy link
Copy Markdown
Contributor Author

@LukasTy fixed!
was uncertain if you wanted me to implement something from your comment here, from what i got, you want to make a general change in the library following what i've done here and #3756 , and therefore no change in necessary in this pr.

did i get that right? 😅

please note i've also set the props's generic type to equal string by default after i saw similar thing in SliderRootProps.
here and here

@michaelhazan
michaelhazan requested a review from LukasTy January 27, 2026 22:53
Comment thread packages/react/src/toggle/Toggle.tsx Outdated
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.',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)?

@michaelhazan michaelhazan Jan 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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..

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@LukasTy LukasTy changed the title [toggle-group][toggle] type safety [toggle-group][toggle] Improve type safety and inference Jan 28, 2026
Comment thread packages/react/src/toggle/Toggle.tsx Resolved
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line react-hooks/rules-of-hooks
useIsoLayoutEffect(() => {
if (groupContext && valueProp === undefined && groupContext.isValueInitialized) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 👍

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, we can even consider "upgrading" this message to an error, given that it can/will cause actual usage problems.
WDYT?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mj12albert raised a fair point here

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, OK.

if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line react-hooks/rules-of-hooks
useIsoLayoutEffect(() => {
if (groupContext && valueProp === undefined && groupContext.isValueInitialized) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread packages/react/src/toggle/Toggle.tsx Resolved

@michaldudak michaldudak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me now!

@LukasTy LukasTy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for taking so long with this PR; the company retreat delayed IC work. 🙈
Thank you for your contribution. 🙏
Looks good to merge.

@LukasTy
LukasTy merged commit 1390a3b into mui:master Feb 9, 2026
@michaelhazan
michaelhazan deleted the toggle-group-type-safety branch February 9, 2026 15:21
@atomiks atomiks mentioned this pull request Feb 10, 2026
1 task done
@atomiks atomiks mentioned this pull request Feb 19, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: toggle group Changes related to the toggle group component. component: toggle Changes related to the toggle component. type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature. typescript

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[toggle group] any[] value type breaks type safety [ToggleGroup] Better type safety

5 participants