feat(nui): Context menu Lua callbacks

This commit is contained in:
Luke
2022-04-01 21:22:14 +02:00
parent 8156a123f7
commit d6b21a0481
4 changed files with 79 additions and 5 deletions

View File

@@ -1,7 +1,7 @@
import { useNuiEvent } from "../../../hooks/useNuiEvent";
import { Box, Text, Flex, ScaleFade } from "@chakra-ui/react";
import { debugData } from "../../../utils/debugData";
import { useState } from "react";
import { useEffect, useState } from "react";
import { ContextMenuProps } from "../../../interfaces";
import Item from "./Item";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
@@ -52,6 +52,24 @@ const ContextMenu: React.FC = () => {
options: { "": { description: "", metadata: [] } },
});
// Hides the context menu on ESC
useEffect(() => {
if (!visible) return;
const keyHandler = (e: KeyboardEvent) => {
if (["Escape"].includes(e.code)) {
setVisible(false);
fetchNui("closeContext");
}
};
window.addEventListener("keydown", keyHandler);
return () => window.removeEventListener("keydown", keyHandler);
}, [visible]);
useNuiEvent("hideContext", () => setVisible(false));
useNuiEvent<ContextMenuProps>("showContext", async (data) => {
if (visible) {
setVisible(false);