Customize the toolbar
The getting started guide introduces the basics of setting up toolbar components. This guide provides some extra context, best practices, and patterns to get you started.
Render the toolbar inside the provider
You must render any toolbars within EditorProvider
, as it requires access to the instance of the editor. This is done with the userEditor
hook.
Use the schema as a foundation
Rather than write each control manually, generate the toolbar based on the schema. You may need to export and import your schema if working in multiple files. Alternatively, you can pass the schema as props to the toolbar.
Here’s an example from the editor playground:
Map over the schema’s categories to generate each section of your toolbar.
Use selectors to reflect editor state
The PTE ships with a collection of helpful selectors. Selectors are pure functions that derive state from the editor context. You can find the full list in the selectors reference.
A few useful selectors for using in the toolbar are:
getActiveStyle
: Get’s the active style of the selection.isActiveDecorator
: Returnstrue
if the active selection matches the decorator.isActiveAnnotation
: Returnstrue
if the active selection matches the annotation.isActiveStyle
: Returnstrue
if the active selection matches the style.
You can import each selector individually from @portabletext/editor/selectors
or import them all.
You can then combine these with the useEditorSelector
hook in your toolbar components. For example, this button will underline if the selected text matches the annotation.
Send events from toolbar items
You can send synthetic events from within the toolbar using editor.send
.