npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lyra/block-content-to-react

v0.3.0

Published

React component for transforming Lyra block content to React components

Downloads

25

Readme

block-content-to-react

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

Installing

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

Usage

const React = require('react')
const ReactDOM = require('react-dom')
const BlockContent = require('@lyra/block-content-to-react')
const client = require('@lyra/client')({
  dataset: '<some dataset>'
})

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:

  • dataset - Name of the Lyra dataset containing the document that is being rendered.

Examples

Rendering custom marks

const input = [{
  _type: 'block',
  children: [{
    _key: 'a1ph4',
    _type: 'span',
    marks: ['s0m3k3y'],
    text: 'Lyra'
  }],
  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'}}
  dataset="mydataset"
/>

Customizing default serializer for block-type

const BlockRenderer = props => {
  const style = props.node.style || 'normal'

  if (/^h\d/.test(style)) {
    const level = style.replace(/[^\d]/g, '')
    return <h2 className={`my-heading level-${level}`}>{props.children}</h2>
  }

  return style === 'blockquote'
    ? <blockquote className="my-block-quote">{props.children}</blockquote>
    : <p className="my-paragraph">{props.children}</p>
}

<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.