@ledgerhq/react-ui 0.7.1 → 0.7.2
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.
|
@@ -26,11 +26,17 @@ export declare type Props = {
|
|
|
26
26
|
*/
|
|
27
27
|
children: React.ReactNode;
|
|
28
28
|
/**
|
|
29
|
-
*
|
|
29
|
+
* Vertical alignment of the dropdown relative to the dropdown button.
|
|
30
30
|
* Will automatically adjust to the document to avoid overflowing.
|
|
31
31
|
* Defaults to "bottom".
|
|
32
32
|
*/
|
|
33
33
|
placement?: Placement;
|
|
34
|
+
/**
|
|
35
|
+
* Controls whether the dropdown will flip its side to keep it in view
|
|
36
|
+
* in case there isn't enough space available. See https://floating-ui.com/docs/flip
|
|
37
|
+
* Defaults to false.
|
|
38
|
+
*/
|
|
39
|
+
flipDisabled?: boolean;
|
|
34
40
|
};
|
|
35
|
-
declare const DropdownGeneric: ({ label, closeOnClickOutside, closeOnClickInside, disabled, placement, children, }: Props) => JSX.Element;
|
|
41
|
+
declare const DropdownGeneric: ({ label, closeOnClickOutside, closeOnClickInside, disabled, placement, flipDisabled, children, }: Props) => JSX.Element;
|
|
36
42
|
export default DropdownGeneric;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React, { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
-
import { useFloating, getScrollParents, shift, flip } from "@floating-ui/react-dom";
|
|
2
|
+
import { useFloating, getScrollParents, shift, size, flip } from "@floating-ui/react-dom";
|
|
3
3
|
import styled from "styled-components";
|
|
4
4
|
import { Icons } from "../../../";
|
|
5
5
|
import Flex from "../../layout/Flex";
|
|
@@ -15,13 +15,13 @@ const ButtonContainer = styled(Box).attrs({
|
|
|
15
15
|
> :last-child {
|
|
16
16
|
/* targeting the dropdown icon */
|
|
17
17
|
${(p) => p.opened && "transform: rotate(180deg);"}
|
|
18
|
-
margin:
|
|
18
|
+
margin-left: ${(p) => p.theme.space[3]}px;
|
|
19
19
|
}
|
|
20
20
|
`;
|
|
21
21
|
const DropdownContainer = styled(Flex).attrs(({ theme }) => {
|
|
22
22
|
const isLight = theme.colors.type === "light";
|
|
23
23
|
return {
|
|
24
|
-
|
|
24
|
+
zIndex: 1,
|
|
25
25
|
flexDirection: "column",
|
|
26
26
|
padding: 3,
|
|
27
27
|
border: `1px solid ${theme.colors.neutral[isLight ? "c20" : "c30"]}`,
|
|
@@ -30,11 +30,14 @@ const DropdownContainer = styled(Flex).attrs(({ theme }) => {
|
|
|
30
30
|
color: theme.colors.neutral.c80,
|
|
31
31
|
};
|
|
32
32
|
}) `
|
|
33
|
+
overflow-y: auto;
|
|
33
34
|
box-shadow: 0px 6px 12px rgba(0, 0, 0, ${(p) => (p.theme.colors.type === "light" ? 0.04 : 0.08)});
|
|
34
35
|
`;
|
|
35
36
|
export const placements = ["bottom-start", "bottom", "bottom-end"];
|
|
36
|
-
const DropdownGeneric = ({ label, closeOnClickOutside = true, closeOnClickInside = false, disabled = false, placement = "bottom", children, }) => {
|
|
37
|
+
const DropdownGeneric = ({ label, closeOnClickOutside = true, closeOnClickInside = false, disabled = false, placement = "bottom", flipDisabled = false, children, }) => {
|
|
37
38
|
const divRef = useRef(null);
|
|
39
|
+
const [maxHeight, setMaxHeight] = useState();
|
|
40
|
+
const [maxWidth, setMaxWidth] = useState();
|
|
38
41
|
const [opened, setOpened] = useState(false);
|
|
39
42
|
const handleClickButton = useCallback(() => {
|
|
40
43
|
if (!disabled)
|
|
@@ -46,7 +49,17 @@ const DropdownGeneric = ({ label, closeOnClickOutside = true, closeOnClickInside
|
|
|
46
49
|
}, [setOpened, closeOnClickInside]);
|
|
47
50
|
const { x, y, reference, floating, strategy, update, refs } = useFloating({
|
|
48
51
|
placement: placements.includes(placement) ? placement : "bottom",
|
|
49
|
-
middleware: [
|
|
52
|
+
middleware: [
|
|
53
|
+
shift(),
|
|
54
|
+
...(flipDisabled ? [] : [flip()]),
|
|
55
|
+
size({
|
|
56
|
+
padding: 6,
|
|
57
|
+
apply({ height, width }) {
|
|
58
|
+
setMaxHeight(height);
|
|
59
|
+
setMaxWidth(width);
|
|
60
|
+
},
|
|
61
|
+
}),
|
|
62
|
+
],
|
|
50
63
|
});
|
|
51
64
|
const handleResizeOrScroll = useCallback(() => {
|
|
52
65
|
if (opened && !disabled)
|
|
@@ -70,7 +83,7 @@ const DropdownGeneric = ({ label, closeOnClickOutside = true, closeOnClickInside
|
|
|
70
83
|
parent.removeEventListener("resize", handleResizeOrScroll);
|
|
71
84
|
});
|
|
72
85
|
};
|
|
73
|
-
}, [
|
|
86
|
+
}, [flipDisabled, refs.reference, refs.floating, handleResizeOrScroll]);
|
|
74
87
|
useEffect(() => {
|
|
75
88
|
function handleClickOutside(event) {
|
|
76
89
|
if (closeOnClickOutside &&
|
|
@@ -86,10 +99,17 @@ const DropdownGeneric = ({ label, closeOnClickOutside = true, closeOnClickInside
|
|
|
86
99
|
};
|
|
87
100
|
}, [closeOnClickOutside, opened, setOpened, divRef]);
|
|
88
101
|
const color = disabled ? "neutral.c50" : "neutral.c100";
|
|
89
|
-
return (React.createElement(
|
|
102
|
+
return (React.createElement(Box, { ref: divRef },
|
|
90
103
|
React.createElement(ButtonContainer, { ref: reference, onClick: handleClickButton, disabled: disabled, opened: opened && !disabled },
|
|
91
104
|
React.createElement(Text, { variant: "paragraph", fontWeight: "medium", color: color }, label),
|
|
92
105
|
React.createElement(Icons.DropdownMedium, { size: 20, color: color })),
|
|
93
|
-
opened && !disabled && (React.createElement(DropdownContainer, { ref: floating, style: {
|
|
106
|
+
opened && !disabled && (React.createElement(DropdownContainer, { ref: floating, style: {
|
|
107
|
+
position: strategy,
|
|
108
|
+
top: y !== null && y !== void 0 ? y : "",
|
|
109
|
+
left: x !== null && x !== void 0 ? x : "",
|
|
110
|
+
maxHeight: maxHeight ? `${maxHeight}px` : "",
|
|
111
|
+
maxWidth: maxWidth ? "" : "",
|
|
112
|
+
// maxWidth: maxWidth ? `${maxWidth}px` : "", /* TODO: fix this */
|
|
113
|
+
}, onClick: handleClickInside }, children))));
|
|
94
114
|
};
|
|
95
115
|
export default DropdownGeneric;
|
package/package.json
CHANGED