base-ui
base-ui copied to clipboard
[ToggleButton] Create the unstyled component and hook
I'd like to style a <ToggleButton>
to look and behave exactly like a <Button>
without explicitly overriding all of the ToggleButton
's class styles.
I've learned that I can inject Button
styles this way:
<ToggleButton
classes={{
root: 'MuiButtonBase-root MuiButton-root MuiButton-contained MuiButton-containedSecondary MuiButton-disableElevation',
selected: 'MuiButton-root:hover MuiButton-containedSecondary:hover MuiButton-disableElevation:active MuiButton-disableElevation:hover MuiButton-contained:active MuiButton-contained:hover',
}}
>
This halfway solves my problem. However, I'd like to avoid injecting the classes MuiToggleButton-root MuiToggleButtonGroup-grouped MuiToggleButtonGroup-groupedHorizontal
because they're overriding the ones I've added above.
Is this sort of thing possible? The reason I'm trying this is because I can't put a <Button>
inside of a <ToggleButton>
without getting an error, so I figured I'd try to make it look and behave like a <Button>
.
Thanks
I don't quite understand the issue.
I'd like to style a
<ToggleButton>
to look and behave exactly like a<Button>
without explicitly overriding all of theToggleButton
's class styles.
If you like it to behave like a Button, why don't you use the Button?
Is the use-case that you would like to actually use a ToggleButton that looks like Button? What about the toggled state?
It would partially be possible to do this once we have unstyled
version of this component, once we have it.
@mnajdova Thanks, sorry I forgot to clarify something. I'm using ToggleButton for radio-button-like functionality, but I need the radios to look exactly like <Button>
, including hover and active states.
I couldn't get Mui's <Radio>
to style like a <Button>
so I'm trying to use <ToggleButton>
instead. However, I get an error when using <Button>
inside of <ToggleButton>
.
Does that make sense?
Got it yes, sounds like something that will be possible with the unstyled
components. Until then, we don’t have option to reset styles.
Thanks for clearing that up and looking forward to that. Until then, manually restyling isn't too big of a deal.
This makes me think about:
- ButtonGroup and ToggleButton are very close in UI. We could have almost built ToggleButton on top of ButtonGroup.
- ToggleButton adds functionality over ButtonGroup. In mui/material-ui#17203 we explore how to improve the a11y.
- Maybe we could extract the ToggleButton behavior into a headless hook, and expose it, which would allow @dungle-scrubs to implement the same behavior with a Button. I doubt that an unstyled component API would work for this use case.
- Reading https://www.w3.org/TR/wai-aria-practices/examples/toolbar/toolbar.html, the hook we need might even be at the radio level, not sure.