Skip to content

import className from fileName #40

Closed

Description

@ChristopherBiscardi

Let's say I have a file in an npm module as such:

/* Card.css in the npm module `cards`*/
.card {
  background: white;
}

In another project, I want to target .card from cards.

/* Thing.css */
.row {
  /* some random grid stuff */
  .card from "cards" {
    grid-width: 1/3;
  }
}

I've seen :import from ICSS but have hanged my local setup (webpack, etc) trying to figure out how I can import an identifier from a different file.

Is this css-modules territory or should I look at other ways of importing the identifiers?

Activity

  1. NekR commented on Aug 26, 2015

    @NekR

    Seems like this one is dupe of #25

  2. ChristopherBiscardi commented on Aug 26, 2015

    @ChristopherBiscardi
    Author

    @NekR I thought it might be, but #25 seems to be talking about importing variables such as colors. What I am trying to achieve seems more limited in scope since it is only for .card to be compiled to the same classname across two files.

  3. NekR commented on Aug 26, 2015

    @NekR

    @ChristopherBiscardi hmm.. I might be wrong, but card should be compiled with the same name anyway.

    Okay, here when accesing .row from JS you will get class names of .row and .card in one string. If you will then create .row2 and compose it with .card then .row2 will use same class of .card as ``row`. Although I am not sure how this will work with external modules, but if you are using webpack and css-loader then I think everything should be fine.

    .row {
      composes: card from 'cards.css';
      /* some random grid stuff */
      grid-width: 1/3;
    }
  4. cliffrowley commented on Aug 27, 2015

    @cliffrowley

    This is rather good timing for me, I'm also having exactly this issue. I'll post back with any success / failure.

  5. joshwnj commented on Aug 27, 2015

    @joshwnj
    Member

    If it's coming from an npm module cards should it be composes: card from "cards/cards.css" ?

  6. cliffrowley commented on Aug 27, 2015

    @cliffrowley

    Ok, I've just tried using composes to solve this and I've stumbled on another issue before I can even test it. I'm using sass (via sass-loader) and each of my component sass files imports a utils.scss a couple of directories down, which works fine without composes, but with it I am getting a file not found on the utils.scss from the file being imported for composition.

    E.g. this is my directory structure:

    src/
      components/
        Card.jsx
        Card.scss
        CardContainer.jsx
        CardContainer.scss
        ...
      sass/
        _utils.scss
        ...
    

    My (simplified) Card.scss looks like this:

    @import '../sass/utils';
    
    .card {
      ...
    }
    

    My (again simplified) CardContainer.scss looks like this:

    @import '../sass/utils';
    
    .card {
        composes: card from './Card.scss';
    }
    
    .cardContainer {
      & > .card {
        ...
      }
    }
    

    The error I see is:

    Module not found: Error: Cannot resolve 'file' or 'directory' ../sass/utils in <path to project>/src/components
    

    I don't fully understand how this all fits together but It seems to me that this mechanism is trying to resolve a js/jsx file rather than going through the sass pipeline. Does this make sense to you? It's resolving ./Card.scss just fine - it's the secondary import that doesn't seem to be working.

    Edit: apologies, it was late last night and today I realised i should take this issue to webpack/css-loader!

  7. ChristopherBiscardi commented on Aug 27, 2015

    @ChristopherBiscardi
    Author

    @joshwnj Consider the second snippet in my original post as pseduocode.

    @NekR using composes as such:

    .row {
      composes: card from 'cards';
    }

    results in this JS object:

    {
     row: 'Thing__row___9NgKF Card__card___1RA4j'
    }

    which works perfectly, but is not what I'm trying to do.


    What I am trying to do is render the following final css, where Card__card___1RA4j is an identifier defined in another file.

    .Thing__row___9NgKF {
      /* some grid stuff here */
    }
    
    .Thing__row___9NgKF .Card__card___1RA4j {
      grid-width: 1/3;
    }
  8. joshwnj commented on Aug 27, 2015

    @joshwnj
    Member

    @ChristopherBiscardi i see, that makes sense thanks.

    Would this give the result you're after?

    .row { 
      /* grid stuff */
    }
    
    .card {
      composes: card from "cards";
    }
    
    .row .card {
      grid-width: 1/3;
    }
    
  9. ChristopherBiscardi commented on Aug 27, 2015

    @ChristopherBiscardi
    Author

    That produces:

    .Thing__row___9NgKF .Thing__card___1mL-s {
    }

    so it would work if I was using the .card from Thing.css, but what I want to do is target the .card from cards.

  10. joshwnj commented on Aug 27, 2015

    @joshwnj
    Member

    That's why I'm wondering, do you have a specific need to do that? In this case .card in Thing.css composes .card from cards so in your template when you do:

    import styles from "./Thing.css"
    
    `<div class="${styles.row}">
      <div class="${styles.card}" />
    </div>`
    

    it should give you the specificity of "a card inside a row" that you're after. Or does that example not cover some of the functionality you're after?

  11. ChristopherBiscardi commented on Aug 27, 2015

    @ChristopherBiscardi
    Author

    The specificity desired isn't "a card, inside a row" it's "the card class from cards, inside of a row".

    To expand on this a bit, let's say I am rendering this Card React element, which comes from (for all intents and purposes) a third party node module named cards.

    // Card.jsx
    
    import React, { Component, PropTypes } from 'react';
    const { string } = PropTypes;
    import styles from './Card.css';
    
    export class Card extends Component {
      render() {
        return <div className={styles.card}>{this.props.children}</div>
      }
    }
    /* Card.css */
    .card {
      background: white;
    }

    The usage (in an app) would look something like this:

    import React, { Component } from 'react';
    import Card from 'cards';
    import styles from './App.css';
    
    class App extends Component {
      render() {
        return (
          <div className={styles.row}>
            <Card><p>stuff here</p></Card>
            <Card><p>stuff here</p></Card>
            <Card><p>stuff here</p></Card>
            <Card><p>stuff here</p></Card>
          </div>
        )
      }
    }

    Which brings us to App.css.

    .row {
      lost-center: 968px;
      .card from "cards" {
        lost-column: 1/4;
      }
    }

    So specifically, I'd like to target the card class from Card.css inside App.css.

  12. joshwnj commented on Aug 27, 2015

    @joshwnj
    Member

    I see, thanks for clarifying. So in this case the Card component doesn't allow you to supply your own className. If it did we could sidestep this issue altogether.

    At the moment there's no support in css-modules for using .card from "cards" as a selector. It's not out of the question, but I'm trying to think through the ramifications.

    To use this approach would involve a fair bit of knowledge about the internals of the 3rd-party module, correct? You'd need to assume that it was also using the same version of css-modules, otherwise the className Thing.css gets for .card won't be guaranteed to match the className that the <Card> component applies to its children.

    I think this is where ICSS comes into play. If a 3rd-party module was distributed with styles and classNames in this common format there would be less assumptions involved.

    Is this the kind of thing you had in mind?

  13. ChristopherBiscardi commented on Aug 27, 2015

    @ChristopherBiscardi
    Author

    If it did we could sidestep this issue altogether.

    Indeed. In this specific case I control all of the relevant code and could add the className support, but the intent is to distribute a set of abstractions as modules themselves. Part of this is exploring where the boundaries actually lie in terms of what has to be packaged with what (clearly that would mean shipping today would mean implementing className support, which I already have a working model of). Ideally I'd like to build upon this to create elements which can work as a group or individually without requiring passing new classes around. Which leads to:

    To use this approach would involve a fair bit of knowledge about the internals of the 3rd-party module, correct?

    Yes, it would. Rather, it would except for the fact that the constellation of element packages will all be built on a common base; Which makes the base assumptions a known quantity shared across all elements. There are other significant facets, such as the style of grid system that lost grid is, etc. but all that is stuff I'm finalizing after I figure out the base issue of "Can elements integrate deeply with other elements based on pre-existing class names".

    You'd need to assume that it was also using the same version of css-modules

    The elements are being distributed as raw code (ie: as pre babel and pre PostCSS compilation processes) rather than pre-compiled for various other reasons, such as enabling per-project configuration of constants such as "default grid width", "media query bases" and "default border radius". The intent being to enable deep theming, "element collections" (roughly analogous to a Bootstrap, Foundation, Semantic, etc), among other things. As a result, they will all be compiled at the same time, with the same version of css-modules. (pre-compilation for use as raw css is an interesting discussion topic I've explored briefly but is not currently the focus).

    I think this is where ICSS comes into play. If a 3rd-party module was distributed with styles and classNames in this common format there would be less assumptions involved.
    Is this the kind of thing you had in mind?

    yes, pretty much, although I don't know if ICSS is the solution. How would one go about distributing as ICSS? I haven't come across any "Dump ICSS" debug options, icss-loader, etc.

  14. joshwnj commented on Aug 27, 2015

    @joshwnj
    Member

    How would one go about distributing as ICSS?

    I don't know of any examples of this, just thinking ahead of possible solutions :)

    Thanks for sharing the context of the problem you're trying to solve, that makes more sense now.

  15. NekR commented on Aug 27, 2015

    @NekR

    hmm.. modifying classes from another modules seems like violation of module principles. While this all makes sense in global CSS, it does not makes sense in CSS Modules since the idea is to eliminate such side effects. Image that you could modify a constructor of imported class and this will affect all other users of that class. Looks like a mot such a best practice, right? What is common in OOP is inheritance, and while it's called differently, composition from the CSS Modules is almost the same. e.g. to be able to modify (override) Card class in your code, you need to inherit from original Card and then use that subclass across your application. I believe same applies for .card and composition.

    P.S. I understand that Card is React class and it provides already an interface to modifying/customizing it, but this is just an implementation detail of that class.

  16. 29 remaining items

  17. tomdye commented on Dec 5, 2016

    @tomdye

    I am currently having the same issue when assessing our use of css-modules within the dojo 2 toolkit. We are considering using css modules to ship structural styles along with our widgets and themes in a separate repo. In order to use the generated class names at build time we would need to be able to load class names from the structural files into the theming files. Otherwise we'd have to fall back to custom tag names, or adding separate class names specifically for theming which will break the encapsulation that css modules provides.

  18. Billy- commented on Mar 3, 2017

    @Billy-

    Hi all, have just come across this issue. Has there been any updates on this?

    Kind regards,

  19. vinipace commented on May 2, 2017

    @vinipace

    Any update?

  20. tivac commented on May 2, 2017

    @tivac

    :external(...) has been working well for us the few times we've needed this capability when using modular-css.

  21. arahansen commented on Jul 18, 2017

    @arahansen

    @tivac could you provide an example of how this works? That syntax doesn't seem to do the trick for me. Is there an additional loader I need in place?

  22. tivac commented on Jul 18, 2017

    @tivac

    @arahansen I don't think CSS Modules ever added support for :external, I built my own thing for a variety of reasons and implemented it there.

    https://github.com/tivac/modular-css/blob/master/docs/features.md#overriding-styles

  23. Reverbe commented on Nov 2, 2017

    @Reverbe

    any update?

  24. lawrencebla commented on Aug 16, 2018

    @lawrencebla

    any update?

  25. nkbt commented on Aug 17, 2018

    @nkbt

    @lawrencebla I guess since issue is closed it is not going to happen.

    On a topic:

    We already have pretty good dependency/composition management in JS, adding another layer of dependency management to CSS seems like an overkill.

    This seems like a perfect solution to the problem. And you get if for free with all the existing tooling

    import commonCss from `../Button.css`;
    import css from `./RedButton.css`;
    
    <button className={`${commonCss.commonButton} ${css.redButton}`} />
  26. tomdye commented on Aug 17, 2018

    @tomdye

    @nkbt that's essentially what we have adopted for Dojo

  27. hrasoa commented on Sep 12, 2018

    @hrasoa

    So i'm facing the same problem. I have:

    Cover.scss

    .cover { ... }

    Cover.js

    import styles from './Cover.scss';
    export default () => <div className={styles.cover}>cover</div>

    Content.scss

    .content { ... }

    Content.js

    import styles from './Content.scss';
    export default () => <div className={styles.content}>content</div>

    App.js

    export default () => (
      <div>
        <Cover />
        <Content />
      </div>
    );

    As the Cover component may not always be in the page, I want to style the Content component depending on the Cover with this simple rule:

    .cover + .content {
     ...
    }
  28. jquense commented on Sep 12, 2018

    @jquense

    @nkbt thats the same thing as composes:? The discussion here is the need for an API that allows selecting for a class e.g. .toolbar > .button {} vs .toolbar > .form-control {} which is not possible by either existing means but generally required (albeit infrequently)

  29. nkbt commented on Sep 13, 2018

    @nkbt

    @hrasoa if you want composition of component styles, you need to act like you do with components, to step above and compose them. So it would be something like

    App.js

    import css from 'App.css';
    export default () => (
      <div>
        <Cover className={css.cover} />
        <Content className={css.component} />
      </div>
    );

    With support of className in each component respectively.

    I do believe that idea of doing .cover + .content inside either of those components is straight up incorrect, since components should be able to work in isolation and not know anything about any adjacent components. Only parent should be able to compose components and their styles if styles depend on composition.

  30. nkbt commented on Sep 13, 2018

    @nkbt

    @jquense we do not use composes:, it's pretty much unofficially banned from being used entirely. Each css is self contained and never aware of any other things on a page except its own component. For theming we use var() which works perfectly fine in all modern browsers we support. For any styles that depend on multiple component - we step above and style element that composes those components instead (see my previous comment for example of that)

    In this way we are as close to StyledComponents as it is technically possible. Like full on "css-in-js" but with nice and familiar CSS syntax with all its advantages and no drawbacks.

  31. jquense commented on Sep 13, 2018

    @jquense

    Ya'll if anyone wants to see this happen, I think it think it should. Having used css-modules exclusively for the last few years on large and small projects there are definitely valid use cases for it.

    The issue here is there isn't anyone to work on this, so if someone wants to see it happen, the best way forward would be to show up with a PR. no one is really working on css-modules anymore, i think it just takes someone who wants to do the work, see: #187 and #187

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