0.8.1 • Published 5 months ago

@react-visual/contentful v0.8.1

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

@react-visual/contentful react-visual

Renders Contentful images and videos into a container. Features:

  • Automatically defines a loader functions for generating srcsets
  • Supports responsive image and video assets

Install

yarn add @react-visual/contentful

Usage

Asset fields

import Visual from '@react-visual/contentful'

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

Where image and video are asset fields defined by these GQL fragments:

fragment image on Asset {
  title
  description
  fileName
  width
  height
  url
}

fragment video on Asset {
  title
  description
  fileName
  url
}

Visual entryType reference

This is the expected pattern for rendering responsive images and videos.

import Visual from '@react-visual/contentful'

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

Where background is defined by this GQL fragment (this consumes the previous fragments):

fragment visual on Visual {
  image { ...image }
  portraitImage { ...image }
  video { ...video }
  portraitVideo { ...video }
  alt
}

For more examples, read the Cypress component tests.

Props

Sources

PropTypeDescription
imageobjectA Contentful image Asset.
videoobjectA Contentful video Asset.
srcobjectAn object with keys of responsive keys. See examples above.

Layout

PropTypeDescription
expandbooleanMake the Visual fill it's container via CSS using absolute positioning.
aspectnumberForce the Visual to a specific aspect ratio. If empty, this will be set using width and height fields from Contentful queries.
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.

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