@fivem-nui/svelte-components 0.0.0-stage → 1.0.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 +101 -2
- package/dist/components/actions/NuiButton.svelte +292 -0
- package/dist/components/actions/NuiButton.svelte.d.ts +60 -0
- package/dist/components/data/NuiBadge.svelte +277 -0
- package/dist/components/data/NuiBadge.svelte.d.ts +74 -0
- package/dist/components/data/NuiCodeBlock.svelte +256 -0
- package/dist/components/data/NuiCodeBlock.svelte.d.ts +49 -0
- package/dist/components/data/NuiList.svelte +341 -0
- package/dist/components/data/NuiList.svelte.d.ts +52 -0
- package/dist/components/data/NuiProgressBar.svelte +264 -0
- package/dist/components/data/NuiProgressBar.svelte.d.ts +51 -0
- package/dist/components/data/NuiRichText.svelte +260 -0
- package/dist/components/data/NuiRichText.svelte.d.ts +33 -0
- package/dist/components/data/NuiTable.svelte +470 -0
- package/dist/components/data/NuiTable.svelte.d.ts +53 -0
- package/dist/components/data/chart/NuiChart.svelte +384 -0
- package/dist/components/data/chart/NuiChart.svelte.d.ts +49 -0
- package/dist/components/data/chart/color.d.ts +9 -0
- package/dist/components/data/chart/color.js +78 -0
- package/dist/components/data/chart/index.d.ts +10 -0
- package/dist/components/data/chart/index.js +10 -0
- package/dist/components/data/chart/registry.d.ts +14 -0
- package/dist/components/data/chart/registry.js +53 -0
- package/dist/components/data/chart/renderers/bar.d.ts +8 -0
- package/dist/components/data/chart/renderers/bar.js +79 -0
- package/dist/components/data/chart/renderers/base.d.ts +18 -0
- package/dist/components/data/chart/renderers/base.js +10 -0
- package/dist/components/data/chart/renderers/line.d.ts +8 -0
- package/dist/components/data/chart/renderers/line.js +89 -0
- package/dist/components/data/chart/renderers/pie.d.ts +10 -0
- package/dist/components/data/chart/renderers/pie.js +55 -0
- package/dist/components/data/chart/scale.d.ts +22 -0
- package/dist/components/data/chart/scale.js +101 -0
- package/dist/components/data/chart/types.d.ts +84 -0
- package/dist/components/data/chart/types.js +1 -0
- package/dist/components/data/chart/validator.d.ts +10 -0
- package/dist/components/data/chart/validator.js +82 -0
- package/dist/components/forms/NuiCheckbox.svelte +218 -0
- package/dist/components/forms/NuiCheckbox.svelte.d.ts +37 -0
- package/dist/components/forms/NuiDatepicker.svelte +755 -0
- package/dist/components/forms/NuiDatepicker.svelte.d.ts +34 -0
- package/dist/components/forms/NuiFileInput.svelte +1047 -0
- package/dist/components/forms/NuiFileInput.svelte.d.ts +93 -0
- package/dist/components/forms/NuiFileInput.types.d.ts +28 -0
- package/dist/components/forms/NuiFileInput.types.js +1 -0
- package/dist/components/forms/NuiForm.svelte +391 -0
- package/dist/components/forms/NuiForm.svelte.d.ts +69 -0
- package/dist/components/forms/NuiInput.svelte +296 -0
- package/dist/components/forms/NuiInput.svelte.d.ts +62 -0
- package/dist/components/forms/NuiRadioGroup.svelte +238 -0
- package/dist/components/forms/NuiRadioGroup.svelte.d.ts +47 -0
- package/dist/components/forms/NuiSelect.svelte +819 -0
- package/dist/components/forms/NuiSelect.svelte.d.ts +49 -0
- package/dist/components/layout/NuiCard.svelte +314 -0
- package/dist/components/layout/NuiCard.svelte.d.ts +48 -0
- package/dist/components/layout/NuiCardGrid.svelte +98 -0
- package/dist/components/layout/NuiCardGrid.svelte.d.ts +22 -0
- package/dist/components/layout/NuiFlex.svelte +121 -0
- package/dist/components/layout/NuiFlex.svelte.d.ts +23 -0
- package/dist/components/layout/NuiGrid.svelte +66 -0
- package/dist/components/layout/NuiGrid.svelte.d.ts +14 -0
- package/dist/components/layout/NuiStack.svelte +138 -0
- package/dist/components/layout/NuiStack.svelte.d.ts +26 -0
- package/dist/components/navigation/NuiAccordion.svelte +323 -0
- package/dist/components/navigation/NuiAccordion.svelte.d.ts +38 -0
- package/dist/components/navigation/NuiBreadcrumbs.svelte +133 -0
- package/dist/components/navigation/NuiBreadcrumbs.svelte.d.ts +19 -0
- package/dist/components/navigation/NuiMenu.svelte +596 -0
- package/dist/components/navigation/NuiMenu.svelte.d.ts +28 -0
- package/dist/components/navigation/NuiPagination.svelte +306 -0
- package/dist/components/navigation/NuiPagination.svelte.d.ts +40 -0
- package/dist/components/navigation/NuiSidebar.svelte +186 -0
- package/dist/components/navigation/NuiSidebar.svelte.d.ts +26 -0
- package/dist/components/navigation/NuiTabs.svelte +249 -0
- package/dist/components/navigation/NuiTabs.svelte.d.ts +31 -0
- package/dist/components/overlay/NuiDialog.svelte +118 -0
- package/dist/components/overlay/NuiDialog.svelte.d.ts +34 -0
- package/dist/components/overlay/NuiModal.svelte +992 -0
- package/dist/components/overlay/NuiModal.svelte.d.ts +72 -0
- package/dist/components/overlay/NuiTooltip.svelte +159 -0
- package/dist/components/overlay/NuiTooltip.svelte.d.ts +13 -0
- package/dist/composables/useClickOutside.d.ts +43 -0
- package/dist/composables/useClickOutside.js +73 -0
- package/dist/composables/useFormValidation.d.ts +23 -0
- package/dist/composables/useFormValidation.js +72 -0
- package/dist/composables/useIdle.svelte.d.ts +18 -0
- package/dist/composables/useIdle.svelte.js +144 -0
- package/dist/composables/useKeyboardShortcut.svelte.d.ts +114 -0
- package/dist/composables/useKeyboardShortcut.svelte.js +153 -0
- package/dist/composables/useLocalStorage.svelte.d.ts +22 -0
- package/dist/composables/useLocalStorage.svelte.js +105 -0
- package/dist/composables/useMediaQuery.svelte.d.ts +51 -0
- package/dist/composables/useMediaQuery.svelte.js +80 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.js +42 -0
- package/package.json +58 -3
- package/src/components/actions/NuiButton.svelte +292 -0
- package/src/components/data/NuiBadge.svelte +277 -0
- package/src/components/data/NuiCodeBlock.svelte +256 -0
- package/src/components/data/NuiList.svelte +341 -0
- package/src/components/data/NuiProgressBar.svelte +264 -0
- package/src/components/data/NuiRichText.svelte +260 -0
- package/src/components/data/NuiTable.svelte +470 -0
- package/src/components/data/chart/NuiChart.svelte +384 -0
- package/src/components/data/chart/color.ts +83 -0
- package/src/components/data/chart/index.ts +10 -0
- package/src/components/data/chart/registry.ts +71 -0
- package/src/components/data/chart/renderers/bar.ts +90 -0
- package/src/components/data/chart/renderers/base.ts +32 -0
- package/src/components/data/chart/renderers/line.ts +99 -0
- package/src/components/data/chart/renderers/pie.ts +66 -0
- package/src/components/data/chart/scale.ts +139 -0
- package/src/components/data/chart/types.ts +98 -0
- package/src/components/data/chart/validator.ts +98 -0
- package/src/components/forms/NuiCheckbox.svelte +218 -0
- package/src/components/forms/NuiDatepicker.svelte +755 -0
- package/src/components/forms/NuiFileInput.svelte +1047 -0
- package/src/components/forms/NuiFileInput.types.ts +29 -0
- package/src/components/forms/NuiForm.svelte +391 -0
- package/src/components/forms/NuiInput.svelte +296 -0
- package/src/components/forms/NuiRadioGroup.svelte +238 -0
- package/src/components/forms/NuiSelect.svelte +819 -0
- package/src/components/layout/NuiCard.svelte +314 -0
- package/src/components/layout/NuiCardGrid.svelte +98 -0
- package/src/components/layout/NuiFlex.svelte +121 -0
- package/src/components/layout/NuiGrid.svelte +66 -0
- package/src/components/layout/NuiStack.svelte +138 -0
- package/src/components/navigation/NuiAccordion.svelte +323 -0
- package/src/components/navigation/NuiBreadcrumbs.svelte +133 -0
- package/src/components/navigation/NuiMenu.svelte +596 -0
- package/src/components/navigation/NuiPagination.svelte +306 -0
- package/src/components/navigation/NuiSidebar.svelte +186 -0
- package/src/components/navigation/NuiTabs.svelte +249 -0
- package/src/components/overlay/NuiDialog.svelte +118 -0
- package/src/components/overlay/NuiModal.svelte +992 -0
- package/src/components/overlay/NuiTooltip.svelte +159 -0
- package/src/composables/useClickOutside.ts +132 -0
- package/src/composables/useFormValidation.ts +105 -0
- package/src/composables/useIdle.svelte.ts +191 -0
- package/src/composables/useKeyboardShortcut.svelte.ts +278 -0
- package/src/composables/useLocalStorage.svelte.ts +137 -0
- package/src/composables/useMediaQuery.svelte.ts +102 -0
- package/src/index.ts +66 -0
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
<script module lang="ts">
|
|
2
|
+
export interface Tab {
|
|
3
|
+
id: string;
|
|
4
|
+
label: string;
|
|
5
|
+
disabled?: boolean;
|
|
6
|
+
icon?: string;
|
|
7
|
+
}
|
|
8
|
+
</script>
|
|
9
|
+
|
|
10
|
+
<script lang="ts">
|
|
11
|
+
import { nui } from "@fivem-nui/svelte";
|
|
12
|
+
import type { SvelteNuiClient } from "@fivem-nui/svelte";
|
|
13
|
+
import type { Snippet } from "svelte";
|
|
14
|
+
import type { HTMLAttributes } from "svelte/elements";
|
|
15
|
+
|
|
16
|
+
interface Props extends Omit<HTMLAttributes<HTMLDivElement>, "children"> {
|
|
17
|
+
tabs: Tab[];
|
|
18
|
+
client?: SvelteNuiClient;
|
|
19
|
+
/** Active tab id. Supports `bind:activeTab` */
|
|
20
|
+
activeTab?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Called after the user selects a tab (via click or keyboard navigation).
|
|
23
|
+
* Note: programmatic fallback adjustments (such as when `tabs` list updates or the active tab is removed) do not trigger this callback.
|
|
24
|
+
*/
|
|
25
|
+
onTabChange?: (tabId: string) => void;
|
|
26
|
+
/** Tab used when `activeTab` is empty or invalid (default: first enabled tab) */
|
|
27
|
+
initialTab?: string;
|
|
28
|
+
/** Event name sent to FiveM on every change */
|
|
29
|
+
eventName?: string;
|
|
30
|
+
/** Accessible name of the tab list */
|
|
31
|
+
label?: string;
|
|
32
|
+
/** Panel content. Receives the active tab id */
|
|
33
|
+
children?: Snippet<[string]>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const uid = $props.id();
|
|
37
|
+
|
|
38
|
+
let {
|
|
39
|
+
tabs = [],
|
|
40
|
+
client = nui,
|
|
41
|
+
activeTab = $bindable(""),
|
|
42
|
+
onTabChange,
|
|
43
|
+
initialTab,
|
|
44
|
+
eventName,
|
|
45
|
+
label = "Tabs",
|
|
46
|
+
children,
|
|
47
|
+
class: className = "",
|
|
48
|
+
...rest
|
|
49
|
+
}: Props = $props();
|
|
50
|
+
|
|
51
|
+
const tabId = (id: string) => `nui-tabs-${uid}-${id}-tab`;
|
|
52
|
+
const panelId = `nui-tabs-${uid}-panel`;
|
|
53
|
+
|
|
54
|
+
const firstEnabled = $derived(tabs.find((t) => !t.disabled)?.id);
|
|
55
|
+
|
|
56
|
+
// Keep `activeTab` valid: it must exist in `tabs` and must not be disabled
|
|
57
|
+
const resolvedTab = $derived.by(() => {
|
|
58
|
+
const valid = (id?: string) =>
|
|
59
|
+
id ? tabs.some((t) => t.id === id && !t.disabled) : false;
|
|
60
|
+
if (valid(activeTab)) return activeTab;
|
|
61
|
+
if (valid(initialTab)) return initialTab as string;
|
|
62
|
+
return firstEnabled ?? "";
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
$effect.pre(() => {
|
|
66
|
+
if (resolvedTab !== activeTab) activeTab = resolvedTab;
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
function emit(from: string, to: string) {
|
|
70
|
+
onTabChange?.(to);
|
|
71
|
+
if (eventName) {
|
|
72
|
+
try {
|
|
73
|
+
const res: unknown = client?.send(eventName, { from, to });
|
|
74
|
+
if (res && typeof (res as { catch?: unknown }).catch === "function") {
|
|
75
|
+
(res as Promise<unknown>).catch((err: unknown) => {
|
|
76
|
+
console.warn(
|
|
77
|
+
"[fivem-nui-components] NuiTabs: failed to send event",
|
|
78
|
+
err,
|
|
79
|
+
);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
} catch (err) {
|
|
83
|
+
console.warn(
|
|
84
|
+
"[fivem-nui-components] NuiTabs: failed to send event",
|
|
85
|
+
err,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function selectTab(id: string) {
|
|
92
|
+
const tab = tabs.find((t) => t.id === id);
|
|
93
|
+
if (!tab || tab.disabled || resolvedTab === id) return;
|
|
94
|
+
const from = resolvedTab;
|
|
95
|
+
activeTab = id;
|
|
96
|
+
emit(from, id);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function onKeydown(event: KeyboardEvent) {
|
|
100
|
+
const keys = ["ArrowLeft", "ArrowRight", "Home", "End"];
|
|
101
|
+
if (!keys.includes(event.key)) return;
|
|
102
|
+
|
|
103
|
+
const enabled = tabs.filter((t) => !t.disabled);
|
|
104
|
+
if (enabled.length === 0) return;
|
|
105
|
+
|
|
106
|
+
const current = enabled.findIndex((t) => t.id === resolvedTab);
|
|
107
|
+
let next = current;
|
|
108
|
+
if (event.key === "ArrowRight") next = (current + 1) % enabled.length;
|
|
109
|
+
else if (event.key === "ArrowLeft")
|
|
110
|
+
next = (current - 1 + enabled.length) % enabled.length;
|
|
111
|
+
else if (event.key === "Home") next = 0;
|
|
112
|
+
else if (event.key === "End") next = enabled.length - 1;
|
|
113
|
+
|
|
114
|
+
event.preventDefault();
|
|
115
|
+
const target = enabled[next];
|
|
116
|
+
selectTab(target.id);
|
|
117
|
+
(event.currentTarget as HTMLElement)
|
|
118
|
+
.querySelector<HTMLElement>(`#${CSS.escape(tabId(target.id))}`)
|
|
119
|
+
?.focus();
|
|
120
|
+
}
|
|
121
|
+
</script>
|
|
122
|
+
|
|
123
|
+
<div {...rest} class={["nui-tabs", className]}>
|
|
124
|
+
<div
|
|
125
|
+
class="nui-tabs__list"
|
|
126
|
+
role="tablist"
|
|
127
|
+
aria-label={label}
|
|
128
|
+
tabindex="-1"
|
|
129
|
+
onkeydown={onKeydown}
|
|
130
|
+
>
|
|
131
|
+
{#each tabs as tab (tab.id)}
|
|
132
|
+
{@const selected = tab.id === resolvedTab}
|
|
133
|
+
<button
|
|
134
|
+
type="button"
|
|
135
|
+
id={tabId(tab.id)}
|
|
136
|
+
class="nui-tabs__tab"
|
|
137
|
+
class:nui-tabs__tab--active={selected}
|
|
138
|
+
role="tab"
|
|
139
|
+
aria-selected={selected}
|
|
140
|
+
aria-controls={children ? panelId : undefined}
|
|
141
|
+
tabindex={selected ? 0 : -1}
|
|
142
|
+
disabled={tab.disabled}
|
|
143
|
+
onclick={() => selectTab(tab.id)}
|
|
144
|
+
>
|
|
145
|
+
{#if tab.icon}<span class="nui-tabs__icon" aria-hidden="true"
|
|
146
|
+
>{tab.icon}</span
|
|
147
|
+
>{/if}
|
|
148
|
+
<span>{tab.label}</span>
|
|
149
|
+
</button>
|
|
150
|
+
{/each}
|
|
151
|
+
</div>
|
|
152
|
+
|
|
153
|
+
{#if children && resolvedTab}
|
|
154
|
+
<div
|
|
155
|
+
id={panelId}
|
|
156
|
+
class="nui-tabs__content"
|
|
157
|
+
role="tabpanel"
|
|
158
|
+
tabindex="0"
|
|
159
|
+
aria-labelledby={tabId(resolvedTab)}
|
|
160
|
+
>
|
|
161
|
+
{@render children(resolvedTab)}
|
|
162
|
+
</div>
|
|
163
|
+
{/if}
|
|
164
|
+
</div>
|
|
165
|
+
|
|
166
|
+
<style>
|
|
167
|
+
.nui-tabs {
|
|
168
|
+
display: flex;
|
|
169
|
+
flex-direction: column;
|
|
170
|
+
font-family: inherit;
|
|
171
|
+
box-sizing: border-box;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
.nui-tabs__list {
|
|
175
|
+
display: flex;
|
|
176
|
+
gap: 0.25rem;
|
|
177
|
+
border-bottom: 1px solid var(--nui-border, #27272a);
|
|
178
|
+
background: var(--nui-surface-alt, #101012);
|
|
179
|
+
padding: 0.25rem 0.5rem 0;
|
|
180
|
+
overflow-x: auto;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
.nui-tabs__list:focus {
|
|
184
|
+
outline: none;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
.nui-tabs__tab {
|
|
188
|
+
display: inline-flex;
|
|
189
|
+
align-items: center;
|
|
190
|
+
gap: 0.4rem;
|
|
191
|
+
padding: 0.625rem 1rem;
|
|
192
|
+
border: 1px solid transparent;
|
|
193
|
+
border-bottom: 2px solid transparent;
|
|
194
|
+
border-radius: var(--nui-radius, 0.375rem) var(--nui-radius, 0.375rem) 0 0;
|
|
195
|
+
background: transparent;
|
|
196
|
+
color: var(--nui-fg-secondary, #8b8b99);
|
|
197
|
+
font: inherit;
|
|
198
|
+
font-size: 0.875rem;
|
|
199
|
+
font-weight: 500;
|
|
200
|
+
white-space: nowrap;
|
|
201
|
+
cursor: pointer;
|
|
202
|
+
transition:
|
|
203
|
+
background 0.15s ease,
|
|
204
|
+
color 0.15s ease,
|
|
205
|
+
border-color 0.15s ease;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
.nui-tabs__tab:hover:not(:disabled) {
|
|
209
|
+
background: var(--nui-hover, rgb(255 255 255 / 0.06));
|
|
210
|
+
color: var(--nui-fg, #fafafa);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
.nui-tabs__tab:focus-visible {
|
|
214
|
+
outline: 2px solid var(--nui-primary, #3b82f6);
|
|
215
|
+
outline-offset: -2px;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
.nui-tabs__tab--active {
|
|
219
|
+
border-color: var(--nui-border, #27272a);
|
|
220
|
+
border-bottom-color: var(--nui-primary, #3b82f6);
|
|
221
|
+
background: var(--nui-surface, #18181b);
|
|
222
|
+
color: var(--nui-fg, #fafafa);
|
|
223
|
+
font-weight: 600;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
.nui-tabs__tab:disabled {
|
|
227
|
+
opacity: 0.4;
|
|
228
|
+
cursor: not-allowed;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
.nui-tabs__content {
|
|
232
|
+
flex: 1;
|
|
233
|
+
padding: 1rem;
|
|
234
|
+
overflow-y: auto;
|
|
235
|
+
background: var(--nui-surface, #18181b);
|
|
236
|
+
color: var(--nui-fg, #fafafa);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
.nui-tabs__content:focus-visible {
|
|
240
|
+
outline: 2px solid var(--nui-primary, #3b82f6);
|
|
241
|
+
outline-offset: -2px;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
@media (prefers-reduced-motion: reduce) {
|
|
245
|
+
.nui-tabs__tab {
|
|
246
|
+
transition: none;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
</style>
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
<script module lang="ts">
|
|
2
|
+
export interface NuiDialogAction {
|
|
3
|
+
id: string;
|
|
4
|
+
label: string;
|
|
5
|
+
variant?: "primary" | "secondary" | "danger";
|
|
6
|
+
disabled?: boolean;
|
|
7
|
+
}
|
|
8
|
+
</script>
|
|
9
|
+
|
|
10
|
+
<script lang="ts">
|
|
11
|
+
import type { Snippet } from "svelte";
|
|
12
|
+
import type { HTMLAttributes } from "svelte/elements";
|
|
13
|
+
import { nui } from "@fivem-nui/svelte";
|
|
14
|
+
import type { SvelteNuiClient } from "@fivem-nui/svelte";
|
|
15
|
+
import NuiModal from "./NuiModal.svelte";
|
|
16
|
+
import type { ModalAction } from "./NuiModal.svelte";
|
|
17
|
+
|
|
18
|
+
interface Props
|
|
19
|
+
extends Omit<
|
|
20
|
+
HTMLAttributes<HTMLDivElement>,
|
|
21
|
+
"children" | "title" | "role"
|
|
22
|
+
> {
|
|
23
|
+
open?: boolean;
|
|
24
|
+
title?: string;
|
|
25
|
+
message?: string;
|
|
26
|
+
actions?: NuiDialogAction[];
|
|
27
|
+
client?: SvelteNuiClient;
|
|
28
|
+
/** Event sent to the game when an action is chosen. */
|
|
29
|
+
eventName?: string;
|
|
30
|
+
/** Event sent when dismissed through Escape or the backdrop. */
|
|
31
|
+
cancelEvent?: string;
|
|
32
|
+
/** Receives the selected action id, or null when cancelled. */
|
|
33
|
+
onAction?: (actionId: string | null) => void;
|
|
34
|
+
/** Allow dismissal through Escape and the backdrop. */
|
|
35
|
+
dismissible?: boolean;
|
|
36
|
+
/** Explicit semantic role; danger styling does not imply alertdialog. */
|
|
37
|
+
role?: "dialog" | "alertdialog";
|
|
38
|
+
/** Action id to focus first; defaults to primary, then first focusable. */
|
|
39
|
+
initialFocus?: string;
|
|
40
|
+
/** Accessible name used when no title is provided. */
|
|
41
|
+
label?: string;
|
|
42
|
+
children?: Snippet;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
let {
|
|
46
|
+
open = $bindable(false),
|
|
47
|
+
title = "",
|
|
48
|
+
message = "",
|
|
49
|
+
actions = [{ id: "ok", label: "OK", variant: "primary" }],
|
|
50
|
+
client = nui,
|
|
51
|
+
eventName = "",
|
|
52
|
+
cancelEvent = "",
|
|
53
|
+
onAction,
|
|
54
|
+
dismissible = true,
|
|
55
|
+
role = "dialog",
|
|
56
|
+
initialFocus,
|
|
57
|
+
label = "Dialog",
|
|
58
|
+
children,
|
|
59
|
+
...rest
|
|
60
|
+
}: Props = $props();
|
|
61
|
+
|
|
62
|
+
function send(name: string, payload: Record<string, unknown>) {
|
|
63
|
+
try {
|
|
64
|
+
void Promise.resolve(client.send(name, payload)).catch(
|
|
65
|
+
(error: unknown) => {
|
|
66
|
+
console.warn(
|
|
67
|
+
"[fivem-nui-components] NuiDialog: failed to send event",
|
|
68
|
+
error,
|
|
69
|
+
);
|
|
70
|
+
},
|
|
71
|
+
);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
console.warn(
|
|
74
|
+
"[fivem-nui-components] NuiDialog: failed to send event",
|
|
75
|
+
error,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function finishWithAction(actionId: string | null) {
|
|
81
|
+
open = false;
|
|
82
|
+
try {
|
|
83
|
+
onAction?.(actionId);
|
|
84
|
+
} finally {
|
|
85
|
+
if (actionId === null) {
|
|
86
|
+
if (cancelEvent) send(cancelEvent, { cancelled: true });
|
|
87
|
+
} else if (eventName) {
|
|
88
|
+
send(eventName, { action: actionId });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const mappedActions: ModalAction[] = $derived(
|
|
94
|
+
actions.map((action) => ({
|
|
95
|
+
id: action.id,
|
|
96
|
+
label: action.label,
|
|
97
|
+
variant: action.variant,
|
|
98
|
+
disabled: action.disabled,
|
|
99
|
+
onclick: () => finishWithAction(action.id),
|
|
100
|
+
})),
|
|
101
|
+
);
|
|
102
|
+
</script>
|
|
103
|
+
|
|
104
|
+
<NuiModal
|
|
105
|
+
{...rest}
|
|
106
|
+
bind:open
|
|
107
|
+
{title}
|
|
108
|
+
description={message}
|
|
109
|
+
actions={mappedActions}
|
|
110
|
+
closable={dismissible}
|
|
111
|
+
showClose={false}
|
|
112
|
+
size="sm"
|
|
113
|
+
{role}
|
|
114
|
+
{initialFocus}
|
|
115
|
+
labels={{ dialog: label }}
|
|
116
|
+
onClose={() => finishWithAction(null)}
|
|
117
|
+
{children}
|
|
118
|
+
/>
|