Repository navigation
Description
Let's say I have a <MyComponent> and <MyList> that contains a list of <MyComponent>.
Only when in under <MyList>, I want <MyComponent> to have some different styles, maybe flex, or a different background-color. flex is a good example because MyList may have display: flex and might want its children to have flex: 25% and also stretch vertically.
Currently it doesn't seem like I can override <MyComponent> that is nested under <MyList>, as I would with regular css.
Here's a suggestion (I posted in https://github.com/gajus/react-css-modules/issues/94 but it might make more sense here):
// MyComponent.scss
.component {
background-color: yellow;
font-weight: bold;
}
// MyList.scss
.list {
// ...
}
.listItem {
border: 1px solid blue;
}
.listItem {
// ▼ this will generate ".listItem_[hash] .component_[hash] {}" rule instead of just .listItem_[hash]
override-child: component from './MyComponent.scss';
background-color: red;
}// MyComponent.jsx
import styles from './MyComponent.scss'
export default ({ title }) => <div className={styles.component}>{title}</div>
// MyList.jsx
import styles from './MyList.scss'
export default ({ titles }) => (
<div className={styles.list}>
{titles.map(title => <div key={title} className={styles.listItem}><MyComponent title={title} /></div>)}
</div>
)And ideally that would work in all hierarchies. If I have <MyButton> in MyComponent, I can affect if from .MyList.scss:
.listItem {
// ▼ this will generate ".listItem_[hash] .component_[hash] .button_[hash] {}" rule
override-child:
component from './MyComponent.scss',
button from './MyButton.scss';
font-size: 17px;
}I read the suggestions by @joeybaker here: #21 (comment) but I think there should be a better way.
Thoughts?
Activity
liady commentedon Apr 19, 2016
@elado +1 that's a very good idea. The reference can even be in the selector level:
And that way even the child element can have a specific rule for when it is inside a container:
sohkai commentedon Apr 19, 2016
@elado So you can accomplish your first example right now if you allow your
MyComponentto take in a classname (or entire style object) as a prop and apply it on itself, and then inMyList, create a class that composes the class fromMyComponentwith the override and then pass the new class back toMyComponent. This is essentially what @joeybaker suggested as approach 2:Not that I dislike what you're proposing, since it would certainly be convenient at times, but on some level I do feel like this kind of goes against some of the things that CSS modules was designed to protect you from. Although it's always on the user to responsibly use the provided tools, allowing for child overrides would get us closer to the messy world of normal CSS's spaghetti overrides.
It's a little more complicated, but in something like your case with flexbox, or specific style overrides, what if you made a base component that had the common styles and then put a shim component around the base component that added the positional / spacing styles, or any other specific style overrides with the class names approach above? If necessary, the shim component could also be made more generic with props. Alternatively, sometimes these overrides are because of theming (ie. override colors and whatnot) and maybe building functionality (such as HOCs in React) to switch between themes would be more idiomatic.
elado commentedon Apr 19, 2016
@sohkai with your example, imagine a little more complex situation where there's a logic on which class to give based on a prop.
I don't want to replicate the
isMinimizedlogic to theMyListor any other container ofMyComponent. It could be a little more complex if multiple variants are involved.This is a real-world example from a GChat style chat windows I'm building.
sohkai commentedon Apr 19, 2016
@elado With multiple classes like that, I'm currently using react-css-modules to provide a new style object that's been generated with sass mixins to get the necessary compositions in containing components. It's not ideal, I know.
I'm just afraid that if something like this is provided, a lot of people are going to be tempted to take the easy way out when they start using CSS modules and end up with stylesheets that are more or less as messy as they would've been as a normal stylesheet. It's kind of like
!important; we know we shouldn't use it, but invariably, someone will and someone else is going to have to maintain their work. Maybe it'd be nice to have this functionality provided as a seperate postcss plugin that builds on top of the core plugin so you can opt into it if you'd like.elado commentedon Apr 19, 2016
@sohkai I wouldn't say completely it's like
!importantor the way spaghetti css is today, becausecss-modulesalready teaches developers how to think in modules (which is great) and also the reference of the class overridden is explicitly written, so it's clear where things come from.Even a separate postcss plugin is great. I'd be happy to help/test (not familiar with postcss ecosystem too much yet).
panuhorsmalahti commentedon Aug 16, 2017
Overriding styles from parent components is not messy if the child component explicitly defines which styles are allowed to be overridden. For an example of how this can be accomplished in practice, see e.g. the Polymer framework.
samends commentedon Jun 24, 2019
I agree with @panuhorsmalahti and wanted to bump this as I had an example of this currently. I'm trying to override a style created by google material:
However I can't do this without a
!importanton max-width. This dirties the code base and forces developers to use worst practices.adamreisnz commentedon Oct 8, 2020
@sohkai how would you solve the use case with your approach when the parent component cannot directly apply another class on the child component? E.g. when using
<slot />inside the parent component.For example, consider this simplified group component:
And then using it as follows:
I would like the
groupcomponent, which is aware of any errors and validation to be able to override styles of the child components that are passed into its slot. As far as I know and have tried, I cannot apply classes on the<slot>element itself (they are not propagated).So there doesn't seem to be a clean way to apply an error class on the
<form-input>component through the parent<group>component.The only workaround I can see would be to switch back to scoped or global styles, or to have to pass validations to the
<form-input>component as well, which would be a bit redundant.JasonTheAdams commentedon Mar 3, 2022
Just ran into this myself when using a 3rd party component. I'm able to apply a class on the top-level node in the component, but I can't target any of the child nodes being rendered. Having a way around this would be useful.