-
Notifications
You must be signed in to change notification settings - Fork 10
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
UX-223 Add Picture Component #637
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React from 'react'; | ||
import { layout, margin, compose } from 'styled-system'; | ||
import styled from 'styled-components'; | ||
import { createPropTypes } from '@styled-system/prop-types'; | ||
import PropTypes from 'prop-types'; | ||
import { pick } from '../../helpers/systemProps'; | ||
import { Box } from '../Box'; | ||
|
||
const system = compose(layout, margin); | ||
const StyledFigure = styled.figure` | ||
${system}; | ||
${({ seeThrough }) => (seeThrough ? 'mix-blend-mode: multiply;' : '')} | ||
`; | ||
|
||
// Picture Component | ||
const Picture = React.forwardRef(function Picture(props, userRef) { | ||
const { children, seeThrough, role, ...rest } = props; | ||
const systemProps = pick(rest, system.propNames); | ||
return ( | ||
<StyledFigure | ||
as="figure" | ||
m="0" | ||
role={role} | ||
ref={userRef} | ||
seeThrough={seeThrough} | ||
{...systemProps} | ||
> | ||
<picture>{children}</picture> | ||
</StyledFigure> | ||
); | ||
}); | ||
|
||
Picture.displayName = 'Picture'; | ||
Picture.propTypes = { | ||
children: PropTypes.node, // For passing in child `<source />` elements - may have additional uses in the future, so not restricting that now | ||
role: PropTypes.string, | ||
seeThrough: PropTypes.bool, | ||
...createPropTypes(margin.propNames), | ||
...createPropTypes(layout.propNames), | ||
}; | ||
|
||
// Picture.Image Component | ||
const Image = React.forwardRef(function Image(props, userRef) { | ||
const { alt, className, src } = props; | ||
return <Box as="img" alt={alt} className={className} src={src} ref={userRef} width="100%" />; | ||
}); | ||
|
||
Image.displayName = 'Picture.Image'; | ||
|
||
Image.propTypes = { | ||
alt: PropTypes.string, | ||
className: PropTypes.string, | ||
src: PropTypes.string.isRequired, | ||
}; | ||
|
||
Image.defaultProps = { | ||
alt: '', | ||
}; | ||
|
||
Picture.Image = Image; | ||
export default Picture; |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as Picture } from './Picture'; |
35 changes: 35 additions & 0 deletions
35
packages/matchbox/src/components/Picture/tests/Picture.test.js
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react'; | ||
import Picture from '../Picture'; | ||
import 'jest-styled-components'; | ||
|
||
describe('Picture Components', () => { | ||
const subject = props => global.mountStyled(<Picture {...props} />); | ||
|
||
it('renders a child image correctly', () => { | ||
let wrapper = subject({ children: <Picture.Image className="test" src="/test.jpg" /> }); | ||
expect(wrapper.find('.test')).toExist(); | ||
expect(wrapper.find('img').props().alt).toEqual(''); | ||
expect(wrapper.find('img').props().src).toEqual('/test.jpg'); | ||
}); | ||
|
||
it('renders a child image with alt correctly', () => { | ||
let wrapper = subject({ children: <Picture.Image alt="test-alt" src="/test.jpg" /> }); | ||
expect(wrapper.find('img').props().alt).toEqual('test-alt'); | ||
}); | ||
|
||
it('renders a role correctly', () => { | ||
let wrapper = subject({ role: 'presentation' }); | ||
expect(wrapper.find('[role="presentation"]')).toExist(); | ||
}); | ||
|
||
it('renders as see through', () => { | ||
let wrapper = subject({ seeThrough: true }); | ||
expect(wrapper.find('figure')).toHaveStyleRule('mix-blend-mode', 'multiply'); | ||
}); | ||
|
||
it('renders system props', () => { | ||
let wrapper = subject({ m: '500px', width: '100px' }); | ||
expect(wrapper.find('figure')).toHaveStyleRule('margin', '500px'); | ||
expect(wrapper.find('figure')).toHaveStyleRule('width', '100px'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React from 'react'; | ||
import { withInfo } from '@storybook/addon-info'; | ||
import { Picture } from '@sparkpost/matchbox/components/Picture'; | ||
import { Box } from '@sparkpost/matchbox/components/Box'; | ||
import Image from '@sparkpost/matchbox-media/images/Accounts.jpg'; | ||
|
||
export default { | ||
title: 'media|Picture', | ||
}; | ||
|
||
export const BasicImage = withInfo({ propTables: [Picture] })(() => ( | ||
<Picture> | ||
<Picture.Image src={Image} /> | ||
</Picture> | ||
)); | ||
|
||
export const MultiplyBackground = withInfo({ propTables: [Picture] })(() => ( | ||
<Box bg="blue.100"> | ||
<Picture seeThrough> | ||
<Picture.Image src={Image} /> | ||
</Picture> | ||
</Box> | ||
)); | ||
|
||
export const SystemProps = withInfo({ propTables: [Picture] })(() => ( | ||
<Picture mx="500" width={['200px', '400px', '500px', '600px']} role="presentation"> | ||
<Picture.Image src={Image} /> | ||
</Picture> | ||
)); |
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.
Does it make sense to set this since we're using
margin
system props too? Could just be a default?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.
Nvm I see what this is doing