@@ -5,6 +5,7 @@ import React, {
55 forwardRef ,
66 useImperativeHandle ,
77} from 'react' ;
8+ import { useEditorState } from '@tiptap/react' ;
89import type { Editor } from '@tiptap/react' ;
910import { getMarkRange } from '@tiptap/core' ;
1011import { Button , ButtonSize , ButtonVariant } from '../../buttons/Button' ;
@@ -119,39 +120,52 @@ function RichTextToolbarComponent(
119120 setIsLinkModalOpen ( false ) ;
120121 } , [ ] ) ;
121122
123+ const editorState = useEditorState ( {
124+ editor,
125+ selector : ( { editor : currentEditor } ) => ( {
126+ isBold : currentEditor . isActive ( 'bold' ) ,
127+ isItalic : currentEditor . isActive ( 'italic' ) ,
128+ isBulletList : currentEditor . isActive ( 'bulletList' ) ,
129+ isOrderedList : currentEditor . isActive ( 'orderedList' ) ,
130+ isLink : currentEditor . isActive ( 'link' ) ,
131+ canUndo : currentEditor . can ( ) . undo ( ) ,
132+ canRedo : currentEditor . can ( ) . redo ( ) ,
133+ } ) ,
134+ } ) ;
135+
122136 return (
123137 < >
124138 < div className = "flex items-center gap-1 border-b border-border-subtlest-tertiary p-2" >
125139 < ToolbarButton
126140 tooltip = "Bold (⌘B)"
127141 icon = { < BoldIcon /> }
128- isActive = { editor . isActive ( 'bold' ) }
142+ isActive = { editorState . isBold }
129143 onClick = { ( ) => editor . chain ( ) . focus ( ) . toggleBold ( ) . run ( ) }
130144 />
131145 < ToolbarButton
132146 tooltip = "Italic (⌘I)"
133147 icon = { < ItalicIcon /> }
134- isActive = { editor . isActive ( 'italic' ) }
148+ isActive = { editorState . isItalic }
135149 onClick = { ( ) => editor . chain ( ) . focus ( ) . toggleItalic ( ) . run ( ) }
136150 />
137151 < div className = "mx-1 h-4 w-px bg-border-subtlest-tertiary" />
138152 < ToolbarButton
139153 tooltip = "Bullet list (⌘⇧8)"
140154 icon = { < BulletListIcon /> }
141- isActive = { editor . isActive ( 'bulletList' ) }
155+ isActive = { editorState . isBulletList }
142156 onClick = { ( ) => editor . chain ( ) . focus ( ) . toggleBulletList ( ) . run ( ) }
143157 />
144158 < ToolbarButton
145159 tooltip = "Numbered list (⌘⇧7)"
146160 icon = { < NumberedListIcon /> }
147- isActive = { editor . isActive ( 'orderedList' ) }
161+ isActive = { editorState . isOrderedList }
148162 onClick = { ( ) => editor . chain ( ) . focus ( ) . toggleOrderedList ( ) . run ( ) }
149163 />
150164 < div className = "mx-1 h-4 w-px bg-border-subtlest-tertiary" />
151165 < ToolbarButton
152- tooltip = { editor . isActive ( 'link' ) ? 'Edit link (⌘K)' : 'Add link (⌘K)' }
166+ tooltip = { editorState . isLink ? 'Edit link (⌘K)' : 'Add link (⌘K)' }
153167 icon = { < LinkIcon /> }
154- isActive = { editor . isActive ( 'link' ) }
168+ isActive = { editorState . isLink }
155169 onClick = { openLinkModal }
156170 />
157171 { inlineActions && (
@@ -160,22 +174,22 @@ function RichTextToolbarComponent(
160174 < div className = "flex items-center gap-1" > { inlineActions } </ div >
161175 </ >
162176 ) }
163- { ( editor . can ( ) . undo ( ) || editor . can ( ) . redo ( ) ) && (
177+ { ( editorState . canUndo || editorState . canRedo ) && (
164178 < div className = "mx-1 h-4 w-px bg-border-subtlest-tertiary" />
165179 ) }
166180 < ToolbarButton
167181 tooltip = "Undo (⌘Z)"
168182 icon = { < UndoIcon /> }
169183 isActive = { false }
170184 onClick = { ( ) => editor . chain ( ) . focus ( ) . undo ( ) . run ( ) }
171- disabled = { ! editor . can ( ) . undo ( ) }
185+ disabled = { ! editorState . canUndo }
172186 />
173187 < ToolbarButton
174188 tooltip = "Redo (⌘⇧Z)"
175189 icon = { < RedoIcon /> }
176190 isActive = { false }
177191 onClick = { ( ) => editor . chain ( ) . focus ( ) . redo ( ) . run ( ) }
178- disabled = { ! editor . can ( ) . redo ( ) }
192+ disabled = { ! editorState . canRedo }
179193 />
180194 { rightActions && (
181195 < div className = "ml-auto flex items-center gap-1" > { rightActions } </ div >
0 commit comments