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

Another rant about not being able to import a class from another file #391

Open
thojanssens opened this issue Apr 10, 2023 · 0 comments
Open

Comments

@thojanssens
Copy link

thojanssens commented Apr 10, 2023

Say we have a parent component with the following css styles:

/* Parent.module.css */

.foo {
  /* code */
}

Problem: we want to use the .foo class from the style of a child component like so:

/* Child.module.css */

.foo .bar {
  /* code */
}

If only could we just import the class, for example like so:

/* Child.module.css */

@value foo from "./Parent.module.css";

.foo .bar {
  /* code */
}

Unfortunately the result of the above seems to be inconsistent among bundlers and compilers, which doesn't make it a reliable solution (the very frustrating thing is that it "almost" works with Vite... but not). According to what I've seen on SO, some users could make it work but then I don't know which bundler/compiler they are using.

Really the only way I found is to use :global, but I hate polluting my global name space.

Another way to reuse parent styling is to use composes, however the example that I had in mind is a parent class that may be set or not, and we style the child accordingly.

Even though this case should be very rare, as the whole idea is to scope locally per module, it may be very useful on RARE occasions when a parent and a child are very tightly related, and we still want separate component files.

CSS Modules is a beautiful project, it's old but for me personally it's the best CSS tool available. It's unfortunate that it does not address some shortcomings like the recurrent question of importing a class in a css file from another css file, while technically it could be feasible, it just needs to be spec'ed out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant