|
1 | | -import React from "react" |
| 1 | +import React, { Fragment } from "react" |
2 | 2 | import Flex from "@/components/templates/flex" |
3 | 3 | import { Text } from "@/components/typography" |
4 | 4 | import { Icon } from "@/components/icon" |
5 | 5 |
|
6 | 6 | const rotateMap = { right: 1, bottom: 2, left: 3 } |
| 7 | +const iconAlignmentProps = { alignSelf: "center" } |
7 | 8 |
|
8 | | -const Container = ({ children, align, margin = [1], background = "tooltip", ref, ...rest }) => ( |
9 | | - <Flex |
10 | | - ref={ref} |
11 | | - column={align === "top"} |
12 | | - columnReverse={align === "bottom"} |
13 | | - rowReverse={align === "right"} |
14 | | - margin={margin} |
15 | | - > |
16 | | - <Flex background={background} padding={[1, 2]} round column {...rest}> |
17 | | - {typeof children === "string" ? <Text color="tooltipText">{children}</Text> : children} |
| 9 | +const Container = ({ |
| 10 | + children, |
| 11 | + align, |
| 12 | + margin = [1], |
| 13 | + background = "tooltip", |
| 14 | + ref, |
| 15 | + iconContainerProps = {}, |
| 16 | + ...rest |
| 17 | +}) => { |
| 18 | + const hasIconContainerProps = !!Object.keys(iconContainerProps) |
| 19 | + const IconContainer = hasIconContainerProps ? Flex : Fragment |
| 20 | + |
| 21 | + return ( |
| 22 | + <Flex |
| 23 | + ref={ref} |
| 24 | + column={align === "top"} |
| 25 | + columnReverse={align === "bottom"} |
| 26 | + rowReverse={align === "right"} |
| 27 | + margin={margin} |
| 28 | + > |
| 29 | + <Flex background={background} padding={[1, 2]} round column {...rest}> |
| 30 | + {typeof children === "string" ? <Text color="tooltipText">{children}</Text> : children} |
| 31 | + </Flex> |
| 32 | + {align && ( |
| 33 | + <IconContainer {...iconAlignmentProps} {...iconContainerProps}> |
| 34 | + <Icon |
| 35 | + name="triangle" |
| 36 | + color={background} |
| 37 | + rotate={rotateMap[align]} |
| 38 | + height="8px" |
| 39 | + width="8px" |
| 40 | + data-testid="drop-arrow" |
| 41 | + {...(hasIconContainerProps ? {} : iconAlignmentProps)} |
| 42 | + /> |
| 43 | + </IconContainer> |
| 44 | + )} |
18 | 45 | </Flex> |
19 | | - {align && ( |
20 | | - <Icon |
21 | | - name="triangle" |
22 | | - alignSelf="center" |
23 | | - color={background} |
24 | | - rotate={rotateMap[align]} |
25 | | - height="8px" |
26 | | - width="8px" |
27 | | - data-testid="drop-arrow" |
28 | | - /> |
29 | | - )} |
30 | | - </Flex> |
31 | | -) |
| 46 | + ) |
| 47 | +} |
32 | 48 |
|
33 | 49 | export default Container |
0 commit comments