- Notifications
You must be signed in to change notification settings - Fork1
A collection of elements to use with Slate.
License
diegocouto/react-slate-elements
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
react-slate-elements
is a set of React components to make your life easier when building your own rich text editor using Slate. Check it out:
Install it as any other package:
yarn add react-slate-elements
Now let's see how to put it into action:
importReact,{useState,useCallback}from'react';import{Value}from'slate';import{Editor}from'slate-react';import{EditorSchema,EditorToolbar,Bold}from'react-slate-elements';constplugins=[Bold.Plugin,]constApp=()=>{const[value,setValue]=useState(Value.fromJS(initialValue));const[editor,setEditorRef]=useState(null);constonChangeValue=({ value})=>setValue(value);constonRenderEditor=useCallback(ref=>setEditorRef(ref),[]);constrenderMark=(props,editor,next)=>{switch(props.mark.type){caseBold.TYPE:returnBold.Mark(props);default:returnnext();}}return(<divstyle={{margin:20}}><EditorToolbareditor={editor}><Bold.Button/></EditorToolbar><Editorref={onRenderEditor}schema={EditorSchema}value={value}onChange={onChangeValue}renderMark={renderMark}plugins={plugins}/></div>);}constinitialValue={document:{nodes:[{object:'block',type:'paragraph',nodes:[{object:'text',text:'A line of text in a paragraph.',},],},],},};exportdefaultApp;
Although it's an arguably quite simple example, all the main concepts are present here and you'll be able to easily extend it to fulfill all your requirements.
In order to allow a bold mark to be used we've:
- Added a condition to verify if it should be applied or not on Slate's
renderMark
function. We check if the current node has theBold.TYPE
and, if positive, we render the mark accordingly throughBold.Mark(props)
; - Included the
<Bold.Button />
inside our<EditorToolbar editor={editor}>
, so we'll have a button to toggle this mark; - Added the
Bold.Plugin
into our editors' plugin array. Because of this, we can pressCmd+b
to toggle our bold mark without having to press the toolbar button.
For a complete example, you cancheck the code used on the demonstration page.
UI
Mark
Blocks
Helpers
Plugins
All icons used here are part of theMaterialDesign project.
About
A collection of elements to use with Slate.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.