Skip to content

Commit

Permalink
Support generating multiple properties from one prop + fix for respon…
Browse files Browse the repository at this point in the history
…sive styles
  • Loading branch information
hamlim committed Jan 21, 2021
1 parent 22de11a commit 6430f90
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-props",
"version": "0.4.2",
"version": "0.5.0",
"main": "dist/index.js",
"source": "src/index.tsx",
"types": "dist/index.d.ts",
Expand Down
21 changes: 21 additions & 0 deletions src/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,24 @@ test('supports non-variable style props', () => {
}
`)
})

test('supports multiple properties for a single prop', () => {
let simpleProps = createSimpleProps({
props: {
paddingX: {
scale: 'space',
properties: ['paddingLeft', 'paddingRight'],
},
},
})

let styles = simpleProps({
paddingX: 4,
})
expect(styles).toMatchInlineSnapshot(`
Object {
"paddingLeft": 4,
"paddingRight": 4,
}
`)
})
82 changes: 53 additions & 29 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ function defaultToVariable({ scale, value }) {
}

interface Props {
[name: string]: { scale: string; property: string } | boolean
[name: string]:
| { scale: string; property: string }
| { scale: string; properties: Array<string> }
| boolean
}
export interface PseudoProps {
[name: string]: string
Expand Down Expand Up @@ -83,13 +86,16 @@ export default function createSimpleProps({
}

if (isSimpleProp) {
let scale, property
let scale,
properties = []
let resolvedConfig = propConfig[prop]
if (typeof resolvedConfig === 'boolean') {
scale = property = prop
scale = prop
properties.push(prop)
} else {
scale = resolvedConfig.scale
property = resolvedConfig.property
// @ts-ignore
properties = resolvedConfig.properties || [resolvedConfig.property]
}
if (typeof propValue === 'object' && propValue != null) {
// _, 360
Expand All @@ -98,36 +104,48 @@ export default function createSimpleProps({
let tokenMatch =
// This value could be a number which doesn't support `.match`
typeof propValue[query] === 'string'
? typeof propValue[query].match(/\$([^\s]+)/)
? propValue[query].match(/\$([^\s]+)/)
: null
let tokenValue =
tokenMatch && tokenMatch.length > 0 ? tokenMatch[1] : null
if (query === '_') {
return {
...newStyles,
[property]: tokenValue
? toVariable({
scale,
value: tokenValue,
props,
breakpoint: query,
})
: propValue[query],
...properties.reduce(
(sty, property) => ({
...sty,
[property]: tokenValue
? toVariable({
scale,
value: tokenValue,
props,
breakpoint: query,
})
: propValue[query],
}),
{},
),
}
}
let queryKey = createMediaQuery({ query })
return {
...newStyles,
[queryKey]: {
...(newStyles[queryKey] || {}),
[property]: tokenValue
? toVariable({
scale,
value: tokenValue,
props,
breakpoint: query,
})
: propValue[query],
...properties.reduce(
(sty, property) => ({
...sty,
[property]: tokenValue
? toVariable({
scale,
value: tokenValue,
props,
breakpoint: query,
})
: propValue[query],
}),
{},
),
},
}
}, styles)
Expand All @@ -140,14 +158,20 @@ export default function createSimpleProps({
// non-responsive prop values
styles = {
...styles,
[property]: tokenValue
? toVariable({
scale,
value: tokenValue,
props,
breakpoint: '_',
})
: propValue,
...properties.reduce(
(sty, property) => ({
...sty,
[property]: tokenValue
? toVariable({
scale,
value: tokenValue,
props,
breakpoint: '_',
})
: propValue,
}),
{},
),
}
}
}
Expand Down

0 comments on commit 6430f90

Please sign in to comment.