prosemirror-slash-menu-react v0.4.6
prosemirror-slash-menu-react
A UI package used together with prosemirror-slash-menu to display the menu with react.
By Horváth Áron & Viktor Váczi at Emergence Engineering
Try it out at https://emergence-engineering.com/blog/prosemirror-slash-menu

Features
- Displaying
prosemirror-slash-menuwith react - Menu positioning at the cursor position
- Displaying the menu upwards in case of overflow
- Default styling
- Custom styling with css classnames
- Optional popper reference element, placement and offset
- Outside click handling
Behavior
You can open the menu with the / key in an empty paragraph or after a space and you can filter the elements just by
typing, or you can navigate with the keyboard. For exact behaviour description
checkout prosemirror-slash-menu.
Installation and Usage
Install from npm with:
npm install prosemirror-slash-menu-react
Usage in the app:
import React, { useEffect, useRef, useState } from "react";
import { exampleSetup } from "prosemirror-example-setup";
import { EditorState } from "prosemirror-state";
import { EditorView } from "prosemirror-view";
import schema from "./schema";
import { SlashMenuPlugin } from "prosemirror-slash-menu";
import {
defaultElements,
defaultIcons,
Icons,
SlashMenuReact,
} from "prosemirror-slash-menu-react";
const ProseMirrorSlashMenuDemo = () => {
const [pmState, setPmState] = useState<EditorState>();
const [editorView, setEditorView] = useState<EditorView>();
const editorRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!editorRef.current) return;
const state = EditorState.create({
doc: schema.nodeFromJSON({
content: [
{
content: [
{
text: "Type '/' after a space to open the menu. ",
type: "text",
},
],
type: "paragraph",
},
],
type: "doc",
}),
plugins: [
SlashMenuPlugin(defaultElements),
...exampleSetup({
schema,
}),
],
});
const view: EditorView = new EditorView(editorRef.current, {
state,
dispatchTransaction: (tr) => {
try {
const newState = view.state.apply(tr);
view.updateState(newState);
setPmState(newState);
} catch (e) {}
},
});
setEditorView(view);
return () => {
view && view.destroy();
};
}, [editorRef]);
return (
<>
<div ref={editorRef} id="editor" />
{pmState && editorView && (
<SlashMenuReact
icons={{
[Icons.HeaderMenu]: defaultIcons.H1Icon,
[Icons.Level1]: defaultIcons.H1Icon,
[Icons.Level2]: defaultIcons.H2Icon,
[Icons.Level3]: defaultIcons.H3Icon,
[Icons.Bold]: defaultIcons.BoldIcon,
[Icons.Italic]: defaultIcons.ItalicIcon,
[Icons.Code]: defaultIcons.CodeIcon,
[Icons.Link]: defaultIcons.LinkIcon,
}}
editorState={pmState}
editorView={editorView}
/>
)}
</>
);
};Styling
To use the basic styling you can import menu-style.css into your project. If you want to use your own styling you can
override the following classnames.
menu-display-rootroot div for the menumenu-element-wrapperroot of menu elementsmenu-element-wrapper-clickableroot of menu elements when the menu items are set to be clickablemenu-element-selectedclassname that is added alongsidemenu-element-wrapperwhen an element is selectedmenu-element-iconif icon is provided for the element it's rendered in this divmenu-element-right-iconif right icon is provided its rendered in this divmenu-element-labellabel of the menu elementmenu-placeholderwhen there is no matching items for the filter, this is displayed with the text "No matching items"menu-filter-wrapperroot of the filter display, positioned above the menu by defaultmenu-filterthe filter textmenu-filter-placeholderplaceholder text for the filter fieldmenu-filter-iconif icon is provided for the filter field it's rendered in this divsubmenu-labelThe label of the submenu is shown above the menu elements when its openedgroup-wrapperwrapper around the menu elements, if there are multiple groups of elementsgroup-labellabel/title of the group
Props
editorStateprosemirrors editor stateeditorViewprosemirror editor viewiconsOptional, if you want to provide icons for your menu elements. Type of{[key: string]: FC}where the key is the id of the menu element and the value is aFunctionComponentthat renders the iconrightIconsSame as icons but these appear on the right on the menu element, most commonly used for indicating a submenu with an arrowsubMenuIconOptional icon for submenu label. By default, when a submenu is open an arrow is displayed indicating that the user is in a subMenu, it can be replaced with a react node of your choicefilterFieldIconOptional icon in the filter field.filterPlaceHolderOptional placeholder text for the filter field.mainMenuLabelOptional label for the main menu. By default, there is none.popperReferenceOptional popper reference HTMLElement, for displaying the menu next to whatever element you wantpopperOptionsYou can pass inplacementandoffsetto position your menu around the reference ElementclickableOptional boolean, if true the menu items are clickable, by default they are used only with keyboard
11 months ago
10 months ago
11 months ago
8 months ago
8 months ago
8 months ago
10 months ago
10 months ago
10 months ago
10 months ago
12 months ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago