@octanejs/lexical 0.1.57 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/README.md +16 -10
  2. package/package.json +31 -22
  3. package/src/LexicalAutoEmbedPlugin.tsrx +30 -17
  4. package/src/{autoEmbedShared.ts → LexicalAutoEmbedPluginUtils.ts} +4 -4
  5. package/src/LexicalCollaborationContext.ts +7 -66
  6. package/src/LexicalCollaborationContextUtils.ts +76 -0
  7. package/src/LexicalDecoratorBlockNode.ts +41 -23
  8. package/src/LexicalDraggableBlockPlugin.tsrx +37 -39
  9. package/src/LexicalHorizontalRuleNode.tsrx +13 -21
  10. package/src/LexicalMarkdownShortcutPlugin.tsrx +12 -1
  11. package/src/LexicalMenuOption.ts +26 -0
  12. package/src/LexicalNodeContextMenuPlugin.tsrx +20 -14
  13. package/src/LexicalNodeEventPlugin.tsrx +2 -3
  14. package/src/LexicalNodeMenuPlugin.tsrx +2 -1
  15. package/src/LexicalTypeaheadMenuPlugin.tsrx +10 -9
  16. package/src/LexicalTypeaheadMenuPluginUtils.ts +78 -0
  17. package/src/index.ts +19 -4
  18. package/src/shared/LexicalContentEditableElement.tsrx +1 -1
  19. package/src/shared/LexicalDecorators.tsrx +23 -35
  20. package/src/shared/LexicalMenu.tsrx +63 -45
  21. package/src/shared/LexicalReactGeneratedJSON.ts +78 -0
  22. package/src/shared/menuShared.ts +75 -24
  23. package/src/shared/useCharacterLimit.ts +48 -23
  24. package/src/shared/useDynamicPositioning.ts +25 -18
  25. package/src/shared/useMenuAnchorRef.ts +28 -12
  26. package/src/typeaheadShared.ts +2 -55
  27. package/src/useLexicalAriaLiveRegion.ts +25 -0
  28. package/src/useLexicalFocusManagerRef.ts +40 -0
  29. package/src/useLexicalFocusTrapRef.ts +65 -0
  30. package/src/useLexicalIsTextContentEmpty.ts +5 -1
  31. package/src/useLexicalNodeSelection.ts +11 -0
  32. package/src/useLexicalRovingTabIndexRef.ts +41 -0
@@ -10,7 +10,6 @@ import {
10
10
  } from '@lexical/extension';
11
11
  import { useLexicalComposerContext } from './LexicalComposerContext';
12
12
  import { useLexicalNodeSelection } from './useLexicalNodeSelection';
13
- import type { SerializedHorizontalRuleNode } from '@lexical/extension';
14
13
  import {
15
14
  $applyNodeReplacement,
16
15
  addClassNamesToElement,
@@ -20,7 +19,7 @@ import {
20
19
  mergeRegister,
21
20
  removeClassNamesFromElement,
22
21
  } from 'lexical';
23
- import type { DOMConversionMap, DOMConversionOutput, NodeKey } from 'lexical';
22
+ import type { DOMConversionOutput, NodeKey } from 'lexical';
24
23
  import { useEffect } from 'octane';
25
24
  import type { OctaneNode } from 'octane';
26
25
 
@@ -67,25 +66,18 @@ function HorizontalRuleComponent(props: { nodeKey: NodeKey }) {
67
66
  }
68
67
 
69
68
  export class HorizontalRuleNode extends BaseHorizontalRuleNode {
70
- static getType() {
71
- return 'horizontalrule';
72
- }
73
-
74
- static clone(node: HorizontalRuleNode): HorizontalRuleNode {
75
- return new HorizontalRuleNode(node.__key);
76
- }
77
-
78
- static importJSON(serializedNode: SerializedHorizontalRuleNode): HorizontalRuleNode {
79
- return $createHorizontalRuleNode().updateFromJSON(serializedNode);
80
- }
81
-
82
- static importDOM(): DOMConversionMap | null {
83
- return {
84
- hr: () => ({
85
- conversion: $convertHorizontalRuleElement,
86
- priority: 0,
87
- }),
88
- };
69
+ $config() {
70
+ // Named rather than left to the runtime default, matching the base node:
71
+ // this deprecated subclass reuses the base node's 'horizontalrule' type.
72
+ return this.config('horizontalrule', {
73
+ extends: BaseHorizontalRuleNode,
74
+ importDOM: {
75
+ hr: () => ({
76
+ conversion: $convertHorizontalRuleElement,
77
+ priority: 0,
78
+ }),
79
+ },
80
+ });
89
81
  }
90
82
 
91
83
  decorate(): OctaneNode {
@@ -27,7 +27,18 @@ const HR: ElementTransformer = {
27
27
  type: 'element',
28
28
  };
29
29
 
30
- export const DEFAULT_TRANSFORMERS: Transformer[] = [HR, ...TRANSFORMERS];
30
+ /**
31
+ * A side-effect-free function rather than an array spread at module scope, which
32
+ * bundlers treat as a side effect that pins every transformer into any bundle
33
+ * importing this module.
34
+ *
35
+ * @__NO_SIDE_EFFECTS__
36
+ */
37
+ function defaultTransformers(): Transformer[] {
38
+ return [HR, ...TRANSFORMERS];
39
+ }
40
+
41
+ export const DEFAULT_TRANSFORMERS: Transformer[] = /* @__PURE__ */ defaultTransformers();
31
42
 
32
43
  type Props =
33
44
  Readonly<{
@@ -0,0 +1,26 @@
1
+ // Ported from @lexical/react/src/LexicalMenuOption.ts. Its own entry point (as
2
+ // upstream) so every menu module, and consumers subclassing it, share one class.
3
+ import type { MenuRef } from './shared/menuShared';
4
+
5
+ /**
6
+ * The base class for an item shown in a {@link LexicalTypeaheadMenuPlugin} or
7
+ * {@link LexicalNodeMenuPlugin} menu. Each option has a unique `key` and a `ref`
8
+ * to its rendered element (used for scrolling and keyboard navigation).
9
+ * Subclass it to attach your own data such as a label or callback.
10
+ */
11
+ export class MenuOption {
12
+ key: string;
13
+ ref?: MenuRef;
14
+ icon?: unknown;
15
+ title?: unknown;
16
+
17
+ constructor(key: string) {
18
+ this.key = key;
19
+ this.ref = { current: null };
20
+ this.setRefElement = this.setRefElement.bind(this);
21
+ }
22
+
23
+ setRefElement(element: HTMLElement | null) {
24
+ this.ref = { current: element };
25
+ }
26
+ }
@@ -1,4 +1,4 @@
1
- // Ported 1:1 from @lexical/react/src/LexicalNodeContextMenuPlugin.tsx (0.46.0). The
1
+ // Ported 1:1 from @lexical/react/src/LexicalNodeContextMenuPlugin.tsx (0.51.0). The
2
2
  // React version is built entirely on @floating-ui/REACT — useFloating, useRole,
3
3
  // useDismiss, useListNavigation, useTypeahead, useInteractions, and the
4
4
  // FloatingPortal/FloatingOverlay/FloatingFocusManager components. octane now has the
@@ -23,7 +23,7 @@ import {
23
23
  } from '@octanejs/floating-ui';
24
24
  import type { FloatingStyles } from '@octanejs/floating-ui';
25
25
  import { useLexicalComposerContext } from './LexicalComposerContext';
26
- import { $getNearestNodeFromDOMNode, $getRoot } from 'lexical';
26
+ import { $getNearestNodeFromDOMNode, $getRoot, registerEventListener } from 'lexical';
27
27
  import type { LexicalNode } from 'lexical';
28
28
  import { useEffect, useRef, useState } from 'octane';
29
29
  import type { OctaneNode } from 'octane';
@@ -203,6 +203,23 @@ export function NodeContextMenuPlugin(props: Props) @{
203
203
  // Build the menu model on right-click and open the menu at the cursor.
204
204
  useEffect(() => {
205
205
  function onContextMenu(e: MouseEvent) {
206
+ let visibleItems: ContextMenuType[] = [];
207
+ if (items) {
208
+ editor.read(() => {
209
+ const node = $getNearestNodeFromDOMNode(e.target as Element) ?? $getRoot();
210
+ if (node) {
211
+ visibleItems = items.filter((option) => (option.$showOn ? option.$showOn(node) : true));
212
+ }
213
+ });
214
+ }
215
+
216
+ // Nothing is left to show for this node -- separators on their own draw
217
+ // a menu with no items in it -- so let the browser's own context menu
218
+ // open rather than suppressing it and mounting an overlay around nothing.
219
+ if (!visibleItems.some((option) => option.type !== 'separator')) {
220
+ return;
221
+ }
222
+
206
223
  e.preventDefault();
207
224
 
208
225
  f.refs.setPositionReference({
@@ -220,16 +237,6 @@ export function NodeContextMenuPlugin(props: Props) @{
220
237
  },
221
238
  });
222
239
 
223
- let visibleItems: ContextMenuType[] = [];
224
- if (items) {
225
- editor.read(() => {
226
- const node = $getNearestNodeFromDOMNode(e.target as Element) ?? $getRoot();
227
- if (node) {
228
- visibleItems = items.filter((option) => (option.$showOn ? option.$showOn(node) : true));
229
- }
230
- });
231
- }
232
-
233
240
  const renderableItems = visibleItems.map((option, index) => {
234
241
  if (option.type === 'separator') {
235
242
  return {
@@ -257,8 +264,7 @@ export function NodeContextMenuPlugin(props: Props) @{
257
264
 
258
265
  return editor.registerRootListener((rootElement) => {
259
266
  if (rootElement !== null) {
260
- rootElement.addEventListener('contextmenu', onContextMenu);
261
- return () => rootElement.removeEventListener('contextmenu', onContextMenu);
267
+ return registerEventListener(rootElement, 'contextmenu', onContextMenu);
262
268
  }
263
269
  });
264
270
  }, [items, itemClassName, separatorClassName, editor]);
@@ -1,6 +1,6 @@
1
1
  // Ported from @lexical/react/src/LexicalNodeEventPlugin.ts.
2
2
  import { useLexicalComposerContext } from './LexicalComposerContext';
3
- import { $findMatchingParent, $getNearestNodeFromDOMNode } from 'lexical';
3
+ import { $findMatchingParent, $getNearestNodeFromDOMNode, registerEventListener } from 'lexical';
4
4
  import type { Klass, LexicalEditor, LexicalNode, NodeKey } from 'lexical';
5
5
  import { useEffect, useRef } from 'octane';
6
6
 
@@ -44,8 +44,7 @@ export function NodeEventPlugin(props: Props) {
44
44
 
45
45
  return editor.registerRootListener((rootElement) => {
46
46
  if (rootElement) {
47
- rootElement.addEventListener(eventType, onEvent, isCaptured);
48
- return () => rootElement.removeEventListener(eventType, onEvent, isCaptured);
47
+ return registerEventListener(rootElement, eventType, onEvent, isCaptured);
49
48
  }
50
49
  });
51
50
  // We intentionally don't respect changes to eventType.
@@ -8,7 +8,8 @@ import type { CommandListenerPriority, NodeKey, TextNode } from 'lexical';
8
8
  import { startTransition, useCallback, useEffect, useState } from 'octane';
9
9
  import type { MenuOption, MenuRenderFn, MenuResolution } from './shared/menuShared';
10
10
 
11
- export { MenuOption } from './shared/menuShared';
11
+ export { MenuOption } from './LexicalMenuOption';
12
+ export type { MenuRenderFn, MenuResolution } from './shared/menuShared';
12
13
 
13
14
  /**
14
15
  * Props for the {@link LexicalNodeMenuPlugin} component.
@@ -15,11 +15,10 @@ import type { CommandListenerPriority, TextNode } from 'lexical';
15
15
  import { startTransition, useCallback, useEffect, useState } from 'octane';
16
16
  import type { MenuOption, MenuRenderFn, MenuResolution, TriggerFn } from './shared/menuShared';
17
17
 
18
- export { useBasicTypeaheadTriggerMatch, PUNCTUATION } from './typeaheadShared';
19
- export { MenuOption, SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND } from './shared/menuShared';
18
+ export { MenuOption } from './LexicalMenuOption';
19
+ export type { MenuRenderFn, MenuResolution, MenuTextMatch, TriggerFn } from './shared/menuShared';
20
20
  export { useDynamicPositioning } from './shared/useDynamicPositioning';
21
- // Upstream re-exports @lexical/utils' getScrollParent from this module.
22
- export { getScrollParent } from '@lexical/utils';
21
+ export { getScrollParent, PUNCTUATION, SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND, useBasicTypeaheadTriggerMatch } from './LexicalTypeaheadMenuPluginUtils';
23
22
 
24
23
  /**
25
24
  * Props for the {@link LexicalTypeaheadMenuPlugin} component.
@@ -92,9 +91,11 @@ export function LexicalTypeaheadMenuPlugin<TOption extends MenuOption>(
92
91
  closeTypeahead();
93
92
  return;
94
93
  }
95
- if (editor.isComposing()) {
96
- return;
97
- }
94
+ const closeUnlessComposing = () => {
95
+ if (!editor.isComposing()) {
96
+ closeTypeahead();
97
+ }
98
+ };
98
99
  const editorWindow = editor._window || window;
99
100
  const range = editorWindow.document.createRange();
100
101
  const selection = $getSelection();
@@ -103,7 +104,7 @@ export function LexicalTypeaheadMenuPlugin<TOption extends MenuOption>(
103
104
  !$isRangeSelection(selection) || !selection.isCollapsed() || text === null ||
104
105
  range === null
105
106
  ) {
106
- closeTypeahead();
107
+ closeUnlessComposing();
107
108
  return;
108
109
  }
109
110
  const match = triggerFn(text, editor);
@@ -125,7 +126,7 @@ export function LexicalTypeaheadMenuPlugin<TOption extends MenuOption>(
125
126
  return;
126
127
  }
127
128
  }
128
- closeTypeahead();
129
+ closeUnlessComposing();
129
130
  });
130
131
  };
131
132
  return editor.registerUpdateListener(updateListener);
@@ -0,0 +1,78 @@
1
+ // Ported from @lexical/react/src/LexicalTypeaheadMenuPluginUtils.ts: the
2
+ // component-free surface of LexicalTypeaheadMenuPlugin, as its own entry point.
3
+ import type { MenuOption } from './LexicalMenuOption';
4
+ import type { TriggerFn } from './shared/menuShared';
5
+
6
+ import { getScrollParent as getScrollParent_ } from '@lexical/utils';
7
+ import { createCommand, type LexicalCommand } from 'lexical';
8
+ import { useCallback } from 'octane';
9
+
10
+ /**
11
+ * The default set of punctuation characters (as a character-class fragment)
12
+ * that terminate a typeahead query. Used as the default `punctuation` option of
13
+ * {@link useBasicTypeaheadTriggerMatch}.
14
+ */
15
+ export const PUNCTUATION = '\\.,\\+\\*\\?\\$\\@\\|#{}\\(\\)\\^\\-\\[\\]\\\\/!%\'"~=<>_:;';
16
+
17
+ /**
18
+ * Command dispatched while the typeahead menu is open to scroll the option at
19
+ * the given `index` into view. The built-in menu registers a default handler;
20
+ * custom implementations can register their own at a higher priority.
21
+ */
22
+ export const SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND: LexicalCommand<{
23
+ index: number;
24
+ option: MenuOption;
25
+ }> = createCommand('SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND');
26
+
27
+ /** @deprecated Moved to `@lexical/utils`. Import `getScrollParent` from there. */
28
+ export const getScrollParent = getScrollParent_;
29
+
30
+ // Two required user args (trigger, options), so the trailing slot is positional.
31
+ export function useBasicTypeaheadTriggerMatch(
32
+ trigger: string,
33
+ options: {
34
+ minLength?: number;
35
+ maxLength?: number;
36
+ punctuation?: string;
37
+ allowWhitespace?: boolean;
38
+ },
39
+ slot?: symbol,
40
+ ): TriggerFn {
41
+ const minLength = options.minLength ?? 1;
42
+ const maxLength = options.maxLength ?? 75;
43
+ const punctuation = options.punctuation ?? PUNCTUATION;
44
+ const allowWhitespace = options.allowWhitespace ?? false;
45
+ return useCallback(
46
+ (text: string) => {
47
+ const validCharsSuffix = allowWhitespace ? '' : '\\s';
48
+ const validChars = '[^' + trigger + punctuation + validCharsSuffix + ']';
49
+ const TypeaheadTriggerRegex = new RegExp(
50
+ '(^|\\s|\\()(' +
51
+ '[' +
52
+ trigger +
53
+ ']' +
54
+ '((?:' +
55
+ validChars +
56
+ '){0,' +
57
+ maxLength +
58
+ '})' +
59
+ ')$',
60
+ );
61
+ const match = TypeaheadTriggerRegex.exec(text);
62
+ if (match !== null) {
63
+ const maybeLeadingWhitespace = match[1];
64
+ const matchingString = match[3];
65
+ if (matchingString.length >= minLength) {
66
+ return {
67
+ leadOffset: match.index + maybeLeadingWhitespace.length,
68
+ matchingString,
69
+ replaceableString: match[2],
70
+ };
71
+ }
72
+ }
73
+ return null;
74
+ },
75
+ [allowWhitespace, trigger, punctuation, maxLength, minLength],
76
+ slot,
77
+ );
78
+ }
package/src/index.ts CHANGED
@@ -60,11 +60,26 @@ export { useLexicalNodeSelection } from './useLexicalNodeSelection';
60
60
  export { useLexicalTextEntity } from './useLexicalTextEntity';
61
61
  export { useLexicalIsTextContentEmpty } from './useLexicalIsTextContentEmpty';
62
62
 
63
+ // --- Accessibility hooks (require the matching @lexical/a11y extension) ---
64
+ export { useLexicalAriaLiveRegion } from './useLexicalAriaLiveRegion';
65
+ export { useLexicalFocusManagerRef, type FocusManagerOptions } from './useLexicalFocusManagerRef';
66
+ export { useLexicalFocusTrapRef, type FocusTrapInitialFocus } from './useLexicalFocusTrapRef';
67
+ export {
68
+ useLexicalRovingTabIndexRef,
69
+ type RovingOrientation,
70
+ type RovingTabIndexOptions,
71
+ } from './useLexicalRovingTabIndexRef';
72
+
63
73
  // --- Phase 4: menus + utilities ---
64
74
  export { LexicalTypeaheadMenuPlugin } from './LexicalTypeaheadMenuPlugin.tsrx';
65
75
  export { LexicalNodeMenuPlugin } from './LexicalNodeMenuPlugin.tsrx';
66
- export { useBasicTypeaheadTriggerMatch, PUNCTUATION } from './typeaheadShared';
67
- export { MenuOption, SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND } from './shared/menuShared';
76
+ export {
77
+ getScrollParent,
78
+ useBasicTypeaheadTriggerMatch,
79
+ PUNCTUATION,
80
+ SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND,
81
+ } from './LexicalTypeaheadMenuPluginUtils';
82
+ export { MenuOption } from './LexicalMenuOption';
68
83
  export type {
69
84
  MenuRef,
70
85
  MenuRenderFn,
@@ -89,8 +104,8 @@ export { CharacterLimitPlugin } from './LexicalCharacterLimitPlugin.tsrx';
89
104
  export { useCharacterLimit } from './shared/useCharacterLimit';
90
105
  export { TableOfContentsPlugin } from './LexicalTableOfContentsPlugin.tsrx';
91
106
  export { LexicalAutoEmbedPlugin } from './LexicalAutoEmbedPlugin.tsrx';
92
- export { URL_MATCHER, INSERT_EMBED_COMMAND, AutoEmbedOption } from './autoEmbedShared';
93
- export type { EmbedConfig, EmbedMatchResult } from './autoEmbedShared';
107
+ export { URL_MATCHER, INSERT_EMBED_COMMAND, AutoEmbedOption } from './LexicalAutoEmbedPluginUtils';
108
+ export type { EmbedConfig, EmbedMatchResult } from './LexicalAutoEmbedPluginUtils';
94
109
  export { DraggableBlockPlugin_EXPERIMENTAL } from './LexicalDraggableBlockPlugin.tsrx';
95
110
  export {
96
111
  NodeContextMenuPlugin,
@@ -144,7 +144,7 @@ export function ContentEditableElement(props: ContentEditableElementProps) @{
144
144
  role={role}
145
145
  spellCheck={props.spellCheck === false ? 'false' : 'true'}
146
146
  style={props.style}
147
- tabIndex={props.tabIndex}
147
+ tabIndex={props.tabIndex ?? (isEditable ? undefined : -1)}
148
148
  {...rest}
149
149
  />
150
150
  }
@@ -1,25 +1,13 @@
1
- // Ported from @lexical/react/src/shared/useDecorators.tsx (the LegacyDecorators
2
- // path used by Rich/PlainTextPlugin). React's `useDecorators` hook is realized as
3
- // a `Decorators` COMPONENT here: each <Decorators/> usage is its own octane scope,
4
- // so its useState/useEffect/useMemo get per-instance identity automatically.
1
+ // Ported from @lexical/react/src/shared/useReactDecorators.tsx (the LegacyDecorators
2
+ // path used by Rich/PlainTextPlugin). React's hook is realized as a `Decorators`
3
+ // COMPONENT here: each <Decorators/> usage is its own octane scope, so its hooks
4
+ // get per-instance identity automatically.
5
5
  //
6
6
  // Each decorator node renders into the DOM element Lexical created for it, via a
7
- // portal. octane's createPortal lowers to portal() only as a DIRECT child of a host
8
- // element (a createPortal VALUE is inert), so each portal lives inside its own
9
- // `DecoratorPortal` (a display:contents wrapper — the content escapes into the
10
- // node's element).
7
+ // `DecoratorPortal` component that returns a createPortal value.
11
8
  import type { LexicalEditor } from 'lexical';
12
9
  import type { OctaneNode } from 'octane';
13
- import {
14
- useState,
15
- useEffect,
16
- useLayoutEffect,
17
- useMemo,
18
- flushSync,
19
- createElement,
20
- createPortal,
21
- Suspense,
22
- } from 'octane';
10
+ import { createElement, createPortal, Suspense, useMemo, useSyncExternalStore } from 'octane';
23
11
 
24
12
  type ErrorBoundaryType =
25
13
  (props: {
@@ -58,25 +46,25 @@ export function Decorators(props: DecoratorsProps) {
58
46
  const editor = props.editor;
59
47
  const ErrorBoundary = props.ErrorBoundary;
60
48
 
61
- const [decorators, setDecorators] = useState(() => editor.getDecorators());
62
-
63
- // Subscribe to decorator changes.
64
- useLayoutEffect(() => {
65
- return editor.registerDecoratorListener((nextDecorators: Record<string, OctaneNode>) => {
66
- flushSync(() => {
67
- setDecorators(nextDecorators);
68
- });
69
- });
70
- }, [editor]);
71
-
72
- useEffect(() => {
73
- // If the content editable mounts before the subscription is added, nothing
74
- // renders on the initial pass — re-read on mount to cover that.
75
- setDecorators(editor.getDecorators());
76
- }, [editor]);
49
+ const [subscribe, getSnapshot] = useMemo(
50
+ () => [
51
+ (cb: () => void) => editor.registerDecoratorListener(cb),
52
+ () => editor.getDecorators<OctaneNode>(),
53
+ ] as const,
54
+ [editor],
55
+ );
56
+ const decorators = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
57
+ // A root remount recreates every decorator's DOM element without changing the
58
+ // decorator map, so the portals are re-derived when the root element changes.
59
+ const [subscribeRoot, getRootSnapshot] = useMemo(
60
+ () => [editor.registerRootListener.bind(editor), editor.getRootElement.bind(editor)] as const,
61
+ [editor],
62
+ );
63
+ const rootElement = useSyncExternalStore(subscribeRoot, getRootSnapshot, getRootSnapshot);
77
64
 
78
65
  // Resolve each decorator node's DOM element + its decorator content.
79
66
  const entries = useMemo(() => {
67
+ void rootElement;
80
68
  const out = [];
81
69
  const decoratorKeys = Object.keys(decorators);
82
70
  for (let i = 0; i < decoratorKeys.length; i++) {
@@ -87,7 +75,7 @@ export function Decorators(props: DecoratorsProps) {
87
75
  }
88
76
  }
89
77
  return out;
90
- }, [decorators, editor]);
78
+ }, [decorators, editor, rootElement]);
91
79
 
92
80
  // Render nothing when there are no decorators (matches @lexical/react). Each
93
81
  // decorator is a portal into its node's element — a keyed list of DecoratorPortal
@@ -148,6 +148,13 @@ export function LexicalMenu<TOption extends MenuOption>(props: LexicalMenuProps<
148
148
  }
149
149
  }, [options, selectedIndex, updateSelectedIndex, preselectFirstItem]);
150
150
 
151
+ // Whether this menu currently puts nothing on screen, and so must not
152
+ // consume the keys that would otherwise reach the editor. Only true for the
153
+ // default renderer: a `menuRenderFn` is called whatever the option list
154
+ // looks like, and is free to draw a "no results" panel that the user still
155
+ // has to be able to arrow through and dismiss.
156
+ const rendersNothing = menuRenderFnProp == null && (options === null || !options.length);
157
+
151
158
  useEffect(() => {
152
159
  return mergeRegister(
153
160
  editor.registerCommand(
@@ -169,30 +176,34 @@ export function LexicalMenu<TOption extends MenuOption>(props: LexicalMenuProps<
169
176
  editor.registerCommand(
170
177
  KEY_ARROW_DOWN_COMMAND,
171
178
  (event) => {
172
- if (options !== null && options.length) {
173
- const newSelectedIndex =
174
- selectedIndex === null
175
- ? 0
176
- : selectedIndex !== options.length - 1
177
- ? selectedIndex + 1
178
- : 0;
179
- updateSelectedIndex(newSelectedIndex);
180
- const option = options[newSelectedIndex];
181
- if (!option) {
182
- updateSelectedIndex(-1);
183
- event.preventDefault();
184
- event.stopImmediatePropagation();
185
- return true;
186
- }
187
- if (option.ref && option.ref.current) {
188
- editor.dispatchCommand(SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND, {
189
- index: newSelectedIndex,
190
- option,
191
- });
192
- }
179
+ if (rendersNothing) {
180
+ // There is nothing to move through, and the default renderer draws
181
+ // no menu for an empty list, so the key has to keep propagating to
182
+ // whatever would otherwise move the caret.
183
+ return false;
184
+ }
185
+ const newSelectedIndex =
186
+ selectedIndex === null
187
+ ? 0
188
+ : selectedIndex !== options.length - 1
189
+ ? selectedIndex + 1
190
+ : 0;
191
+ updateSelectedIndex(newSelectedIndex);
192
+ const option = options[newSelectedIndex];
193
+ if (!option) {
194
+ updateSelectedIndex(-1);
193
195
  event.preventDefault();
194
196
  event.stopImmediatePropagation();
197
+ return true;
198
+ }
199
+ if (option.ref && option.ref.current) {
200
+ editor.dispatchCommand(SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND, {
201
+ index: newSelectedIndex,
202
+ option,
203
+ });
195
204
  }
205
+ event.preventDefault();
206
+ event.stopImmediatePropagation();
196
207
  return true;
197
208
  },
198
209
  commandPriority,
@@ -200,27 +211,29 @@ export function LexicalMenu<TOption extends MenuOption>(props: LexicalMenuProps<
200
211
  editor.registerCommand(
201
212
  KEY_ARROW_UP_COMMAND,
202
213
  (event) => {
203
- if (options !== null && options.length) {
204
- const newSelectedIndex =
205
- selectedIndex === null
206
- ? options.length - 1
207
- : selectedIndex !== 0
208
- ? selectedIndex - 1
209
- : options.length - 1;
210
- updateSelectedIndex(newSelectedIndex);
211
- const option = options[newSelectedIndex];
212
- if (!option) {
213
- updateSelectedIndex(-1);
214
- event.preventDefault();
215
- event.stopImmediatePropagation();
216
- return true;
217
- }
218
- if (option.ref && option.ref.current) {
219
- scrollIntoViewIfNeeded(option.ref.current);
220
- }
214
+ if (rendersNothing) {
215
+ // See KEY_ARROW_DOWN_COMMAND above.
216
+ return false;
217
+ }
218
+ const newSelectedIndex =
219
+ selectedIndex === null
220
+ ? options.length - 1
221
+ : selectedIndex !== 0
222
+ ? selectedIndex - 1
223
+ : options.length - 1;
224
+ updateSelectedIndex(newSelectedIndex);
225
+ const option = options[newSelectedIndex];
226
+ if (!option) {
227
+ updateSelectedIndex(-1);
221
228
  event.preventDefault();
222
229
  event.stopImmediatePropagation();
230
+ return true;
231
+ }
232
+ if (option.ref && option.ref.current) {
233
+ scrollIntoViewIfNeeded(option.ref.current);
223
234
  }
235
+ event.preventDefault();
236
+ event.stopImmediatePropagation();
224
237
  return true;
225
238
  },
226
239
  commandPriority,
@@ -228,6 +241,12 @@ export function LexicalMenu<TOption extends MenuOption>(props: LexicalMenuProps<
228
241
  editor.registerCommand(
229
242
  KEY_ESCAPE_COMMAND,
230
243
  (event) => {
244
+ if (rendersNothing) {
245
+ // See KEY_ARROW_DOWN_COMMAND above: with nothing on screen there
246
+ // is no menu to dismiss, so Escape has to keep propagating to
247
+ // whatever would otherwise handle it.
248
+ return false;
249
+ }
231
250
  event.preventDefault();
232
251
  event.stopImmediatePropagation();
233
252
  close();
@@ -272,6 +291,7 @@ export function LexicalMenu<TOption extends MenuOption>(props: LexicalMenuProps<
272
291
  close,
273
292
  editor,
274
293
  options,
294
+ rendersNothing,
275
295
  selectedIndex,
276
296
  updateSelectedIndex,
277
297
  commandPriority,
@@ -282,11 +302,9 @@ export function LexicalMenu<TOption extends MenuOption>(props: LexicalMenuProps<
282
302
  [selectOptionAndCleanUp, selectedIndex, options],
283
303
  );
284
304
 
285
- if (anchorElementRef.current === null) {
286
- return null;
287
- }
288
- // A custom menuRenderFn returns a portal directly (React-compatible); the default
289
- // renders DefaultMenu into the anchor. octane renders a createPortal VALUE at any
305
+ // A custom menuRenderFn returns a portal directly (React-compatible) and, as
306
+ // upstream, is called whatever the anchor/option state; the default renders
307
+ // DefaultMenu into the anchor. octane renders a createPortal VALUE at any
290
308
  // position, so no host-element wrapper is needed.
291
309
  if (menuRenderFnProp != null) {
292
310
  return menuRenderFnProp(
@@ -295,7 +313,7 @@ export function LexicalMenu<TOption extends MenuOption>(props: LexicalMenuProps<
295
313
  resolution.match ? resolution.match.matchingString : '',
296
314
  );
297
315
  }
298
- return options.length
316
+ return anchorElementRef.current !== null && options.length
299
317
  ? createPortal(DefaultMenu, anchorElementRef.current, {
300
318
  options,
301
319
  selectedIndex,