Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
babel-preset-enhanced-react-style
Advanced tools
Automatically splits static and dynamic styling from the style attribute into CSS class names and inline styles.
Writing styles is a hassle and though great iterations has been done with css-in-js libraries we still have to think about a few things. Things like... this dynamic css, how many class names will it produce? And this prop... will it be passed into the DOM as well? And do I really have to set up all this stuff to get automatic class creation?
We wanted to see if we could just make this stuff work. So basically you just style up your elements and to not think any more on it. One attribute style and just add the rules, them being dynamic or not.
npm install babel-preset-enhanced-react-style
babel config
{
"presets": ["@babel/react", "babel-preset-enhanced-react-style"]
}
babel config (TYPESCRIPT)
{
"presets": ["@babel/typescript", "@babel/react", "babel-preset-enhanced-react-style"]
}
Currently you have to manually augment React to allow the & property, in your index:
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
declare module 'react' {
interface CSSProperties {
'&'?: {
[selector: string]: Omit<CSSProperties, '&'>
}
}
}
NOTE! We will find a way to automate this
Basically just start using the style attribute as normal. When you look at the element inspector in the browser you will see it splits out static styling to class names.
const MyComponent: React.FunctionComponent = ({ width }) => {
return (
<div
style={{
color: 'red',
width: width + 'px'
}}
/>
)
}
You can now use CSS selectors behind the & prop:
const MyComponent: React.FunctionComponent = ({ width }) => {
return (
<div
style={{
color: 'red',
'&': {
':hover': {
color: 'blue'
}
}
}}
/>
)
}
Selectors can not be dynamic.
Typically you want to define some element more explicitly. Do that by defining them as their own components:
const Header: React.FunctionComponent = ({ children }) => (
<h1
style={{
color: '#333',
fontSize: 28
}}
>
{children}
</h1>
)
It just works, critical CSS is extracted with renderToString
.
To emotion which is an awesome css-in-js library. We are just lowering the threshold to use it... to the extent you do not even know you are using it :)
FAQs
Styles in react without thinking about it
We found that babel-preset-enhanced-react-style demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers 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
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.