diff --git a/packages/react/README.md b/packages/react/README.md index 185c91e..253b502 100644 --- a/packages/react/README.md +++ b/packages/react/README.md @@ -33,7 +33,7 @@ export default function VideoExample() { } ``` -Generate multiple landscape and portrait sources in webp and avif using an image CDN to create a srcset. +Generate multiple landscape and portrait sources using an image CDN to create a srcset. ```jsx import Visual from '@react-visual/react' @@ -42,7 +42,7 @@ export default function ResponsiveExample() { return ( { const height = media?.includes('landscape') ? width * 0.5 : width @@ -58,30 +58,103 @@ export default function ResponsiveExample() { The above would produce: ```html - - - - - - Example of responsive images - +
+ + + + Example of responsive images + +
+``` + +Accept objects from a CMS to produce responsive assets at different aspect ratios. + +```jsx +import Visual from '@react-visual/react' + +export default function ResponsiveExample() { + return ( + { + + // Choose the right source + const asset = media?.includes('landscape') ? + src.landscape : src.portrait + + // Make the dimensions + const dimensions = `${width}x${width / asset.aspect}` + + // Choose the right format + const ext = type?.includes('webp') ? '.webp' : '.jpg' + + // Make the url + return `https://placehold.co/${dimensions}${ext}` + + }} + aspect={({ image, media }) => { + return media?.includes('landscape') ? + image.landscape.aspect : + image.portrait.aspect + }} + alt='Example of responsive images'/> + ) +} +``` + +This produces: + +```html +
+ + + + Example of responsive images + + +
``` For more examples, read [the Cypress component tests](./cypress/component). @@ -92,15 +165,15 @@ For more examples, read [the Cypress component tests](./cypress/component). | Prop | Type | Description | -- | -- | -- -| `image` | `string` | URL to an image asset. -| `video` | `string` | URL to a video asset asset. +| `image` | `string | object` | URL to an image asset. +| `video` | `string | object` | URL to a video asset asset. ### Layout | Prop | Type | Description | -- | -- | -- | `expand` | `boolean` | Make the Visual fill it's container via CSS using absolute positioning. -| `aspect` | `number` | Force the Visual to a specific aspect ratio. +| `aspect` | `number | function` | Force the Visual to a specific aspect ratio. | `width` | `number`, `string` | A CSS dimension value or a px number. | `height` | `number`, `string` | A CSS dimension value or a px number. | `fit` | `string` | An [`object-fit`](https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit) value that is applied to the assets. Defaults to `cover`. diff --git a/packages/react/cypress/component/ReactVisual.cy.tsx b/packages/react/cypress/component/ReactVisual.cy.tsx index fdb5645..caca052 100644 --- a/packages/react/cypress/component/ReactVisual.cy.tsx +++ b/packages/react/cypress/component/ReactVisual.cy.tsx @@ -200,7 +200,7 @@ describe('sources', () => { aspect: 1, } }} - sourceTypes={['image/webp', 'image/jpeg']} + sourceTypes={['image/webp']} sourceMedia={['(orientation: landscape)', '(orientation: portrait)']} imageLoader={({ src, type, media, width }) => {