Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DEV-15524] Fix - Make sure FloatingSnippetInput is not re-rendering if not needed #577

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { DocumentNode } from '@prezly/slate-types';
import type { SlateEditor } from '@udecode/plate-common';
import { useState } from 'react';

import { useFunction } from '#lib';

import { EventsEditor } from '#modules/events';

interface State {
Expand All @@ -13,29 +15,29 @@ interface Actions {
close: () => void;
open: () => void;
rootClose: () => void;
submit: (node: DocumentNode) => Promise<void>;
submit: (node: DocumentNode) => void;
}

export function useFloatingSnippetInput(editor: SlateEditor): [State, Actions] {
const [isOpen, setIsOpen] = useState<boolean>(false);
const savedSelection = useSavedSelection();

function close() {
const close = useFunction(() => {
savedSelection.restore(editor, { focus: true });
setIsOpen(false);
}
});

function rootClose() {
const rootClose = useFunction(() => {
setIsOpen(false);
}
});

function open() {
const open = useFunction(() => {
EventsEditor.dispatchEvent(editor, 'snippet-dialog-opened');
setIsOpen(true);
savedSelection.save(editor);
}
});

async function submit(node: DocumentNode) {
const submit = useFunction((node: DocumentNode) => {
EventsEditor.dispatchEvent(editor, 'snippet-dialog-submitted');

close();
Expand All @@ -56,7 +58,7 @@ export function useFloatingSnippetInput(editor: SlateEditor): [State, Actions] {
type: 'error',
});
}
}
});

return [{ isOpen }, { close, open, rootClose, submit }];
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReactNode, RefObject } from 'react';
import React from 'react';
import React, { memo } from 'react';

import { FloatingContainer } from '#modules/components';

Expand All @@ -13,7 +13,7 @@ interface Props {
renderInput: () => ReactNode;
}

export function FloatingSnippetInput({
function FloatingSnippetInputComponent({
availableWidth,
containerRef,
onClose,
Expand All @@ -34,3 +34,5 @@ export function FloatingSnippetInput({
</FloatingContainer.Container>
);
}

export const FloatingSnippetInput = memo(FloatingSnippetInputComponent);
15 changes: 10 additions & 5 deletions packages/slate-editor/src/modules/editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,15 @@ export const Editor = forwardRef<EditorRef, EditorProps>((props, forwardedRef) =
props.onChange(editor.serialize(value) as Value);
});

const floatingSnippetRenderInput = useFunction(() => {
return (
withSnippets &&
withSnippets.renderInput({
onCreate: submitFloatingSnippetInput,
})
);
});

return (
<PopperOptionsContext.Provider value={popperMenuOptions}>
<div
Expand Down Expand Up @@ -983,11 +992,7 @@ export const Editor = forwardRef<EditorRef, EditorProps>((props, forwardedRef) =
containerRef={containerRef}
onClose={closeFloatingSnippetInput}
onRootClose={rootCloseFloatingSnippetInput}
renderInput={() =>
withSnippets.renderInput({
onCreate: submitFloatingSnippetInput,
})
}
renderInput={floatingSnippetRenderInput}
/>
)}
</>
Expand Down
Loading