mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-19 16:06:02 +01:00
feat(nui): Context menu Lua callbacks
This commit is contained in:
42
resource/interface/context/client.lua
Normal file
42
resource/interface/context/client.lua
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
local contextMenus = {}
|
||||||
|
|
||||||
|
local function showContext(id)
|
||||||
|
local data = contextMenus[id]
|
||||||
|
SetNuiFocus(true, true)
|
||||||
|
SendNUIMessage({
|
||||||
|
action = 'showContext',
|
||||||
|
data = {
|
||||||
|
title = data.title,
|
||||||
|
menu = data.menu,
|
||||||
|
options = data.options
|
||||||
|
}
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
local function registerContext(context)
|
||||||
|
for k, v in pairs(context) do
|
||||||
|
if type(k) == 'number' then
|
||||||
|
contextMenus[v.id] = v
|
||||||
|
else
|
||||||
|
contextMenus[context.id] = context
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
RegisterNUICallback('openContext', function(id)
|
||||||
|
showContext(id)
|
||||||
|
end)
|
||||||
|
|
||||||
|
RegisterNUICallback('clickContext', function(data)
|
||||||
|
if data.event then TriggerEvent(data.event, data.args) end
|
||||||
|
if data.serverEvent then TriggerServerEvent(data.serverEvent, data.args) end
|
||||||
|
SetNuiFocus(false, false)
|
||||||
|
SendNUIMessage({
|
||||||
|
action = 'hideContext'
|
||||||
|
})
|
||||||
|
end)
|
||||||
|
|
||||||
|
RegisterNUICallback('closeContext', function()
|
||||||
|
SetNuiFocus(false, false)
|
||||||
|
end)
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { useNuiEvent } from "../../../hooks/useNuiEvent";
|
import { useNuiEvent } from "../../../hooks/useNuiEvent";
|
||||||
import { Box, Text, Flex, ScaleFade } from "@chakra-ui/react";
|
import { Box, Text, Flex, ScaleFade } from "@chakra-ui/react";
|
||||||
import { debugData } from "../../../utils/debugData";
|
import { debugData } from "../../../utils/debugData";
|
||||||
import { useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { ContextMenuProps } from "../../../interfaces";
|
import { ContextMenuProps } from "../../../interfaces";
|
||||||
import Item from "./Item";
|
import Item from "./Item";
|
||||||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
||||||
@@ -52,6 +52,24 @@ const ContextMenu: React.FC = () => {
|
|||||||
options: { "": { description: "", metadata: [] } },
|
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) => {
|
useNuiEvent<ContextMenuProps>("showContext", async (data) => {
|
||||||
if (visible) {
|
if (visible) {
|
||||||
setVisible(false);
|
setVisible(false);
|
||||||
|
|||||||
@@ -13,13 +13,18 @@ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
|||||||
import { Option, ContextMenuProps } from "../../../interfaces";
|
import { Option, ContextMenuProps } from "../../../interfaces";
|
||||||
import { fetchNui } from "../../../utils/fetchNui";
|
import { fetchNui } from "../../../utils/fetchNui";
|
||||||
|
|
||||||
|
interface DataProps {
|
||||||
|
event?: string;
|
||||||
|
serverEvent?: string;
|
||||||
|
args?: any;
|
||||||
|
}
|
||||||
|
|
||||||
const openMenu = (id: string | undefined) => {
|
const openMenu = (id: string | undefined) => {
|
||||||
fetchNui<ContextMenuProps>("openContext", id);
|
fetchNui<ContextMenuProps>("openContext", id);
|
||||||
};
|
};
|
||||||
|
|
||||||
const clickContext = () => {
|
const clickContext = (data: DataProps) => {
|
||||||
// todo
|
fetchNui("clickContext", data);
|
||||||
fetchNui("clickContext");
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const Item: React.FC<{
|
const Item: React.FC<{
|
||||||
@@ -49,7 +54,13 @@ const Item: React.FC<{
|
|||||||
<Flex
|
<Flex
|
||||||
w="100%"
|
w="100%"
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
option[1].menu ? openMenu(option[1].menu) : clickContext()
|
option[1].menu
|
||||||
|
? openMenu(option[1].menu)
|
||||||
|
: clickContext({
|
||||||
|
event: option[1].event,
|
||||||
|
serverEvent: option[1].serverEvent,
|
||||||
|
args: option[1].args,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
<Box>
|
<Box>
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ export interface Option {
|
|||||||
menu?: string;
|
menu?: string;
|
||||||
description?: string;
|
description?: string;
|
||||||
metadata?: string[] | { [key: string]: any };
|
metadata?: string[] | { [key: string]: any };
|
||||||
|
event?: string;
|
||||||
|
serverEvent?: string;
|
||||||
|
args?: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Options {
|
export interface Options {
|
||||||
|
|||||||
Reference in New Issue
Block a user