File size: 3,070 Bytes
6680666
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import React, { useRef, useState } from 'react';
import { MoreHorizontal } from 'lucide-react';
import { FloatingPopover } from './FloatingPopover';

export interface RowOverflowAction {
  id?: string;
  label: string;
  icon: React.ReactNode;
  onClick: () => void;
  danger?: boolean;
  disabled?: boolean;
  title?: string;
}

interface RowOverflowMenuProps {
  id?: string;
  actions: RowOverflowAction[];
  triggerTitle?: string;
  buttonClassName?: string;
  iconClassName?: string;
  zIndexClass?: string;
}

export const RowOverflowMenu: React.FC<RowOverflowMenuProps> = ({
  id,
  actions,
  triggerTitle = 'Ещё действия',
  buttonClassName = 'p-1 rounded-md text-stone-500 dark:text-stone-400 hover:text-stone-900 dark:hover:text-stone-100 hover:bg-stone-100 dark:hover:bg-stone-800 transition-colors',
  iconClassName = 'w-3.5 h-3.5',
  zIndexClass = 'z-[100]',
}) => {
  const [isOpen, setIsOpen] = useState(false);
  const triggerRef = useRef<HTMLButtonElement>(null);

  if (actions.length === 0) return null;

  return (
    <>
      <button
        ref={triggerRef}
        id={id}
        type="button"
        onClick={(e) => {
          e.stopPropagation();
          setIsOpen((v) => !v);
        }}
        className={buttonClassName}
        title={triggerTitle}
        aria-expanded={isOpen}
        aria-haspopup="menu"
      >
        <MoreHorizontal className={iconClassName} />
      </button>

      <FloatingPopover
        isOpen={isOpen}
        onClose={() => setIsOpen(false)}
        triggerRef={triggerRef}
        align="right"
        direction="down"
        minWidth={195}
        maxWidth={260}
        zIndexClass={zIndexClass}
        className="text-xs"
      >
        <div
          className="p-1.5 space-y-0.5"
          role="menu"
          onClick={(e) => e.stopPropagation()}
        >
          {actions.map((action, idx) => (
            <button
              key={action.id || `${action.label}-${idx}`}
              id={action.id}
              type="button"
              disabled={action.disabled}
              title={action.title || action.label}
              onClick={(e) => {
                e.stopPropagation();
                if (action.disabled) return;
                setIsOpen(false);
                action.onClick();
              }}
              className={`w-full px-2.5 py-2 rounded-lg text-left flex items-center gap-2 transition-colors ${
                action.disabled
                  ? 'opacity-40 cursor-not-allowed text-stone-400 dark:text-stone-600'
                  : action.danger
                  ? 'text-rose-700 dark:text-rose-400 hover:bg-rose-50 dark:hover:bg-rose-950/50'
                  : 'text-stone-700 dark:text-stone-200 hover:bg-stone-100 dark:hover:bg-stone-800'
              }`}
              role="menuitem"
            >
              <span className="shrink-0">{action.icon}</span>
              <span className="truncate font-medium">{action.label}</span>
            </button>
          ))}
        </div>
      </FloatingPopover>
    </>
  );
};