Extreme lightweight CSS parser for stealing rules from stylesheets (without adding the stylesheet to your bundle), to compose into Styled Components or anywhere else you might be doing CSS in JS.
Working with global or 3rd party CSS creates constant challenges when implementing other CSS solutions. styled-import is meant to ease some of the pain by letting you steal styles from those stylesheets without needing to link or bundle or otherwise include the stylesheets themselves.
Note that this library currently operates as a Babel macro, replacing all references to styled-import calls at compile-time with the actual style declarations from the referenced stylesheets.
$ npm install -D styled-import
Styled Import currently runs only as a Babel macro, so be sure you have configured Babel to use babel-plugin-macros.
Here is one example of how to do that:
.babelrc
{
"plugins": ["macros"]
}
NOTE: Macros are included in create-react-app 2 by default.
./stylesheets/global.css
.button {
color: blue;
}
./component.js
const styledImport = require('styled-import/macro')
const btnStyle = styledImport('./stylesheets/global.css', '.button')
console.log(btnStyle) // 'color: blue;'
const styled = require('styled-components')
const styledImport = require('styled-import/macro')
const btnStyle = styledImport('./stylesheets/global.css', '.button')
const Button = styled.button`
padding: 10px;
${btnStyle}
`
String composition works like inheritance/cascade:
const btnBlue = styledImport('./stylesheets/global.css', '.button-blue')
const Button = styled.button`
color: green;
padding: 10px;
${btnBlue}
`
// color: green is overridden by color: blue in btnBlue
const btnStyle = styledImport.react('./stylesheets/global.css', '.button')
// btnStyle is now an object {'color': 'blue'} with camelCased properties, instead of a CSS string
const btnStyle = styledImport('@org/stylesheets/global.css', '.button')
const [btnStyle, headerStyle] = styledImport('@org/styles/global.css', ['.button', '.header'])
const {button, header} = styledImport('@org/styles/global.css', {button: '.button', header: '.header'})
const cardBtnStyle = styledImport('./stylesheets/global.css', '.card .button')
const cardBtnStyle = styledImport('./stylesheets/global.css', /\.button/gi)
// returns an array of declarations from selectors that matched the regex (omit
g flag to return just the first match)
Make sure you're on the development branch and then:
$ npm test
NOTE: Tests will only run in a git cloned repo. They are disabled in the published npm module.
- This currently only works with static values. Dynamic arguments can/will break it. Some dynamic support is coming soon.
- Better error handling coming soon!
- Selectors passed as arguments must match stylesheet selectors exactly. Partial matches/regex matches coming soon.
- There is no de-duplication or other optimizations at this time. Currently styled-import just copies out the rules from the classes. It does not import the whole stylesheet into your bundle. Optimizations coming soon.
- See the warning at top -- this is experimental and untested in many environments! Production-ready version...you guessed it...is coming soon.