@cfast/ui 0.0.1

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.
Files changed (49) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +727 -0
  3. package/dist/chunk-755IRYDN.js +941 -0
  4. package/dist/chunk-7SNK37GF.js +418 -0
  5. package/dist/chunk-ASMYTWTR.js +356 -0
  6. package/dist/chunk-B2XXH5V4.js +66 -0
  7. package/dist/chunk-BQMXYYEV.js +348 -0
  8. package/dist/chunk-DTKBXCTU.js +211 -0
  9. package/dist/chunk-EYIBATYR.js +33 -0
  10. package/dist/chunk-FPZAQ2YQ.js +474 -0
  11. package/dist/chunk-G2OU4BYC.js +205 -0
  12. package/dist/chunk-JEGEIQ3R.js +925 -0
  13. package/dist/chunk-JUNLQJ6H.js +1013 -0
  14. package/dist/chunk-NRGMW3JA.js +906 -0
  15. package/dist/chunk-Q6FPL2OJ.js +1086 -0
  16. package/dist/chunk-QHWAGKNW.js +456 -0
  17. package/dist/chunk-QZT62CGJ.js +924 -0
  18. package/dist/chunk-RDTUEOLK.js +486 -0
  19. package/dist/chunk-RESL4IJJ.js +112 -0
  20. package/dist/chunk-UDCWQUTR.js +221 -0
  21. package/dist/chunk-UE7PZOIJ.js +11 -0
  22. package/dist/chunk-UTZTHGNE.js +84 -0
  23. package/dist/chunk-UVRXMOX5.js +439 -0
  24. package/dist/chunk-XFD3N2D4.js +161 -0
  25. package/dist/client-CXIHCQtA.d.ts +274 -0
  26. package/dist/client.d.ts +617 -0
  27. package/dist/client.js +54 -0
  28. package/dist/index.d.ts +415 -0
  29. package/dist/index.js +296 -0
  30. package/dist/joy.d.ts +199 -0
  31. package/dist/joy.js +1150 -0
  32. package/dist/permission-gate-DVmY42oz.d.ts +1269 -0
  33. package/dist/permission-gate-apt9T9Mu.d.ts +1256 -0
  34. package/dist/types-1bAiH2uK.d.ts +392 -0
  35. package/dist/types-BX6u5sAd.d.ts +403 -0
  36. package/dist/types-BpdY7w5l.d.ts +403 -0
  37. package/dist/types-BrepeVp8.d.ts +403 -0
  38. package/dist/types-BvAqMZhn.d.ts +403 -0
  39. package/dist/types-C74nSscq.d.ts +403 -0
  40. package/dist/types-DD1Cpx8F.d.ts +403 -0
  41. package/dist/types-DHUhQwJn.d.ts +403 -0
  42. package/dist/types-DZSJNt_M.d.ts +392 -0
  43. package/dist/types-DaaJiIjW.d.ts +391 -0
  44. package/dist/types-LUpWJwps.d.ts +403 -0
  45. package/dist/types-a7zVU6WE.d.ts +394 -0
  46. package/dist/types-biJTHMcH.d.ts +403 -0
  47. package/dist/types-ow_qSEYJ.d.ts +392 -0
  48. package/dist/types-wnLasZaB.d.ts +1234 -0
  49. package/package.json +88 -0
@@ -0,0 +1,418 @@
1
+ import {
2
+ useActionStatus,
3
+ useToast
4
+ } from "./chunk-RESL4IJJ.js";
5
+
6
+ // src/plugin.ts
7
+ import { createContext, useContext, createElement as createElement2 } from "react";
8
+
9
+ // src/headless-defaults.ts
10
+ import { createElement } from "react";
11
+ var headlessDefaults = {
12
+ // Actions
13
+ button: ({ children, onClick, disabled, loading, type }) => createElement(
14
+ "button",
15
+ { onClick, disabled: disabled || loading, type: type ?? "button" },
16
+ loading ? "Loading..." : children
17
+ ),
18
+ tooltip: ({ children, title }) => createElement("span", { title }, children),
19
+ confirmDialog: ({ open, onClose, onConfirm, title, description, confirmLabel, cancelLabel }) => open ? createElement(
20
+ "dialog",
21
+ { open: true },
22
+ createElement("p", null, createElement("strong", null, title)),
23
+ description ? createElement("p", null, description) : null,
24
+ createElement(
25
+ "div",
26
+ null,
27
+ createElement("button", { onClick: onClose }, cancelLabel ?? "Cancel"),
28
+ createElement("button", { onClick: onConfirm }, confirmLabel ?? "Confirm")
29
+ )
30
+ ) : null,
31
+ // Data display
32
+ table: ({ children }) => createElement("table", null, children),
33
+ tableHead: ({ children }) => createElement("thead", null, children),
34
+ tableBody: ({ children }) => createElement("tbody", null, children),
35
+ tableRow: ({ children, onClick }) => createElement("tr", { onClick }, children),
36
+ tableCell: ({ children, header, sortable, sortDirection, onSort }) => createElement(
37
+ header ? "th" : "td",
38
+ {
39
+ onClick: sortable ? onSort : void 0,
40
+ style: sortable ? { cursor: "pointer" } : void 0
41
+ },
42
+ children,
43
+ sortable && sortDirection ? sortDirection === "asc" ? " \u2191" : " \u2193" : null
44
+ ),
45
+ chip: ({ children, size }) => createElement(
46
+ "span",
47
+ {
48
+ style: {
49
+ display: "inline-block",
50
+ padding: size === "sm" ? "1px 6px" : "2px 8px",
51
+ borderRadius: "12px",
52
+ fontSize: size === "sm" ? "12px" : "14px",
53
+ backgroundColor: "#eee"
54
+ }
55
+ },
56
+ children
57
+ ),
58
+ // Layout
59
+ appShell: ({ children, sidebar, header }) => createElement(
60
+ "div",
61
+ { style: { display: "flex", minHeight: "100vh" } },
62
+ sidebar ? createElement("nav", null, sidebar) : null,
63
+ createElement(
64
+ "div",
65
+ { style: { flex: 1 } },
66
+ header ?? null,
67
+ createElement("main", null, children)
68
+ )
69
+ ),
70
+ sidebar: ({ children }) => createElement(
71
+ "aside",
72
+ { style: { width: "240px", borderRight: "1px solid #ddd" } },
73
+ children
74
+ ),
75
+ pageContainer: ({ children, title, actions }) => createElement(
76
+ "div",
77
+ null,
78
+ title || actions ? createElement(
79
+ "div",
80
+ { style: { display: "flex", justifyContent: "space-between", alignItems: "center" } },
81
+ title ? createElement("h1", null, title) : null,
82
+ actions ?? null
83
+ ) : null,
84
+ children
85
+ ),
86
+ breadcrumb: ({ items }) => createElement(
87
+ "nav",
88
+ { "aria-label": "breadcrumb" },
89
+ items.map(
90
+ (item, i) => createElement(
91
+ "span",
92
+ { key: i },
93
+ i > 0 ? " / " : null,
94
+ item.to ? createElement("a", { href: item.to }, item.label) : item.label
95
+ )
96
+ )
97
+ ),
98
+ // Feedback
99
+ toast: ({ children }) => createElement("div", null, children),
100
+ alert: ({ children, color }) => createElement(
101
+ "div",
102
+ {
103
+ role: "alert",
104
+ style: {
105
+ padding: "8px 12px",
106
+ borderRadius: "4px",
107
+ backgroundColor: color === "danger" ? "#fee" : color === "success" ? "#efe" : color === "warning" ? "#ffe" : "#f5f5f5"
108
+ }
109
+ },
110
+ children
111
+ ),
112
+ // File
113
+ dropZone: ({ children, isDragOver, onClick, onDrop, onDragOver, onDragLeave }) => createElement(
114
+ "div",
115
+ {
116
+ onClick,
117
+ onDrop: (e) => {
118
+ e.preventDefault();
119
+ onDrop(e.dataTransfer.files);
120
+ },
121
+ onDragOver: (e) => {
122
+ e.preventDefault();
123
+ onDragOver(e);
124
+ },
125
+ onDragLeave,
126
+ style: {
127
+ border: `2px dashed ${isDragOver ? "#4caf50" : "#ccc"}`,
128
+ borderRadius: "8px",
129
+ padding: "32px",
130
+ textAlign: "center",
131
+ cursor: "pointer"
132
+ }
133
+ },
134
+ children
135
+ )
136
+ };
137
+
138
+ // src/plugin.ts
139
+ var UIPluginContext = createContext(null);
140
+ function createUIPlugin(config) {
141
+ return { components: config.components };
142
+ }
143
+ function UIPluginProvider({
144
+ plugin,
145
+ children
146
+ }) {
147
+ return createElement2(UIPluginContext.Provider, { value: plugin }, children);
148
+ }
149
+ function useUIPlugin() {
150
+ return useContext(UIPluginContext);
151
+ }
152
+ function useComponent(slot) {
153
+ const plugin = useUIPlugin();
154
+ const component = plugin?.components[slot];
155
+ if (component) {
156
+ return component;
157
+ }
158
+ return headlessDefaults[slot];
159
+ }
160
+
161
+ // src/hooks/use-confirm.ts
162
+ import { createContext as createContext2, useContext as useContext2, useCallback } from "react";
163
+ var ConfirmContext = createContext2(null);
164
+ function useConfirm() {
165
+ const ctx = useContext2(ConfirmContext);
166
+ if (!ctx) {
167
+ throw new Error("useConfirm must be used within a <ConfirmProvider>");
168
+ }
169
+ return useCallback(
170
+ (options) => ctx.confirm(options),
171
+ [ctx]
172
+ );
173
+ }
174
+
175
+ // src/hooks/use-action-toast.ts
176
+ import { useEffect, useRef } from "react";
177
+ import { useActions } from "@cfast/actions/client";
178
+ function useActionToast(descriptor, config) {
179
+ const actions = useActions(descriptor);
180
+ const toast = useToast();
181
+ const prevDataRef = useRef({});
182
+ useEffect(() => {
183
+ for (const [name, cfg] of Object.entries(config)) {
184
+ const actionFn = actions[name];
185
+ if (!actionFn) continue;
186
+ const result = actionFn();
187
+ const prevData = prevDataRef.current[name];
188
+ if (result.data !== void 0 && result.data !== prevData) {
189
+ prevDataRef.current[name] = result.data;
190
+ if (cfg.success) {
191
+ toast.success(cfg.success);
192
+ }
193
+ }
194
+ if (result.error !== void 0 && result.error !== prevData) {
195
+ prevDataRef.current[name] = result.error;
196
+ if (cfg.error) {
197
+ toast.error(cfg.error);
198
+ }
199
+ }
200
+ }
201
+ });
202
+ }
203
+
204
+ // src/components/permission-gate.tsx
205
+ import { createElement as createElement3, Fragment } from "react";
206
+ function PermissionGate({
207
+ action,
208
+ actionName,
209
+ input,
210
+ children,
211
+ fallback
212
+ }) {
213
+ const status = useActionStatus(action, actionName, input);
214
+ if (status.invisible) {
215
+ return null;
216
+ }
217
+ if (!status.permitted) {
218
+ return fallback ? createElement3(Fragment, null, fallback) : null;
219
+ }
220
+ return createElement3(Fragment, null, children);
221
+ }
222
+
223
+ // src/components/action-button.tsx
224
+ import { createElement as createElement4 } from "react";
225
+ function ActionButton({
226
+ action,
227
+ actionName,
228
+ input,
229
+ children,
230
+ whenForbidden = "disable",
231
+ confirmation: _confirmation,
232
+ variant,
233
+ color,
234
+ size,
235
+ startDecorator
236
+ }) {
237
+ const status = useActionStatus(action, actionName, input);
238
+ const Button = useComponent("button");
239
+ if (status.invisible) {
240
+ return null;
241
+ }
242
+ if (!status.permitted && whenForbidden === "hide") {
243
+ return null;
244
+ }
245
+ const disabled = !status.permitted && whenForbidden === "disable";
246
+ return createElement4(Button, {
247
+ children,
248
+ onClick: () => status.submit(),
249
+ disabled,
250
+ loading: status.pending,
251
+ variant,
252
+ color,
253
+ size,
254
+ startDecorator
255
+ });
256
+ }
257
+
258
+ // src/components/confirm-provider.tsx
259
+ import { createElement as createElement5, useState, useCallback as useCallback2, useRef as useRef2 } from "react";
260
+ function ConfirmProvider({ children }) {
261
+ const [state, setState] = useState(null);
262
+ const ConfirmDialog = useComponent("confirmDialog");
263
+ const resolveRef = useRef2(null);
264
+ const confirm = useCallback2((options) => {
265
+ return new Promise((resolve) => {
266
+ resolveRef.current = resolve;
267
+ setState({ ...options, resolve });
268
+ });
269
+ }, []);
270
+ const handleClose = useCallback2(() => {
271
+ resolveRef.current?.(false);
272
+ resolveRef.current = null;
273
+ setState(null);
274
+ }, []);
275
+ const handleConfirm = useCallback2(() => {
276
+ resolveRef.current?.(true);
277
+ resolveRef.current = null;
278
+ setState(null);
279
+ }, []);
280
+ return createElement5(
281
+ ConfirmContext.Provider,
282
+ { value: { confirm } },
283
+ children,
284
+ state ? createElement5(ConfirmDialog, {
285
+ open: true,
286
+ onClose: handleClose,
287
+ onConfirm: handleConfirm,
288
+ title: state.title,
289
+ description: state.description,
290
+ confirmLabel: state.confirmLabel,
291
+ cancelLabel: state.cancelLabel,
292
+ variant: state.variant
293
+ }) : null
294
+ );
295
+ }
296
+
297
+ // src/components/form-status.tsx
298
+ import { createElement as createElement6 } from "react";
299
+ function FormStatus({ data }) {
300
+ const Alert = useComponent("alert");
301
+ if (!data) return null;
302
+ const elements = [];
303
+ if (data.success) {
304
+ elements.push(
305
+ createElement6(Alert, { key: "success", color: "success", children: data.success })
306
+ );
307
+ }
308
+ if (data.error) {
309
+ elements.push(
310
+ createElement6(Alert, { key: "error", color: "danger", children: data.error })
311
+ );
312
+ }
313
+ if (data.fieldErrors) {
314
+ const errorMessages = Object.entries(data.fieldErrors).flatMap(
315
+ ([field, errors]) => errors.map((err) => `${field}: ${err}`)
316
+ );
317
+ if (errorMessages.length > 0) {
318
+ elements.push(
319
+ createElement6(Alert, {
320
+ key: "field-errors",
321
+ color: "danger",
322
+ children: createElement6(
323
+ "ul",
324
+ { style: { margin: 0, paddingLeft: "16px" } },
325
+ ...errorMessages.map(
326
+ (msg, i) => createElement6("li", { key: i }, msg)
327
+ )
328
+ )
329
+ })
330
+ );
331
+ }
332
+ }
333
+ if (elements.length === 0) return null;
334
+ return createElement6("div", { style: { display: "flex", flexDirection: "column", gap: "8px" } }, ...elements);
335
+ }
336
+
337
+ // src/components/role-badge.tsx
338
+ import { createElement as createElement7 } from "react";
339
+ var defaultColors = {
340
+ admin: "danger",
341
+ editor: "primary",
342
+ author: "success",
343
+ reader: "neutral"
344
+ };
345
+ function RoleBadge({ role, colors }) {
346
+ const Chip = useComponent("chip");
347
+ const colorMap = colors ? { ...defaultColors, ...Object.fromEntries(
348
+ Object.entries(colors).map(([k, v]) => [k, v])
349
+ ) } : defaultColors;
350
+ const chipColor = colorMap[role] ?? "neutral";
351
+ return createElement7(Chip, {
352
+ children: role,
353
+ color: chipColor,
354
+ variant: "soft",
355
+ size: "sm"
356
+ });
357
+ }
358
+
359
+ // src/components/impersonation-banner.tsx
360
+ import { createElement as createElement8 } from "react";
361
+ import { useCurrentUser } from "@cfast/auth/client";
362
+ function ImpersonationBanner({
363
+ stopAction = "/admin/stop-impersonation"
364
+ }) {
365
+ const user = useCurrentUser();
366
+ const Alert = useComponent("alert");
367
+ const Button = useComponent("button");
368
+ if (!user?.isImpersonating) {
369
+ return null;
370
+ }
371
+ return createElement8(
372
+ Alert,
373
+ {
374
+ color: "warning",
375
+ children: createElement8(
376
+ "div",
377
+ {
378
+ style: {
379
+ display: "flex",
380
+ alignItems: "center",
381
+ justifyContent: "center",
382
+ gap: "12px"
383
+ }
384
+ },
385
+ createElement8(
386
+ "strong",
387
+ null,
388
+ `Viewing as ${user.name} (${user.email})`
389
+ ),
390
+ createElement8(
391
+ "form",
392
+ { method: "post", action: stopAction },
393
+ createElement8(Button, {
394
+ type: "submit",
395
+ variant: "outlined",
396
+ size: "sm",
397
+ children: "Stop Impersonating"
398
+ })
399
+ )
400
+ )
401
+ }
402
+ );
403
+ }
404
+
405
+ export {
406
+ createUIPlugin,
407
+ UIPluginProvider,
408
+ useUIPlugin,
409
+ useComponent,
410
+ useConfirm,
411
+ useActionToast,
412
+ PermissionGate,
413
+ ActionButton,
414
+ ConfirmProvider,
415
+ FormStatus,
416
+ RoleBadge,
417
+ ImpersonationBanner
418
+ };