Skip to content

Commit

Permalink
feat(TextEditor): Toolbar can be rendered into a Portal
Browse files Browse the repository at this point in the history
  • Loading branch information
johan-fx committed Nov 29, 2023
1 parent e0758ad commit ea025f0
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions packages/editors/src/form/TextEditor/TextEditor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useEffect } from 'react';
import { createPortal } from 'react-dom';
import { EditorContent, useEditor } from '@tiptap/react';
import { Box } from '@bubbles-ui/components';
import PropTypes from 'prop-types';
Expand All @@ -21,7 +22,6 @@ export const TEXT_EDITOR_DEFAULT_PROPS = {

export const TEXT_EDITOR_PROP_TYPES = {
content: PropTypes.string,
library: PropTypes.element,
onChange: PropTypes.func,
onSchemaChange: PropTypes.func,
editorClassname: PropTypes.string,
Expand All @@ -38,6 +38,7 @@ export const TEXT_EDITOR_PROP_TYPES = {
placeholder: PropTypes.string,
readOnly: PropTypes.bool,
children: PropTypes.node,
toolbarPortal: PropTypes.any,
};

const TextEditor = ({
Expand All @@ -53,6 +54,7 @@ const TextEditor = ({
useSchema,
acceptedTags = [],
toolbarPosition,
toolbarPortal,
}) => {
const store = React.useRef({
isFocus: false,
Expand Down Expand Up @@ -183,7 +185,7 @@ const TextEditor = ({
editor.on('transaction', handleTransactions);
return () => editor.off('transaction', handleTransactions);
}
return null;
return () => {};
}, [editor, handleTransactions]);

useEffect(() => {
Expand All @@ -196,6 +198,32 @@ const TextEditor = ({
}
}, [placeholder]);

const ToolbarComponent = React.useMemo(
() =>
toolbarPortal
? () =>
createPortal(
<Toolbar
toolbarLabel={'Toolbar'}
className={toolbarClassname}
toolbarPosition={toolbarPosition}
>
{children}
</Toolbar>,
toolbarPortal,
)
: () => (
<Toolbar
toolbarLabel={'Toolbar'}
className={toolbarClassname}
toolbarPosition={toolbarPosition}
>
{children}
</Toolbar>
),
[toolbarPortal],
);

return (
<Box
ref={ref}
Expand All @@ -207,13 +235,7 @@ const TextEditor = ({
<TextEditorProvider editor={editor} readOnly={readOnly}>
{readOnly ? null : (
<Box style={{ zIndex: 1 }}>
<Toolbar
toolbarLabel={'Toolbar'}
className={toolbarClassname}
toolbarPosition={toolbarPosition}
>
{children}
</Toolbar>
<ToolbarComponent />
<BubbleMenu />
</Box>
)}
Expand Down

0 comments on commit ea025f0

Please sign in to comment.