2020-11-11 10:54:12 +05:00
|
|
|
import { useAnimation } from "framer-motion";
|
2021-02-18 12:03:54 +05:00
|
|
|
import React, { useEffect, useMemo } from "react";
|
2019-12-02 10:19:32 +05:00
|
|
|
import { Flex, Box, Text } from "rebass";
|
2021-01-12 23:21:43 +05:00
|
|
|
import { isUserPremium } from "../../common";
|
2020-11-11 10:54:12 +05:00
|
|
|
import useMobile from "../../utils/use-mobile";
|
|
|
|
|
import Animated from "../animated";
|
2019-12-02 10:19:32 +05:00
|
|
|
|
|
|
|
|
function Menu(props) {
|
2021-04-20 09:33:19 +05:00
|
|
|
const { menuItems, data, closeMenu, id, style, sx, state } = props;
|
2020-11-11 10:54:12 +05:00
|
|
|
const isMobile = useMobile();
|
2021-02-18 12:03:54 +05:00
|
|
|
const Container = useMemo(
|
|
|
|
|
() => (isMobile ? MobileMenuContainer : MenuContainer),
|
|
|
|
|
[isMobile]
|
|
|
|
|
);
|
2020-05-15 00:35:20 +05:00
|
|
|
|
2020-11-11 10:54:12 +05:00
|
|
|
return (
|
2021-04-20 09:33:19 +05:00
|
|
|
<Container id={id} style={style} sx={sx} state={state}>
|
2021-02-18 12:03:54 +05:00
|
|
|
{menuItems.map(
|
|
|
|
|
({ title, key, onClick, component: Component, color, isPro }) => (
|
|
|
|
|
<Flex
|
|
|
|
|
data-test-id={`menuitem-${title(data)
|
|
|
|
|
.split(" ")
|
|
|
|
|
.join("")
|
|
|
|
|
.toLowerCase()}`}
|
|
|
|
|
key={key}
|
|
|
|
|
onClick={async (e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
if (closeMenu) {
|
|
|
|
|
closeMenu();
|
|
|
|
|
}
|
2020-11-11 10:54:12 +05:00
|
|
|
|
2021-02-18 12:03:54 +05:00
|
|
|
if (!Component) {
|
|
|
|
|
onClick(data);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
flexDirection="row"
|
|
|
|
|
alignItems="center"
|
|
|
|
|
justifyContent="space-between"
|
|
|
|
|
py={"8px"}
|
|
|
|
|
px={3}
|
|
|
|
|
sx={{
|
|
|
|
|
color: color || "text",
|
|
|
|
|
cursor: "pointer",
|
|
|
|
|
":hover": {
|
|
|
|
|
backgroundColor: "shade",
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{Component ? (
|
|
|
|
|
<Component data={data} />
|
|
|
|
|
) : (
|
|
|
|
|
<Text as="span" fontFamily="body" fontSize="menu">
|
|
|
|
|
{title(data)}
|
|
|
|
|
</Text>
|
|
|
|
|
)}
|
|
|
|
|
{isPro && !isUserPremium() && (
|
|
|
|
|
<Text
|
|
|
|
|
fontSize="body"
|
|
|
|
|
bg="primary"
|
|
|
|
|
color="static"
|
|
|
|
|
px={1}
|
|
|
|
|
sx={{ borderRadius: "default" }}
|
|
|
|
|
>
|
|
|
|
|
Pro
|
|
|
|
|
</Text>
|
|
|
|
|
)}
|
|
|
|
|
</Flex>
|
|
|
|
|
)
|
2020-11-11 10:54:12 +05:00
|
|
|
)}
|
|
|
|
|
</Container>
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-02-18 12:03:54 +05:00
|
|
|
export default React.memo(Menu, (prev, next) => {
|
|
|
|
|
return prev.state === next.state;
|
|
|
|
|
});
|
2020-11-11 10:54:12 +05:00
|
|
|
|
2021-02-18 12:03:54 +05:00
|
|
|
function MenuContainer({ id, style, sx, children }) {
|
2019-12-02 10:19:32 +05:00
|
|
|
return (
|
2019-12-19 10:04:46 +05:00
|
|
|
<Flex
|
2021-02-18 12:03:54 +05:00
|
|
|
id={id}
|
2020-01-08 15:04:18 +05:00
|
|
|
bg="background"
|
2019-12-19 10:04:46 +05:00
|
|
|
py={1}
|
2021-02-18 12:03:54 +05:00
|
|
|
style={style}
|
2019-12-19 10:04:46 +05:00
|
|
|
sx={{
|
|
|
|
|
position: "relative",
|
|
|
|
|
borderRadius: "default",
|
2020-03-28 23:29:04 +05:00
|
|
|
border: "2px solid",
|
|
|
|
|
borderColor: "border",
|
2020-04-21 10:56:43 +05:00
|
|
|
width: 180,
|
2021-02-18 12:03:54 +05:00
|
|
|
...sx,
|
2019-12-19 10:04:46 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2020-01-08 15:04:18 +05:00
|
|
|
<Box width="100%">
|
2020-03-02 15:00:53 +05:00
|
|
|
<Text
|
|
|
|
|
fontFamily="body"
|
2020-11-11 10:54:12 +05:00
|
|
|
fontSize="subtitle"
|
2020-03-02 15:00:53 +05:00
|
|
|
color="primary"
|
|
|
|
|
py={"8px"}
|
|
|
|
|
px={3}
|
|
|
|
|
sx={{ borderBottom: "1px solid", borderBottomColor: "border" }}
|
|
|
|
|
>
|
|
|
|
|
Properties
|
|
|
|
|
</Text>
|
2021-02-18 12:03:54 +05:00
|
|
|
{children}
|
2019-12-02 10:19:32 +05:00
|
|
|
</Box>
|
|
|
|
|
</Flex>
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-11-11 10:54:12 +05:00
|
|
|
|
2021-02-18 12:03:54 +05:00
|
|
|
function MobileMenuContainer({ style, id, state, children }) {
|
2020-11-11 10:54:12 +05:00
|
|
|
const animation = useAnimation();
|
2021-04-20 09:33:19 +05:00
|
|
|
|
2020-11-11 10:54:12 +05:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (state === "open") {
|
|
|
|
|
animation.start({ y: 0 });
|
|
|
|
|
const menu = document.getElementById(id);
|
|
|
|
|
menu.style.top = 0;
|
|
|
|
|
menu.style.left = 0;
|
|
|
|
|
} else {
|
|
|
|
|
animation.start({ y: 500 });
|
|
|
|
|
}
|
|
|
|
|
}, [state, animation, id]);
|
2021-02-18 12:03:54 +05:00
|
|
|
|
2020-11-11 10:54:12 +05:00
|
|
|
return (
|
|
|
|
|
<Flex
|
|
|
|
|
flexDirection="column"
|
|
|
|
|
id={id}
|
|
|
|
|
style={style}
|
|
|
|
|
width="100%"
|
|
|
|
|
height="100%"
|
|
|
|
|
bg="overlay"
|
|
|
|
|
overflow="hidden"
|
|
|
|
|
sx={{ position: "relative" }}
|
|
|
|
|
>
|
|
|
|
|
<Animated.Flex
|
|
|
|
|
width="100%"
|
|
|
|
|
bg="background"
|
|
|
|
|
sx={{
|
|
|
|
|
position: "absolute",
|
|
|
|
|
bottom: 0,
|
|
|
|
|
borderTopLeftRadius: 10,
|
|
|
|
|
borderTopRightRadius: 10,
|
|
|
|
|
overflow: "hidden",
|
|
|
|
|
}}
|
|
|
|
|
initial={{ y: 500 }}
|
|
|
|
|
animate={animation}
|
|
|
|
|
flexDirection="column"
|
|
|
|
|
p={2}
|
|
|
|
|
>
|
|
|
|
|
<Box
|
|
|
|
|
width={50}
|
|
|
|
|
height={7}
|
|
|
|
|
bg="shade"
|
|
|
|
|
alignSelf="center"
|
|
|
|
|
sx={{ borderRadius: "default" }}
|
|
|
|
|
/>
|
|
|
|
|
<Flex flex="1" flexDirection="column" overflowY="scroll">
|
|
|
|
|
<Text variant="title" mt={2} alignSelf="center">
|
|
|
|
|
Properties
|
|
|
|
|
</Text>
|
2021-02-18 12:03:54 +05:00
|
|
|
{children}
|
2020-11-11 10:54:12 +05:00
|
|
|
</Flex>
|
|
|
|
|
</Animated.Flex>
|
|
|
|
|
</Flex>
|
|
|
|
|
);
|
|
|
|
|
}
|