question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Button] className is not the last class applied to a button

See original GitHub issue
  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

The value of the className property is not the last class name applied to a button.

This seems to be a transpile issue problem because the source code clearly puts the className value last. See: https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/Button/Button.js#L307

Expected Behavior 🤔

When passing a className property:

<Button variant="contained" color="primary" className="test-class">Test</Button>

The class test-class should be after all the button classes. However it appears before some of the button classes:

<button class="MuiButtonBase-root MuiButton-root MuiButton-contained test-class MuiButton-containedPrimary" tabindex="0" type="button"><span class="MuiButton-label">Test</span><span class="MuiTouchRipple-root"></span></button>

Steps to Reproduce 🕹

https://codesandbox.io/s/button-classname-issue-bs6m2

Steps:

  1. Create a Button with contained / color variants
  2. Pass a className attribute
  3. Check the generated HTML

Note that in Material v3 it works as expected. I was able to reproduce the problem in v4.0.0 and v4.9.5 (I didn’t check with intermediate versions).

Context 🔦

This seems to be a bug in transpile. The original code from Button (https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/Button/Button.js#L307https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/Button/Button.js#L307) is transpiled to this:

return React.createElement(ButtonBase, _extends({
    className: clsx(classes.root, classes[variant], className, color === 'inherit' ? classes.colorInherit : color !== 'default' && classes[`${variant}${capitalize(color)}`], size !== 'medium' && [classes[`${variant}Size${capitalize(size)}`], classes[`size${capitalize(size)}`]], disableElevation && classes.disableElevation, disabled && classes.disabled, fullWidth && classes.fullWidth),
    component: component,
    disabled: disabled,
    focusRipple: !disableFocusRipple,
    focusVisibleClassName: clsx(classes.focusVisible, focusVisibleClassName),
    ref: ref,
    type: type
  }, other), React.createElement("span", {
    className: classes.label
  }, startIcon, children, endIcon));

Note the className attribute that appears before color === 'inherit'. Is this a Babel bug?

Your Environment 🌎

Tech Version
Material-UI v4.9.5
React 16.12.0
Browser Chrome

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
dfernandez-asappcommented, Mar 5, 2020

Ok, my bad the order doesn’t matter. I got an instant of craziness due to hours of bug fixing. Probably the issue is caused by a declaration order change between versions. Going to close this, and dig more on why the computed CSS is different in my case. Sorry for the back and forward.

2reactions
eps1loncommented, Mar 5, 2020

I found a slight difference in one of our pages that was caused by the order

The order of the classes in the class attribute has no effect on styling. Only specificity and order of the declarations has an effect. Could you share an example where the order affects styles?

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Button] className is not the last class applied to a button
The issue is present in the latest release. I have searched the issues of this repository and believe that this is not a...
Read more >
Class is not applied to Material UI button component
I am trying to apply a class (redirectButton) to material UI button , but it is not getting applied. here is my html....
Read more >
Class on submit button not working - WordPress.org
Using Chrome inspect, this is the button. Class name is not being passed (search with inspect returns nothing, indicating it is not being...
Read more >
last-child - CSS: Cascading Style Sheets - MDN Web Docs
The :last-child CSS pseudo-class represents the last element among a group of sibling elements.
Read more >
buttons.buttons.className - DataTables
Additionally, if you extend a button that has its own class name defined, any classes you set will automatically be appended to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found