Skip to content

Commit

Permalink
FloatingUI Menu Positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
ivailop7 committed Dec 9, 2024
1 parent bcaf785 commit 9bfcfa3
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 76 deletions.
106 changes: 106 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/lexical-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"license": "MIT",
"version": "0.21.0",
"dependencies": {
"@floating-ui/react": "^0.27.0",
"@lexical/clipboard": "0.21.0",
"@lexical/code": "0.21.0",
"@lexical/devtools-core": "0.21.0",
Expand Down
4 changes: 0 additions & 4 deletions packages/lexical-react/src/LexicalAutoEmbedPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {
LexicalNodeMenuPlugin,
MenuOption,
MenuRenderFn,
} from '@lexical/react/LexicalNodeMenuPlugin';
import {mergeRegister} from '@lexical/utils';
import {
Expand Down Expand Up @@ -82,15 +81,13 @@ type LexicalAutoEmbedPluginProps<TEmbedConfig extends EmbedConfig> = {
embedFn: () => void,
dismissFn: () => void,
) => Array<AutoEmbedOption>;
menuRenderFn: MenuRenderFn<AutoEmbedOption>;
menuCommandPriority?: CommandListenerPriority;
};

export function LexicalAutoEmbedPlugin<TEmbedConfig extends EmbedConfig>({
embedConfigs,
onOpenEmbedModalForConfig,
getMenuOptions,
menuRenderFn,
menuCommandPriority = COMMAND_PRIORITY_LOW,
}: LexicalAutoEmbedPluginProps<TEmbedConfig>): JSX.Element | null {
const [editor] = useLexicalComposerContext();
Expand Down Expand Up @@ -233,7 +230,6 @@ export function LexicalAutoEmbedPlugin<TEmbedConfig extends EmbedConfig>({
onClose={reset}
onSelectOption={onSelectOption}
options={options}
menuRenderFn={menuRenderFn}
commandPriority={menuCommandPriority}
/>
) : null;
Expand Down
30 changes: 4 additions & 26 deletions packages/lexical-react/src/LexicalContextMenuPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*
*/
import type {MenuRenderFn, MenuResolution} from './shared/LexicalMenu';
import type {MenuResolution} from './shared/LexicalMenu';

import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {calculateZoomLevel} from '@lexical/utils';
Expand All @@ -15,8 +15,8 @@ import {
LexicalNode,
} from 'lexical';
import {
MutableRefObject,
ReactPortal,
// MutableRefObject,
// ReactPortal,
useCallback,
useEffect,
useState,
Expand All @@ -25,19 +25,6 @@ import * as React from 'react';

import {LexicalMenu, MenuOption, useMenuAnchorRef} from './shared/LexicalMenu';

export type ContextMenuRenderFn<TOption extends MenuOption> = (
anchorElementRef: MutableRefObject<HTMLElement | null>,
itemProps: {
selectedIndex: number | null;
selectOptionAndCleanUp: (option: TOption) => void;
setHighlightedIndex: (index: number) => void;
options: Array<TOption>;
},
menuProps: {
setMenuRef: (element: HTMLElement | null) => void;
},
) => ReactPortal | JSX.Element | null;

export type LexicalContextMenuPluginProps<TOption extends MenuOption> = {
onSelectOption: (
option: TOption,
Expand All @@ -49,7 +36,6 @@ export type LexicalContextMenuPluginProps<TOption extends MenuOption> = {
onClose?: () => void;
onWillOpen?: (event: MouseEvent) => void;
onOpen?: (resolution: MenuResolution) => void;
menuRenderFn: ContextMenuRenderFn<TOption>;
anchorClassName?: string;
commandPriority?: CommandListenerPriority;
parent?: HTMLElement;
Expand All @@ -63,7 +49,6 @@ export function LexicalContextMenuPlugin<TOption extends MenuOption>({
onClose,
onOpen,
onSelectOption,
menuRenderFn: contextMenuRenderFn,
anchorClassName,
commandPriority = COMMAND_PRIORITY_LOW,
parent,
Expand Down Expand Up @@ -153,17 +138,10 @@ export function LexicalContextMenuPlugin<TOption extends MenuOption>({
editor={editor}
anchorElementRef={anchorElementRef}
options={options}
menuRenderFn={(anchorRef, itemProps) =>
contextMenuRenderFn(anchorRef, itemProps, {
setMenuRef: (ref) => {
menuRef.current = ref;
},
})
}
onSelectOption={onSelectOption}
commandPriority={commandPriority}
/>
);
}

export {MenuOption, MenuRenderFn, MenuResolution};
export {MenuOption, MenuResolution};
7 changes: 2 additions & 5 deletions packages/lexical-react/src/LexicalNodeMenuPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
*/

import type {MenuRenderFn, MenuResolution} from './shared/LexicalMenu';
import type {MenuResolution} from './shared/LexicalMenu';

import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {
Expand All @@ -33,7 +33,6 @@ export type NodeMenuPluginProps<TOption extends MenuOption> = {
nodeKey: NodeKey | null;
onClose?: () => void;
onOpen?: (resolution: MenuResolution) => void;
menuRenderFn: MenuRenderFn<TOption>;
anchorClassName?: string;
commandPriority?: CommandListenerPriority;
parent?: HTMLElement;
Expand All @@ -45,7 +44,6 @@ export function LexicalNodeMenuPlugin<TOption extends MenuOption>({
onClose,
onOpen,
onSelectOption,
menuRenderFn,
anchorClassName,
commandPriority = COMMAND_PRIORITY_LOW,
parent,
Expand Down Expand Up @@ -119,11 +117,10 @@ export function LexicalNodeMenuPlugin<TOption extends MenuOption>({
editor={editor}
anchorElementRef={anchorElementRef}
options={options}
menuRenderFn={menuRenderFn}
onSelectOption={onSelectOption}
commandPriority={commandPriority}
/>
);
}

export {MenuOption, MenuRenderFn, MenuResolution};
export {MenuOption, MenuResolution};
16 changes: 3 additions & 13 deletions packages/lexical-react/src/LexicalTypeaheadMenuPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import type {
MenuRenderFn,
MenuResolution,
MenuTextMatch,
TriggerFn,
Expand All @@ -20,15 +19,14 @@ import {
$isTextNode,
COMMAND_PRIORITY_LOW,
CommandListenerPriority,
createCommand,
// createCommand,
getDOMSelection,
LexicalCommand,
// LexicalCommand,
LexicalEditor,
RangeSelection,
TextNode,
} from 'lexical';
import {useCallback, useEffect, useState} from 'react';
import * as React from 'react';
import {startTransition} from 'shared/reactPatches';

import {LexicalMenu, MenuOption, useMenuAnchorRef} from './shared/LexicalMenu';
Expand Down Expand Up @@ -140,11 +138,6 @@ export function getScrollParent(

export {useDynamicPositioning} from './shared/LexicalMenu';

export const SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND: LexicalCommand<{
index: number;
option: MenuOption;
}> = createCommand('SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND');

export function useBasicTypeaheadTriggerMatch(
trigger: string,
{minLength = 1, maxLength = 75}: {minLength?: number; maxLength?: number},
Expand Down Expand Up @@ -191,7 +184,6 @@ export type TypeaheadMenuPluginProps<TOption extends MenuOption> = {
matchingString: string,
) => void;
options: Array<TOption>;
menuRenderFn: MenuRenderFn<TOption>;
triggerFn: TriggerFn;
onOpen?: (resolution: MenuResolution) => void;
onClose?: () => void;
Expand All @@ -206,7 +198,6 @@ export function LexicalTypeaheadMenuPlugin<TOption extends MenuOption>({
onSelectOption,
onOpen,
onClose,
menuRenderFn,
triggerFn,
anchorClassName,
commandPriority = COMMAND_PRIORITY_LOW,
Expand Down Expand Up @@ -305,12 +296,11 @@ export function LexicalTypeaheadMenuPlugin<TOption extends MenuOption>({
editor={editor}
anchorElementRef={anchorElementRef}
options={options}
menuRenderFn={menuRenderFn}
shouldSplitNodeWithQuery={true}
onSelectOption={onSelectOption}
commandPriority={commandPriority}
/>
);
}

export {MenuOption, MenuRenderFn, MenuResolution, MenuTextMatch, TriggerFn};
export {MenuOption, MenuResolution, MenuTextMatch, TriggerFn};
Loading

0 comments on commit 9bfcfa3

Please sign in to comment.