Files
notesnook/apps/web/src/components/menu/index.js

165 lines
4.1 KiB
JavaScript
Raw Normal View History

2020-11-11 10:54:12 +05:00
import { useAnimation } from "framer-motion";
import React, { useEffect } from "react";
2019-12-02 10:19:32 +05:00
import { Flex, Box, Text } from "rebass";
2020-10-28 12:22:01 +05:00
import { SUBSCRIPTION_STATUS } from "../../common";
import { useStore as useUserStore } from "../../stores/user-store";
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) {
const isTrial = useUserStore(
2020-10-28 12:22:01 +05:00
(store) => store.user?.subscription?.status === SUBSCRIPTION_STATUS.TRIAL
);
2020-11-11 10:54:12 +05:00
const isMobile = useMobile();
const Container = isMobile ? MobileMenuContainer : MenuContainer;
2020-11-11 10:54:12 +05:00
return (
<Container {...props}>
{props.menuItems.map(
(item) =>
item.visible !== false && (
<Flex
data-test-id={`menuitem-${item.title
.split(" ")
.join("")
.toLowerCase()}`}
key={item.title}
onClick={async (e) => {
e.stopPropagation();
if (props.closeMenu) {
props.closeMenu();
}
if (!item.component) {
item.onClick(props.data);
}
}}
flexDirection="row"
alignItems="center"
justifyContent="space-between"
py={"8px"}
px={3}
sx={{
color: item.color || "text",
cursor: "pointer",
":hover": {
backgroundColor: "shade",
},
}}
>
{item.component ? (
2020-11-26 10:47:04 +05:00
item.component
2020-11-11 10:54:12 +05:00
) : (
<Text as="span" fontFamily="body" fontSize="menu">
{item.title}
</Text>
)}
{item.onlyPro && !isTrial && (
<Text
fontSize="body"
bg="primary"
color="static"
px={1}
sx={{ borderRadius: "default" }}
>
Pro
</Text>
)}
</Flex>
)
)}
</Container>
);
}
export default Menu;
function MenuContainer(props) {
2019-12-02 10:19:32 +05:00
return (
2019-12-19 10:04:46 +05:00
<Flex
id={props.id}
2020-01-08 15:04:18 +05:00
bg="background"
2019-12-19 10:04:46 +05:00
py={1}
style={props.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,
...props.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>
2020-11-11 10:54:12 +05:00
{props.children}
2019-12-02 10:19:32 +05:00
</Box>
</Flex>
);
}
2020-11-11 10:54:12 +05:00
function MobileMenuContainer(props) {
const { style, id, state } = props;
const animation = useAnimation();
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]);
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>
{props.children}
</Flex>
</Animated.Flex>
</Flex>
);
}