-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce selection toolbar rerenders (#291)
- Loading branch information
Showing
6 changed files
with
419 additions
and
389 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import React, { MouseEvent } from 'react'; | ||
import { | ||
createPluginFactory, | ||
isRangeInSameBlock, | ||
isSelectionExpanded, | ||
useEditorReadOnly, | ||
useEditorState, | ||
} from '@udecode/plate'; | ||
import { useSlateSelector } from 'slate-react'; | ||
import { FloatingToolbar, FloatingToolbarItem } from './FloatingToolbar'; | ||
import { | ||
formattingButtonClassNames, | ||
useInlineFormattingButtons, | ||
} from './FormattingToolbar'; | ||
|
||
const SelectionToolbar = () => { | ||
const open = useSlateSelector( | ||
(editor) => | ||
isSelectionExpanded(editor as any) && | ||
(isRangeInSameBlock(editor as any) ?? false), | ||
/** | ||
* If open is true, we want to rerender every time the selection changes so | ||
* that we keep the toolbar position up to date. If open is false, we only | ||
* want to rerender when it becomes true. | ||
*/ | ||
(prevOpen, newOpen) => !prevOpen && !newOpen | ||
); | ||
|
||
const readOnly = useEditorReadOnly(); | ||
if (readOnly) return null; | ||
|
||
return ( | ||
<FloatingToolbar | ||
open={open} | ||
items={open && <SelectionToolbarInner />} | ||
tippyProps={{ | ||
getReferenceClientRect: () => { | ||
const selection = window.getSelection()!; | ||
const range = selection.getRangeAt(0); | ||
return range.getBoundingClientRect(); | ||
}, | ||
}} | ||
containerProps={ | ||
{ | ||
'data-testid': 'selection-toolbar', | ||
} as any | ||
} | ||
/> | ||
); | ||
}; | ||
|
||
const SelectionToolbarInner = () => { | ||
const editor = useEditorState(); | ||
|
||
const formattingButtons = useInlineFormattingButtons(editor).filter( | ||
({ disabled }) => !disabled | ||
); | ||
|
||
return ( | ||
<> | ||
{formattingButtons.map(({ label, icon, active, onClick }) => ( | ||
<FloatingToolbarItem | ||
key={label} | ||
icon={icon} | ||
label={label} | ||
className={formattingButtonClassNames} | ||
data-active={active} | ||
onClick={onClick} | ||
onMouseDown={(event: MouseEvent) => event.preventDefault()} | ||
/> | ||
))} | ||
</> | ||
); | ||
}; | ||
|
||
export const createSelectionToolbarPlugin = createPluginFactory({ | ||
key: 'selectionToolbar', | ||
renderAfterEditable: SelectionToolbar, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.