Skip to content

[ButtonBase] Fix native button detection#47985

Merged
mj12albert merged 8 commits intomui:masterfrom
mj12albert:fix/button-base
Mar 16, 2026
Merged

[ButtonBase] Fix native button detection#47985
mj12albert merged 8 commits intomui:masterfrom
mj12albert:fix/button-base

Conversation

@mj12albert
Copy link
Member

@mj12albert mj12albert commented Mar 15, 2026

The "is native button" check only checked component !== 'button'. When component={StyledButton} where <StyledButton> renders a <button> element, event handlers incorrectly apply the logic for non-native buttons

@mj12albert mj12albert added the component: ButtonBase The React component. label Mar 15, 2026
@mui-bot
Copy link

mui-bot commented Mar 15, 2026

Netlify deploy preview

https://deploy-preview-47985--material-ui.netlify.app/

Bundle size report

Bundle Parsed size Gzip size
@mui/material 🔺+31B(+0.01%) 🔺+17B(+0.01%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/system 0B(0.00%) 0B(0.00%)
@mui/utils 0B(0.00%) 0B(0.00%)

Details of bundle changes

Generated by 🚫 dangerJS against 2bc0a4b

@mj12albert mj12albert marked this pull request as ready for review March 15, 2026 17:33
@mj12albert mj12albert added the type: bug It doesn't behave as expected. label Mar 16, 2026
Comment on lines +259 to +269
const handleClick = useEventCallback((event) => {
if (disabled) {
event.preventDefault();
return;
}

if (onClick) {
onClick(event);
}
});

Copy link
Member

Choose a reason for hiding this comment

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

I think there is a risk to intercept the click event here given that the ButtonBase is used by a lot of components. What's the issue it fixes? or is this change align with Base UI?

Copy link
Member Author

@mj12albert mj12albert Mar 16, 2026

Choose a reason for hiding this comment

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

This possibly relates more to #47989 than this PR

Currently when disabled we are relying on CSS pointer-events none to "disable" the click. However I want to extract these event handlers to a hook so that new ChipButton/ChipDelete can reuse the logic, but may not necessarily want to set pointer-events: none on them @siriwatknp

@mj12albert mj12albert requested a review from siriwatknp March 16, 2026 09:56
@mj12albert mj12albert changed the title [ButtonBase] Fix native button detection and disabled state [ButtonBase] Fix native button detection Mar 16, 2026

const button = screen.getByRole('button');

await act(async () => {
Copy link
Member

Choose a reason for hiding this comment

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

Would user.tab() work here instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is kind of an edge case to begin with since it's a disabled non-native button, because it's disabled a Tab wouldn't land focus there to begin with, only programmatic focus is possible (added a comment in the test)

button.focus();
});

fireEvent.keyUp(button, { key: ' ' });
Copy link
Member

Choose a reason for hiding this comment

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

having just a keyUp is definitely not what is going to happen in production, even though the test logic is sound. would it work if we use user.keyboard? Again, the test makes sense, it's fine, I'm just asking if we can mimic the user behavior better here.

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 changed to user.keyboard(' ')

@silviuaavram silviuaavram added needs cherry-pick The PR should be cherry-picked to master after merge. v7.x labels Mar 16, 2026
@zannager zannager added the scope: button Changes related to the button. label Mar 16, 2026
@mj12albert mj12albert merged commit 6b4c356 into mui:master Mar 16, 2026
19 checks passed
@mj12albert mj12albert deleted the fix/button-base branch March 16, 2026 14:27
@github-actions
Copy link

Cherry-pick PRs will be created targeting branches: v7.x

siriwatknp pushed a commit to siriwatknp/material-ui that referenced this pull request Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: ButtonBase The React component. needs cherry-pick The PR should be cherry-picked to master after merge. scope: button Changes related to the button. type: bug It doesn't behave as expected. v7.x

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants