headplane/app/components/Menu.tsx

92 lines
2 KiB
TypeScript
Raw Normal View History

2024-12-31 10:31:50 +05:30
import type { Dispatch, ReactNode, SetStateAction } from 'react';
import {
Button as AriaButton,
Menu as AriaMenu,
MenuItem,
MenuTrigger,
2024-12-31 10:30:14 +05:30
Popover,
} from 'react-aria-components';
import { cn } from '~/utils/cn';
2024-12-31 10:30:14 +05:30
function Button(props: Parameters<typeof AriaButton>[0]) {
return (
<AriaButton
2024-12-31 10:30:14 +05:30
{...props}
className={cn('outline-none', props.className)}
aria-label="Menu"
/>
2024-12-31 10:30:14 +05:30
);
}
2024-12-31 10:30:14 +05:30
function Items(props: Parameters<typeof AriaMenu>[0]) {
return (
2024-12-31 10:30:14 +05:30
<Popover
className={cn(
'mt-2 rounded-md',
'bg-ui-50 dark:bg-ui-800',
'overflow-hidden z-50',
'border border-ui-200 dark:border-ui-600',
'entering:animate-in exiting:animate-out',
'entering:fade-in entering:zoom-in-95',
'exiting:fade-out exiting:zoom-out-95',
'fill-mode-forwards origin-left-right',
)}
>
<AriaMenu
2024-12-31 10:30:14 +05:30
{...props}
className={cn(
'outline-none',
'divide-y divide-ui-200 dark:divide-ui-600',
2024-12-31 10:30:14 +05:30
props.className,
)}
>
2024-12-31 10:30:14 +05:30
{props.children}
</AriaMenu>
</Popover>
2024-12-31 10:30:14 +05:30
);
}
2024-12-31 10:30:14 +05:30
type ButtonProps = Parameters<typeof AriaButton>[0] & {
readonly control?: [boolean, Dispatch<SetStateAction<boolean>>];
2024-12-31 10:30:14 +05:30
};
2024-12-31 10:30:14 +05:30
function ItemButton(props: ButtonProps) {
return (
2024-12-31 10:30:14 +05:30
<MenuItem className="outline-none">
<AriaButton
2024-12-31 10:30:14 +05:30
{...props}
className={cn(
'px-4 py-2 w-full outline-none text-left',
'hover:bg-ui-200 dark:hover:bg-ui-700',
2024-12-31 10:30:14 +05:30
props.className,
)}
2024-12-31 10:30:14 +05:30
aria-label="Menu Dialog"
// If control is passed, set the state value
2024-12-31 10:30:14 +05:30
onPress={(event) => {
props.onPress?.(event);
props.control?.[1](true);
}}
/>
</MenuItem>
2024-12-31 10:30:14 +05:30
);
}
2024-12-31 10:30:14 +05:30
function Item(props: Parameters<typeof MenuItem>[0]) {
return (
<MenuItem
2024-12-31 10:30:14 +05:30
{...props}
className={cn(
'px-4 py-2 w-full outline-none',
'hover:bg-ui-200 dark:hover:bg-ui-700',
2024-12-31 10:30:14 +05:30
props.className,
)}
/>
2024-12-31 10:30:14 +05:30
);
}
2024-12-31 10:30:14 +05:30
function Menu({ children }: { children: ReactNode }) {
return <MenuTrigger>{children}</MenuTrigger>;
}
2024-12-31 10:30:14 +05:30
export default Object.assign(Menu, { Button, Item, ItemButton, Items });