Skip to content
Open
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
4 changes: 2 additions & 2 deletions packages/lexical-playground/__tests__/e2e/Tables.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ test.describe.parallel('Tables', () => {

await waitForSelector(
page,
`div[class="typeahead-popover mentions-menu"] ul li:first-child.selected`,
`div[class="typeahead-popover"] ul li:first-child.PlaygroundEditorTheme__menuItemSelected`,
);

await moveDown(page, 1);
Expand All @@ -790,7 +790,7 @@ test.describe.parallel('Tables', () => {

await waitForSelector(
page,
'div[class="typeahead-popover mentions-menu"] ul li:nth-child(2).selected',
'div[class="typeahead-popover"] ul li:nth-child(2).PlaygroundEditorTheme__menuItemSelected',
);
});
});
Expand Down
5 changes: 1 addition & 4 deletions packages/lexical-playground/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ pre::-webkit-scrollbar-thumb {
box-shadow: 0px 5px 10px rgba(0, 0, 0, 0.3);
border-radius: 8px;
position: relative;
width: fit-content;
}

.typeahead-popover ul {
Expand Down Expand Up @@ -355,10 +356,6 @@ pre::-webkit-scrollbar-thumb {
background-position: center;
}

.mentions-menu {
width: 250px;
}

i.palette {
background-image: url(images/icons/palette.svg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ const theme: EditorThemeClasses = {
},
mark: 'PlaygroundEditorTheme__mark',
markOverlap: 'PlaygroundEditorTheme__markOverlap',
menu: {
container: 'typeahead-popover',
item: 'PlaygroundEditorTheme__menuItem',
itemSelected: 'PlaygroundEditorTheme__menuItemSelected',
itemText: 'PlaygroundEditorTheme__menuItemText',
},
paragraph: 'PlaygroundEditorTheme__paragraph',
quote: 'PlaygroundEditorTheme__quote',
specialText: 'PlaygroundEditorTheme__specialText',
Expand Down
3 changes: 3 additions & 0 deletions packages/lexical-react/src/LexicalNodeMenuPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type NodeMenuPluginProps<TOption extends MenuOption> = {
matchingString: string,
) => void;
options: Array<TOption>;
containerClassName?: string;
nodeKey: NodeKey | null;
onClose?: () => void;
onOpen?: (resolution: MenuResolution) => void;
Expand All @@ -41,6 +42,7 @@ export type NodeMenuPluginProps<TOption extends MenuOption> = {

export function LexicalNodeMenuPlugin<TOption extends MenuOption>({
options,
containerClassName,
nodeKey,
onClose,
onOpen,
Expand Down Expand Up @@ -118,6 +120,7 @@ export function LexicalNodeMenuPlugin<TOption extends MenuOption>({
editor={editor}
anchorElementRef={anchorElementRef}
options={options}
className={containerClassName}
onSelectOption={onSelectOption}
commandPriority={commandPriority}
/>
Expand Down
3 changes: 3 additions & 0 deletions packages/lexical-react/src/LexicalTypeaheadMenuPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ export type TypeaheadMenuPluginProps<TOption extends MenuOption> = {
matchingString: string,
) => void;
options: Array<TOption>;
containerClassName?: string;
triggerFn: TriggerFn;
onOpen?: (resolution: MenuResolution) => void;
onClose?: () => void;
Expand All @@ -213,6 +214,7 @@ export type TypeaheadMenuPluginProps<TOption extends MenuOption> = {

export function LexicalTypeaheadMenuPlugin<TOption extends MenuOption>({
options,
containerClassName,
onQueryChange,
onSelectOption,
onOpen,
Expand Down Expand Up @@ -339,6 +341,7 @@ export function LexicalTypeaheadMenuPlugin<TOption extends MenuOption>({
editor={editor}
anchorElementRef={anchorElementRef}
options={options}
className={containerClassName}
shouldSplitNodeWithQuery={true}
onSelectOption={onSelectOption}
commandPriority={commandPriority}
Expand Down
50 changes: 45 additions & 5 deletions packages/lexical-react/src/shared/LexicalMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

import type {JSX} from 'react';

import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {
type LexicalComposerContextWithEditor,
useLexicalComposerContext,
} from '@lexical/react/LexicalComposerContext';
import {mergeRegister} from '@lexical/utils';
import {
$getSelection,
Expand Down Expand Up @@ -249,16 +252,34 @@ function MenuItem({
onClick,
onMouseEnter,
option,
theme,
}: {
index: number;
isSelected: boolean;
onClick: () => void;
onMouseEnter: () => void;
option: MenuOption;
theme: LexicalComposerContextWithEditor[1]['getTheme'];
}) {
let className = 'item';
const themeClasses = theme();
const menuTheme =
themeClasses != null && themeClasses.menu != null
? themeClasses.menu
: null;
const itemClassName =
menuTheme != null && menuTheme.item != null ? menuTheme.item : 'item';
const itemSelectedClassName =
menuTheme != null && menuTheme.itemSelected != null
? menuTheme.itemSelected
: 'selected';
const itemTextClassName =
menuTheme != null && menuTheme.itemText != null
? menuTheme.itemText
: 'text';

let className = itemClassName;
if (isSelected) {
className += ' selected';
className += ` ${itemSelectedClassName}`;
}
return (
<li
Expand All @@ -272,7 +293,7 @@ function MenuItem({
onMouseEnter={onMouseEnter}
onClick={onClick}>
{option.icon}
<span className="text">{option.title}</span>
<span className={itemTextClassName}>{option.title}</span>
</li>
);
}
Expand All @@ -281,6 +302,7 @@ export function LexicalMenu<TOption extends MenuOption>({
close,
editor,
anchorElementRef,
className: classNameProp,
resolution,
options,
onSelectOption,
Expand All @@ -291,6 +313,7 @@ export function LexicalMenu<TOption extends MenuOption>({
close: () => void;
editor: LexicalEditor;
anchorElementRef: RefObject<HTMLElement | null>;
className?: string;
resolution: MenuResolution;
options: Array<TOption>;
shouldSplitNodeWithQuery?: boolean;
Expand All @@ -303,6 +326,7 @@ export function LexicalMenu<TOption extends MenuOption>({
commandPriority?: CommandListenerPriority;
preselectFirstItem?: boolean;
}): JSX.Element | null {
const [, {getTheme}] = useLexicalComposerContext();
const [rawSelectedIndex, setHighlightedIndex] = useState<null | number>(null);
// Clamp highlighted index if options list shrinks
const selectedIndex =
Expand Down Expand Up @@ -352,9 +376,22 @@ export function LexicalMenu<TOption extends MenuOption>({
);

const menuRenderFn = useCallback(() => {
const themeClasses = getTheme();
const menuTheme =
themeClasses != null && themeClasses.menu != null
? themeClasses.menu
: null;
const containerThemeClassName =
menuTheme != null && menuTheme.container != null
? menuTheme.container
: null;
const containerClassName = [classNameProp, containerThemeClassName]
.filter(Boolean)
.join(' ');

return anchorElementRef.current && options.length
? ReactDOM.createPortal(
<div className="typeahead-popover mentions-menu">
<div className={containerClassName}>
<ul>
{options.map((option, i: number) => (
<MenuItem
Expand All @@ -369,6 +406,7 @@ export function LexicalMenu<TOption extends MenuOption>({
}}
key={option.key}
option={option}
theme={getTheme}
/>
))}
</ul>
Expand All @@ -378,10 +416,12 @@ export function LexicalMenu<TOption extends MenuOption>({
: null;
}, [
anchorElementRef,
classNameProp,
options,
selectedIndex,
selectOptionAndCleanUp,
setHighlightedIndex,
getTheme,
]);

useEffect(() => {
Expand Down
6 changes: 6 additions & 0 deletions packages/lexical/flow/Lexical.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,12 @@ export type EditorThemeClasses = {
base?: EditorThemeClassName,
focus?: EditorThemeClassName,
},
menu?: {
container?: EditorThemeClassName,
item?: EditorThemeClassName,
itemSelected?: EditorThemeClassName,
itemText?: EditorThemeClassName,
},
// Handle other generic values
[string]: EditorThemeClassName | {[string]: EditorThemeClassName},
};
Expand Down
6 changes: 6 additions & 0 deletions packages/lexical/src/LexicalEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ export type EditorThemeClasses = {
focus?: EditorThemeClassName;
};
indent?: EditorThemeClassName;
menu?: {
container?: EditorThemeClassName;
item?: EditorThemeClassName;
itemSelected?: EditorThemeClassName;
itemText?: EditorThemeClassName;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
};
Expand Down