Menu.jsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { h } from 'preact';
  2. import RelativeModal from './RelativeModal';
  3. import { useCallback } from 'preact/hooks';
  4. export default function Menu({ className, children, onDismiss, relativeTo, widthRelative }) {
  5. return relativeTo ? (
  6. <RelativeModal
  7. children={children}
  8. className={`${className || ''} py-2`}
  9. role="listbox"
  10. onDismiss={onDismiss}
  11. portalRootID="menus"
  12. relativeTo={relativeTo}
  13. widthRelative={widthRelative}
  14. />
  15. ) : null;
  16. }
  17. export function MenuItem({ focus, icon: Icon, label, onSelect, value }) {
  18. const handleClick = useCallback(() => {
  19. onSelect && onSelect(value, label);
  20. }, [onSelect, value, label]);
  21. return (
  22. <div
  23. className={`flex space-x-2 p-2 px-5 hover:bg-gray-200 dark:hover:bg-gray-800 dark:hover:text-white cursor-pointer ${
  24. focus ? 'bg-gray-200 dark:bg-gray-800 dark:text-white' : ''
  25. }`}
  26. onClick={handleClick}
  27. role="option"
  28. >
  29. {Icon ? (
  30. <div className="w-6 h-6 self-center mr-4 text-gray-500 flex-shrink-0">
  31. <Icon />
  32. </div>
  33. ) : null}
  34. <div className="whitespace-nowrap">{label}</div>
  35. </div>
  36. );
  37. }
  38. export function MenuSeparator() {
  39. return <div className="border-b border-gray-200 dark:border-gray-800 my-2" />;
  40. }