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

[ButtonBase] Check if margin: 0 can be removed #26384

Closed
goffioul opened this issue May 19, 2021 · 5 comments
Closed

[ButtonBase] Check if margin: 0 can be removed #26384

goffioul opened this issue May 19, 2021 · 5 comments
Labels
component: ButtonBase The React component. customization: css Design CSS customizability

Comments

@goffioul
Copy link
Contributor

I have a CSS injection problem that is illustrated by this sandbox:
https://codesandbox.io/s/material-ui-v5-css-injection-order-9y2oe

In essence, I define a component as follow:

const styles = {
    display: "flex",
    flexWrap: "wrap",
    margin: -0.5,
    "& > *": {
        margin: 0.5
    }
};

export function ButtonBox({ sx, ...props }) {
    return <Box sx={{ ...sx, ...styles }} {...props} />;
}

I then use the component to host a series of buttons:

<ButtonBox>
    <Button variant="contained">Button 1</Button>
    <Button variant="contained">Button 2</Button>
</ButtonBox>

The problem is that Button component sets margin to 0. This rule has the same priority as the one (& > *) used in the container. So the end result will depends on the order of the injection of CSS. And it seems to be non-deterministic. While building the sandbox, the order what was I wanted, and I couldn't reproduce the issue I was having in my larger app. So I started to add more elements from my app, namely a custom ThemeProvider and CacheProvider. Then the order changed, showing the issue. But after reverting all those changes and getting back to the simpler example, the order didn't change back. I have no idea what made the injection order to change and/or how to control it.

@goffioul goffioul added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label May 19, 2021
@mnajdova
Copy link
Member

Generally, none of the components should set margin as a rule of thumb. We plan to revisit this issue, which is specific to the Button component. My suggestion would be instead of using & > *, to use & > :not(style), it will allow you to bump specificity. We use it in the Stack component. Here is a working sandbox: https://codesandbox.io/s/material-ui-v5-css-injection-order-forked-ie3oo?file=/ButtonBox.js

@mnajdova mnajdova changed the title How to control injection of CSS rules? [Button] Check if margin: 0 can be removed May 20, 2021
@mnajdova mnajdova added component: ButtonBase The React component. new feature New feature or request and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels May 20, 2021
@mnajdova mnajdova changed the title [Button] Check if margin: 0 can be removed [ButtonBase] Check if margin: 0 can be removed May 20, 2021
@goffioul
Copy link
Contributor Author

Thanks for the trick (I already used a workaround in my code, using !important). The question was more whether it's possible to have some sort of control on the injection order, or one must use CSS specificity instead.

@mnajdova
Copy link
Member

The question was more whether it's possible to have some sort of control on the injection order, or one must use CSS specificity instead.

Emotion is injecting the CSS based on the rendering order, so you cannot really change this. In cases like this, increasing specificity is required.

@siriwatknp
Copy link
Member

I believe that the intention of the author is to implement gap-like. flexbox gap is now fully supported and should be used instead of the margin hack.

I am closing this issue.

@siriwatknp siriwatknp added customization: css Design CSS customizability and removed new feature New feature or request labels Dec 20, 2024
Copy link

This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

Note

@goffioul How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: ButtonBase The React component. customization: css Design CSS customizability
Projects
None yet
Development

No branches or pull requests

3 participants