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

218 lines
5.5 KiB
JavaScript
Raw Normal View History

2020-11-11 10:54:12 +05:00
import { useAnimation } from "framer-motion";
2021-07-17 00:25:21 +05:00
import { Check } from "../icons";
import React, { useEffect, useMemo } from "react";
2021-06-16 09:58:15 +05:00
import { Flex, Box, Text, Button } from "rebass";
import { useIsUserPremium } from "../../hooks/use-is-user-premium";
2020-11-11 10:54:12 +05:00
import useMobile from "../../utils/use-mobile";
import { AnimatedFlex } 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;
const isUserPremium = useIsUserPremium();
2020-11-11 10:54:12 +05:00
const isMobile = useMobile();
const Container = useMemo(
() => (isMobile ? MobileMenuContainer : MenuContainer),
[isMobile]
);
2020-11-11 10:54:12 +05:00
return (
2021-07-17 00:25:21 +05:00
<Container id={id} title={data?.title} style={style} sx={sx} state={state}>
{menuItems.map(
2021-07-17 00:25:21 +05:00
(
{
title,
key,
onClick,
component: Component,
color,
isPro,
isNew,
disabled,
disableReason,
checked,
icon: Icon,
type,
},
index
) =>
type === "seperator" ? (
<Box
key={key}
width="95%"
height="0.5px"
bg="border"
my={2}
alignSelf="center"
/>
) : (
<Button
variant="menuitem"
color={color || "text"}
display="flex"
alignItems="center"
justifyContent="center"
2021-07-17 00:25:21 +05:00
title={
(disabled && disabled(data) && disableReason) || title(data)
}
2021-07-17 00:25:21 +05:00
disabled={disabled && disabled(data)}
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-07-17 00:25:21 +05:00
if (!Component) {
onClick(data, menuItems[index]);
}
}}
>
{Icon && (
<Icon
color={color || "text"}
size={15}
sx={{ mr: 2, ml: -1 }}
/>
)}
{Component ? (
<Component data={data} />
) : (
<Text
as="span"
textAlign="left"
fontFamily="body"
fontSize="menu"
flex={1}
>
{title(data)}
</Text>
)}
{isPro && !isUserPremium && (
<Text
fontSize="subBody"
bg="primary"
color="static"
px={1}
sx={{ borderRadius: "default" }}
>
Pro
</Text>
)}
{isNew && (
<Text
fontSize="subBody"
bg="primary"
color="static"
px={1}
sx={{ borderRadius: "default" }}
>
NEW
</Text>
)}
{checked && <Check size={14} />}
</Button>
)
2020-11-11 10:54:12 +05:00
)}
</Container>
);
}
export default React.memo(Menu, (prev, next) => {
return prev.state === next.state;
});
2020-11-11 10:54:12 +05:00
2021-07-17 00:25:21 +05:00
function MenuContainer({ id, style, sx, title, children }) {
2019-12-02 10:19:32 +05:00
return (
2019-12-19 10:04:46 +05:00
<Flex
id={id}
2020-01-08 15:04:18 +05:00
bg="background"
2019-12-19 10:04:46 +05:00
py={1}
style={style}
2019-12-19 10:04:46 +05:00
sx={{
position: "relative",
2021-07-17 00:25:21 +05:00
width: "11em",
2019-12-19 10:04:46 +05:00
borderRadius: "default",
2021-07-17 00:25:21 +05:00
boxShadow: "0px 10px 10px 0px #00000022",
border: "1px solid",
2020-03-28 23:29:04 +05:00
borderColor: "border",
...sx,
2019-12-19 10:04:46 +05:00
}}
>
2021-06-16 09:58:15 +05:00
<Flex flexDirection="column" 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" }}
>
2021-07-17 00:25:21 +05:00
{title || "Properties"}
2020-03-02 15:00:53 +05:00
</Text>
{children}
2021-06-16 09:58:15 +05:00
</Flex>
2019-12-02 10:19:32 +05:00
</Flex>
);
}
2020-11-11 10:54:12 +05:00
2021-07-17 00:25:21 +05:00
function MobileMenuContainer({ style, id, state, title, 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]);
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" }}
>
<AnimatedFlex
2020-11-11 10:54:12 +05:00
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">
2021-07-17 00:25:21 +05:00
{title || "Properties"}
2020-11-11 10:54:12 +05:00
</Text>
{children}
2020-11-11 10:54:12 +05:00
</Flex>
</AnimatedFlex>
2020-11-11 10:54:12 +05:00
</Flex>
);
}