├── .gitignore
├── .npmignore
├── .babelrc
├── webpack.config.js
├── README.md
├── docs
└── index.js
├── src
└── index.js
├── test
└── index.js
└── package.json
/.gitignore:
--------------------------------------------------------------------------------
1 | dist
2 | coverage
3 |
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | src
2 | test
3 | .babelrc
4 | webpack.config.js
5 | coverage
6 |
--------------------------------------------------------------------------------
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | presets: [
3 | 'env',
4 | 'react'
5 | ],
6 | plugins: [
7 | 'styled-jsx/babel'
8 | ]
9 | }
10 |
--------------------------------------------------------------------------------
/webpack.config.js:
--------------------------------------------------------------------------------
1 | const HTMLPlugin = require('mini-html-webpack-plugin')
2 |
3 | module.exports = {
4 | mode: 'development',
5 | entry: [
6 | './docs/index.js',
7 | ],
8 | module: {
9 | rules: [
10 | {
11 | test: /\.js$/,
12 | exclude: /node_modules/,
13 | use: 'babel-loader'
14 | }
15 | ]
16 | },
17 | plugins: [
18 | new HTMLPlugin()
19 | ]
20 | }
21 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # styled-jsx-components
3 |
4 | [styled-components][]-like HOC for [styled-jsx][]
5 |
6 | ```sh
7 | npm i styled-jsx-components
8 | ```
9 |
10 | ```js
11 | import styled from 'styled-jsx-components'
12 | import { color } from 'styled-system'
13 |
14 | const Box = styled('div')`
15 | padding: 32px;
16 | ${color}
17 | `
18 | ```
19 |
20 | [styled-components]: https://github.com/styled-components/styled-components
21 | [styled-jsx]: https://github.com/zeit/styled-jsx
22 |
--------------------------------------------------------------------------------
/docs/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import { render } from 'react-dom'
3 | import { space, color } from 'styled-system'
4 | import styled from '../src'
5 |
6 | const Box = styled.div`
7 | font-family: system-ui, sans-serif;
8 | padding: 32px;
9 | ${space}
10 | ${color}
11 | `
12 |
13 | const div = document.body.appendChild(
14 | document.createElement('div')
15 | )
16 |
17 | const App = () =>
18 |
19 | hello
20 |
21 |
22 | render(, div)
23 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import objss from 'objss'
3 | import tags from 'html-tags'
4 |
5 | const join = (...args) => args.filter(Boolean).join(' ')
6 |
7 | const styled = Tag => (strings = [], ...args) => props => {
8 | const css = [strings[0]]
9 | for (let i = 0; i < args.length; i++) {
10 | const val = args[i]
11 | const rule = typeof val === 'function' ? val(props) : val
12 | const str = typeof rule === 'object' ? objss(rule) : rule
13 | css.push(str, strings[i + 1])
14 | }
15 |
16 | const style = `.x{${css.join('')}}`
17 |
18 | return (
19 |
22 | {props.children}
23 |
24 |
25 | )
26 | }
27 |
28 | tags.forEach(tag => {
29 | styled[tag] = styled(tag)
30 | })
31 |
32 | export default styled
33 |
--------------------------------------------------------------------------------
/test/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import { create as render } from 'react-test-renderer'
3 | import flush from 'styled-jsx/server'
4 | import styled from '../src'
5 |
6 | test('returns a component', () => {
7 | const Comp = styled('div')``
8 | expect(typeof Comp).toBe('function')
9 | })
10 |
11 | test('keeps classNames', () => {
12 | const Comp = styled('div')``
13 | const json = render().toJSON()
14 | expect(json.props.className).toEqual(
15 | expect.stringContaining('hello')
16 | )
17 | })
18 |
19 | // no idea how to test this...
20 | test.skip('interpolates arguments', () => {
21 | const blue = '#07c'
22 | const Comp = styled('div')`
23 | font-size: 32px;
24 | color: ${blue};
25 | `
26 | const json = render().toJSON()
27 | const [ style ] = flush()
28 | // console.log(json)
29 | // console.log(style.props.dangerouslySetInnerHTML.__html)
30 | })
31 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "styled-jsx-components",
3 | "version": "1.0.0-0",
4 | "description": "styled-components-like HOC for styled-jsx",
5 | "main": "dist/index.js",
6 | "scripts": {
7 | "prepare": "babel src -d dist",
8 | "start": "webpack-serve",
9 | "test": "jest"
10 | },
11 | "keywords": [],
12 | "author": "Brent Jackson",
13 | "license": "MIT",
14 | "dependencies": {
15 | "html-tags": "^2.0.0",
16 | "objss": "^1.0.3",
17 | "styled-jsx": "^3.0.2"
18 | },
19 | "devDependencies": {
20 | "babel-cli": "^6.26.0",
21 | "babel-core": "^6.26.3",
22 | "babel-loader": "^7.1.5",
23 | "babel-preset-env": "^1.7.0",
24 | "babel-preset-react": "^6.24.1",
25 | "jest": "^23.5.0",
26 | "mini-html-webpack-plugin": "^0.2.3",
27 | "react": "^16.4.2",
28 | "react-dom": "^16.4.2",
29 | "react-test-renderer": "^16.4.2",
30 | "styled-system": "^3.0.2",
31 | "webpack": "^4.16.5",
32 | "webpack-cli": "^3.1.0",
33 | "webpack-serve": "^2.0.2"
34 | },
35 | "jest": {
36 | "testMatch": [
37 | "**/test/**/*.js"
38 | ],
39 | "coverageReporters": [
40 | "html"
41 | ]
42 | }
43 | }
44 |
--------------------------------------------------------------------------------