-
Notifications
You must be signed in to change notification settings - Fork 673
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
Closed
New variant API #823
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1ae38e1
add display names to components
dburles ace6549
refactor to useVariant hook
dburles 93170bd
return empty object if no variant match
dburles 930c06c
rename
dburles 22b0b98
update docs theme
dburles c886237
__css
dburles 4a93012
Alert uses Box
dburles File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}} | ||
/> | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}} | ||
/> | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This word/meaning bothers me, sorry...
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||||||
}} | ||||||
/> | ||||||
) | ||||||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}} | ||
/> | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} /> | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 usageThere was a problem hiding this comment.
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.