Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New variant API #823

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
},
"dependencies": {
"@emotion/core": "^10.0.0",
"@emotion/styled": "^10.0.0",
"@styled-system/color": "^5.1.2",
"@styled-system/should-forward-prop": "^5.1.2",
"@styled-system/space": "^5.1.2",
"@theme-ui/css": "^0.4.0-alpha.0"
"@theme-ui/core": "^0.4.0-alpha.0"
},
"peerDependencies": {
"react": "^16.8.0"
Expand Down
41 changes: 23 additions & 18 deletions packages/components/src/Alert.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
/** @jsx jsx */
import { jsx } from '@theme-ui/core'
import React from 'react'
import Box from './Box'
import { useVariant } from './util'

export const Alert = React.forwardRef((props, ref) => (
<Box
ref={ref}
{...props}
__themeKey="alerts"
__css={{
display: 'flex',
alignItems: 'center',
px: 3,
py: 2,
fontWeight: 'bold',
color: 'white',
bg: 'primary',
borderRadius: 4,
}}
/>
))
export const Alert = React.forwardRef(function Alert({ variant, ...props }, ref) {
const variation = useVariant('alerts', variant)
return (
<div
ref={ref}
{...props}
sx={{
display: 'flex',
alignItems: 'center',
px: 3,
py: 2,
fontWeight: 'bold',
color: 'white',
bg: 'primary',
borderRadius: 4,
...variation,
}}
/>
)
})
29 changes: 18 additions & 11 deletions packages/components/src/AspectImage.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
/** @jsx jsx */
import { jsx } from '@theme-ui/core'
import React from 'react'
import { AspectRatio } from './AspectRatio'
import { Image } from './Image'

export const AspectImage = React.forwardRef(({ ratio, ...props }, ref) => (
<AspectRatio ratio={ratio}>
<Image
ref={ref}
{...props}
__css={{
objectFit: 'cover',
}}
/>
</AspectRatio>
))
export const AspectImage = React.forwardRef(function AspectImage(
{ ratio, ...props },
ref
) {
return (
<AspectRatio ratio={ratio}>
<Image
ref={ref}
{...props}
sx={{
objectFit: 'cover',
}}
/>
</AspectRatio>
)
})
13 changes: 9 additions & 4 deletions packages/components/src/AspectRatio.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
/** @jsx jsx */
import { jsx } from '@theme-ui/core'
import React from 'react'
import Box from './Box'

export const AspectRatio = React.forwardRef(
({ ratio = 4 / 3, children, ...props }, ref) => (
export const AspectRatio = React.forwardRef(function AspectRatio(
{ ratio = 4 / 3, children, ...props },
ref
) {
return (
<Box
ref={ref}
sx={{
Expand All @@ -18,7 +23,7 @@ export const AspectRatio = React.forwardRef(
/>
<Box
{...props}
__css={{
sx={{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be considering changing the private API a bit here, but does this mean that props.sx is no longer forwarded to the component here? This looks like it'd break in MDX usage

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you're right, sorry had overlooked that, I've re-added the __css API.

position: 'absolute',
top: 0,
right: 0,
Expand All @@ -29,4 +34,4 @@ export const AspectRatio = React.forwardRef(
</Box>
</Box>
)
)
})
31 changes: 19 additions & 12 deletions packages/components/src/Avatar.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
/** @jsx jsx */
import { jsx } from '@theme-ui/core'
import React from 'react'
import { Image } from './Image'

export const Avatar = React.forwardRef(({ size = 48, ...props }, ref) => (
<Image
ref={ref}
width={size}
height={size}
variant="avatar"
{...props}
__css={{
borderRadius: 9999,
}}
/>
))
export const Avatar = React.forwardRef(function Avatar(
{ size = 48, ...props },
ref
) {
return (
<Image
ref={ref}
width={size}
height={size}
variant="avatar"
{...props}
sx={{
borderRadius: 9999,
}}
/>
)
})
45 changes: 27 additions & 18 deletions packages/components/src/Badge.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
/** @jsx jsx */
import { jsx } from '@theme-ui/core'
import React from 'react'
import Box from './Box'
import { useVariant } from './util'

export const Badge = React.forwardRef((props, ref) => (
<Box
ref={ref}
{...props}
__themeKey="badges"
__css={{
display: 'inline-block',
verticalAlign: 'baseline',
fontSize: 0,
fontWeight: 'bold',
whiteSpace: 'nowrap',
px: 1,
borderRadius: 2,
color: 'white',
bg: 'primary',
}}
/>
))
export const Badge = React.forwardRef(function Badge(
{ variant, ...props },
ref
) {
const variation = useVariant('badges', variant)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This word/meaning bothers me, sorry...

Suggested change
const variation = useVariant('badges', variant)
const variantStyle = useVariant('badges', variant)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all good!

return (
<Box
ref={ref}
{...props}
sx={{
display: 'inline-block',
verticalAlign: 'baseline',
fontSize: 0,
fontWeight: 'bold',
whiteSpace: 'nowrap',
px: 1,
borderRadius: 2,
color: 'white',
bg: 'primary',
...variation,
}}
/>
)
})
44 changes: 13 additions & 31 deletions packages/components/src/Box.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,15 @@
import styled from '@emotion/styled'
import { css, get } from '@theme-ui/css'
import { createShouldForwardProp } from '@styled-system/should-forward-prop'
import space from '@styled-system/space'
import color from '@styled-system/color'

const shouldForwardProp = createShouldForwardProp([
...space.propNames,
...color.propNames,
])

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)))

export const Box = styled('div', {
shouldForwardProp,
})(
{
boxSizing: 'border-box',
margin: 0,
minWidth: 0,
},
base,
variant,
space,
color,
sx,
props => props.css
)
/** @jsx jsx */
import { jsx } from '@theme-ui/core'

export const Box = ({ as = 'div', ...props }) => {
return jsx(as, {
sx: {
boxSizing: 'border-box',
margin: 0,
minWidth: 0,
},
...props,
})
}

export default Box
54 changes: 31 additions & 23 deletions packages/components/src/Button.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
/** @jsx jsx */
import { jsx } from '@theme-ui/core'
import React from 'react'
import Box from './Box'
import { useVariant } from './util'

export const Button = React.forwardRef((props, ref) => (
<Box
ref={ref}
as="button"
variant="primary"
{...props}
__themeKey="buttons"
__css={{
appearance: 'none',
display: 'inline-block',
textAlign: 'center',
lineHeight: 'inherit',
textDecoration: 'none',
fontSize: 'inherit',
px: 3,
py: 2,
color: 'white',
bg: 'primary',
border: 0,
borderRadius: 4,
}}
/>
))
export const Button = React.forwardRef(function Button(
{ variant = 'primary', ...props },
ref
) {
const variation = useVariant('buttons', variant)
return (
<Box
ref={ref}
as="button"
{...props}
sx={{
appearance: 'none',
display: 'inline-block',
textAlign: 'center',
lineHeight: 'inherit',
textDecoration: 'none',
fontSize: 'inherit',
px: 3,
py: 2,
color: 'white',
bg: 'primary',
border: 0,
borderRadius: 4,
...variation,
}}
/>
)
})
13 changes: 10 additions & 3 deletions packages/components/src/Card.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
/** @jsx jsx */
import { jsx } from '@theme-ui/core'
import React from 'react'
import Box from './Box'
import { useVariant } from './util'

export const Card = React.forwardRef((props, ref) => (
<Box ref={ref} variant="primary" {...props} __themeKey="cards" />
))
export const Card = React.forwardRef(function Card(
{ variant = 'primary', ...props },
ref
) {
const variation = useVariant('cards', variant)
return <Box ref={ref} {...props} sx={variation} />
})
Loading