Getting started
This guide leads you through installing and using the basic features of the Portable Text Editor (PTE).
In order to set up an editor, you’ll need to:
- Create a schema that defines the rich text and block content elements.
- Create a toolbar to toggle and insert these elements.
- Write render functions to style and display each element type in the editor.
- Render the editor.
Parts of the editor
Before starting, it’s helpful to understand the components that make up the editor.
- Schema: A description of the type of content the editor accepts. Think of this as the foundation for configuring the editor.
EditorProvider
: Supplies the schema and initial state to the editor.EventListenerPlugin
: Allows you to listen to events emitted by the editor and act on them. Commonly used to update application state.- Toolbars: Toolbars allow you to create UI elements that interact with the editor.
PortableTextEditable
: The core editor component. It handles the rendering of text and manages behavior.
Add the library to your project
Start by installing the editor to your project
Next, import EditorProvider
, EventListenerPlugin
, PortableTextEditable
, defineSchema
, and the types in the code below.
You won’t need all of these right away, but you can add them now.
Define your schema
Before you can render the editor, you need a schema. The editor schema configures the types of content rendered by the editor.
We’ll start with a schema that includes some common rich text elements.
Render the editor
With a schema defined, you have enough to render the editor. It won’t do much yet, but you can confirm your progress.
Add react
and useState
, then scaffold out a basic application component. For example:
Include the App
component in your application and run it. You should see an outlined editor that accepts text, but doesn’t do much else.
Create render functions for schema elements
At this point the PTE only has a schema, but it doesn’t know how to render anything. Fix that by creating render functions for each property in the schema.
Start by creating a render function for styles.
Render functions all follow the same format.
- They take in props and return JSX elements.
- They use the schema to make decisions.
- They return JSX and pass
children
as a fallback.
With this in mind, continue for the remaining schema types.
Create a render function for decorators.
Update the PortableTextEditable
with each corresponding function to attach them to the editor.
You may notice that we skipped a few types from the schema. Declare these inline in the configuration, like in the code below.
Before you can see if anything changed, you need a way to interact with the editor.
Create a toolbar
A toolbar is a collection of UI elements for interacting with the editor. The PTE library gives you the necessary hooks to create a toolbar however you like.
- Create a
Toolbar
component in the same file. - Import the
useEditor
hook, and declare aneditor
constant in the component. - Iterate over the schema types to create toggle buttons for each style and decorator.
- Send events to the editor to toggle the styles and decorators whenever the buttons are clicked.
- Render the toolbar buttons.
The useEditor
hook gives you access to the active editor. send
lets you send events to the editor. You can view the full list of events in the Behavior API reference.
Bring it all together
With render functions created and a toolbar in place, you can fully render the editor. Add the Toolbar
inside the EditorProvider
.
You can now enter text and interact with the toolbar buttons to toggle the styles and decorators. These are only a small portion of the types of things you can do. Check out the custom rendering guide and the toolbar customization guide for options.
View the Portable Text data
You can preview the Portable Text from the editor by reading the state. Add the following after the EditorProvider
.
This displays the raw Portable Text. To customize how Portable Text renders in your apps, explore the collection of serializers.
Behavior API
The Behavior API is a new way of interfacing with the Portable Text Editor. It allows you to think of and treat the editor as a state machine by:
- Declaratively hooking into editor events and defining new behaviors.
- Imperatively triggering events.
- Deriving editor state using pure functions.
- Subscribing to emitted editor events.
Learn more about the Behaviors and how to create your own behaviors in the documentation.