Skip to content

Commit

Permalink
[Tabs] Fix RTL indicator (#26470)
Browse files Browse the repository at this point in the history
  • Loading branch information
siriwatknp committed Jun 2, 2021
1 parent 1c54c8e commit 34aa839
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/src/pages/components/tabs/ScrollableTabsButtonAuto.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function ScrollableTabsButtonAuto() {
};

return (
<Box sx={{ width: '100%', bgcolor: 'background.paper' }}>
<Box sx={{ maxWidth: 480, bgcolor: 'background.paper' }}>
<Tabs
value={value}
onChange={handleChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function ScrollableTabsButtonAuto() {
};

return (
<Box sx={{ width: '100%', bgcolor: 'background.paper' }}>
<Box sx={{ maxWidth: 480, bgcolor: 'background.paper' }}>
<Tabs
value={value}
onChange={handleChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function ScrollableTabsButtonForce() {
};

return (
<Box sx={{ width: '100%', bgcolor: 'background.paper' }}>
<Box sx={{ maxWidth: 480, bgcolor: 'background.paper' }}>
<Tabs
value={value}
onChange={handleChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function ScrollableTabsButtonForce() {
};

return (
<Box sx={{ width: '100%', bgcolor: 'background.paper' }}>
<Box sx={{ maxWidth: 480, bgcolor: 'background.paper' }}>
<Tabs
value={value}
onChange={handleChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function ScrollableTabsButtonPrevent() {
};

return (
<Box sx={{ width: '100%', bgcolor: 'background.paper' }}>
<Box sx={{ maxWidth: 480, bgcolor: 'background.paper' }}>
<Tabs
value={value}
onChange={handleChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function ScrollableTabsButtonPrevent() {
};

return (
<Box sx={{ width: '100%', bgcolor: 'background.paper' }}>
<Box sx={{ maxWidth: 480, bgcolor: 'background.paper' }}>
<Tabs
value={value}
onChange={handleChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function ScrollableTabsButtonVisible() {
};

return (
<Box sx={{ flexGrow: 1, width: '100%', bgcolor: 'background.paper' }}>
<Box sx={{ flexGrow: 1, maxWidth: 480, bgcolor: 'background.paper' }}>
<Tabs
value={value}
onChange={handleChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function ScrollableTabsButtonVisible() {
};

return (
<Box sx={{ flexGrow: 1, width: '100%', bgcolor: 'background.paper' }}>
<Box sx={{ flexGrow: 1, maxWidth: 480, bgcolor: 'background.paper' }}>
<Tabs
value={value}
onChange={handleChange}
Expand Down
20 changes: 13 additions & 7 deletions packages/material-ui/src/Tabs/Tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,30 +356,36 @@ const Tabs = React.forwardRef(function Tabs(inProps, ref) {
const updateIndicatorState = useEventCallback(() => {
const { tabsMeta, tabMeta } = getTabsMeta();
let startValue = 0;
let startIndicator;

if (tabMeta && tabsMeta) {
if (vertical) {
if (vertical) {
startIndicator = 'top';
if (tabMeta && tabsMeta) {
startValue = tabMeta.top - tabsMeta.top + tabsMeta.scrollTop;
} else {
}
} else {
startIndicator = isRtl ? 'right' : 'left';
if (tabMeta && tabsMeta) {
const correction = isRtl
? tabsMeta.scrollLeftNormalized + tabsMeta.clientWidth - tabsMeta.scrollWidth
: tabsMeta.scrollLeft;
startValue = tabMeta.left - tabsMeta.left + correction;
startValue =
(isRtl ? -1 : 1) * (tabMeta[startIndicator] - tabsMeta[startIndicator] + correction);
}
}

const newIndicatorStyle = {
[start]: startValue,
[startIndicator]: startValue,
// May be wrong until the font is loaded.
[size]: tabMeta ? tabMeta[size] : 0,
};

// IE11 support, replace with Number.isNaN
// eslint-disable-next-line no-restricted-globals
if (isNaN(indicatorStyle[start]) || isNaN(indicatorStyle[size])) {
if (isNaN(indicatorStyle[startIndicator]) || isNaN(indicatorStyle[size])) {
setIndicatorStyle(newIndicatorStyle);
} else {
const dStart = Math.abs(indicatorStyle[start] - newIndicatorStyle[start]);
const dStart = Math.abs(indicatorStyle[startIndicator] - newIndicatorStyle[startIndicator]);
const dSize = Math.abs(indicatorStyle[size] - newIndicatorStyle[size]);

if (dStart >= 1 || dSize >= 1) {
Expand Down
46 changes: 46 additions & 0 deletions packages/material-ui/src/Tabs/Tabs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,52 @@ describe('<Tabs />', () => {
expect(style.left).to.equal('60px');
expect(style.width).to.equal('50px');
});

it('should have "right" for RTL', () => {
const { forceUpdate, container, getByRole } = render(
<div dir="rtl">
<Tabs value={1}>
<Tab />
<Tab />
</Tabs>
</div>,
{
wrapper: ({ children }) => (
<ThemeProvider theme={createTheme({ direction: 'rtl' })}>{children}</ThemeProvider>
),
},
);

const tablistContainer = getByRole('tablist').parentElement;
const tab = getByRole('tablist').children[1];

Object.defineProperty(tablistContainer, 'clientWidth', { value: 100 });
Object.defineProperty(tablistContainer, 'scrollWidth', { value: 100 });
tablistContainer.getBoundingClientRect = () => ({
left: 0,
right: 100,
});
tab.getBoundingClientRect = () => ({
left: 50,
width: 50,
right: 100,
});
forceUpdate();
expect(container.querySelector(`.${classes.indicator}`)).toHaveInlineStyle({
right: '0px',
width: '50px',
});
tab.getBoundingClientRect = () => ({
left: 40,
width: 50,
right: 90,
});
forceUpdate();
expect(container.querySelector(`.${classes.indicator}`)).toHaveInlineStyle({
right: '10px',
width: '50px',
});
});
});

describe('warnings', () => {
Expand Down

0 comments on commit 34aa839

Please sign in to comment.