0.8.1 • Published 5 months ago

@react-visual/sanity-next v0.8.1

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

@react-visual/sanity-next react-visual

Renders Sanity images and videos into a container using Next.js. Features:

  • Uses next/image to render images
  • Generates responsive images using Sanity CDN
  • Automatically sets aspect ratio, placeholder, and hotspot
  • Videos are lazyloaded (unless priority flag is set)

Install

yarn add @react-visual/sanity-next

Define these .env variables:

NEXT_PUBLIC_SANITY_PROJECT_ID=
NEXT_PUBLIC_SANITY_DATASET=

Usage

You can use explicit image and video props. These are expected to be Sanity image and file objects, respectively, and should have a child field called alt that contains a description of the asset.

import Visual from '@react-visual/sanity-next'

export default function Example({
  sanityImageAsset,
  sanityVideoAsset,
}) {
  return (
    <Visual
      image={ sanityImageAsset }
      video={ sanityVideoAsset }
      sizes='100vw' />
  )
}

Alternatively, you can model a Sanity object containing image, video, and alt fields and use the src shorthand prop:

import Visual from '@react-visual/sanity-next'

export default function Example({ sanityVisualObject }) {
  return <Visual src={ sanityVisualObject } sizes='100vw' />
}

If you de-reference your image objects in groq with a query like:

*[_type == 'article']{
  image: { ..., asset-> }
}

...then the component will read the following properties from your Image automatically:

  • Aspect Ratio
  • Hotspot / Focal Point
  • Placeholder blur data

For more examples, read the Cypress component tests.

Props

Sources

PropTypeDescription
imageSanityImageSourceA Sanity image object.
videoSanityFileSourceA Sanity file object for a video.
srcobjectAn object containg properites of image, video, and alt. This is intended to modeled in Sanity as an object.
placeholderDatastringA Data URL that is rendered before the image loads via next/image's blurDataURL. If you de-reference your images, this will be set automatically.

Layout

PropTypeDescription
expandbooleanMake the Visual fill it's container via CSS using absolute positioning.
aspectnumberForce the Visual to a specific aspect ratio. If you de-reference your images, this will be set automatically to the aspect ratio of the image..
widthnumber, stringA CSS dimension value or a px number.
heightnumber, stringA CSS dimension value or a px number.
fitstringAn object-fit value that is applied to the assets. Defaults to cover.
positionstringAn object-position value. If you de-reference your images, this will be set automatically to the choices made in your image hotspot.

Loading

PropTypeDescription
prioritybooleanSets next/image's priority and videos to not lazy load.
sizesstringSets next/image's sizes prop.

Video

PropTypeDescription
pausedbooleanDisables autoplay of videos. This prop is reactive, unlike the paused property of the html <video> tag. You can set it to true to pause a playing video or set it to false to play a paused video.

Accessibility

PropTypeDescription
altstringSets the alt attribute or aria-label value, depending on asset type.

Theming

PropTypeDescription
classNamestringAdd a custom CSS class.
styleCSSPropertiesAdd additional styles.
0.8.1

5 months ago

0.8.0

5 months ago

0.7.1

5 months ago

0.7.0

5 months ago

0.6.0

6 months ago

0.5.1

7 months ago

0.5.0

7 months ago

0.4.1

7 months ago

0.4.0

7 months ago

0.3.6

8 months ago

0.3.6-alpha.2

8 months ago

0.3.6-alpha.1

8 months ago

0.3.6-alpha.0

8 months ago

0.3.5

8 months ago

0.3.4

9 months ago

0.3.3

9 months ago

0.3.2

9 months ago

0.3.1

9 months ago

0.3.0

9 months ago