Skip to content

Commit 46a8506

Browse files
[Tabs] Fix not scrolling to correct tab after refresh when auto scrollable (#46869)
Co-authored-by: Zeeshan Tamboli <[email protected]>
1 parent 71e7997 commit 46a8506

File tree

6 files changed

+123
-1
lines changed

6 files changed

+123
-1
lines changed

docs/next-env.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference types="next" />
22
/// <reference types="next/image-types/global" />
3-
/// <reference path="./.next/types/routes.d.ts" />
3+
/// <reference path="./export/types/routes.d.ts" />
44

55
// NOTE: This file should not be edited
66
// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information.

packages/mui-material/src/Tabs/Tabs.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,12 @@ const Tabs = React.forwardRef(function Tabs(inProps, ref) {
745745
return undefined;
746746
}, [scrollable, scrollButtons, updateScrollObserver, childrenProp?.length]);
747747

748+
React.useEffect(() => {
749+
if (scrollable && scrollButtons === 'auto' && (displayEndScroll || displayStartScroll)) {
750+
scrollSelectedIntoView(true);
751+
}
752+
}, [displayEndScroll, displayStartScroll, scrollable, scrollButtons, scrollSelectedIntoView]);
753+
748754
React.useEffect(() => {
749755
setMounted(true);
750756
}, []);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as React from 'react';
2+
import Box from '@mui/material/Box';
3+
import Tabs from '@mui/material/Tabs';
4+
import Tab from '@mui/material/Tab';
5+
6+
export default function LTRHorizontalTabsAutoScrollable() {
7+
return (
8+
<Box sx={{ width: 300, display: 'flex' }}>
9+
<Tabs value={4} variant="scrollable" scrollButtons="auto" orientation="horizontal">
10+
<Tab label="Tab A" />
11+
<Tab label="Tab B" />
12+
<Tab label="Tab C" />
13+
<Tab label="Tab D" />
14+
<Tab label="Tab E" />
15+
<Tab label="Tab F" />
16+
<Tab label="Tab G" />
17+
</Tabs>
18+
</Box>
19+
);
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as React from 'react';
2+
import Box from '@mui/material/Box';
3+
import Tabs from '@mui/material/Tabs';
4+
import Tab from '@mui/material/Tab';
5+
6+
export default function LTRVerticalTabsAutoScrollable() {
7+
return (
8+
<Box sx={{ height: 200, display: 'flex' }}>
9+
<Tabs value={4} variant="scrollable" scrollButtons="auto" orientation="vertical">
10+
<Tab label="Tab A" />
11+
<Tab label="Tab B" />
12+
<Tab label="Tab C" />
13+
<Tab label="Tab D" />
14+
<Tab label="Tab E" />
15+
<Tab label="Tab F" />
16+
<Tab label="Tab G" />
17+
</Tabs>
18+
</Box>
19+
);
20+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import * as React from 'react';
2+
import { prefixer } from 'stylis';
3+
import rtlPlugin from '@mui/stylis-plugin-rtl';
4+
import { CacheProvider } from '@emotion/react';
5+
import createCache from '@emotion/cache';
6+
import { StyleSheetManager } from 'styled-components';
7+
import { ThemeProvider, createTheme } from '@mui/material/styles';
8+
import Box from '@mui/material/Box';
9+
import Tabs from '@mui/material/Tabs';
10+
import Tab from '@mui/material/Tab';
11+
12+
// Create rtl cache
13+
const cacheRtl = createCache({
14+
key: 'muirtl',
15+
stylisPlugins: [prefixer, rtlPlugin],
16+
});
17+
18+
export default function RTLHorizontalTabsAutoScrollable() {
19+
return (
20+
<StyleSheetManager stylisPlugins={[rtlPlugin]}>
21+
<CacheProvider value={cacheRtl}>
22+
<ThemeProvider theme={createTheme({ direction: 'rtl' })}>
23+
<Box dir="rtl" sx={{ width: 300, display: 'flex' }}>
24+
<Tabs value={2} variant="scrollable" scrollButtons="auto" orientation="horizontal">
25+
<Tab label="Tab A" />
26+
<Tab label="Tab B" />
27+
<Tab label="Tab C" />
28+
<Tab label="Tab D" />
29+
<Tab label="Tab E" />
30+
<Tab label="Tab F" />
31+
<Tab label="Tab G" />
32+
</Tabs>
33+
</Box>
34+
</ThemeProvider>
35+
</CacheProvider>
36+
</StyleSheetManager>
37+
);
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import * as React from 'react';
2+
import { prefixer } from 'stylis';
3+
import rtlPlugin from '@mui/stylis-plugin-rtl';
4+
import { CacheProvider } from '@emotion/react';
5+
import createCache from '@emotion/cache';
6+
import { StyleSheetManager } from 'styled-components';
7+
import { ThemeProvider, createTheme } from '@mui/material/styles';
8+
import Box from '@mui/material/Box';
9+
import Tabs from '@mui/material/Tabs';
10+
import Tab from '@mui/material/Tab';
11+
12+
// Create rtl cache
13+
const cacheRtl = createCache({
14+
key: 'muirtl',
15+
stylisPlugins: [prefixer, rtlPlugin],
16+
});
17+
18+
export default function RTLVerticalTabs() {
19+
return (
20+
<StyleSheetManager stylisPlugins={[rtlPlugin]}>
21+
<CacheProvider value={cacheRtl}>
22+
<ThemeProvider theme={createTheme({ direction: 'rtl' })}>
23+
<Box dir="rtl" sx={{ height: 200, display: 'flex' }}>
24+
<Tabs value={2} variant="scrollable" scrollButtons="auto" orientation="vertical">
25+
<Tab label="Tab A" />
26+
<Tab label="Tab B" />
27+
<Tab label="Tab C" />
28+
<Tab label="Tab D" />
29+
<Tab label="Tab E" />
30+
<Tab label="Tab F" />
31+
<Tab label="Tab G" />
32+
</Tabs>
33+
</Box>
34+
</ThemeProvider>
35+
</CacheProvider>
36+
</StyleSheetManager>
37+
);
38+
}

0 commit comments

Comments
 (0)