Gathering detailed insights and metrics for react-scoped-css-loader
Gathering detailed insights and metrics for react-scoped-css-loader
Gathering detailed insights and metrics for react-scoped-css-loader
Gathering detailed insights and metrics for react-scoped-css-loader
scoped-css-loader
CSS encapsulation solution for React
style-it
Component for writing plaintext CSS in React apps -- isomorphic, scoped, FOUC-free, fully featured, CSS-in-JS
@someonewithpc/scoped-css-loader
CSS encapsulation solution for React
inciduntpariatur
Component for writing plaintext CSS in React apps -- isomorphic, scoped, FOUC-free, fully featured, CSS-in-JS
A simple scoped css loader for react to use with webpack
npm install react-scoped-css-loader
Typescript
Module System
Node Version
NPM Version
TypeScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
14 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Sep 05, 2022
Latest Version
1.1.1
Package Id
react-scoped-css-loader@1.1.1
Unpacked Size
22.08 kB
Size
6.28 kB
File Count
10
NPM Version
6.14.15
Node Version
12.22.7
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
A webpack loader to build scoped css for react applications
React is lacking the scoped css/styling feature. Even though there are other ways to achive the scoped styling, there are bit tedious to implement.
I'm working with react applications on daily basis and couldn't find a better sollution. I wanted to build a simple solution. This has only been tested (so far) with a react/typescript application which uses sass.
Objective of this library to give the developer a simple way to implement scoped css/styling to their react applications. You can keep working on the applcation and styles as you did earlier, react-scope-css-loader
will take care of the scoping :smiley:
Install the library npm i react-scoped-css-loader
Then update the webpack.config.js
file.
This has two loaders
style-loader
will handle the style sheets (tested with sass). Add the loader after css-loader
and before sass-loader
script-loader
will handle the scripts (tested with typescript). Add the loader before the ts-loader
// webpack.config.js
module: {
rules: [
{
test: /\.(sa|sc|c)ss$/,
use: [
"style-loader",
"css-loader",
"react-scoped-css-loader/lib/style-loader"
"sass-loader"
],
},
{
test: /\.ts(x?)$/,
exclude: /node_modules/,
use: [
"react-scoped-css-loader/lib/script-loader",
'ts-loader',
]
},
]
}
now create a stylesheet with the same file name as .tsx
file and start writing your styles.
// index.tsx
<div className="container"> </div>
// or you can use variables
const containerClass = "container"
<div className={containerClass}></div>
// index.scss
.conatainer {
// your styles
}
// index.tsx
const [hasHeader, setHasHeader] = useState(false);
<div className={`container ${hasHeader ? 'has-header' : ''}`}>
// or
const containerClass = "container"
const [hasHeader, setHasHeader] = useState(false);
<div className={`${containerClass} ${hasHeader ? 'has-header' : ''}`}>
// index.scss
.conatainer {
// your styles
&.has-header {
// your styles
}
}
react-scoped-css-loader
has a classNames
functiction to apply class names conditionally
// index.tsx
import { classNames } from "react-scoped-css-loader";
const [hasHeader, setHasHeader] = useState(false);
<div className={classNames("container", { "has-header": hasHeader })}> </div>
In simple terms react-scoped-css-loader
identifies the valid classNames and append a hash value generated based on the configurations (check the configuration section for more details).
Basic configuration will generate a hash value based on the file path (Ex: ./index
) and append that hash at the end of the class name. Ex: container-816e6e0ba3
If you want to share your component across multiple projects you can pass a salt
(a unique string) and generate a unique hash value.
There are three configuration options. values for these options must be same for the both loaders.
salt
- a unique string - If you want to share your components across multiple projects make sure to pass a different salt for each projectuseGlobalHash
- accepts boolean value - default is false - if the value is true, you must pass a salt
. When this option is enabled it will use the given salt to generate the hash (file path will not be used here). you will have a one global hash for the entire projectexclude
- a list (array) of class name prefixes. if not provided library will use app
as default value. This option will exclude class names that are prefixed with the given prefixes from appending the hash value.// webpack.config.js
module: {
rules: [
{
test: /\.(sa|sc|c)ss$/,
use: [
"style-loader",
"css-loader",
{
loader: "react-scoped-css-loader/lib/style-loader",
options: {
salt: "some random/unique string",
useGlobalHash: true,
exclude: ['app']
}
},
"sass-loader"
],
},
{
test: /\.ts(x?)$/,
exclude: /node_modules/,
use: [
{
loader: "react-scoped-css-loader/lib/script-loader",
options: {
salt: "some random/unique string",
useGlobalHash: true,
exclude: ['app']
}
},
'ts-loader',
]
},
]
}
There are two limitations.
className
.check the sample below
// common.tsx
// re-usable component
interface IProps {
...
className?: string
}
const CommonComponent: React.FC<IProps> =(props) => {
return <div className={classNames('common-component', props.className)} ></div>
}
and whe you use it
// index.tsx
return <div>
<CommonComponent className='custom-class-name'>
</div>
// index.tsx
// below code will not exclude the 'app-container' class and it will be appended with the hash value.
const appContainer = 'app-container'
<div className={appContainer}>
// instead of above code use the classname directly
<div className={'app-container'}>
// you can do the same with other options (interpolation and classNames function) as well. They also will not be able to evealuate the varibles
You can use exclude
option in the configuration to have some global styles in your project.
Update the configuration and pass the prefixes you want to use for global styling. Ex. app
// webpack.config.js
...
{
loader: "react-scoped-css-loader/lib/script-loader",
options: {
exclude: ['app']
}
},
then in you style sheet write your styles. prefix the classnames with app
// global.scss
.app-container {
// your styles
}
.app-header {
// your styles
}
then use those styles anywhere
// index.tsx
<div className='app-container'> </div>
// header.tsx
<div claName='app-header'> </div>
Feel free to report any issues or open a PR.
This is a free and open source application. Just doing this for following reasons
so enjoy. use, modify, re-destribute do what ever you want.
just be civil :wink:!
No vulnerabilities found.
No security vulnerabilities found.