Inline Image Block


The Inline BlockImage field represents an image input. This field supports drag and drop upload, or via clicking on the image to select media from the local filesystem.

Definition

Below is a simple example of how BlockImage could be implemented in a Block Component definition.

import { BlocksControls, BlockImage } from 'react-tinacms-inline'

export function Image({ index }) {
  return (
    <BlocksControls index={index}>
      <BlockImage
        name="src"
        parse={filename => `/img/${filename}`}
        uploadDir={() => '/public/img/'}
      />
    </BlocksControls>
  )
}

There are two ways to use BlockImage, with and without children. If no children are passed, BlockImage will render a default img element. However, you may want more control over the image behavior, in which case you can pass children to Block Image.

Tip: Reference this example to see how alt text metadata could be configured via the Settings Modal.

Options

KeyDescription
nameThe path to some value in the data being edited.
parseDefines how the actual front matter or data value gets populated. The name of the file gets passed as an argument, and one can set the path this image as defined by the uploadDir property.
uploadDirDefines the upload directory for the image. All of the post data is passed in, fileRelativePath is most useful in defining the upload directory, but you can also statically define the upload directory.
previewSrcDefines the path for the src attribute on the image preview. If using gatsby-image, the path to the childImageSharp.fluid.src needs to be provided.
childrenAny child elements.

Interface

export interface InlineImageProps {
  name: string
  parse(filename: string): string
  uploadDir(form: Form): string
  previewSrc(formValues: any): string
  children?: any
}

Example

Below is an example of how you could pass children to BlockImage to work with Gatsby Image. Notice how children need to be passed via render props. Read more on proper image paths in Gatsby to get context on the parse & uploadDir configuration.

import { BlocksControls, BlockImage } from 'react-tinacms-inline'
import Img from 'gatsby-image'

// Using BlockImage with Gatsby Image
export function Image({ data, index }) {
  return (
    <BlocksControls index={index}>
      <BlockImage
        name="thumbnail"
        parse={filename => (filename ? `./${filename}` : null)}
        uploadDir={blogPost => {
          // Upload the image to the same directory as the source file
          const postPathParts = blogPost.initialValues.fileRelativePath.split(
            '/'
          )
          const postDirectory = postPathParts
            .splice(0, postPathParts.length - 1)
            .join('/')

          return postDirectory
        }}
        previewSrc={formValues => {
          return formValues.blocks[index].src
        }}
      >
        {props => (
          <Img
            fluid={data.thumbnail.childImageSharp.fluid}
            alt={data.alt}
            {...props}
          />
        )}
      </BlockImage>
    </BlocksControls>
  )
}