Skip to content
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] Fix tabs activating incorrectly on non-primary button clicks #1318

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/react/src/tabs/root/TabsRoot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,21 @@ describe('<Tabs.Root />', () => {
expect(handleChange.firstCall.args[0]).to.equal(1);
});

it('should not call onValueChange on non-primary button clicks', async () => {
const handleChange = spy();
const { getAllByRole } = await render(
<Tabs.Root value={0} onValueChange={handleChange}>
<Tabs.List>
<Tabs.Tab value={0} />
<Tabs.Tab value={1} />
</Tabs.List>
</Tabs.Root>,
);

fireEvent.click(getAllByRole('tab')[1], { button: 2 });
expect(handleChange.callCount).to.equal(0);
});

it('should not call onValueChange when already selected', async () => {
const handleChange = spy();
const { getAllByRole } = await render(
Expand Down
66 changes: 45 additions & 21 deletions packages/react/src/tabs/tab/useTabsTab.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use client';
import * as React from 'react';
import { mergeReactProps } from '../../utils/mergeReactProps';
import { ownerDocument } from '../../utils/owner';
import { useEnhancedEffect } from '../../utils/useEnhancedEffect';
import { useForkRef } from '../../utils/useForkRef';
import { useBaseUiId } from '../../utils/useBaseUiId';
Expand Down Expand Up @@ -75,32 +76,56 @@ function useTabsTab(parameters: useTabsTab.Parameters): useTabsTab.ReturnValue {

const tabPanelId = index > -1 ? getTabPanelIdByTabValueOrIndex(valueParam, index) : undefined;

const isPressingRef = React.useRef(false);
const isPrimaryButtonRef = React.useRef(false);

const getRootProps = React.useCallback(
(externalProps = {}) => {
return mergeReactProps<'button'>(
externalProps,
mergeReactProps<'button'>(
{
role: 'tab',
'aria-controls': tabPanelId,
'aria-selected': selected,
id,
ref: handleRef,
onClick(event) {
{
role: 'tab',
'aria-controls': tabPanelId,
'aria-selected': selected,
id,
ref: handleRef,
onClick(event) {
if (selected) {
return;
}

onTabActivation(tabValue, event.nativeEvent);
},
onFocus(event) {
if (!activateOnFocus || selected) {
return;
}

if (!isPressingRef.current || (isPressingRef.current && isPrimaryButtonRef.current)) {
onTabActivation(tabValue, event.nativeEvent);
},
onFocus(event) {
mj12albert marked this conversation as resolved.
Show resolved Hide resolved
if (!activateOnFocus) {
return;
}

if (selectedTabValue !== tabValue) {
onTabActivation(tabValue, event.nativeEvent);
}
},
}
},
onPointerDown(event) {
if (selected) {
return;
}

isPressingRef.current = true;

function handlePointerUp() {
isPressingRef.current = false;
isPrimaryButtonRef.current = false;
}

if (!event.button || event.button === 0) {
Copy link
Member

@oliviertassinari oliviertassinari Jan 10, 2025

Choose a reason for hiding this comment

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

We use

Suggested change
if (!event.button || event.button === 0) {
if (event.button === 0) {

in all Material UI codebase. I have never seen someone open a bug because event.button is missing. Could this reduce the bundle size here?

isPrimaryButtonRef.current = true;

const doc = ownerDocument(event.currentTarget);
doc.addEventListener('pointerup', handlePointerUp, { once: true });
}
},
mergeReactProps(getItemProps(), getButtonProps()),
),
},
mergeReactProps(getItemProps(), getButtonProps()),
);
},
[
Expand All @@ -111,7 +136,6 @@ function useTabsTab(parameters: useTabsTab.Parameters): useTabsTab.ReturnValue {
id,
onTabActivation,
selected,
selectedTabValue,
tabPanelId,
tabValue,
],
Expand Down
Loading