├── .babelrc
├── .gitignore
├── .npmignore
├── .travis.yml
├── LICENSE.md
├── README.md
├── package.json
├── src
├── System.js
└── index.js
└── test.js
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": [
3 | "env",
4 | "stage-0",
5 | "react"
6 | ]
7 | }
8 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .nyc_output
2 | dist
3 | coverage
4 |
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | .nyc_output
2 | coverage
3 | .babelrc
4 | src
5 | test.js
6 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - 8
4 | after_success:
5 | - npm run cover
6 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 |
2 | # The MIT License (MIT)
3 | Copyright (c) 2018 Brent Jackson
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 |
7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 |
9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 |
11 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # system-components
3 |
4 | **This repo has moved**
5 |
6 | https://github.com/jxnblk/styled-system/tree/master/system-components
7 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "system-components",
3 | "version": "1.1.3",
4 | "description": "Create consistent design-system-driven React UI components",
5 | "main": "dist/index",
6 | "scripts": {
7 | "prepublishOnly": "babel src -d dist",
8 | "start": "x0 dev docs/App.js -o",
9 | "cover": "nyc report --reporter=html --reporter=lcov",
10 | "test": "nyc ava"
11 | },
12 | "keywords": [
13 | "react",
14 | "components",
15 | "styled-components",
16 | "styled-system",
17 | "design-system",
18 | "theme",
19 | "css-in-js"
20 | ],
21 | "author": "Brent Jackson",
22 | "license": "MIT",
23 | "dependencies": {
24 | "styled-components": ">=3.0.0",
25 | "styled-system": "^1.1.1"
26 | },
27 | "devDependencies": {
28 | "ava": "^0.25.0",
29 | "babel-cli": "^6.26.0",
30 | "babel-preset-env": "^1.6.1",
31 | "babel-preset-react": "^6.24.1",
32 | "babel-preset-stage-0": "^6.24.1",
33 | "babel-register": "^6.26.0",
34 | "nyc": "^11.4.1",
35 | "react": "^16.2.0",
36 | "react-dom": "^16.2.0",
37 | "react-test-renderer": "^16.2.0"
38 | },
39 | "ava": {
40 | "require": [
41 | "babel-register"
42 | ],
43 | "babel": "inherit"
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/System.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import system from 'styled-system'
3 |
4 | const funcNames = Object.keys(system)
5 | const unique = arr => [...new Set(arr)]
6 | const isPOJO = n => typeof n === 'object' && n !== null && !Array.isArray(n)
7 |
8 | const dict = Object.keys(system.propTypes)
9 | .map(key => ({
10 | key,
11 | propNames: Object.keys(system.propTypes[key])
12 | }))
13 | .reduce((acc, b) => {
14 | const vals = b.propNames.reduce((a, name) => ({
15 | ...a,
16 | [name]: b.key
17 | }), {})
18 | return { ...acc, ...vals }
19 | }, {})
20 |
21 | const getPropKeys = defaultProps => Object.keys(defaultProps || {})
22 | .map(key => dict[key])
23 | .filter(key => !!key)
24 |
25 | const getFuncs = keys => keys
26 | .map(f => system[f] || f)
27 | .reduce((a, f) => Array.isArray(f) ? [ ...a, ...f ] : [ ...a, f ], [])
28 |
29 | const getPropTypes = keys => keys
30 | .filter(key => typeof key === 'string')
31 | .map(key => system.propTypes[key] || {})
32 | .reduce((a, propType) => ({ ...a, ...propType }), {})
33 |
34 | export const omit = (obj, keys) => {
35 | const next = {}
36 | for (let key in obj) {
37 | if (keys.indexOf(key) > -1) continue
38 | next[key] = obj[key]
39 | }
40 | return next
41 | }
42 |
43 | export class Tag extends React.Component {
44 | render () {
45 | const {
46 | innerRef,
47 | is,
48 | blacklist,
49 | ...props
50 | } = this.props
51 | const attr = omit(props, blacklist)
52 |
53 | return React.createElement(is, {
54 | ref: innerRef,
55 | ...attr
56 | })
57 | }
58 | }
59 |
60 | Tag.defaultProps = {
61 | is: 'div',
62 | blacklist: []
63 | }
64 |
65 | // Trick styled-components into passing innerRef
66 | Tag.styledComponentId = 'lol'
67 |
68 | class System {
69 | constructor (opts) {
70 | const {
71 | createComponent, // c(type)(...args)
72 | } = opts
73 |
74 | this.create = (...args) => {
75 | const [ first, ...rest ] = args
76 |
77 | const defaultProps = isPOJO(first) ? first : null
78 | const propKeys = getPropKeys(defaultProps)
79 | const funcsOrKeys = defaultProps ? rest : args
80 | const combined = unique([ ...propKeys, ...funcsOrKeys ])
81 | const funcs = getFuncs(combined)
82 | const propTypes = getPropTypes(combined)
83 |
84 | const div = props => React.createElement(Tag, props)
85 | div.defaultProps = {
86 | blacklist: Object.keys(propTypes)
87 | }
88 |
89 | const Component = createComponent(div)(...funcs)
90 |
91 | Component.defaultProps = defaultProps
92 | Component.propTypes = propTypes
93 |
94 | return Component
95 | }
96 |
97 | return this.create
98 | }
99 | }
100 |
101 | export default System
102 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import styled from 'styled-components'
2 | import System from './System'
3 |
4 | const create = new System({
5 | createComponent: type => (...args) => styled(type)([], ...args)
6 | })
7 |
8 | export default create
9 |
--------------------------------------------------------------------------------
/test.js:
--------------------------------------------------------------------------------
1 | import test from 'ava'
2 | import styled, { css as scCSS, isStyledComponent } from 'styled-components'
3 | import {
4 | __DO_NOT_USE_OR_YOU_WILL_BE_HAUNTED_BY_SPOOKY_GHOSTS
5 | } from 'styled-components'
6 | import { propTypes } from 'styled-system'
7 | import React from 'react'
8 | import { create as render } from 'react-test-renderer'
9 | import { isDOMComponent, isCompositeComponent } from 'react-dom/test-utils'
10 | import system from './src'
11 |
12 | // 😎
13 | const { StyleSheet } = __DO_NOT_USE_OR_YOU_WILL_BE_HAUNTED_BY_SPOOKY_GHOSTS
14 |
15 | const getCSS = () => StyleSheet.instance.toReactElements()
16 | .map(el => el.props.dangerouslySetInnerHTML.__html)
17 | .join('')
18 |
19 | test.afterEach(() => {
20 | StyleSheet.reset()
21 | })
22 |
23 | test('returns a React component', t => {
24 | const Box = system()
25 | const box = render().getInstance()
26 | t.true(isCompositeComponent(box))
27 | })
28 |
29 | test('returns a styled-component', t => {
30 | const Box = system()
31 | t.is(typeof Box, 'function')
32 | t.is(typeof Box.styledComponentId, 'string')
33 | // t.true(isStyledComponent(Box)) // not yet published
34 | })
35 |
36 | test('Adds defaultProps', t => {
37 | const Box = system({
38 | p: 2,
39 | bg: 'tomato'
40 | })
41 | t.deepEqual(Box.defaultProps, {
42 | p: 2,
43 | bg: 'tomato'
44 | })
45 | })
46 |
47 | test('adds propTypes', t => {
48 | const Box = system({
49 | p: 2,
50 | bg: 'tomato'
51 | })
52 | t.deepEqual(Box.propTypes, {
53 | ...propTypes.space,
54 | ...propTypes.color,
55 | })
56 | })
57 |
58 | test('adds styled-system functions based on default props', t => {
59 | const Box = system({
60 | p: 2,
61 | bg: 'tomato'
62 | })
63 | const json = render().toJSON()
64 | const css = getCSS()
65 | t.regex(css, /padding:16px/)
66 | t.regex(css, /background-color:tomato/)
67 | })
68 |
69 | test('accepts system key arguments', t => {
70 | const Box = system('space', 'color')
71 | t.is(typeof Box.propTypes.m, 'function')
72 | t.is(typeof Box.propTypes.color, 'function')
73 | t.is(typeof Box.propTypes.bg, 'function')
74 | })
75 |
76 | test('ignores nonexistant function keys', t => {
77 | const Box = system('foo', 'bar')
78 | t.is(Box.propsTypes, undefined)
79 | })
80 |
81 | test('accepts custom function arguments', t => {
82 | const big = props => props.big ? { padding: '64px' } : null
83 | const Box = system(big)
84 | const json = render().toJSON()
85 | const css = getCSS()
86 | t.regex(css, /padding:64px/)
87 | })
88 |
89 | test('removes styled-system props from underlying DOM element', t => {
90 | const Box = system({
91 | color: 'blue'
92 | })
93 | const json = render().toJSON()
94 | t.is(json.props.color, undefined)
95 | t.is(typeof json.props.className, 'string')
96 | })
97 |
98 | test('accepts an `is` prop to change the underlying DOM element', t => {
99 | const Box = system({
100 | p: 2
101 | })
102 | const json = render().toJSON()
103 | t.is(json.type, 'h1')
104 | })
105 |
106 | test('accepts a style function argument', t => {
107 | const Box = system(props => `color:${props.color};`)
108 | const json = render().toJSON()
109 | const css = getCSS()
110 | t.regex(css, /color:magenta/)
111 | })
112 |
113 | test('accepts theme as a default prop', t => {
114 | const theme = {
115 | colors: {
116 | blue: '#0af'
117 | }
118 | }
119 | const Box = system({ color: 'blue', theme })
120 | const json = render().toJSON()
121 | const css = getCSS()
122 | t.regex(css, /color:#0af/)
123 | })
124 |
125 | test('passes css string arguments', t => {
126 | const Box = system('color:cyan;')
127 | const json = render().toJSON()
128 | const css = getCSS()
129 | t.regex(css, /color:cyan;/)
130 | })
131 |
132 | test('works with the styled-component `css` helper', t => {
133 | const Box = system(scCSS`
134 | color: ${props => props.color};
135 | `)
136 | const json = render().toJSON()
137 | const css = getCSS()
138 | t.regex(css, /color:yellow/)
139 | })
140 |
--------------------------------------------------------------------------------