Registry / web-framework / slate-react

slate-react

JSON →
library0.124.0jsnpmunverified

slate-react provides the essential React-specific components, hooks, and plugins for building highly customizable rich-text editors with Slate. It integrates the core Slate data model and operations with React's rendering capabilities, leveraging `contenteditable` for a robust editing experience. The current stable version is 0.124.0, which typically releases in conjunction with its core `slate` peer dependency, following a frequent, often weekly, release cadence for minor and patch updates. Key differentiators include its unopinionated core, allowing developers complete control over rendering and behavior, and a powerful plugin-based architecture for extending functionality without modifying the core. This package focuses on flexibility and a declarative approach to content modeling, making it suitable for complex document editing scenarios rather than being an off-the-shelf WYSIWYG solution.

npm install slate-react
INSTALL
IMPORT
SIG · SLATE-REACT
S
slate-react
web-frameworkjavascriptv0.124.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Slate
import { Slate } from 'slate-react'
const Slate = require('slate-react').Slate
The main context provider for a Slate editor instance.
Editable
import { Editable } from 'slate-react'
const Editable = require('slate-react').Editable
The primary component for rendering a Slate editor's content as a `contenteditable` div.
useSlate
import { useSlate } from 'slate-react'
import useSlate from 'slate-react'
A React hook to access the current `Editor` instance from within the `Slate` context. Must be called inside a `Slate` component tree.
withReact
import { withReact } from 'slate-react'
import { withReactEditor } from 'slate-react'
A higher-order function that adds React-specific behaviors (e.g., event handling, `contenteditable` integration) to a Slate editor instance.

Demonstrates a basic Slate editor setup with `Slate` and `Editable` components, including custom leaf rendering and a simple keyboard shortcut for bolding text. This example uses `createEditor` and `withReact` to initialize the editor instance, `useState` for managing the editor's value, and `useMemo` to ensure the editor instance is stable across re-renders.

import React, { useState, useMemo, useCallback } from 'react'; import { createEditor, Editor, Text } from 'slate'; import { Slate, Editable, withReact } from 'slate-react'; const initialValue = [ { type: 'paragraph', children: [{ text: 'This is editable ' }, { text: 'rich', bold: true }, { text: ' text, that you can try out yourself!' }], }, { type: 'paragraph', children: [ { text: 'Try making some words ' }, { text: 'bold', bold: true }, { text: ', ' }, { text: 'italic', italic: true }, { text: ', or even ' }, { text: 'underlined', underline: true }, { text: '.' }, ], }, ]; function MyEditor() { const editor = useMemo(() => withReact(createEditor()), []); const [value, setValue] = useState(initialValue); const renderLeaf = useCallback(({ attributes, children, leaf }) => { if (leaf.bold) { children = <strong>{children}</strong>; } if (leaf.italic) { children = <em>{children}</em>; } if (leaf.underline) { children = <u>{children}</u>; } return <span {...attributes}>{children}</span>; }, []); return ( <Slate editor={editor} value={value} onChange={newValue => setValue(newValue)}> <Editable renderLeaf={renderLeaf} placeholder="Enter some rich text..." onKeyDown={event => { if (event.key === '`' && event.ctrlKey) { event.preventDefault(); Editor.addMark(editor, 'bold', true); } }} /> </Slate> ); } export default MyEditor;
Debug
Known issues
breakingSlate's API is intentionally unopinionated and evolves frequently, particularly in `0.x` releases. Minor version updates can introduce breaking changes due to the project's philosophy of iterating quickly towards a stable v1.0.
fix
Always review the release notes carefully before updating to a new minor version. Consult the official migration guides on the Slate GitHub for specific changes.
affects: >=0.1.0
gotchaStarting from `slate@0.123.0` (and `slate-react@0.123.0`), dedicated type discriminators like `Location.isPath`, `Location.isPoint`, `Location.isRange`, and `Location.isSpan` were added. While `Path.isPath` etc. still exist, the new `Location` methods are more efficient and preferred.
fix
Migrate existing type checks from `Path.isPath` to `Location.isPath`, `Point.isPoint` to `Location.isPoint`, etc., to leverage the more efficient type discriminators.
affects: >=0.123.0
gotchaThe `Slate` component's internal `useEffect` hook now properly includes `editor` as a dependency. In `slate-react@0.124.0`, a fix was applied to address potential issues where editor updates might not propagate correctly if the `editor` instance was not explicitly listed as a dependency, leading to stale closures or unexpected behavior.
fix
Ensure your `editor` instance is stable (e.g., created with `useMemo`) and that any custom hooks or effects relying on the editor instance correctly list it in their dependency arrays to avoid stale closures and ensure reactivity.
affects: <0.124.0
gotchaDue to the nature of `contenteditable`, integrating with external UI libraries or complex DOM manipulations can be challenging. Slate aims to abstract this, but direct DOM manipulation outside of Slate's APIs can lead to unexpected behavior or inconsistencies.
fix
Always use Slate's provided APIs (e.g., `Transforms`, `Editor` methods) to modify the editor's state and content. For custom UI elements, ensure they are rendered outside the `Editable` component or carefully manage their interaction with the editor's DOM to prevent conflicts.
affects: >=0.1.0
Errors
Common errors & fixes
Cannot read properties of undefined (reading 'type') at Editor.normalizeNode
Often caused by an `initialValue` that does not conform to the Slate schema (e.g., missing `type` or `children` properties on elements, or incorrect structure for text nodes).
fix
Ensure your editor's `initialValue` adheres strictly to the Slate document model, where every element has a `type` and `children` array, and every text node has a `text` property, even if empty. Review `Editor.isEditor`, `Element.isElement`, and `Text.isText` for guidance.
Error: `useSlate` must be used inside a <Slate> component.
Attempting to call the `useSlate` hook outside of the React component tree managed by a `<Slate>` provider.
fix
Wrap any component that calls `useSlate` (or `useSelected`, `useFocused`, `useReadOnly`) with the `<Slate>` component. Ensure the hook call occurs within a child component of `<Slate>`.
React Hook "useMemo" cannot be called at the top level. React Hooks must be called in a React function component or a custom React Hook function.
Forgetting to initialize the Slate editor instance (`createEditor()` and `withReact()`) inside a `useMemo` hook within a functional component, leading to a new editor instance on every render.
fix
Always initialize your editor instance using `useMemo` within your functional component to ensure it's stable across renders, e.g., `const editor = useMemo(() => withReact(createEditor()), []);`.
Upgrade
Version history
0.124.0latest on npm
Audit
Dependencies
reactrequiredCore React library for UI components.
react-domrequiredReact DOM rendering utilities.
slaterequiredThe core Slate data model and editor operations.
slate-domrequiredDOM-related utilities for Slate's `contenteditable` integration.
Agent activity
5 hits · last 30 days
node
4
Resources
slate-react — npm install slate-react · libregistry