fivem-nui-react 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -12,7 +12,8 @@ npm install fivem-nui-react
12
12
 
13
13
  - **useNuiEvent** - Listen for NUI messages from Lua
14
14
  - **fetchNui** - Send requests to Lua client and receive responses
15
- - **useNuiCallback** - Hook for NUI requests with loading/error states
15
+ - **useNuiCallback** - Hook for NUI requests with loading/error states and callback
16
+ - **useSendNui** - Hook for sending data without expecting a response
16
17
  - **isEnvBrowser** - Check if running in browser (debug mode)
17
18
  - **Mock data support** - Test your UI in browser with simulated responses
18
19
 
@@ -185,6 +186,59 @@ function PlayerInfo() {
185
186
 
186
187
  ---
187
188
 
189
+ ### useSendNui
190
+
191
+ A hook for sending data to the Lua client without expecting a response.
192
+
193
+ ```tsx
194
+ useSendNui<D>(
195
+ eventName: string,
196
+ options?: UseSendNuiOptions
197
+ ): [sendFn: (data?: D) => Promise<void>, state: { loading: boolean, error: Error | null }]
198
+ ```
199
+
200
+ **Parameters:**
201
+
202
+ - `eventName` - The NUI callback event name
203
+ - `options` - Optional mock delay configuration for browser testing
204
+
205
+ **Returns:**
206
+
207
+ - `sendFn` - Function to send data
208
+ - `state.loading` - Boolean indicating if request is in progress
209
+ - `state.error` - Error object if request failed
210
+
211
+ **Example:**
212
+
213
+ ```tsx
214
+ import { useSendNui } from "fivem-nui-react";
215
+
216
+ function CloseButton() {
217
+ const [closeUI, { loading }] = useSendNui<{ reason: string }>("closeUI");
218
+
219
+ const handleClose = () => {
220
+ closeUI({ reason: "user_clicked" });
221
+ };
222
+
223
+ return (
224
+ <button onClick={handleClose} disabled={loading}>
225
+ {loading ? "Closing..." : "Close"}
226
+ </button>
227
+ );
228
+ }
229
+ ```
230
+
231
+ **Lua side:**
232
+
233
+ ```lua
234
+ RegisterNUICallback("closeUI", function(data, cb)
235
+ SetNuiFocus(false, false)
236
+ cb("ok")
237
+ end)
238
+ ```
239
+
240
+ ---
241
+
188
242
  ### isEnvBrowser
189
243
 
190
244
  Check if running in browser (outside FiveM).
@@ -216,6 +270,7 @@ When `isEnvBrowser()` returns `true`:
216
270
  - `useNuiEvent` will trigger the handler with `mockData` after `mockDelay` milliseconds
217
271
  - `fetchNui` will return `mockData` after `mockDelay` milliseconds
218
272
  - `useNuiCallback` will call the callback with `mockData` after `mockDelay` milliseconds
273
+ - `useSendNui` will simulate a delay with `mockDelay` milliseconds
219
274
 
220
275
  **Options:**
221
276
 
package/dist/index.d.mts CHANGED
@@ -70,5 +70,26 @@ type UseNuiCallbackReturn<T, D> = [
70
70
  * }, [fetchPlayer]);
71
71
  */
72
72
  declare function useNuiCallback<T = unknown, D = unknown>(eventName: string, callback: (data: T) => void, options?: UseNuiCallbackOptions<T>): UseNuiCallbackReturn<T, D>;
73
+ interface UseSendNuiOptions {
74
+ mockDelay?: number;
75
+ }
76
+ interface UseSendNuiState {
77
+ loading: boolean;
78
+ error: Error | null;
79
+ }
80
+ type UseSendNuiReturn<D> = [(data?: D) => Promise<void>, UseSendNuiState];
81
+ /**
82
+ * React hook for sending data to NUI callback without expecting a response
83
+ * @param eventName - The callback event name
84
+ * @param options - Options for mock delay in browser mode
85
+ * @returns [sendFunction, { loading, error }]
86
+ * @example
87
+ * const [closeUI, { loading }] = useSendNui("closeUI");
88
+ *
89
+ * const handleClose = () => {
90
+ * closeUI({ reason: "user_clicked" });
91
+ * };
92
+ */
93
+ declare function useSendNui<D = unknown>(eventName: string, options?: UseSendNuiOptions): UseSendNuiReturn<D>;
73
94
 
74
- export { type FetchNuiOptions, type NuiEventHandler, type NuiMessageEvent, type UseNuiCallbackOptions, type UseNuiCallbackReturn, type UseNuiCallbackState, type UseNuiEventOptions, fetchNui, isEnvBrowser, useNuiCallback, useNuiEvent };
95
+ export { type FetchNuiOptions, type NuiEventHandler, type NuiMessageEvent, type UseNuiCallbackOptions, type UseNuiCallbackReturn, type UseNuiCallbackState, type UseNuiEventOptions, type UseSendNuiOptions, type UseSendNuiReturn, type UseSendNuiState, fetchNui, isEnvBrowser, useNuiCallback, useNuiEvent, useSendNui };
package/dist/index.d.ts CHANGED
@@ -70,5 +70,26 @@ type UseNuiCallbackReturn<T, D> = [
70
70
  * }, [fetchPlayer]);
71
71
  */
72
72
  declare function useNuiCallback<T = unknown, D = unknown>(eventName: string, callback: (data: T) => void, options?: UseNuiCallbackOptions<T>): UseNuiCallbackReturn<T, D>;
73
+ interface UseSendNuiOptions {
74
+ mockDelay?: number;
75
+ }
76
+ interface UseSendNuiState {
77
+ loading: boolean;
78
+ error: Error | null;
79
+ }
80
+ type UseSendNuiReturn<D> = [(data?: D) => Promise<void>, UseSendNuiState];
81
+ /**
82
+ * React hook for sending data to NUI callback without expecting a response
83
+ * @param eventName - The callback event name
84
+ * @param options - Options for mock delay in browser mode
85
+ * @returns [sendFunction, { loading, error }]
86
+ * @example
87
+ * const [closeUI, { loading }] = useSendNui("closeUI");
88
+ *
89
+ * const handleClose = () => {
90
+ * closeUI({ reason: "user_clicked" });
91
+ * };
92
+ */
93
+ declare function useSendNui<D = unknown>(eventName: string, options?: UseSendNuiOptions): UseSendNuiReturn<D>;
73
94
 
74
- export { type FetchNuiOptions, type NuiEventHandler, type NuiMessageEvent, type UseNuiCallbackOptions, type UseNuiCallbackReturn, type UseNuiCallbackState, type UseNuiEventOptions, fetchNui, isEnvBrowser, useNuiCallback, useNuiEvent };
95
+ export { type FetchNuiOptions, type NuiEventHandler, type NuiMessageEvent, type UseNuiCallbackOptions, type UseNuiCallbackReturn, type UseNuiCallbackState, type UseNuiEventOptions, type UseSendNuiOptions, type UseSendNuiReturn, type UseSendNuiState, fetchNui, isEnvBrowser, useNuiCallback, useNuiEvent, useSendNui };
package/dist/index.js CHANGED
@@ -22,7 +22,8 @@ __export(index_exports, {
22
22
  fetchNui: () => fetchNui,
23
23
  isEnvBrowser: () => isEnvBrowser,
24
24
  useNuiCallback: () => useNuiCallback,
25
- useNuiEvent: () => useNuiEvent
25
+ useNuiEvent: () => useNuiEvent,
26
+ useSendNui: () => useSendNui
26
27
  });
27
28
  module.exports = __toCommonJS(index_exports);
28
29
  var import_react = require("react");
@@ -96,10 +97,43 @@ function useNuiCallback(eventName, callback, options) {
96
97
  );
97
98
  return [fetch2, state];
98
99
  }
100
+ function useSendNui(eventName, options) {
101
+ const [state, setState] = (0, import_react.useState)({
102
+ loading: false,
103
+ error: null
104
+ });
105
+ const send = (0, import_react.useCallback)(
106
+ async (data) => {
107
+ setState({ loading: true, error: null });
108
+ try {
109
+ if (isEnvBrowser()) {
110
+ const delay = (options == null ? void 0 : options.mockDelay) ?? 500;
111
+ await new Promise((resolve) => setTimeout(resolve, delay));
112
+ } else {
113
+ await fetch(`https://${resourceName}/${eventName}`, {
114
+ method: "POST",
115
+ headers: {
116
+ "Content-Type": "application/json; charset=UTF-8"
117
+ },
118
+ body: JSON.stringify(data ?? {})
119
+ });
120
+ }
121
+ setState({ loading: false, error: null });
122
+ } catch (err) {
123
+ const error = err instanceof Error ? err : new Error(String(err));
124
+ setState({ loading: false, error });
125
+ throw error;
126
+ }
127
+ },
128
+ [eventName, options]
129
+ );
130
+ return [send, state];
131
+ }
99
132
  // Annotate the CommonJS export names for ESM import in node:
100
133
  0 && (module.exports = {
101
134
  fetchNui,
102
135
  isEnvBrowser,
103
136
  useNuiCallback,
104
- useNuiEvent
137
+ useNuiEvent,
138
+ useSendNui
105
139
  });
package/dist/index.mjs CHANGED
@@ -70,9 +70,42 @@ function useNuiCallback(eventName, callback, options) {
70
70
  );
71
71
  return [fetch2, state];
72
72
  }
73
+ function useSendNui(eventName, options) {
74
+ const [state, setState] = useState({
75
+ loading: false,
76
+ error: null
77
+ });
78
+ const send = useCallback(
79
+ async (data) => {
80
+ setState({ loading: true, error: null });
81
+ try {
82
+ if (isEnvBrowser()) {
83
+ const delay = (options == null ? void 0 : options.mockDelay) ?? 500;
84
+ await new Promise((resolve) => setTimeout(resolve, delay));
85
+ } else {
86
+ await fetch(`https://${resourceName}/${eventName}`, {
87
+ method: "POST",
88
+ headers: {
89
+ "Content-Type": "application/json; charset=UTF-8"
90
+ },
91
+ body: JSON.stringify(data ?? {})
92
+ });
93
+ }
94
+ setState({ loading: false, error: null });
95
+ } catch (err) {
96
+ const error = err instanceof Error ? err : new Error(String(err));
97
+ setState({ loading: false, error });
98
+ throw error;
99
+ }
100
+ },
101
+ [eventName, options]
102
+ );
103
+ return [send, state];
104
+ }
73
105
  export {
74
106
  fetchNui,
75
107
  isEnvBrowser,
76
108
  useNuiCallback,
77
- useNuiEvent
109
+ useNuiEvent,
110
+ useSendNui
78
111
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fivem-nui-react",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "React hooks and utilities for FiveM NUI development",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",