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

Add support for :ref pseudoselector for accessing imports #212

Conversation

CubedEye
Copy link

@CubedEye CubedEye commented Jan 6, 2016

When importing class names or ids with the :imports pseudoselector,
this commit allows you to use these class names in CSS selectors by
wrapping them in a :ref pseudoselector. For example, if you have a
file which exports a CSS class like this...

/* imported.css */
.classA {
}

:export {
    classA: classA;
}

And import it into a CSS file like this...

/* source.css */
:import("imported.css") {
    localClassA: classA;
}

:ref(.localClassA) > .classB {
}

Then that last selector will be compiled down to somthing like

/* compiled.css */
._imported_classA > ._source_classB {
}

The CSS Modules discussion around this is here css-modules/css-modules#102

When importing class names or ids with the `:imports` pseudoselector,
this commit allows you to use these class names in CSS selectors by
wrapping them in a `:ref` pseudoselector. For example, if you have a
file which exports a CSS class like this...

```css
/* imported.css */
.classA {
}

:export {
    classA: classA;
}
```

And import it into a CSS file like this...
```css
/* source.css */
:import("imported.css") {
    localClassA: classA;
}

:ref(.localClassA) > .classB {
}
```

Then that last selector will be compiled down to somthing like
```css
/* compiled.css */
._imported_classA > ._source_classB {
}
```
@KJTsanaktsidis
Copy link

Ek, the code coverage decreased because I added a bunch of guard branches that throw errors. Guess I could test some of them...

@KJTsanaktsidis
Copy link

Anybody interested in discussing this proposal?

@sokra
Copy link
Member

sokra commented Apr 10, 2016

Is would be a addition to the CSS modules spec and should be discussed there.

But we disallowed referencing class names from other modules by intent. This breaks encapsulation. You should better solve this problem by using parameters to your components.

i. e.

:ref(.localClassA) > .classB can be solved by a parameter to component B.
.classB > :ref(.localClassA) can be solved by a parameter to component A.

@sokra sokra closed this Apr 10, 2016
@isuvorov
Copy link

wonderful PR

@lijunle
Copy link

lijunle commented Feb 16, 2017

It am looking for similar feature in CSS, and found ICSS actually define the :import pseudo-selector.

It mentions that...

The places within the CSS file that are checked for localAlias are:

  • In any declaration value: e.g. border: 1px solid localAlias;
  • In any selector: e.g. .localAlias .MyComponent {}
  • In a media query argument: e.g. @media screen and localAlias

But the second bullet does not work with this CSS-Loader. It could be great if this loader could support that.

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

Successfully merging this pull request may close these issues.

None yet

5 participants