npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mintuz/react-css-loader

v3.0.2

Published

A React high order component that provides functionality for tree shaking CSS per component

Downloads

28

Readme

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">
*/