import { useDevOverlayContext } from '../../dev-overlay.browser' import { useClickOutsideAndEscape } from '../components/errors/dev-tools-indicator/utils' import { useLayoutEffect, useRef, createContext, useContext, type CSSProperties, type Dispatch, type SetStateAction, } from 'react' import { getIndicatorOffset } from '../utils/indicator-metrics' import { INDICATOR_PADDING } from '../components/devtools-indicator/devtools-indicator' import { usePanelRouterContext } from './context' import { usePanelContext } from './panel-router' interface C { closeMenu?: () => void selectedIndex: number setSelectedIndex: Dispatch> } export const MenuContext = createContext({} as C) export function MenuItem({ index, label, value, onClick, href, ...props }: { index?: number title?: string label: string value: React.ReactNode href?: string onClick?: () => void }) { const isInteractive = typeof onClick === 'function' || typeof href === 'string' const { closeMenu, selectedIndex, setSelectedIndex } = useContext(MenuContext) const selected = selectedIndex === index function click() { if (isInteractive) { onClick?.() closeMenu?.() if (href) { window.open(href, '_blank', 'noopener, noreferrer') } } } return (
{ if (isInteractive && index !== undefined && selectedIndex !== index) { setSelectedIndex(index) } }} onMouseLeave={() => setSelectedIndex(-1)} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') { click() } }} role={isInteractive ? 'menuitem' : undefined} tabIndex={selected ? 0 : -1} {...props} > {label} {value}
) } export const DevtoolMenu = ({ closeOnClickOutside = true, items, }: { closeOnClickOutside?: boolean items: Array< | false | undefined | null | { onClick?: () => void title?: string label: string value: React.ReactNode attributes?: Record footer?: boolean } > }) => { const { state } = useDevOverlayContext() const { setPanel, triggerRef, setSelectedIndex, selectedIndex } = usePanelRouterContext() const { mounted } = usePanelContext() const [vertical, horizontal] = state.devToolsPosition.split('-', 2) const menuRef = useRef(null) useClickOutsideAndEscape( menuRef, triggerRef, closeOnClickOutside && mounted, (reason) => { switch (reason) { case 'escape': { setPanel(null) setSelectedIndex(-1) return } case 'outside': { if (!closeOnClickOutside) { return } setPanel(null) setSelectedIndex(-1) return } default: { return null! } } } ) useLayoutEffect(() => { menuRef.current?.focus() // allows keydown to be captured selectMenuItem({ index: selectedIndex === -1 ? 'first' : selectedIndex, menuRef, setSelectedIndex, }) // eslint-disable-next-line react-hooks/react-compiler // eslint-disable-next-line react-hooks/exhaustive-deps }, []) const indicatorOffset = getIndicatorOffset(state) const [indicatorVertical, indicatorHorizontal] = state.devToolsPosition.split( '-', 2 ) const verticalOffset = vertical === indicatorVertical && horizontal === indicatorHorizontal ? indicatorOffset : INDICATOR_PADDING const positionStyle = { [vertical]: `${verticalOffset}px`, [horizontal]: `${INDICATOR_PADDING}px`, [vertical === 'top' ? 'bottom' : 'top']: 'auto', [horizontal === 'left' ? 'right' : 'left']: 'auto', } as CSSProperties const definedItems = items.filter((item) => !!item) const itemsAboveFooter = definedItems.filter((item) => !item.footer) const itemsBelowFooter = definedItems.filter((item) => item.footer) function onMenuKeydown(e: React.KeyboardEvent) { e.preventDefault() const clickableItems = definedItems.filter((item) => item.onClick) const totalClickableItems = clickableItems.length switch (e.key) { case 'ArrowDown': const next = selectedIndex >= totalClickableItems - 1 ? 0 : selectedIndex + 1 selectMenuItem({ index: next, menuRef, setSelectedIndex }) break case 'ArrowUp': const prev = selectedIndex <= 0 ? totalClickableItems - 1 : selectedIndex - 1 selectMenuItem({ index: prev, menuRef, setSelectedIndex }) break case 'Home': selectMenuItem({ index: 'first', menuRef, setSelectedIndex }) break case 'End': selectMenuItem({ index: 'last', menuRef, setSelectedIndex }) break case 'n': if (e.ctrlKey) { const nextCtrl = selectedIndex >= totalClickableItems - 1 ? 0 : selectedIndex + 1 selectMenuItem({ index: nextCtrl, menuRef, setSelectedIndex }) } break case 'p': if (e.ctrlKey) { const prevCtrl = selectedIndex <= 0 ? totalClickableItems - 1 : selectedIndex - 1 selectMenuItem({ index: prevCtrl, menuRef, setSelectedIndex }) } break default: break } } return ( ) } export function getAdjustedIndex( items: Array<{ onClick?: () => void }>, targetIndex: number ): number { let adjustedIndex = 0 for (let i = 0; i <= targetIndex && i < items.length; i++) { if (items[i].onClick) { if (i === targetIndex) { return adjustedIndex } adjustedIndex++ } } return adjustedIndex } export function getClickableItemsCount( items: Array<{ onClick?: () => void }> ): number { return items.filter((item) => item.onClick).length } export function IssueCount({ children }: { children: number }) { return ( 0} > {children} ) } export function ChevronRight() { return ( ) } export function selectMenuItem({ index, menuRef, setSelectedIndex, }: { index: number | 'first' | 'last' menuRef: React.RefObject setSelectedIndex: (index: number) => void }) { if (index === 'first') { setTimeout(() => { const all = menuRef.current?.querySelectorAll('[role="menuitem"]') if (all) { const firstIndex = all[0].getAttribute('data-index') selectMenuItem({ index: Number(firstIndex), menuRef, setSelectedIndex }) } }) return } if (index === 'last') { setTimeout(() => { const all = menuRef.current?.querySelectorAll('[role="menuitem"]') if (all) { const lastIndex = all.length - 1 selectMenuItem({ index: lastIndex, menuRef, setSelectedIndex }) } }) return } const el = menuRef.current?.querySelector( `[data-index="${index}"]` ) as HTMLElement if (el) { setSelectedIndex(index) el?.focus() } }