Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
react-styles-injector
Advanced tools
React component to inject scoped styles into the DOM. Client and server ready.
React Styles Injector
is a React component that injects scoped styles into the DOM. Client and server ready.
It plays well with pcss-loader for webpack to generate scoped styles for PCSS, SASS or plain CSS.
styles
objectsThe scoped styles are objects with CSS as strings in a styles
key and the hash of that string in a hash
key. For example:
const styles = {
hash: `_01d51083ede2680bfccfbc70b067a28e`
styles: `
body {
color: #c00;
}
._01d51083ede2680bfccfbc70b067a28e {
color: #00c;
}
._01d51083ede2680bfccfbc70b067a28e > h1 {
color: #0c0;
}
`
};
The hash is injected as the class of the enclosing tag. It is used to make sure that we inject the same CSS only once if you reuse your component and that the styles are properly scoped. As you can see, you can also use unscoped CSS (the body
part).
CSS class cannot start with a digit. So the hash is preceded by an underscore in my example. But you can also produce hashes with letters to have a valid class.
You can have an array of styles objects.
const styles = [
{
hash: `_5dfg35fsd0g3df540g3df54g0df33gsd`
styles: `...`
},
{
hash: `_6fds87gsfd68g7fds687gfdgs8fd79g8`
styles: `...`
},
];
The class of the enclosing tag will be a concatenation of all the hash
keys.
Writing all your styles with a greater than sign in front of it (
>
) by default is a good practice. In the previous example, the styles apply only the h1 that are direct children of our component. If there is another h1 (ok, bad example, but you get the idea) in a sub sub subcomponent that someone else put in your main component, it does not inherit the styling.
Install with npm i -S react-styles-injector
.
import React from 'react';
import Styled from 'react-styles-injector';
const styles = {
hash: '_7945a685fd09caf339a73a4023ae8b56',
styles: `
body {
background: #00c
}
._7945a685fd09caf339a73a4023ae8b56 {
background: #c00
}
`
};
export default class MyView extends React.Component {
render() {
return (
<Styled styles={styles}>
My view
</Styled>
);
}
}
This code produces two tags:
<main id='main' class='_7945a685fd09caf339a73a4023ae8b56'>
My view
</main>
and
<style>
body {
background: #00c
}
._7945a685fd09caf339a73a4023ae8b56 {
background: #c00
}
</style>
main
tag is in the standard HTML flow.head
.body
, next to the main
tag.To have the style injector injecting the styles each time they are modified, you have to work on the host localhost or 127.0.0.1.
If you work locally on another host, you can force the injector to go to dev mode:
import { setDevMode } from 'react-styles-injector';
// Before anything executes
setDevMode();
tag
: stringThe default tag for your component is div
.
You can change this by passing the tag argument as a string. Example: aside
, main
, button
...
id
: stringAn id
for the HTML tag.
styles
: object or array of objectsStyles objects must have the following structure:
{
hash: string
styles: string
}
The component uses the hash
as its class name.
To compose styles, you can pass more than one style object. Multiple style
tags are injected, and the component has multiple classes.
className
: stringA string that is added to the classe(s) from the styles. Can be one ar several classes; separated by a space.
initCache
: booleanThis property is used server side only. It helps with rendering time optimization time and HTML weight. The idea is to inject the each style
tag once pet hash.
initCache
to the root component of your application, each time you use an instance of the injector, the hashes of the styles are cached and won’t be injected again.initCache
completely, the injector injects the style
tags for each instance.initCache
multiple times, it resets the cache each time it encounters the property.Caching cannot be the default behavior because the renderer server side may not be stateless and no styles would be injected in the next render.
containerRef
: functionIf you need a reference to the actual DOM element for the component container, you can pass a containerRef
function.
<Styled
styles={{}}
tag='main'
id='mainContainer'
containerRef={element => (this.containerRef = element)}
>
...
In the parent of the Styled
component, this.containerRef
will be a reference to the DOM main #mainContainer
html element.
asFragment
: functionIf you pass asFragment={true}
, there will be no enclosing tag around the children. All the other properties except for styles
will be ignored.
<Styled
styles={{}}
asFragment={true}
>
...
inlinedJSStyles
: objectIf you pass inlinedJSStyles={{color: '#c00'}}
, that style will be inlined in the html tag. This make sense for specific logic but should not be the default way to inject styles.
<Styled
styles={{}}
inlinedJSStyles={{color: '#c00'}}
>
...
NODE_ENV
If you pass development
to the NODE_ENV
environment variable, react-styled-injector
starts in dev mode. It means that the injector re-inject more aggressively the styles in the head
it should solve some rendering issues with react HMR
FAQs
React component to inject scoped styles into the DOM. Client and server ready.
The npm package react-styles-injector receives a total of 5 weekly downloads. As such, react-styles-injector popularity was classified as not popular.
We found that react-styles-injector 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.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.