Skip to content

Commit ce5ef12

Browse files
committed
React me updated
1 parent 6925e54 commit ce5ef12

File tree

3 files changed

+121
-1
lines changed

3 files changed

+121
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
22
coverage/
3+
dist/

README.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# React CSS Loader
2+
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.
3+
4+
## Getting started
5+
To install run `npm install react-css-loader --save`
6+
7+
## Usage
8+
9+
### Client
10+
React CSS Loader provides a Higher Order Component which you pass your own components into and optionally an object as a second paramter with a function called `styles`.
11+
12+
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`.
13+
14+
You can also define your own styles using the styles function which should return an array of strings.
15+
16+
```
17+
/* ./app.js */
18+
import React from 'react';
19+
import {StaticCSS as CSS} from 'react-css-loader';
20+
21+
const MyComponent = () => {
22+
return <div className="my-custom-element"/>;
23+
};
24+
25+
MyComponent.displayName = "Header";
26+
27+
export default CSS(MyComponent, {
28+
styles() {
29+
return [
30+
'MyCustomStyle.css',
31+
];
32+
}
33+
});
34+
```
35+
36+
### Server
37+
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.
38+
39+
#### File system
40+
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.
41+
42+
##### Inline
43+
44+
```
45+
import App from './app'; /* React application */
46+
import {Resolver, FileSystemAdapter} from 'react-css-loader';
47+
48+
const cssResolver = Resolver(App, new FileSystemAdapter({
49+
folderPath: `${__dirname}/styles/`,
50+
inline: true,
51+
}));
52+
53+
const cssString = cssResolver.render();
54+
console.log(cssString);
55+
/*
56+
<style>
57+
.my-custom-element {
58+
background-color: black;
59+
}
60+
</style>
61+
<style>
62+
.my-custom-element-two {
63+
background-color: red;
64+
}
65+
</style>
66+
*/
67+
```
68+
69+
##### Not Inline
70+
71+
```
72+
import App from './app';
73+
import {Resolver, FileSystemAdapter} from 'react-css-loader';
74+
75+
const cssResolver = Resolver(App, new FileSystemAdapter({
76+
folderPath: `${__dirname}/styles/`,
77+
inline: false,
78+
}));
79+
80+
const cssString = cssResolver.render();
81+
console.log(cssString);
82+
/*
83+
.my-custom-element {
84+
background-color: black;
85+
}
86+
87+
.my-custom-element-two {
88+
background-color: red;
89+
}
90+
*/
91+
```
92+
93+
#### CDN File Adapter
94+
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.
95+
96+
```
97+
import App from './app';
98+
import {Resolver, CDNAdapter} from 'react-css-loader';
99+
100+
const cssResolver = Resolver(App, new CDNAdapter({
101+
cdnRoot: 'https://my-cdn.com'
102+
}));
103+
104+
const cssString = cssResolver.render();
105+
console.log(cssString);
106+
/*
107+
<link rel="stylesheet" href="https://my-cdn.com/Header.css">
108+
*/
109+
```

package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@
22
"name": "@mintuz/react-css-loader",
33
"version": "1.0.0",
44
"main": "dist/index.js",
5-
"description": "A React HOC that provides functionality for rendering CSS per component",
5+
"description": "A React high order component that provides functionality for tree shaking CSS per component",
66
"homepage": "http://github.com/mintuz/react-css-loader/tree/master",
7+
"keywords": [
8+
"CSS Loader",
9+
"React CSS Loader",
10+
"Tree Shaking CSS",
11+
"Tree Shake CSS",
12+
"Tree Shake",
13+
"CSS",
14+
"CSS Tree shaking"
15+
],
716
"repository": {
817
"type": "git",
918
"url": "git+https://github.com/mintuz/react-css-loader.git"
1019
},
1120
"bugs": {
1221
"url": "https://github.com/mintuz/react-css-loader/issues"
1322
},
23+
"license": "MIT",
1424
"scripts": {
1525
"prettier": "prettier --arrow-parens=always --single-quote --tab-width=4 --print-width=80 \"./src/**/*.js\" \"./test/**/*js\" --write",
1626
"babel": "babel src --out-dir dist --ignore '**/*.test.js'",

0 commit comments

Comments
 (0)