Skip to content

Scoped styles for child components #573

@BernieCr

Description

@BernieCr

I really love working with this library, the only thing that bugs me is that currently there is no simple way to pass scoped styles to child components:

<div>
   <h1 className="header">Login</h1>
   <CustomButton className="login-button">Submit</Button>
   <CustomButton>Cancel</Button>
</div>
.header {
   font-size: 50px;
}
.login-button { /* won't be applied currently */
   color: red;
}

This issue had already been discussed several times (#273, #197, #121). So I thought the current behavior (with the :global() workaround) was due a technical limitation. However, after digging into the code a bit, I discovered that it's actually quite easy to apply the jsx-* scoped class name to child React components as well.

I already created a PR for this. My change adds the jsx-* class to all child React components where a custom className is passed. In the example above, that apply to the "Submit" button,
but not the "Cancel" button. This leads to the following code being generated:

<div class="jsx-4088848069">
   <h1 class="jsx-4088848069 header">Login</h1>
   <button type="button" class="button button--secondary jsx-4088848069 login-button">Submit</button>
   <button type="button" class="button button--secondary">Cancel</button>
</div>

As you can see, the button with our custom class "login-button" gets the jsx-* class, the other one doesn't. Works like a charm, the scoped style for .login-button is applied correctly. It also doesn't mess with the existing classes of our child components ("button button--secondary" in this example).

So are there any reasons I'm currently missing why this bevahior isn't possible or advisable? I'd really like this feature to be included.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions