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

Box clone not overriding child component styles #15993

Closed
2 tasks done
amakk opened this issue Jun 1, 2019 · 6 comments
Closed
2 tasks done

Box clone not overriding child component styles #15993

amakk opened this issue Jun 1, 2019 · 6 comments
Labels
out of scope The problem looks valid but we won't fix it (maybe we will revisit it in the future)

Comments

@amakk
Copy link

amakk commented Jun 1, 2019

I'm using Box with the clone property (https://material-ui.com/components/box/#overriding-material-ui-components) and I'm seeing that the Box styles get overridden by the component's styles.

I've confirmed that Box is imported last in the file, but its styles are still not taking precedence. I tried to replicate it in a minimal repo, but it is working there, and I'm not able to narrow down to what is breaking the functionality because it's not consistent - sometimes it works, sometimes it doesn't. I'd like to provide information to track down this issue

I'm using Gatsby and the Gatsby plugin for Material UI, if that helps.

  • This is not a v0.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior 🤔

The box CSS should take precedence over the child component class.

Current Behavior 😯

The child component CSS is taking precedence.

Here are my imports:
Screen Shot 2019-05-31 at 9 13 31 PM

Here is my usage of box:
Screen Shot 2019-05-31 at 9 13 56 PM

Here is the CSS that's generated, as you can see the box class has lower precedence
Screen Shot 2019-05-31 at 9 15 36 PM

Steps to Reproduce 🕹

I'm not able to create a repository that reproduces this issue. I started with the example gatsby starter and pasted a lot of my code into it, and got it to a point where it wasn't working. Then I started removing code to see how to minimize the example, and it started working again. When I undid the changes, it continued working, so I'm not able to reproduce this bug consistently.

If anyone has some ideas what it could be, or what I could try, I would appreciate it. At this point I'm really stuck, but would like to work with someone to resolve the issue.

https://codesandbox.io/s/hardcore-resonance-by6zt

Context 🔦

I'm trying to use box to override styles on another component. Would really like to have this functionality working.

Your Environment 🌎

Tech Version
Material-UI v4.0.1
React v16.8.6
Browser Chrome v74.0.3729.157
TypeScript N/A
Gatsby 2.7.3
@oliviertassinari oliviertassinari added the status: waiting for author Issue with insufficient information label Jun 1, 2019
@oliviertassinari
Copy link
Member

@amakk We can't help without a reproduction.

@oliviertassinari
Copy link
Member

@kptlronyttcna thanks for the reproduction. When using the clone prop, the order in which the elements are imported is important:

This works:

import Typography from "@material-ui/core/Typography";
import Box from "@material-ui/core/Box";

This doesn't work:

import Box from "@material-ui/core/Box";
import Typography from "@material-ui/core/Typography";

@kptlronyttcna
Copy link

kptlronyttcna commented Jun 29, 2019

Hmm, It appears that

this works:

import Typography from "@material-ui/core/Typography";
import Box from "@material-ui/core/Box";

but this does not:

import { Typography } from "@material-ui/core";
import Box from "@material-ui/core/Box";

In both cases Box is imported last (to see it in codesandbox I had to manually refresh the preview window)

@amakk
Copy link
Author

amakk commented Jul 3, 2019

Has anyone had a chance to look into this?

In the mean time, what I have done is copied the Box component and increased specificity of CSS generated by Box. Here is the implementation, in case it's helpful for others having this issue:

import {
  borders,
  compose,
  css,
  display,
  flexbox,
  palette,
  positions,
  shadows,
  sizing,
  spacing,
  typography
} from '@material-ui/system'
import { styled } from '@material-ui/styles'

const innerStyleFunction = css(
  compose(
    borders,
    display,
    flexbox,
    positions,
    palette,
    shadows,
    sizing,
    spacing,
    typography
  )
)

const styleFunction = props => ({
  '&&': innerStyleFunction(props)
})

const Box = styled('div')(styleFunction, {
  name: 'MuiBox'
})

export default Box

@oliviertassinari oliviertassinari added the out of scope The problem looks valid but we won't fix it (maybe we will revisit it in the future) label Jul 13, 2019
@oliviertassinari
Copy link
Member

Moving to #15561

@satazor
Copy link

satazor commented Sep 9, 2020

Adding to @amakk suggestion, we should do this:

styleFunction.filterProps = innerStyleFunction.filterProps;

so that Box doesn't inject unknown properties to the DOM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
out of scope The problem looks valid but we won't fix it (maybe we will revisit it in the future)
Projects
None yet
Development

No branches or pull requests

4 participants