From 6430f90880e319a2a879268a79dea623f1b22204 Mon Sep 17 00:00:00 2001 From: Matt Hamlin Date: Wed, 20 Jan 2021 17:13:18 -0800 Subject: [PATCH] Support generating multiple properties from one prop + fix for responsive styles --- package.json | 2 +- src/__tests__/index.test.tsx | 21 +++++++++ src/index.tsx | 82 +++++++++++++++++++++++------------- 3 files changed, 75 insertions(+), 30 deletions(-) diff --git a/package.json b/package.json index 29e157b..af8c598 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/__tests__/index.test.tsx b/src/__tests__/index.test.tsx index e5db1dc..b67c903 100644 --- a/src/__tests__/index.test.tsx +++ b/src/__tests__/index.test.tsx @@ -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, + } + `) +}) diff --git a/src/index.tsx b/src/index.tsx index 425aa7d..1c7d396 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -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 } + | boolean } export interface PseudoProps { [name: string]: string @@ -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 @@ -98,21 +104,27 @@ 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 }) @@ -120,14 +132,20 @@ export default function createSimpleProps({ ...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) @@ -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, + }), + {}, + ), } } }