diff --git a/README.md b/README.md index 2f545c0..d57d887 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,32 @@ A monorepo hosting components for rendering image and video in a single containe ## Examples +Using `sourceTypes` and `sourceMedia` with `imageLoader` to produce multiple `` tags with `srcset` attributes. + +```jsx +import Visual from '@react-visual/react' + +export default function ResponsiveExample() { + return ( + { + const ext = type?.includes("webp") ? ".webp" : ".jpg"; + const height = media?.includes("landscape") ? width * 0.5 : width; + return `https://placehold.co/${width}x${height}${ext}`; + }} + aspect={300 / 150} + sizes="100vw" + alt="Example of responsive images" + /> + ) +} +``` + +[View CodeSandbox demo](https://codesandbox.io/p/sandbox/react-visual-react-demo-w4sh62) + ### @react-visual/next Using framework adapter for Next.js: diff --git a/packages/react/README.md b/packages/react/README.md index 926dcbe..afa723e 100644 --- a/packages/react/README.md +++ b/packages/react/README.md @@ -41,22 +41,49 @@ import Visual from '@react-visual/react' export default function ResponsiveExample() { return ( { - const ext = type?.includes('webp') ? '.webp' : '' - const height = media?.includes('landscape') ? - width * 0.5 : width + imageLoader={({ src, type, media, width }) => { + const height = media?.includes('landscape') ? width * 0.5 : width + const ext = type?.includes('webp') ? '.webp' : '.jpg' return `https://placehold.co/${width}x${height}${ext}` }} - aspect={300/150} - sizes='100vw' - alt='Example of responsive images' /> + width='100%' + alt=''/> ) } ``` +The above would produce: + +```html + + + + + + Example of responsive images + +``` + For more examples, read [the Cypress component tests](./cypress/component). ## Props diff --git a/packages/react/cypress/component/ReactVisual.cy.tsx b/packages/react/cypress/component/ReactVisual.cy.tsx index 0f31e00..c4359bb 100644 --- a/packages/react/cypress/component/ReactVisual.cy.tsx +++ b/packages/react/cypress/component/ReactVisual.cy.tsx @@ -151,7 +151,7 @@ describe('sources', () => { cy.mount( { @@ -159,7 +159,7 @@ describe('sources', () => { const height = media?.includes('landscape') ? width * 0.5 : width - const ext = type?.includes('webp') ? '.webp' : '' + const ext = type?.includes('webp') ? '.webp' : '.jpg' return `https://placehold.co/${width}x${height}${ext}` }} width='100%'