From 5f88388b7c1c4104cd24942b72ba31acd5f9f407 Mon Sep 17 00:00:00 2001 From: Lachlan Campbell Date: Thu, 16 Jan 2020 20:18:03 -0500 Subject: [PATCH 1/7] Add variants support to components --- packages/components/package.json | 1 + packages/components/src/Box.js | 4 ++++ packages/components/test/index.js | 15 +++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/packages/components/package.json b/packages/components/package.json index 849616e59..9ae61ceef 100644 --- a/packages/components/package.json +++ b/packages/components/package.json @@ -14,6 +14,7 @@ "@styled-system/color": "^5.1.2", "@styled-system/should-forward-prop": "^5.1.2", "@styled-system/space": "^5.1.2", + "@theme-ui/core": "^0.3.1", "@theme-ui/css": "^0.3.1" }, "peerDependencies": { diff --git a/packages/components/src/Box.js b/packages/components/src/Box.js index c4c0c0bfa..f8c210055 100644 --- a/packages/components/src/Box.js +++ b/packages/components/src/Box.js @@ -1,5 +1,6 @@ import styled from '@emotion/styled' import { css, get } from '@theme-ui/css' +import { merge } from '@theme-ui/core' import { createShouldForwardProp } from '@styled-system/should-forward-prop' import space from '@styled-system/space' import color from '@styled-system/color' @@ -13,6 +14,8 @@ const sx = props => css(props.sx)(props.theme) const base = props => css(props.__css)(props.theme) const variant = ({ theme, variant, __themeKey = 'variants' }) => css(get(theme, __themeKey + '.' + variant, get(theme, variant))) +const variants = ({ theme, variants = [], __themeKey = 'variants' }) => + css(merge(variants.map(v => get(theme, __themeKey + '.' + v, get(theme, v))))) export const Box = styled('div', { shouldForwardProp, @@ -23,6 +26,7 @@ export const Box = styled('div', { minWidth: 0, }, base, + variants, variant, space, color, diff --git a/packages/components/test/index.js b/packages/components/test/index.js index 3d7a9d279..940980879 100644 --- a/packages/components/test/index.js +++ b/packages/components/test/index.js @@ -48,6 +48,10 @@ const theme = { p: 4, bg: 'highlight', }, + boop: { + bg: 'tomato', + color: 'white', + }, }, cards: { primary: { @@ -132,6 +136,17 @@ describe('Box', () => { expect(json).toHaveStyleRule('padding', '32px') }) + test('renders with variants prop', () => { + const json = renderJSON( + + + + ) + expect(json).toHaveStyleRule('padding', '32px') + expect(json).toHaveStyleRule('background-color', 'tomato') + expect(json).toHaveStyleRule('color', 'white') + }) + test('renders with base styles', () => { const json = renderJSON( Date: Thu, 16 Jan 2020 20:21:39 -0500 Subject: [PATCH 2/7] Add docs --- packages/docs/src/pages/components/variants.mdx | 11 +++++++++++ packages/docs/src/pages/guides/variants.mdx | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/packages/docs/src/pages/components/variants.mdx b/packages/docs/src/pages/components/variants.mdx index 1a7c0828b..6a3aee37b 100644 --- a/packages/docs/src/pages/components/variants.mdx +++ b/packages/docs/src/pages/components/variants.mdx @@ -64,6 +64,17 @@ For example, a secondary button style can be added to `theme.buttons.secondary` ``` +## Combining Multiple Variants + +If you want to combine multiple variants on the same element, pass an array of variants with the `variants` prop. +If styles conflict, those of the variant passed later will render. + +```jsx live=true + + Title text + +``` + ## Example Theme ```js diff --git a/packages/docs/src/pages/guides/variants.mdx b/packages/docs/src/pages/guides/variants.mdx index 3ca36662f..3bd07a615 100644 --- a/packages/docs/src/pages/guides/variants.mdx +++ b/packages/docs/src/pages/guides/variants.mdx @@ -29,6 +29,9 @@ For example, you can define `primary` and `secondary` variants for buttons and u color: 'white', bg: 'secondary', }, + small: { + fontSize: 1 + } }, } ``` @@ -51,6 +54,15 @@ Variants can use any name you choose, and deeply nested objects can be accessed +## Combining Multiple Variants + +If you want to combine multiple variants on the same element, pass an array to `variants` in the `sx` prop. +If styles conflict, those of the variant passed later will render. + +```jsx +