
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
react-style-tag
Advanced tools
Write styles declaratively in React
$ npm i react-style-tag --save
// ES2015
import Style from 'react-style-tag';
// CommonJS
const Style = require('react-style-tag').default;
import React, {
Component
} from 'react';
import Style from 'react-style-tag';
class App extends Component {
render() [
return (
<div>
<h1 className="foo">
Bar
</h1>
<Style>{`
.foo {
color: red;
}
`}</Style>
</div>
);
}
}
react-style-tag
creates a React component that will inject a <style>
tag into the document's head with the styles that you pass as the text content of the tag. Notice above that the styles are wrapped in {`
and `}
, which create a template literal string. Internally, react-style-tag
parses this text and applies all necessary prefixes via autoprefixer
. All valid CSS is able to be used (@media
, @font-face
, you name it).
The style tag that is injected into the head will be automatically mounted whenever the component it is rendered in is mounted, and will be automatically unmounted whenever the component it is rendered in is unmounted.
There is an additional utility provided that can help to scope your styles in the vein of CSS Modules, and this is hashKeys
. This function accepts an array of keys to hash, and returns a map of the keys to their hashed values.
import Style, {
hashKeys
} from 'react-style-tag';
const classNamesToHash = ['foo', 'bar'];
const {
foo,
bar
} = hashKeys(classNamesToHash);
class App extends Component {
render() {
return (
<div>
<div className={foo}>
My text is red due to the scoped style of foo.
</div>
<div className={bar}>
My text is green due to the scoped style of bar.
</div>
<div className="baz">
My text is blue due to the global style of baz.
</div>
<Style>{`
.${foo} {
color: red;
}
.${bar} {
color: green;
}
.baz {
color: blue;
}
`}</Style>
</div>
);
}
}
Notice you can easily mix both scoped and global styles, and for mental mapping the scoped styles all follow the format scoped__{key}__{hash}
, for example scoped__test__3769397038
. The hashes are uniquely based on each execution of hashKeys
, so the implementation can either be Component-specific (if defined outside the class) or instance-specific (if defined inside the class, on componentDidMount
for example).
Naturally you can pass all standard attributes (id
, name
, etc.) and they will be passed to the <style>
tag, but there are a couple of additional props that are specific to the component.
doNotPrefix boolean, defaults to false
If set to true
, it will prevent autoprefixer from processing the CSS and just render whatever text you pass it.
isMinified boolean, defaults to false
If set to true
, it will minify the rendered CSS text. A possible implementation for this would be something like:
const IS_PRODUCTION = process.env.NODE_ENV === 'production';
<Style isMinified={IS_PRODUCTION}>
.test {
display: block;
}
</Style>
This would result in:
<style>.test{display:block}</style>
Standard stuff, clone the repo and npm i
to get the dependencies. npm scripts available:
compile-for-publish
=> runs the lint
and transpile
scriptsdev
=> runs the webpack dev server for the playgroundlint
=> runs ESLint against files in the src
folderprepublish
=> if in publish, runs compile-for-publish
transpile
=> runs Babel against files in src
to files in lib
FAQs
Write scoped, autoprefixed styles declaratively in React
The npm package react-style-tag receives a total of 2,959 weekly downloads. As such, react-style-tag popularity was classified as popular.
We found that react-style-tag demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.