All Projects → sanity-io → Block Content To React

sanity-io / Block Content To React

Licence: mit
React component for transforming Sanity block content to React components

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Block Content To React

Msgpack4nim
MessagePack serializer/deserializer implementation for Nim / msgpack.org[Nim]
Stars: ✭ 89 (-19.82%)
Mutual labels:  serializer
Uniforms
A React library for building forms from any schema.
Stars: ✭ 1,368 (+1132.43%)
Mutual labels:  react-components
Rdflib
RDFLib is a Python library for working with RDF, a simple yet powerful language for representing information.
Stars: ✭ 1,584 (+1327.03%)
Mutual labels:  serializer
Java
jsoniter (json-iterator) is fast and flexible JSON parser available in Java and Go
Stars: ✭ 1,308 (+1078.38%)
Mutual labels:  serializer
Dataformsjs
🌟 DataFormsJS 🌟 A minimal JavaScript Framework and standalone React and Web Components for rapid development of high quality websites and single page applications.
Stars: ✭ 95 (-14.41%)
Mutual labels:  react-components
Re Jok
A React UI Component library built with styled-components
Stars: ✭ 102 (-8.11%)
Mutual labels:  react-components
Udt
UDT spec, reference library and related packages
Stars: ✭ 89 (-19.82%)
Mutual labels:  serializer
Jafar
🌟!(Just another form application renderer)
Stars: ✭ 107 (-3.6%)
Mutual labels:  react-components
Md Components
Material Design React Components
Stars: ✭ 97 (-12.61%)
Mutual labels:  react-components
React Darkreader
🌓 A React Hook for adding a dark / night mode to your site.
Stars: ✭ 104 (-6.31%)
Mutual labels:  react-components
React Pinpoint
An open source utility library for measuring React component render times.
Stars: ✭ 93 (-16.22%)
Mutual labels:  react-components
React Cassette Player
Simple ReactJS HTML5 audio player component built with SVG icons from The Noun Project.
Stars: ✭ 93 (-16.22%)
Mutual labels:  react-components
React Feather
React component for Feather icons
Stars: ✭ 1,379 (+1142.34%)
Mutual labels:  react-components
Go
A high-performance 100% compatible drop-in replacement of "encoding/json"
Stars: ✭ 10,248 (+9132.43%)
Mutual labels:  serializer
React Cropper
Cropperjs as React component
Stars: ✭ 1,600 (+1341.44%)
Mutual labels:  react-components
Sqlalchemy Serializer
Serrializer for SQLAlchemy models.
Stars: ✭ 89 (-19.82%)
Mutual labels:  serializer
Hprose Delphi
Hprose is a cross-language RPC. This project is Hprose 2.0 for Delphi and FreePascal
Stars: ✭ 100 (-9.91%)
Mutual labels:  serializer
Mui Treasury
A collection of ready-to-use components based on Material-UI
Stars: ✭ 1,821 (+1540.54%)
Mutual labels:  react-components
Ui
Talend's unified web UI repository.
Stars: ✭ 105 (-5.41%)
Mutual labels:  react-components
Seriality
Seriality is a library for serializing and de-serializing all the Solidity types in a very efficient way which mostly written in solidity-assembly
Stars: ✭ 105 (-5.41%)
Mutual labels:  serializer

block-content-to-react

Render an array of block text from Sanity with React or React Native.

Installing

npm install --save @sanity/block-content-to-react

Usage

const React = require('react')
const ReactDOM = require('react-dom')
const BlockContent = require('@sanity/block-content-to-react')
const client = require('@sanity/client')({
  projectId: '<your project id>',
  dataset: '<some dataset>',
  useCdn: true
})

const serializers = {
  types: {
    code: props => (
      <pre data-language={props.node.language}>
        <code>{props.node.code}</code>
      </pre>
    )
  }
}

client.fetch('*[_type == "article"][0]').then(article => {
  ReactDOM.render(
    <BlockContent blocks={article.body} serializers={serializers} />,
    document.getElementById('root')
  )
})

PropTypes

  • className - When more than one block is given, a container node has to be created. Passing a className will pass it on to the container. Note: see renderContainerOnSingleChild.
  • renderContainerOnSingleChild - When a single block is given as input, the default behavior is to not render any container. If you always want to render the container, pass true.
  • serializers - Specifies the React components to use for rendering content. Merged with default serializers.
  • serializers.types - Serializers for block types, see example above
  • serializers.marks - Serializers for marks - data that annotates a text child of a block. See example usage below.
  • serializers.list - React component to use when rendering a list node
  • serializers.listItem - React component to use when rendering a list item node
  • serializers.hardBreak - React component to use when transforming newline characters to a hard break (<br/> by default, pass false to render newline character)
  • serializers.container - Serializer for the container wrapping the blocks
  • imageOptions - When encountering image blocks, this defines which query parameters to apply in order to control size/crop mode etc.

In addition, in order to render images without materializing the asset documents, you should also specify:

  • projectId - The ID of your Sanity project.
  • dataset - Name of the Sanity dataset containing the document that is being rendered.

Examples

Rendering custom marks

const input = [{
  _type: 'block',
  children: [{
    _key: 'a1ph4',
    _type: 'span',
    marks: ['s0m3k3y'],
    text: 'Sanity'
  }],
  markDefs: [{
    _key: 's0m3k3y',
    _type: 'highlight',
    color: '#E4FC5B'
  }]
}]

const highlight = props => {
  return (
    <span style={{backgroundColor: props.mark.color}}>
      {props.children}
    </span>
  )
}

<BlockContent
  blocks={input}
  serializers={{marks: {highlight}}}
/>

Specifying image options

<BlockContent
  blocks={input}
  imageOptions={{w: 320, h: 240, fit: 'max'}}
  projectId="myprojectid"
  dataset="mydataset"
/>

Customizing the default serializer for block-type

This code snippet first checks for a heading style (h1, h2, h3, and so on), and returns a React-element with a custom classname that includes the heading level. It has also defined a how blocks with a blockquote style should be rendered, adding a hyphen in front of it's children. If the block doesn't have any customBlocks defined, it will fall back on the default serializers (BlockContent.defaultSerializers.types.block(props)).

const BlockRenderer = props => {
  const {style = 'normal'} = props.node;
  
  if (/^h\d/.test(style)) {
    const level = style.replace(/[^\d]/g, '')
    return React.createElement(style, { className: `heading-${level}`}, props.children)
  }
  
  if (style === 'blockquote') {
    return <blockquote>- {props.children}</blockquote>
  }
  
  // Fall back to default handling
  return BlockContent.defaultSerializers.types.block(props)
}

<BlockContent
  blocks={input}
  serializers={{types: {block: BlockRenderer}}}
/>

Usage with React Native

React Native support is experimental, but should work. Built-in serializers render using React Native elements instead of HTML tags when React Native is detected, and serializers can be specified as you normally would.

Since there are fewer built-in elements in React Native, elements such as lists are implemented using plain (text) views, styled to look rougly like the HTML variant. You'll probably want to customize most of the serializers when using React Native, since styling is not handled in a cascading fashion.

License

MIT-licensed. See LICENSE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].