Software Alternatives & Reviews

Components with separate styles - how to do?

CSS Modules Chakra UI
  1. I’ve been intrigued by CSS Modules lately. If you haven't heard of them, this post is for you. We'll be looking at the project and it's goals and aims. If
    Any time you import CSS files into a module, that CSS becomes active on EVERY component in your entire project, so that's not really a good way to go about it. It essentially creates a tag inside the final rendered html with all of your CSS within it. If you have two CSS files, and they both have a class of <code>.myClass</code> then they will step on each other and cause bad things to happen to your UI.</p> <p>There are a few ways to go about this properly.</p> <p>My first suggestion is to use CSS modules. These are CSS files that work ONLY within the scope of the component's module and don't step on other component module's CSS. Essentially, you are importing a CSS file as an object which React can then use. i.e.</p> <pre><code>import myStyles from './some_css_file.module.css'; Function MyComponent() { return <div className={myStyles.myStyleClassName}>Blah blah blah</div> } </code></pre> <p>What the above does is import your CSS file, but gives every single CSS declaration from the CSS file its own unique class name. This way if two CSS files have the same class name somewhere within them, they'll be renamed to unique class names for each JSX module.</p> <p>If the above is a little confusing, see the following:</p> <p><a href="https://css-tricks.com/css-modules-part-1-need/">https://css-tricks.com/css-modules-part-1-need/</a></p> <p>There are other options such as CSS in JS (personally not a fan) or styled components. There are some absolutely awesome libraries such as <a href="https://chakra-ui.com/">ChakraUI</a> or <a href="https://mui.com/">MUI</a> for React which will make your life easier in every way when it comes to styling, but it's all a matter of preference.</p> <p>Personally, I prefer Chakra UI, but MUI isn't too shabby.</p> </div>.

    #Developer Tools #Design Tools #CSS Framework 11 social mentions

  2. Simple, modular and accessible UI components for your React applications.
    Pricing:
    • Open Source
    Any time you import CSS files into a module, that CSS becomes active on EVERY component in your entire project, so that's not really a good way to go about it. It essentially creates a tag inside the final rendered html with all of your CSS within it. If you have two CSS files, and they both have a class of <code>.myClass</code> then they will step on each other and cause bad things to happen to your UI.</p> <p>There are a few ways to go about this properly.</p> <p>My first suggestion is to use CSS modules. These are CSS files that work ONLY within the scope of the component's module and don't step on other component module's CSS. Essentially, you are importing a CSS file as an object which React can then use. i.e.</p> <pre><code>import myStyles from './some_css_file.module.css'; Function MyComponent() { return <div className={myStyles.myStyleClassName}>Blah blah blah</div> } </code></pre> <p>What the above does is import your CSS file, but gives every single CSS declaration from the CSS file its own unique class name. This way if two CSS files have the same class name somewhere within them, they'll be renamed to unique class names for each JSX module.</p> <p>If the above is a little confusing, see the following:</p> <p><a href="https://css-tricks.com/css-modules-part-1-need/">https://css-tricks.com/css-modules-part-1-need/</a></p> <p>There are other options such as CSS in JS (personally not a fan) or styled components. There are some absolutely awesome libraries such as <a href="https://chakra-ui.com/">ChakraUI</a> or <a href="https://mui.com/">MUI</a> for React which will make your life easier in every way when it comes to styling, but it's all a matter of preference.</p> <p>Personally, I prefer Chakra UI, but MUI isn't too shabby.</p> </div>.

    #Design Tools #Developer Tools #UI 197 social mentions

Discuss: Components with separate styles - how to do?

Log in or Post with