import React, { useEffect, useRef, useState } from 'react';
import { createPortal } from 'react-dom';
import type { SelectionAction } from '../utils/selectionActions';
/**
* The host-actions dropdown: the same context-menu shape `QuickLabelDropdown`
* draws (single column, accent bar, full-width rows) and the same floating
* placement `FloatingQuickLabelPicker` computes, opened directly below the
* toolbar's wand button.
*
* Keyboard here is arrows + Enter + Escape rather than the quick labels'
* digit keys: a host's list is not a fixed ten with memorized numbers.
* Nothing is preselected until the first arrow, so a stray Enter while the
* dropdown is open never invokes a host command.
*/
const PICKER_WIDTH = 224;
const GAP = 6;
const VIEWPORT_PADDING = 12;
/** Room the dropdown wants below the anchor before it flips above. */
const FLIP_THRESHOLD = 220;
function computePosition(anchorEl: HTMLElement): { top: number; left: number; flipAbove: boolean } {
const rect = anchorEl.getBoundingClientRect();
const flipAbove = window.innerHeight - rect.bottom < FLIP_THRESHOLD;
const top = flipAbove ? rect.top - GAP : rect.bottom + GAP;
let left = rect.left + rect.width / 2 - PICKER_WIDTH / 2;
left = Math.max(
VIEWPORT_PADDING,
Math.min(left, window.innerWidth - PICKER_WIDTH - VIEWPORT_PADDING),
);
return { top, left, flipAbove };
}
export const SelectionActionsDropdown: React.FC<{
actions: SelectionAction[];
activeIndex: number | null;
onSelect: (action: SelectionAction) => void;
onHover: (index: number) => void;
}> = ({ actions, activeIndex, onSelect, onHover }) => (