Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import CSS File to Styled Component

Is there a way to import a CSS file into a styled component?

One of my dependencies, React Quill Editor, is themeable by importing their CSS as a base and applying changes on top of it. All of my components are styled components, and I'd like to keep the CSS localized to the JS component rather than import the CSS as a 'global' style.

Right now I've take to copying their CSS into my own file in the following form.

I've written a brief example below.

/** editorCSS.js **/
import { css } from 'styled-components';
export default css`
/* copied CSS here */

.class-to-extend {
   color: green;
}
`


/** EditorComponent.js **/ 
import styled from 'styled-components';
import ReactQuill from 'react-quill';
import editorCSS from './editorCSS';

const StyledReactQuill = styled(ReactQuill)`
    ${editorCSS}
    /** Additional customization if necessary (e.g. positioning) */
`
export default StyledReactQuill;
`

I'd much rather import reference their css file in the scope of the styled component vs copying it.

If I do import ReactQuillCSS from 'react-quill/dist/quill.snow.css'; it will still apply my css globally due to the css-loader plugin.

Best, Daniel

like image 628
dankez Avatar asked Mar 17 '18 13:03

dankez


People also ask

Can I use CSS with styled-components?

You can use standard CSS properties, and styled components will add vendor prefixes should they be needed. Styled components are independent of each other, and you do not have to worry about their names because the library handles that for you.

Is styled-components CSS-in-JS?

Styled-components leverage a mixture of JavaScript and CSS using a technique called CSS-in-JS. Styled-components are based on tagged template literals, meaning actual CSS code is written between backticks when styling your components.

Can you add Classnames to styled-components?

Finally, you can use styled-components with any CSS framework. For example, let's create a Button component with Bootstrap styling. We used the attrs method to add a className attribute to the component with btn btn-primary value.

How can you apply dynamic styles to styled-components?

One way to dynamically change css properties with styled components is to insert a custom prop into your React component and access said property using the dollar sign and curly braces commonly used for template literals. Our current example is testing to see if our use pointer prop is true.

How do I use styled-components?

I'm a styled <Button /> As you can see, styled-components lets you write actual CSS in your JavaScript. This means you can use all the features of CSS you use and love, including (but by far not limited to) media queries, all pseudo-selectors, nesting, etc. The last step is that we need to define what a primary button looks like.

What are the benefits of styled components?

A few benefits of the styled-components library are: It’s plain CSS. Yes, you’re writing the CSS in a JS file, but the CSS syntax is unchanged. Vendor prefixes are automatically added when using Styled Components, improving performance across browsers.

What HTML elements can I use when declaring a styled component?

When declaring a Styled Component, you can use any HTML element here (e.g. <div>, <h1>, <section> etc.) Make sense?

What is styled components in React Native?

Let’s dive in: Styled Components is a library for React & React Native to write and manage your CSS. It’s a “CSS-in-JS” solution, meaning you write your CSS in Javascript files (specifically within your components, which are Javascript files).


3 Answers

You could use raw-loader to load the quill.snow.css stylesheet and then include it in your styled component.

/** EditorComponent.js **/ 
import styled from 'styled-components';
import ReactQuill from 'react-quill';
import quillCSS from '!!raw-loader!react-quill/dist/quill.snow.css';

const StyledReactQuill = styled(ReactQuill)`
    ${quillCSS}
    /** Additional customization if necessary (e.g. positioning) */
`
export default StyledReactQuill;

Per the raw-loader docs, you can use !! to prevent the styles being added globally via css-loader

Adding !! to a request will disable all loaders specified in the configuration

like image 57
jonathanhculver Avatar answered Oct 26 '22 16:10

jonathanhculver


You can add a module rule to import styles locally from a CSS file into a styled component.

E.g. import all third party .css files from node_modules as raw string, others as usual:

// webpack.config.js
const config = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"], // load project styles via style-loader
        exclude: /node_modules/, 
      },
      {
        test: /\.css$/,
        use: ["to-string-loader", "css-loader"], // use to-string-loader for 3rd party css
        include: /node_modules/,
      },
      // ...
    ],
  },
  // ...
}
Usage:
import styled from 'styled-components';
import ReactQuill from 'react-quill';
import ReactQuillCSS from 'react-quill/dist/quill.snow.css' // no custom webpack syntax

const StyledReactQuill = styled(ReactQuill)`
    ${ReactQuillCSS}
    // ... other styles
`

Don't forget to install to-string-loader, if not used yet.


This has some advantages over @jonathanhculver's solution:

  • One central config file determines, how to process .css files
  • Follow Webpack recommendations:

    Use module.rules whenever possible, as this will reduce boilerplate in your source code and allow you to debug or locate a loader faster if something goes south. (docs)

  • Avoid ESLint errors - take a look at the Codesandbox demo

  • css-loader can still resolve @import and url() for external CSS files, raw-loader won't
like image 31
ford04 Avatar answered Oct 26 '22 15:10

ford04


In order to achieve that you would need to use a different loader than css-loader. You could write an different loader which prepares it for styled-components rather than adding it to the global style sheet.

If you need css-loader, you would however need to define which css files are handled by it and which ones are loaded for styled-components, which makes it not really practical imho.

like image 1
Nappy Avatar answered Oct 26 '22 17:10

Nappy