Skip to content

Nesting local classes in separate module #21

Closed

Description

@mwillmott

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

/* button.css */
.button { ... }

/* buttongroup.css */
.buttonGroup > .button { .... }

transforms to something like:

.button---3h-GD { ... }
.buttonGroup---3qigr > .button---3t-2h { ... }

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?

Activity

  1. joeybaker commented on Jul 14, 2015

    @joeybaker

    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:

    1. 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.
    2. 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.
    3. Create a whole new ButtonGroup component that doesn't use the Button component at all.

    IMHO

  2. oliverbenns commented on Jul 14, 2015

    @oliverbenns

    I did @joeybaker 's second suggestion not too long ago. Gist

  3. nkbt commented on Jul 15, 2015

    @nkbt

    👍 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.

  4. princed commented on Aug 22, 2017

    @princed

    There's one trick to make this work:

    /* button.css */
    .button { 
      composes: button from ".../buttongroup.css"
      ... 
    }
    
    /* buttongroup.css */
    .buttonGroup > .button { ... }

    still transforms to something like

    .button---3h-GD { ... }
    .buttonGroup---3qigr > .button---3t-2h { ... }

    but exported from "button.css" styles object now looks like

    {
      button: "button---3h-GD button---3t-2h"
      //  ...
    }

    Thus any modification you may want to apply to .button inbuttongroup.css will work inside button group.

    Obvious downside: button.css has buttongroup.css as a dependency.

  5. added a commit that references this issue on May 7, 2019
  6. bladeSk commented on Apr 22, 2021

    @bladeSk

    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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions