Skip to content

Commit

Permalink
Merge pull request #577 from prezly/feature/dev-15524-story-editor-sn…
Browse files Browse the repository at this point in the history
…ippet-dropdown-scroll-resets

[DEV-15524] Fix - Make sure FloatingSnippetInput is not re-rendering if not needed
  • Loading branch information
kudlajz authored Jan 14, 2025
2 parents cfbb1cf + d3768d6 commit 046755a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
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

0 comments on commit 046755a

Please sign in to comment.