UNPKG

@letanure/resend-cli

Version:

A command-line interface for Resend email API

37 lines 2.39 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { Box, Text, useInput } from 'ink'; import { useState } from 'react'; export const Menu = ({ menuItems, onSelect, onExit, initialSelectedKey, isRootMenu = false, }) => { const initialIndex = initialSelectedKey ? menuItems.findIndex((item) => item.id === initialSelectedKey) : 0; const [selectedIndex, setSelectedIndex] = useState(Math.max(0, initialIndex)); useInput((input, key) => { if (key.upArrow) { // Circular navigation: if at first item, go to last setSelectedIndex(selectedIndex === 0 ? menuItems.length - 1 : selectedIndex - 1); } if (key.downArrow) { // Circular navigation: if at last item, go to first setSelectedIndex(selectedIndex === menuItems.length - 1 ? 0 : selectedIndex + 1); } if (key.return || key.rightArrow) { const selectedItem = menuItems[selectedIndex]; if (selectedItem && !selectedItem.disabled) { onSelect(selectedItem.id); } } // Root menu: only respond to 'q' and 'escape', not left arrow if (isRootMenu) { if (input === 'q' || key.escape) { const currentSelectedItem = menuItems[selectedIndex]; onExit(currentSelectedItem?.id); } } else if (input === 'q' || key.escape || key.leftArrow) { // Non-root menu: respond to 'q', 'escape', and left arrow const currentSelectedItem = menuItems[selectedIndex]; onExit(currentSelectedItem?.id); } }); return (_jsx(Box, { flexDirection: "column", children: menuItems.map((item, index) => (_jsxs(Box, { marginBottom: 1, children: [_jsx(Box, { width: 3, children: _jsx(Text, { color: index === selectedIndex ? 'cyan' : 'gray', children: index === selectedIndex ? '▶' : ' ' }) }), _jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: item.disabled ? 'gray' : index === selectedIndex ? 'cyan' : 'white', bold: index === selectedIndex && !item.disabled, dimColor: item.disabled, children: item.label }), _jsx(Text, { color: item.disabled ? 'gray' : index === selectedIndex ? 'gray' : 'darkGray', dimColor: item.disabled || index !== selectedIndex, children: item.description })] })] }, item.id))) })); }; //# sourceMappingURL=Menu.js.map