Coder Social home page Coder Social logo

notion-react's Introduction

Notion React

Notion React allows you to easily map Notion components to your React projects, while completely maintaining customizability to make the components look and behave however you want.

Installation

Use the following command in the root directory of your React project to install the dependency.

npm i notion-react

Important Notes

This library is based around the data structure of the @notionhq/client package, the block list response structure from this library should be used to map components.

Usage

To create a NotionRenderer, which is what we use to map React components to Notion blocks, we can use the makeNotionRenderer function.

You can do this outside the body of the component that will be containing the Notion content.

Then, you can simply pass the components you'd like to map into the convenient, type-safe makeNotionRenderer function.

// Example.tsx

import NotionHeading from "@/components/NotionHeading";
import makeNotionRenderer, { NotionAPIBlockList } from "notion-react";

const { NotionRenderer } = makeNotionRenderer({
  heading_1: NotionHeading,
});

interface ExampleProps {
  blocks: NotionAPIBlockList;
}

const Example: React.FC<ExampleProps> = ({ blocks }) => {
  return <NotionRenderer blocks={blocks} />;
};

export default Example;

We will also need to create our type-safe NotionHeading component, which is what we mapped the heading_1 component to.

// NotionHeading.tsx

import { NotionBlockComponent, NotionBlock } from "notion-react";

const NotionHeading: NotionBlockComponent<NotionBlock.HEADING_1> = (
	props
) => {
  const [text] = props.heading_1.rich_text;
  const { plain_text } = text;
  return <h1>{plain_text}</h1>;
};

export default NotionHeading;

โœจ Bam! Just like that, we have an easy-to-use, type-safe Notion renderer that is FULLY customizable, no default style-overriding shenanigans.

Now every time a heading_1 block appears on the Notion page that the block is originating from, it will appear on our page as the NotionHeading component we created.

notion-react's People

Contributors

ridafkih avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.