import { Editor, FloatingMenu, useEditorState } from '@tiptap/react' import React, { useRef } from 'react' import { useMenubarNav } from './useMenubarNav.js' /** * A floating menu for inserting new elements like lists, horizontal rules, etc. */ export function InsertMenu({ editor }: { editor: Editor }) { const containerRef = useRef(null) const { activeNodeType } = useEditorState({ editor, selector: ctx => { const activeNode = ctx.editor.state.selection.$from.node(1) return { activeNodeType: activeNode?.type.name ?? 'paragraph', } }, }) // Handle arrow navigation within a menu bar container, and allow to escape to the editor const { getFocusableElements } = useMenubarNav({ editor, ref: containerRef, onEscape: e => { e.preventDefault() // On escape, focus the editor editor.chain().focus().run() }, }) return ( { // The ref we have is to the container, not the menu itself if (containerRef.current === e.target?.parentNode) { // Focus the first enabled button-like when the menu bar is focused getFocusableElements()?.[0]?.focus() } }} tabIndex={0} > ) }