-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from bensmithett/images
Responsive images
- Loading branch information
Showing
10 changed files
with
134 additions
and
2 deletions.
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
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,6 @@ | ||
export function Image({ src: importedImage, ...imgProps }) { | ||
if (typeof importedImage === 'string') return <img src={importedImage} {...imgProps} /> | ||
|
||
const { srcset, ...imgPropsFromImport } = importedImage[importedImage.length - 1] | ||
return <img srcSet={srcset} {...imgPropsFromImport} {...imgProps} /> | ||
} |
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,28 @@ | ||
import { Image } from './Image' | ||
import { useFela } from 'react-fela' | ||
import singleFormat from '../gunayala.jpg?preset=singleFormatExample' | ||
import singleFormatSrc from '../gunayala.jpg?preset=singleFormatExample&src' | ||
import singleFormatSrcset from '../gunayala.jpg?preset=singleFormatExample&srcset' | ||
import multiFormat from '../gunayala.jpg?preset=multiFormatExample' | ||
|
||
export default { title: 'tropical/Image' } | ||
|
||
export const Unstyled = () => <Image src={singleFormat} width='500' /> | ||
|
||
export const Styled = () => { | ||
const { css } = useFela() | ||
return ( | ||
<Image | ||
src={singleFormat} | ||
className={css({ width: '100%', border: '2px dashed rebeccapurple' })} | ||
alt='Guna Yala' | ||
width='500' | ||
/> | ||
) | ||
} | ||
|
||
export const ManualSrcAndSrcset = () => ( | ||
<Image src={singleFormatSrc} srcSet={singleFormatSrcset} width='500' /> | ||
) | ||
|
||
export const IgnoresRedundantSources = () => <Image src={multiFormat} width='500' /> |
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 * from './Image' |
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,22 @@ | ||
export function Picture({ | ||
src: importedImage, | ||
children = (imgProps) => <img {...imgProps} />, | ||
...pictureProps | ||
}) { | ||
if (typeof importedImage === 'string') | ||
throw new Error( | ||
`A string 'src' was passed to <Picture>. If you don't need multiple <source>'s, use <Image> instead.` | ||
) | ||
|
||
return ( | ||
<picture {...pictureProps}> | ||
{importedImage.map(({ srcset, ...sourceOrImgProps }, i) => | ||
i < importedImage.length - 1 ? ( | ||
<source srcSet={srcset} {...sourceOrImgProps} key={i} /> | ||
) : ( | ||
children({ srcSet: srcset, ...sourceOrImgProps, key: i }) | ||
) | ||
)} | ||
</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,48 @@ | ||
import { Picture } from './Picture' | ||
import { useFela } from 'react-fela' | ||
import multiFormat from '../gunayala.jpg?preset=multiFormatExample' | ||
import multiFormatSrc from '../gunayala.jpg?preset=multiFormatExample&src' | ||
import multiFormatSrcset from '../gunayala.jpg?preset=multiFormatExample&srcset' | ||
|
||
export default { title: 'tropical/Picture' } | ||
|
||
export const DefaultImg = () => <Picture src={multiFormat} /> | ||
|
||
export const StyledImg = () => { | ||
const { css } = useFela() | ||
return ( | ||
<Picture src={multiFormat}> | ||
{(imgProps) => ( | ||
<img | ||
{...imgProps} | ||
className={css({ | ||
maxWidth: '100%', | ||
border: '2px dashed rebeccapurple' | ||
})} | ||
width='500' | ||
/> | ||
)} | ||
</Picture> | ||
) | ||
} | ||
|
||
export const StyledPictureAndImg = () => { | ||
const { css } = useFela() | ||
return ( | ||
<Picture | ||
src={multiFormat} | ||
className={css({ display: 'block', border: '5px dashed chartreuse' })} | ||
> | ||
{(imgProps) => ( | ||
<img | ||
{...imgProps} | ||
className={css({ | ||
maxWidth: '100%', | ||
border: '2px dashed rebeccapurple' | ||
})} | ||
width='500' | ||
/> | ||
)} | ||
</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 * from './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,22 @@ | ||
import { widthPreset } from 'vite-plugin-image-presets' | ||
|
||
// Presets for https://github.com/ElMassimo/vite-plugin-image-presets | ||
// See https://tropical.js.org/docs/images | ||
// and stories for tropical/Image & tropical/Picture for usage examples. | ||
|
||
export default { | ||
multiFormatExample: widthPreset({ | ||
widths: [200, 500], | ||
formats: { | ||
webp: { quality: 50 }, | ||
jpg: { quality: 70 } | ||
} | ||
}), | ||
|
||
singleFormatExample: widthPreset({ | ||
widths: [200, 500], | ||
formats: { | ||
original: { quality: 80 } | ||
} | ||
}) | ||
} |
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