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 all 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
40 changes: 23 additions & 17 deletions packages/components/src/Alert.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
/** @jsx jsx */
import { jsx } from '@theme-ui/core'
import React from 'react'
import { useVariant } from './util'
import Box from './Box'

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 variantStyle = useVariant('alerts', variant)
return (
<Box
ref={ref}
{...props}
__css={{
display: 'flex',
alignItems: 'center',
px: 3,
py: 2,
fontWeight: 'bold',
color: 'white',
bg: 'primary',
borderRadius: 4,
...variantStyle,
}}
/>
)
})
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}
__css={{
objectFit: 'cover',
}}
/>
</AspectRatio>
)
})
15 changes: 10 additions & 5 deletions packages/components/src/AspectRatio.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
/** @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={{
__css={{
position: 'relative',
overflow: 'hidden',
}}>
<Box
sx={{
__css={{
width: '100%',
height: 0,
paddingBottom: 100 / ratio + '%',
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}
__css={{
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 variantStyle = useVariant('badges', variant)
return (
<Box
ref={ref}
{...props}
__css={{
display: 'inline-block',
verticalAlign: 'baseline',
fontSize: 0,
fontWeight: 'bold',
whiteSpace: 'nowrap',
px: 1,
borderRadius: 2,
color: 'white',
bg: 'primary',
...variantStyle,
}}
/>
)
})
43 changes: 13 additions & 30 deletions packages/components/src/Box.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
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'
/** @jsx jsx */
import { jsx } from '@theme-ui/core'

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
)
export const Box = ({ as = 'div', __css, ...props }) => {
return jsx(as, {
sx: {
boxSizing: 'border-box',
margin: 0,
minWidth: 0,
...__css,
},
...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 variantStyle = useVariant('buttons', variant)
return (
<Box
ref={ref}
as="button"
{...props}
__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,
...variantStyle,
}}
/>
)
})
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 variantStyle = useVariant('cards', variant)
return <Box ref={ref} {...props} sx={variantStyle} />
})
22 changes: 14 additions & 8 deletions packages/components/src/Checkbox.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/** @jsx jsx */
import { jsx } from '@theme-ui/core'
import React from 'react'
import Box from './Box'
import SVG from './SVG'
import { useVariant } from './util'

const CheckboxChecked = props => (
<SVG {...props}>
Expand Down Expand Up @@ -37,30 +40,32 @@ const CheckboxIcon = props => (
</React.Fragment>
)

export const Checkbox = React.forwardRef(
({ className, sx, variant = 'checkbox', ...props }, ref) => (
<Box>
export const Checkbox = React.forwardRef(function Checkbox(
{ className, variant = 'checkbox', ...props },
ref
) {
const variantStyle = useVariant('forms', variant)
return (
<Box sx={variantStyle.container}>
<Box
ref={ref}
as="input"
type="checkbox"
{...props}
sx={{
__css={{
position: 'absolute',
opacity: 0,
zIndex: -1,
width: 1,
height: 1,
overflow: 'hidden',
...(variantStyle.input && { ...variantStyle.input }),
}}
/>
<Box
as={CheckboxIcon}
aria-hidden="true"
__themeKey="forms"
variant={variant}
className={className}
sx={sx}
__css={{
mr: 2,
borderRadius: 4,
Expand All @@ -72,8 +77,9 @@ export const Checkbox = React.forwardRef(
color: 'primary',
bg: 'highlight',
},
...(variantStyle.icon && { ...variantStyle.icon }),
}}
/>
</Box>
)
)
})
Loading