Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: add animation to global context menu

thecodrr7f3b7a5ca59d
diff --git a/apps/web/src/components/global-menu-wrapper/index.js b/apps/web/src/components/global-menu-wrapper/index.js
index 34e878878..46188fcbb 100644
--- a/apps/web/src/components/global-menu-wrapper/index.js
+++ b/apps/web/src/components/global-menu-wrapper/index.js
@@ -1,23 +1,48 @@
-import React from "react";
+import React, { useEffect, useRef } from "react";
import Menu from "../menu";
import useContextMenu from "../../utils/useContextMenu";
+import Animated from "../animated";
+import { useAnimation } from "framer-motion";
function GlobalMenuWrapper() {
- const [items, data, title, closeMenu] = useContextMenu();
+ const [items, data, title, state, closeMenu] = useContextMenu();
+ const animation = useAnimation();
+ useEffect(() => {
+ if (state === "open") {
+ const menu = document.getElementById("globalContextMenu");
+ menu.style.display = "block";
+ animation.start({
+ opacity: 1,
+ transition: { duration: 0.2 },
+ });
+ } else if (state === "close") {
+ animation
+ .start({
+ opacity: 0,
+ transition: { duration: 0.1 },
+ })
+ .then(() => {
+ const menu = document.getElementById("globalContextMenu");
+ menu.style.display = "none";
+ });
+ }
+ }, [state, animation]);
return (
- <Menu
- id="globalContextMenu"
- menuItems={items}
- data={data}
- title={title}
- style={{
- position: "absolute",
- display: "none",
- zIndex: 999,
- }}
- closeMenu={closeMenu}
- />
+ <Animated.Flex initial={{ opacity: 0 }} animate={animation}>
+ <Menu
+ id="globalContextMenu"
+ menuItems={items}
+ data={data}
+ title={title}
+ style={{
+ position: "absolute",
+ display: "none",
+ zIndex: 999,
+ }}
+ closeMenu={closeMenu}
+ />
+ </Animated.Flex>
);
}
export default GlobalMenuWrapper;
diff --git a/apps/web/src/components/list-item/index.js b/apps/web/src/components/list-item/index.js
index 1ce4a61dc..562fc7306 100644
--- a/apps/web/src/components/list-item/index.js
+++ b/apps/web/src/components/list-item/index.js
@@ -64,6 +64,7 @@ function ListItem(props) {
window.dispatchEvent(
new CustomEvent("globalcontextmenu", {
detail: {
+ state: "open",
items,
data: props.menuData,
internalEvent: event,
diff --git a/apps/web/src/utils/useContextMenu.js b/apps/web/src/utils/useContextMenu.js
index fba743868..6d7241c93 100644
--- a/apps/web/src/utils/useContextMenu.js
+++ b/apps/web/src/utils/useContextMenu.js
@@ -7,12 +7,17 @@ function closeMenu() {
isOpening = false;
return;
}
- const menu = document.getElementById("globalContextMenu");
- menu.style.display = "none";
+ window.dispatchEvent(
+ new CustomEvent("globalcontextmenu", { detail: { state: "close" } })
+ );
window.removeEventListener("click", onWindowClick);
window.removeEventListener("keydown", onKeyDown);
}
+function getGlobalMenu() {
+ return document.getElementById("globalContextMenu");
+}
+
function onKeyDown(event) {
if (event.keyCode === 27) closeMenu();
}
@@ -22,18 +27,15 @@ function onWindowClick() {
}
// updated positionMenu function
-var lastTarget;
function openMenu(e) {
e.preventDefault();
- const menu = document.getElementById("globalContextMenu");
+ const menu = getGlobalMenu();
+ if (menu.style.display === "block") {
+ closeMenu();
+ return;
+ }
if (e.type === "click") {
isOpening = true;
- // make it work like a toggle button
- if (menu.style.display === "block" && e.target === lastTarget) {
- closeMenu();
- return;
- }
- lastTarget = e.target;
}
const clickCoords = getPosition(e);
@@ -57,7 +59,6 @@ function openMenu(e) {
} else {
menu.style.top = clickCoordsY + "px";
}
- menu.style.display = "block";
window.addEventListener("keydown", onKeyDown);
window.addEventListener("click", onWindowClick);
@@ -91,9 +92,14 @@ function useContextMenu() {
const [items, setItems] = useState([]);
const [data, setData] = useState({});
const [title, setTitle] = useState();
+ const [state, setState] = useState();
useEffect(() => {
const onGlobalContextMenu = (e) => {
- const { items, data, title, internalEvent } = e.detail;
+ const { items, data, title, internalEvent, state } = e.detail;
+ setState(state);
+ if (state === "close") {
+ return;
+ }
setItems(items);
setData(data);
setTitle(title);
@@ -104,7 +110,7 @@ function useContextMenu() {
window.removeEventListener("globalcontextmenu", onGlobalContextMenu);
};
}, []);
- return [items, data, title, closeMenu];
+ return [items, data, title, state, closeMenu];
}
export default useContextMenu;