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

slate-react

Package Overview
Dependencies
Maintainers
6
Versions
826
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

slate-react

Tools for building completely customizable richtext editors with React.

  • 0.111.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
844K
increased by5.13%
Maintainers
6
Weekly downloads
 
Created

What is slate-react?

The slate-react package is a highly customizable framework for building rich text editors in React applications. It provides a set of React components and hooks that allow developers to create complex text editing experiences with ease.

What are slate-react's main functionalities?

Basic Editor Setup

This code sets up a basic Slate editor with a single paragraph of text. The `Slate` component provides the context for the editor, and the `Editable` component renders the editable area.

import React, { useMemo } from 'react';
import { Slate, Editable, withReact } from 'slate-react';
import { createEditor } from 'slate';

const BasicEditor = () => {
  const editor = useMemo(() => withReact(createEditor()), []);
  const initialValue = [{ type: 'paragraph', children: [{ text: 'A line of text in a paragraph.' }] }];

  return (
    <Slate editor={editor} value={initialValue} onChange={value => {}}>
      <Editable />
    </Slate>
  );
};

export default BasicEditor;

Custom Elements

This code demonstrates how to create custom elements in the Slate editor. In this example, a custom `quote` element is defined and rendered as a blockquote.

import React, { useMemo } from 'react';
import { Slate, Editable, withReact } from 'slate-react';
import { createEditor } from 'slate';

const CustomElement = ({ attributes, children, element }) => {
  switch (element.type) {
    case 'quote':
      return <blockquote {...attributes}>{children}</blockquote>;
    default:
      return <p {...attributes}>{children}</p>;
  }
};

const CustomEditor = () => {
  const editor = useMemo(() => withReact(createEditor()), []);
  const initialValue = [{ type: 'quote', children: [{ text: 'A line of text in a quote.' }] }];

  return (
    <Slate editor={editor} value={initialValue} onChange={value => {}}>
      <Editable renderElement={props => <CustomElement {...props} />} />
    </Slate>
  );
};

export default CustomEditor;

Custom Leaf Nodes

This code shows how to create custom leaf nodes in the Slate editor. In this example, a custom `bold` leaf is defined and rendered as a strong element.

import React, { useMemo } from 'react';
import { Slate, Editable, withReact } from 'slate-react';
import { createEditor } from 'slate';

const CustomLeaf = ({ attributes, children, leaf }) => {
  if (leaf.bold) {
    children = <strong>{children}</strong>;
  }
  return <span {...attributes}>{children}</span>;
};

const CustomLeafEditor = () => {
  const editor = useMemo(() => withReact(createEditor()), []);
  const initialValue = [{ type: 'paragraph', children: [{ text: 'A bold line of text.', bold: true }] }];

  return (
    <Slate editor={editor} value={initialValue} onChange={value => {}}>
      <Editable renderLeaf={props => <CustomLeaf {...props} />} />
    </Slate>
  );
};

export default CustomLeafEditor;

Other packages similar to slate-react

Keywords

FAQs

Package last updated on 01 Nov 2024

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