GithubHelp home page GithubHelp logo

mintuz / react-css-loader Goto Github PK

View Code? Open in Web Editor NEW
2.0 0.0 0.0 312 KB

React Higher Order Component to enable CSS Tree shaking

JavaScript 96.70% CSS 0.57% HCL 2.73%
css higher-order-component css-in-js css-library css-loader css-load-on-demand react-components css-files inline inline-styles

react-css-loader's Introduction

React CSS Loader

npm package NPM downloads NPM license

A library to load and tree shake standard CSS with a simple, flexible interface allowing you to only load the CSS you need and want, dependent on if your react components are rendering.

Getting started

To install run npm install @mintuz/react-css-loader --save

Usage

Client

React CSS Loader provides a Higher Order Component which you pass your own components into and optionally an object as a second parameter with a function called styles.

When a displayName is present on your component, react-css-loader will try to resolve the displayName to a css file on the server. For example if you had a displayName of Header it will try to resolve to Header.css.

You can also define your own styles using the styles function which should return an array of strings.

/* ./app.js */
import React from 'react';
import { StaticCSS as CSS } from '@mintuz/react-css-loader';

const MyComponent = () => {
    return <div className="my-custom-element"/>;
};

MyComponent.displayName = "Header";

export default CSS(MyComponent, {
    styles() {
        return [
            'MyCustomStyle.css',
        ];
    }
});

Server

On the server you need to import the resolver and an adapter. React CSS Loader provides you with two adapters by default, a file system adapter and a CDN adapter but you are able to create your own if you wish.

File system

The file system adapter will resolve css files from your local file system and accepts two parameters, the folderPath which is where your CSS files live and an inline boolean which determines what the output string should look like.

Inline
import App from './app'; /* React application */
import { Resolver } from '@mintuz/react-css-loader';
import FileSystemAdapter from '@mintuz/react-css-loader/file';

const cssResolver = new Resolver(React.createElement(App), new FileSystemAdapter({
    folderPath: `${__dirname}/styles/`,
    inline: true,
}));

const cssString = cssResolver.render();
console.log(cssString);
/*
    <style>
        .my-custom-element {
            background-color: black;
        }
    </style>
    <style>
        .my-custom-element-two {
            background-color: red;
        }
    </style>
*/
Not Inline
import App from './app';
import { Resolver } from '@mintuz/react-css-loader';
import FileSystemAdapter from '@mintuz/react-css-loader/file';

const cssResolver = new Resolver(React.createElement(App), new FileSystemAdapter({
    folderPath: `${__dirname}/styles/`,
    inline: false,
}));

const cssString = cssResolver.render();
console.log(cssString);
/*
    .my-custom-element {
        background-color: black;
    }

    .my-custom-element-two {
        background-color: red;
    }
*/

CDN File Adapter

If you serve your CSS via a CDN, this is for you. Rather than resolving the CSS files from the filesystem, it will render link tags instead that point to your CSS files on a CDN.

import App from './app';
import { Resolver } from '@mintuz/react-css-loader';
import CDNAdapter from '@mintuz/react-css-loader/cdn';

const cssResolver = new Resolver(React.createElement(App), new CDNAdapter({
    cdnRoot: 'https://my-cdn.com'
}));

const cssString = cssResolver.render();
console.log(cssString);
/*
    <link rel="stylesheet" href="https://my-cdn.com/Header.css">
*/

react-css-loader's People

Contributors

mintuz avatar

Stargazers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.