Skip to content

Commit 73782fd

Browse files
committed
feat: add @xstyled/prop-types utility
Closes #23
1 parent 6daa154 commit 73782fd

File tree

9 files changed

+137
-0
lines changed

9 files changed

+137
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"jest-styled-components": "^6.3.1",
5656
"lerna": "^3.14.1",
5757
"prettier": "^1.18.2",
58+
"prop-types": "^15.7.2",
5859
"react": "^16.8.6",
5960
"react-dom": "^16.8.6",
6061
"rollup": "^1.14.3",

packages/prop-types/.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/*
2+
!/dist/**/*.js
3+
*.test.js

packages/prop-types/LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2019 Smooth Code
2+
3+
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:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
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.

packages/prop-types/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# @xstyled/prop-types
2+
3+
Generate prop-types from [@xstyled/system](https://www.smooth-code.com/open-source/xstyled/docs/system-props/).
4+
5+
See [documentation](https://www.smooth-code.com/open-source/xstyled/docs/prop-types/)

packages/prop-types/package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "@xstyled/prop-types",
3+
"description": "Generate prop-types from xstyled utilities.",
4+
"version": "1.2.0",
5+
"keywords": [
6+
"emotion",
7+
"styled-components",
8+
"css",
9+
"system",
10+
"styled-system",
11+
"prop-types"
12+
],
13+
"sideEffects": false,
14+
"main": "dist/xstyled-prop-types.cjs.js",
15+
"module": "dist/xstyled-prop-types.es.js",
16+
"author": "Greg Bergé <berge.greg@gmail.com>",
17+
"license": "MIT",
18+
"scripts": {
19+
"build": "rollup -c",
20+
"prebuild": "shx rm -rf dist",
21+
"prepublishOnly": "yarn build"
22+
},
23+
"publishConfig": {
24+
"access": "public"
25+
},
26+
"peerDependencies": {
27+
"prop-types": "^15.0.0"
28+
},
29+
"dependencies": {
30+
"@babel/runtime": "^7.4.4"
31+
}
32+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { getRollupConfig } from '../../config/rollup'
2+
import pkg from './package.json'
3+
4+
export default getRollupConfig({
5+
pwd: __dirname,
6+
buildName: 'xstyled-prop-types',
7+
name: 'xstyledPropTypes',
8+
pkg,
9+
globals: {
10+
'prop-types': 'PropTypes',
11+
},
12+
})

packages/prop-types/src/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { oneOfType, number, string, object } from 'prop-types'
2+
3+
export function getSystemPropTypes(system) {
4+
if (!system) return {}
5+
return system.meta.props.reduce((obj, prop) => {
6+
obj[prop] = oneOfType([number, string, object])
7+
return obj
8+
}, {})
9+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { space } from '@xstyled/system'
2+
import { getSystemPropTypes } from '.'
3+
4+
describe('#getSystemPropTypes', () => {
5+
it('generates empty object if falsy', () => {
6+
const propTypes = getSystemPropTypes()
7+
expect(propTypes).toEqual({})
8+
})
9+
10+
it('generates PropTypes from system', () => {
11+
const propTypes = getSystemPropTypes(space)
12+
expect(Object.keys(propTypes)).toEqual([
13+
'margin',
14+
'm',
15+
'marginTop',
16+
'mt',
17+
'marginRight',
18+
'mr',
19+
'marginBottom',
20+
'mb',
21+
'marginLeft',
22+
'ml',
23+
'mx',
24+
'my',
25+
'padding',
26+
'p',
27+
'paddingTop',
28+
'pt',
29+
'paddingRight',
30+
'pr',
31+
'paddingBottom',
32+
'pb',
33+
'paddingLeft',
34+
'pl',
35+
'px',
36+
'py',
37+
])
38+
})
39+
})
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
menu: Guides
3+
title: Prop Types
4+
order: 30
5+
---
6+
7+
# Prop Types
8+
9+
`@xstyled/prop-types` exposes a utility to generate prop-types from a system prop utility.
10+
11+
## How to get Prop Types?
12+
13+
Import `getUtilityPropTypes` from `@xstyled/prop-types` and generates prop-types for a custom utility.
14+
15+
```js
16+
import { compose, space, color } from '@xstyled/system'
17+
import { getUtilityPropTypes } from '@xstyled/prop-types'
18+
19+
const customSystem = compose(
20+
space,
21+
color,
22+
)
23+
24+
const SystemBox = styled.div`
25+
${customSystem}
26+
`
27+
28+
SystemBox.propTypes = getUtilityPropTypes(customSystem)
29+
```

0 commit comments

Comments
 (0)