Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-portable-text

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-portable-text

An easy way to render Portable Text block content in React applications.

  • 0.4.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.8K
decreased by-21%
Maintainers
1
Weekly downloads
 
Created
Source

React Portable Text

An easy way to render Portable Text block content in React applications.

Quick Example

yarn add react-portable-text
import PortableText from "react-portable-text"

const YourComponent = ({ portableTextContent }) => (
  <div>
    <PortableText
      // Pass in block content straight from Sanity.io
      content={portableTextContent}

      // Optionally override marks, decorators, blocks, etc. in a flat
      // structure without doing any gymnastics
      serializers={{
        h1: props => <h1 style={{ color: "red" }} {...props} />,
        li: ({ children }) => <li className="special-list-item">{children}</li>,
        someCustomType: YourComponent,
      }}
    />
  </div>
)

Why not just use @sanity/block-content-to-react directly?

I found it difficult to create abstractions on top of @sanity/block-content-to-react. Remembering whether a serializer needed to be codified as a type, a mark, or as something under block was challenging, and the special treatment for lists and list items was confusing. Further, the props being wrapped in an object under the node property, or extraneous props for mark types meant I was creating intermediate component types just to avoid passing invalid props to the React elements (otherwise they render in the DOM).

React Portable Text uses @sanity/block-content-to-react under the hood, but maps each of these types to the correct place in the serializers for you and normalizing props to match the fields supplied by users in your Sanity Studio, simplifying the cognitive load required to author new ones.

Serializer Documentation

React Portable Text maps the following types explicitly, and treats all other properties of the serializers object as custom types. Custom types are used for both type and block blocks (i.e. custom marks as well as custom block-level insertion types).

SerializerNotes
Marks
linkAll link marks used for anchor links
strongBold/strong text
emEmphasized/italic text
underlineUnderlined text
delText with strikethrough styles
codeInline text with code styling
Lists
ulUnordered lists
olOrdered lists
liList items for any type of list
Blocks
h1Heading level 1
h2Heading level 2
h3Heading level 3
h4Heading level 4
h5Heading level 5
h6Heading level 6
normalParagraph styles
blockquoteBlockquote styles

Additional Props

Additional props are passed through to @sanity/block-content-to-react, so if you want to configure imageOptions or set the projectId and dataset options you can just pass them directly to React Portable Text:

<PortableText
  content={blockContent}
  projectId={process.env.SANITY_PROJECT_ID}
  dataset={process.env.SANITY_DATASET}
/>

Rendering Plain Text

As a bonus, react-portable-text offers a function that will render your portable text content to a plaintext string. This is often useful for previews and such in the Studio and for ancillary uses of content in contexts where formatting is not supported (e.g. calendar invite descriptions, meta tags, etc.).

import { blockContentToPlainText } from "react-portable-text"

const MetaDescription = ({ content }) => (
  <meta name="description" content={blockContentToPlainText(content)} />
)

Contributing

Did I miss something? Is something not compatible with your setup? Open an issue with details, and if possible, a CodeSandbox reproduction. Pull requests are also welcomed!

FAQs

Package last updated on 27 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc