A menu plugin for ProseMirror that uses semantic HTML.
npm i prosemirror-semantic-menu
Import the menuPlugin()
function with all of your other ProseMirror code:
import { EditorState } from "prosemirror-state";
import { EditorView } from "prosemirror-view";
import { menuPlugin } from "prosemirror-semantic-menu";
const schema = new Schema(
// SchemaSpec goes here
);
new EditorView(document.querySelector("#editor"), {
state: EditorState.create({
schema: schema,
plugins: [
// load other plugins
menuPlugin({
items: [
// place your menu items here
]
}),
],
}),
});
An example stylesheet is also provided. You can import it for bundler use from prosemirror-semantic-menu/styles/menu.css
.
The component's shadow root is set to open
, meaning you can append content to it after it is attached to the editor. This is useful for things like improving the editor's initial render time while deferring icon loading via an HTTP request.
This module exports a function, view, and plugin key for creating a menu, as well as classes for creating different types of items.
A few interfaces are defined for type checking, and type declarations for the exported items are generated from JSDoc annotations in the code.
A quick reference is below; refer to the documentation comments in the code and its corresponding type declarations for a more thorough breakdown.
-
interface MenuElement
: All menu items conform to this interface -
interface MenuElementOptions
: The object passed to the constructor of a menu item -
interface SelectChoice
: An object representing a single<option>
element in a<select>
control
The class for creating and updating the menu's markup.
A function that returns a plugin you can pass to the editor state. It takes an object with the following options as an argument:
-
items
: An array of objects that represent the menu items -
element
: TheElement
into which the menu should be adjacently inserted -
where
: TheInsertPosition
indicating where to insert the menu relative to theelement
key
The key of the plugin created by menuPlugin()
.
Create a <button>
element that executes a function when pressed.
Create a <select>
element that executes a function when its value changes. The function is meant to be the same no matter what value is chosen, with the selection acting as an argument to the function.
The class for the custom element that contains the menu. The custom element uses shadow DOM, and it exposes elements for styling via the part
attribute.
An example of how one could style the menu's shadow DOM via the ::part()
pseudo-element. It provides hover, focus, and active styles.