Socket
Socket
Sign inDemoInstall

react-mobiledoc-hooks

Package Overview
Dependencies
8
Maintainers
3
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-mobiledoc-hooks

Collection of React hooks for Mobiledoc


Version published
Maintainers
3
Install size
3.59 MB
Created

Readme

Source

React hooks for Mobiledoc

Install

yarn add react-mobiledoc-hooks

# or

npm install react-mobiledoc-hooks

Usage

The useMobiledoc hook

Given a document in mobiledoc format, useMobiledoc renders a React list of its sections. A custom render configuration can be also provided.

// RichText.js

import React from 'react'
import { useMobiledoc } from 'react-mobiledoc-hooks'

// Read more bellow in "Customization"
const configuration = {
  atoms: [],
  cards: [],
  markups: [],
  sections: [],
  additionalProps: [],
}

function RichText({ doc, ...props }) {
  const output = useMobiledoc(doc, configuration)

  return (
    <div className="richtext" {...props}>
      {output}
    </div>
  )
}

// App.js

import React from 'react'
import RichText from './RichText'

const mobiledoc = {
  version: '0.3.1',
  atoms: [],
  cards: [],
  markups: [['strong']],
  sections: [
    [
      1,
      'p',
      [
        [0, [], 0, 'This is a '],
        [0, [0], 1, 'useMobiledoc'],
        [0, [], 0, ' demo'],
      ],
    ],
  ],
}

function App() {
  return <RichText doc={mobiledoc} />
}

The output should look like this:

This is a useMobiledoc demo

Customization

The second argument in useMobiledoc sets some options:

  • atoms: array
  • cards: array
  • markups: array
  • sections: array
  • additionalProps: object - Extra props that will be passed to every atom, section and card

atoms, cards, markups and sections require array of objects with these attributes:

  • name: string
  • component: Component|Function

Example for a custom Mention atom:

// Mentions.js
export default function Mention({username, ...props}) {
  return (
    <span className="mention" {...props}>
      @{username}
    </span>
  )
}

// RichText.js
import Mention from './Mention'

const configuration = {
  atoms: [
    {
      name: 'mention'
      component: Mention
    }
  ]
}

function RichText({ doc, ...props }) {
  const output = useMobiledoc(doc, configuration)

  return (
    <div className="richtext" {...props}>
      {output}
    </div>
  )
}

Development

Requirements

Install Dependencies

yarn

Build for production

To build a production version, run:

yarn build

This library is built using Rollup.js

Licence

MIT (see LICENCE file).

© 2019 Stimme der Hoffnung e.V in Germany

FAQs

Last updated on 14 Mar 2019

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc