2021-02-02 07:28:25 +03:00
|
|
|
import { h } from 'preact';
|
|
|
|
|
import RelativeModal from './RelativeModal';
|
2021-02-09 22:35:33 +03:00
|
|
|
import { useCallback } from 'preact/hooks';
|
2021-02-02 07:28:25 +03:00
|
|
|
|
2021-02-07 07:59:11 +03:00
|
|
|
export default function Menu({ className, children, onDismiss, relativeTo, widthRelative }) {
|
2021-02-02 07:28:25 +03:00
|
|
|
return relativeTo ? (
|
|
|
|
|
<RelativeModal
|
|
|
|
|
children={children}
|
2021-02-04 21:13:47 +03:00
|
|
|
className={`${className || ''} py-2`}
|
2021-02-02 07:28:25 +03:00
|
|
|
role="listbox"
|
|
|
|
|
onDismiss={onDismiss}
|
|
|
|
|
portalRootID="menus"
|
|
|
|
|
relativeTo={relativeTo}
|
2021-02-07 07:59:11 +03:00
|
|
|
widthRelative={widthRelative}
|
2021-02-02 07:28:25 +03:00
|
|
|
/>
|
|
|
|
|
) : null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-26 22:11:00 +03:00
|
|
|
export function MenuItem({ focus, icon: Icon, label, href, onSelect, value, ...attrs }) {
|
2021-02-02 07:28:25 +03:00
|
|
|
const handleClick = useCallback(() => {
|
|
|
|
|
onSelect && onSelect(value, label);
|
|
|
|
|
}, [onSelect, value, label]);
|
|
|
|
|
|
2022-02-26 22:11:00 +03:00
|
|
|
const Element = href ? 'a' : 'div';
|
|
|
|
|
|
2021-02-02 07:28:25 +03:00
|
|
|
return (
|
2022-02-26 22:11:00 +03:00
|
|
|
<Element
|
2021-02-04 03:00:54 +03:00
|
|
|
className={`flex space-x-2 p-2 px-5 hover:bg-gray-200 dark:hover:bg-gray-800 dark:hover:text-white cursor-pointer ${
|
2021-02-02 07:28:25 +03:00
|
|
|
focus ? 'bg-gray-200 dark:bg-gray-800 dark:text-white' : ''
|
|
|
|
|
}`}
|
2022-02-26 22:11:00 +03:00
|
|
|
href={href}
|
2021-02-09 22:35:33 +03:00
|
|
|
onClick={handleClick}
|
2021-02-02 07:28:25 +03:00
|
|
|
role="option"
|
2022-02-26 22:11:00 +03:00
|
|
|
{...attrs}
|
2021-02-02 07:28:25 +03:00
|
|
|
>
|
2021-02-04 03:00:54 +03:00
|
|
|
{Icon ? (
|
2021-02-07 07:59:11 +03:00
|
|
|
<div className="w-6 h-6 self-center mr-4 text-gray-500 flex-shrink-0">
|
2021-02-04 03:00:54 +03:00
|
|
|
<Icon />
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
2021-02-09 22:35:33 +03:00
|
|
|
<div className="whitespace-nowrap">{label}</div>
|
2022-02-26 22:11:00 +03:00
|
|
|
</Element>
|
2021-02-02 07:28:25 +03:00
|
|
|
);
|
|
|
|
|
}
|
2021-02-04 03:00:54 +03:00
|
|
|
|
|
|
|
|
export function MenuSeparator() {
|
2021-02-04 21:13:47 +03:00
|
|
|
return <div className="border-b border-gray-200 dark:border-gray-800 my-2" />;
|
2021-02-04 03:00:54 +03:00
|
|
|
}
|