Sass ID selector isn't working in React and create-react-library

后端 未结 2 668
被撕碎了的回忆
被撕碎了的回忆 2021-01-16 08:39

I have a library I\'m making with a Header component and a Button component. I gave them ID\'s to identify them in my SASS file, this is my current

2条回答
  •  梦谈多话
    2021-01-16 09:07

    You can either rename your scss file to remove the module from file name:

    import './styles.scss'
    

    or if you want to use module pattern in your file name, do this:

    import styles form './styles.module.scss'
    

    And provide id / class in this manner:

    export const Button = ({text, bgColor, textColor, fontFamily}) => {
    
      return 
    }
    
    export const Header = ({text, size, fontFamily, textColor}) => {
      return 

    {text} // or className={styles.myClass2}

    }

    This behavior is usually implemented by bundlers like webpack, browserify etc. It is not an inherent feature of sass.

    When you use this module pattern for your sass, the generated stylesheet looks vaguely like this:

    .moduleName_className__someUniqueId { // for classes
      color: red; 
    }
    
    #.moduleName_myId__someUniqueId { // for IDs
      color: blue; 
    }
    

    What problem does it solve?

    It provides you unique selectors (IDs and classnames) by adding moduleName and a unique identifier with them. Which helps you keep your styles organized and separated.

    Docs - add css / scss modules.

提交回复
热议问题