You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Drawing from Bootstrap, i have Button and ButtonGroup components (React variety). Each has it's own CSS module. What i'd like to do is nest the Button component class in the ButtonGroup so that its styling changes. At the moment
The nested button class has no reference to the button module so ends up with a difference class name, hence the styling for that component doesn't change when contained within a ButtonGroup.
I've been trying to figure out if the following can/should be done with composes or, rethinking how the component works (e.g. the ButtonGroup component could apply a new class to each child rather than using nested selectors.). Maybe what i'm trying to do is against what is being achieved with CSS modules but if so, what do you see as the suitable alternative to achieving the same behaviour?
If you want to, you can work around the problem with the :global syntax, but I think this is a place where you'll find that bootstrap's way of thinking and the "component" way of thinking endorsed by css-modules don't agree. Bootstrap's ButtonGroup breaks one of the core-tenants: no more conflicts. If a parent component can change the way a child behaves, we no longer have encapsulation.
Some alternative approaches:
Have buttons take a prop that allows their styling to match what a button group would do. This might come close to eliminating the need for a ButtonGroup component.
Have your button group component set its own classnames on its button children. This is kinda "magical" and breaks encapsulation, but might come the closest to what you want.
Create a whole new ButtonGroup component that doesn't use the Button component at all.
IMHO
Reacted by Alexander Palachev
Reacted by Kirill Agalakov, Ben Adamsky, TonCherAmi and Maxime Bernard
For anyone coming here from the search, here's another trick you can use. This one uses a @value import and is arguably cleaner/less hacky: #147 (comment)
Activity
joeybaker commentedon Jul 14, 2015
If you want to, you can work around the problem with the
:globalsyntax, but I think this is a place where you'll find that bootstrap's way of thinking and the "component" way of thinking endorsed by css-modules don't agree. Bootstrap'sButtonGroupbreaks one of the core-tenants: no more conflicts. If a parent component can change the way a child behaves, we no longer have encapsulation.Some alternative approaches:
ButtonGroupcomponent.children. This is kinda "magical" and breaks encapsulation, but might come the closest to what you want.ButtonGroupcomponent that doesn't use theButtoncomponent at all.IMHO
oliverbenns commentedon Jul 14, 2015
I did @joeybaker 's second suggestion not too long ago. Gist
nkbt commentedon Jul 15, 2015
👍 to @oliverbenns
I do the same and believe this is the most reasonable approach. Let's stop adding side-effects and do just plain composition.
princed commentedon Aug 22, 2017
There's one trick to make this work:
still transforms to something like
but exported from "button.css" styles object now looks like
Thus any modification you may want to apply to
.buttoninbuttongroup.csswill work inside button group.Obvious downside:
button.csshasbuttongroup.cssas a dependency.Merge pull request css-modules#21 from css-modules/global-composition
bladeSk commentedon Apr 22, 2021
For anyone coming here from the search, here's another trick you can use. This one uses a
@valueimport and is arguably cleaner/less hacky: #147 (comment)