import React, { createContext, useCallback, useMemo } from "react"; import type { LexicalEditor } from "lexical"; import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext"; import { type EditorTheme, Theme } from "@webiny/lexical-theme"; import type { ToolbarActionPlugin } from "~/types.js"; export interface RichTextEditorContext { editor: LexicalEditor; getOverlaysElement: () => HTMLElement; toolbarActionPlugins: ToolbarActionPlugin[]; theme: Theme; } export const RichTextEditorContext = createContext(undefined); interface RichTextEditorProviderProps { theme: EditorTheme; toolbarActionPlugins?: ToolbarActionPlugin[]; children?: React.ReactNode | React.ReactNode[]; } export const RichTextEditorProvider = ({ theme, toolbarActionPlugins = [], children }: RichTextEditorProviderProps) => { const [editor] = useLexicalComposerContext(); const getOverlaysElement = useCallback(() => { const rootElement = editor.getRootElement(); if (!rootElement) { return document.body; } // Inside a dialog, overlays must stay within the dialog, or its focus trap and // pointer-events lock make them unusable. const dialogContent = rootElement.closest("[role='dialog']"); if (dialogContent) { return dialogContent as HTMLElement; } // Anywhere else, render into the body, so ancestors with `overflow: hidden` // (e.g. accordions around nested object fields) don't clip the overlays. return document.body; }, [editor]); const internalTheme = useMemo( () => new Theme({ colors: theme.colors, typography: theme.typography, fontSizes: theme.fontSizes, tokens: theme.tokens, allowCustomColors: theme.allowCustomColors }), [theme] ); return ( {children} ); };