-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[Tabs] Add selected prop for easy integration with routing libraries #44844
Comments
Hey @itayperry, thanks for contributing. Can you provide the API you envision to support your use case? Take into account that Material UI components can't directly integrate with libraries like React Router or TanStack Router because we can't have access to the state coming from those libraries out of the box. There will be always a small amount of glue code that you'll need to connect these libs with Material UI. Have you taken a look at this guide? https://mui.com/material-ui/integrations/routing/#tabs I just created this naive example on how you might use |
Since the issue is missing key information and has been inactive for 7 days, it has been automatically closed. If you wish to see the issue reopened, please provide the missing information. |
HI there @aarongarciah,
I understand it but as a developer I always try to find a way to make things simpler. Using the const childRoutes = [
{
name: "INBOX",
to: "./inbox",
},
{
name: "DRAFTS",
to: "./drafts",
},
{
name: "TRASH",
to: "./trash",
},
] as const;
function MyTabsToggler() {
return (
<Tabs color="primary" aria-label="basic tabs example">
{childRoutes.map((subRoute) => {
return (
<NavLink to={subRoute.to} style={{ all: "unset" }} key={subRoute.to}>
{({ isActive, isPending }) => (
<Tab
value={subRoute.name}
aria-label={subRoute.name}
selected={isActive}
disabled={isPending}
/>
)}
</NavLink>
);
})}
</Tabs>
);
} This code doesn't work because the tab doesn't have a way (like a Here's an example of doing it without tabs, using const childRoutes = [
{
name: "INBOX",
to: "./inbox",
},
{
name: "DRAFTS",
to: "./drafts",
},
{
name: "TRASH",
to: "./trash",
},
] as const;
function HeaderToggler() {
return (
<ToggleButtonGroup color="primary" exclusive>
{childRoutes.map((subRoute) => {
return (
<NavLink to={subRoute.to} style={{ all: "unset" }} key={subRoute.to}>
{({ isActive, isPending }) => (
<ToggleButton
size="small"
value={subRoute.name}
aria-label={subRoute.name}
selected={isActive}
disabled={isPending}
>
{subRoute.name}
</ToggleButton>
)}
</NavLink>
);
})}
</ToggleButtonGroup>
);
} Anyway, I do not wish to waste your time, I know there are more important issues and feature requests out there :) |
@itayperry Based on your snippet, you can apply styles based on data attributes with const childRoutes = [
{
name: "INBOX",
to: "./inbox",
},
{
name: "DRAFTS",
to: "./drafts",
},
{
name: "TRASH",
to: "./trash",
},
] as const;
function MyTabsToggler() {
return (
<Tabs color="primary" aria-label="basic tabs example">
{childRoutes.map((subRoute) => {
return (
<NavLink to={subRoute.to} style={{ all: "unset" }} key={subRoute.to}>
{({ isActive, isPending }) => (
<Tab
value={subRoute.name}
aria-label={subRoute.name}
data-selected={isActive ? "" : undefined}
disabled={isPending}
sx={{
'&[data-selected]': { background: 'primary.main', color: 'white' },
}}
/>
)}
</NavLink>
);
})}
</Tabs>
);
} |
This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue. Note @itayperry How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey. |
Hi there @siriwatknp, I still suggest changing the API to have self-awareness for the |
@siriwatknp your solution requires users to recreate the selected built-in styles from Tab, which is not ideal. @itayperry although a |
Summary
Hi,
First of all, I do not wish to waste the maintainers' time, I just think this matter will be meaningful for the front-end community :)
The subject at hand is allowing the tabs to be used with navigation in libraries like React Router - making each tab know if it's active using the routes "isActive" prop.
I will start by showing an example of a navigation with different MUI components -
<ToggleButtonGroup/>
combined with
<ToggleButton/>
First, this is a part of an example router - a route that has 3 sub-routes:
This is a component that toggles between the routes,
each is aware of the
isActive
prop and knows whether it is selected or not.This code is extremely comfortable and easy, but unfortunately cannot be implemented with Tabs/Tab.
A tab can never know whether it is selected or not, and it depends on its parent, the
<Tabs>
component.Examples
No response
Motivation
I was just thinking it will be really easy and fun to allow tabs to do what button-toggle(rs) already do..
Plus, many SPA React projects are written with React-Router / TanStack-Router, and I truly believe this will be extremely helpful and widely used in the community 🎄
Search keywords: tab
The text was updated successfully, but these errors were encountered: