@apptimate/ui 4.9.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -17,12 +17,37 @@ interface DropdownProps {
|
|
|
17
17
|
export function Dropdown({ trigger, children, align = 'left', valign = 'bottom', className, contentClassName, closeOnContentClick = true }: DropdownProps) {
|
|
18
18
|
const [isOpen, setIsOpen] = useState(false);
|
|
19
19
|
const [coords, setCoords] = useState({ top: 0, bottom: 0, left: 0, right: 0 });
|
|
20
|
+
const [actualValign, setActualValign] = useState(valign);
|
|
21
|
+
const [actualAlign, setActualAlign] = useState(align);
|
|
20
22
|
const dropdownRef = useRef<HTMLDivElement>(null);
|
|
21
23
|
const contentRef = useRef<HTMLDivElement>(null);
|
|
22
24
|
|
|
23
25
|
const updatePosition = useCallback(() => {
|
|
24
26
|
if (dropdownRef.current) {
|
|
25
27
|
const rect = dropdownRef.current.getBoundingClientRect();
|
|
28
|
+
|
|
29
|
+
let newValign = valign;
|
|
30
|
+
const spaceBelow = window.innerHeight - rect.bottom;
|
|
31
|
+
const spaceAbove = rect.top;
|
|
32
|
+
|
|
33
|
+
if (valign === 'bottom' && spaceBelow < 250 && spaceAbove > spaceBelow) {
|
|
34
|
+
newValign = 'top';
|
|
35
|
+
} else if (valign === 'top' && spaceAbove < 250 && spaceBelow > spaceAbove) {
|
|
36
|
+
newValign = 'bottom';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let newAlign = align;
|
|
40
|
+
const spaceRight = window.innerWidth - rect.right;
|
|
41
|
+
const spaceLeft = rect.left;
|
|
42
|
+
|
|
43
|
+
if (align === 'left' && spaceRight < 180 && spaceLeft > spaceRight) {
|
|
44
|
+
newAlign = 'right';
|
|
45
|
+
} else if (align === 'right' && spaceLeft < 180 && spaceRight > spaceLeft) {
|
|
46
|
+
newAlign = 'left';
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
setActualValign(newValign);
|
|
50
|
+
setActualAlign(newAlign);
|
|
26
51
|
setCoords({
|
|
27
52
|
top: rect.top + window.scrollY,
|
|
28
53
|
bottom: rect.bottom + window.scrollY,
|
|
@@ -89,15 +114,15 @@ export function Dropdown({ trigger, children, align = 'left', valign = 'bottom',
|
|
|
89
114
|
ref={contentRef}
|
|
90
115
|
style={{
|
|
91
116
|
position: 'absolute',
|
|
92
|
-
...(
|
|
117
|
+
...(actualValign === 'bottom'
|
|
93
118
|
? { top: `${coords.bottom}px` }
|
|
94
119
|
: { bottom: `${window.innerHeight - coords.top}px` }
|
|
95
120
|
),
|
|
96
|
-
...(
|
|
121
|
+
...(actualAlign === 'left' ? { left: `${coords.left}px` } : { right: `${coords.right}px` })
|
|
97
122
|
}}
|
|
98
123
|
className={cn(
|
|
99
|
-
"z-[9999] min-w-[160px] rounded-[10px] bg-white border border-gray-200 shadow-[0_8px_30px_rgb(0,0,0,0.12)] p-1.5 animate-in fade-in zoom-in-95 duration-200",
|
|
100
|
-
|
|
124
|
+
"z-[9999] flex flex-col min-w-[160px] rounded-[10px] bg-white border border-gray-200 shadow-[0_8px_30px_rgb(0,0,0,0.12)] p-1.5 animate-in fade-in zoom-in-95 duration-200",
|
|
125
|
+
actualValign === 'bottom' ? "mt-1.5" : "mb-1.5",
|
|
101
126
|
contentClassName
|
|
102
127
|
)}
|
|
103
128
|
onClick={closeOnContentClick ? closeDropdown : undefined}
|