import * as React from 'react'; import type { SuggestionProps, SuggestionKeyDownProps } from '@tiptap/suggestion'; import type { SlashItem } from './slashMenu.js'; export interface SlashMenuListHandle { onKeyDown: (props: SuggestionKeyDownProps) => boolean; } /** * Keyboard-navigable list rendered inside the slash-menu popup. Exposes an * imperative `onKeyDown` so the Suggestion plugin can route ↑/↓/Enter here. */ export const SlashMenuList = React.forwardRef>( function SlashMenuList(props, ref) { const { items, command } = props; const [selected, setSelected] = React.useState(0); React.useEffect(() => setSelected(0), [items]); const select = React.useCallback( (index: number) => { const item = items[index]; if (item) command(item); }, [items, command] ); React.useImperativeHandle(ref, () => ({ onKeyDown: ({ event }) => { if (event.key === 'ArrowUp') { setSelected(s => (s + items.length - 1) % items.length); return true; } if (event.key === 'ArrowDown') { setSelected(s => (s + 1) % items.length); return true; } if (event.key === 'Enter') { select(selected); return true; } return false; }, })); if (items.length === 0) { return (
No matches
); } return (
{items.map((item, index) => ( ))}
); } );