@object-ui/react 0.5.0 → 3.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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +32 -0
- package/dist/LazyPluginLoader.d.ts +44 -0
- package/dist/LazyPluginLoader.js +92 -3
- package/dist/SchemaRenderer.d.ts +20 -1
- package/dist/SchemaRenderer.js +76 -13
- package/dist/components/form/FieldFactory.js +44 -40
- package/dist/components/form/FormRenderer.js +45 -3
- package/dist/context/ActionContext.d.ts +76 -0
- package/dist/context/ActionContext.js +139 -0
- package/dist/context/DndContext.d.ts +135 -0
- package/dist/context/DndContext.js +120 -0
- package/dist/context/NotificationContext.d.ts +113 -0
- package/dist/context/NotificationContext.js +130 -0
- package/dist/context/ThemeContext.d.ts +94 -0
- package/dist/context/ThemeContext.js +215 -0
- package/dist/context/index.d.ts +4 -0
- package/dist/context/index.js +4 -0
- package/dist/hooks/index.d.ts +22 -0
- package/dist/hooks/index.js +22 -0
- package/dist/hooks/useActionRunner.d.ts +21 -7
- package/dist/hooks/useActionRunner.js +27 -7
- package/dist/hooks/useAnimation.d.ts +46 -0
- package/dist/hooks/useAnimation.js +59 -0
- package/dist/hooks/useClientNotifications.d.ts +53 -0
- package/dist/hooks/useClientNotifications.js +190 -0
- package/dist/hooks/useColumnSummary.d.ts +38 -0
- package/dist/hooks/useColumnSummary.js +54 -0
- package/dist/hooks/useCrudShortcuts.d.ts +39 -0
- package/dist/hooks/useCrudShortcuts.js +96 -0
- package/dist/hooks/useDensityMode.d.ts +48 -0
- package/dist/hooks/useDensityMode.js +60 -0
- package/dist/hooks/useDiscovery.d.ts +78 -0
- package/dist/hooks/useDiscovery.js +96 -0
- package/dist/hooks/useDynamicApp.d.ts +70 -0
- package/dist/hooks/useDynamicApp.js +92 -0
- package/dist/hooks/useETagCache.d.ts +75 -0
- package/dist/hooks/useETagCache.js +306 -0
- package/dist/hooks/useFocusManagement.d.ts +89 -0
- package/dist/hooks/useFocusManagement.js +188 -0
- package/dist/hooks/useFocusTrap.d.ts +41 -0
- package/dist/hooks/useFocusTrap.js +103 -0
- package/dist/hooks/useKeyboardShortcuts.d.ts +49 -0
- package/dist/hooks/useKeyboardShortcuts.js +86 -0
- package/dist/hooks/useNavigationOverlay.d.ts +78 -0
- package/dist/hooks/useNavigationOverlay.js +112 -0
- package/dist/hooks/useOffline.d.ts +96 -0
- package/dist/hooks/useOffline.js +185 -0
- package/dist/hooks/usePageTransition.d.ts +60 -0
- package/dist/hooks/usePageTransition.js +111 -0
- package/dist/hooks/usePageVariables.d.ts +67 -0
- package/dist/hooks/usePageVariables.js +109 -0
- package/dist/hooks/usePerformance.d.ts +87 -0
- package/dist/hooks/usePerformance.js +165 -0
- package/dist/hooks/usePerformanceBudget.d.ts +78 -0
- package/dist/hooks/usePerformanceBudget.js +209 -0
- package/dist/hooks/useReducedMotion.d.ts +28 -0
- package/dist/hooks/useReducedMotion.js +48 -0
- package/dist/hooks/useSchemaPersistence.d.ts +87 -0
- package/dist/hooks/useSchemaPersistence.js +173 -0
- package/dist/hooks/useViewData.d.ts +73 -0
- package/dist/hooks/useViewData.js +135 -0
- package/dist/hooks/useViewSharing.d.ts +57 -0
- package/dist/hooks/useViewSharing.js +74 -0
- package/dist/hooks/useViewTransition.d.ts +54 -0
- package/dist/hooks/useViewTransition.js +83 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/utils/i18n.d.ts +10 -0
- package/dist/utils/i18n.js +12 -0
- package/package.json +7 -5
- package/src/LazyPluginLoader.tsx +129 -7
- package/src/SchemaRenderer.tsx +122 -13
- package/src/__tests__/LazyPluginLoader.test.tsx +97 -1
- package/src/__tests__/SchemaRenderer.aria.test.tsx +118 -0
- package/src/__tests__/SchemaRenderer.errorBoundary.test.tsx +114 -0
- package/src/__tests__/SchemaRendererProvider.smoke.test.tsx +132 -0
- package/src/__tests__/ThemeProvider.test.tsx +451 -0
- package/src/__tests__/useNavigationOverlay.test.ts +416 -0
- package/src/__tests__/useOffline.test.ts +198 -0
- package/src/__tests__/usePageTransition.test.ts +214 -0
- package/src/__tests__/usePageVariables.test.tsx +334 -0
- package/src/__tests__/usePerformance.test.ts +137 -0
- package/src/__tests__/useViewData.test.tsx +422 -0
- package/src/__tests__/view-switching.test.tsx +456 -0
- package/src/components/form/FieldFactory.tsx +51 -46
- package/src/components/form/FormRenderer.test.tsx +145 -0
- package/src/components/form/FormRenderer.tsx +52 -3
- package/src/context/ActionContext.tsx +213 -0
- package/src/context/DndContext.tsx +260 -0
- package/src/context/NotificationContext.tsx +280 -0
- package/src/context/ThemeContext.tsx +325 -0
- package/src/context/__tests__/ActionContext.test.tsx +174 -0
- package/src/context/__tests__/DndContext.test.tsx +150 -0
- package/src/context/__tests__/NotificationContext.test.tsx +176 -0
- package/src/context/index.ts +4 -0
- package/src/hooks/__tests__/useActionRunner.test.ts +153 -0
- package/src/hooks/__tests__/useAnimation.test.ts +82 -0
- package/src/hooks/__tests__/useColumnSummary.test.ts +93 -0
- package/src/hooks/__tests__/useDensityMode.test.ts +81 -0
- package/src/hooks/__tests__/useDiscovery.test.tsx +149 -0
- package/src/hooks/__tests__/useDynamicApp.test.ts +161 -0
- package/src/hooks/__tests__/useETagCache.test.ts +153 -0
- package/src/hooks/__tests__/useExpression.test.ts +86 -0
- package/src/hooks/__tests__/useFocusTrap.test.ts +47 -0
- package/src/hooks/__tests__/useKeyboardShortcuts.test.ts +144 -0
- package/src/hooks/__tests__/usePerformanceBudget.test.ts +157 -0
- package/src/hooks/__tests__/useSchemaPersistence.test.ts +220 -0
- package/src/hooks/__tests__/useViewSharing.test.ts +121 -0
- package/src/hooks/__tests__/useViewTransition.test.ts +61 -0
- package/src/hooks/index.ts +22 -1
- package/src/hooks/useActionRunner.ts +49 -11
- package/src/hooks/useAnimation.ts +117 -0
- package/src/hooks/useClientNotifications.ts +251 -0
- package/src/hooks/useColumnSummary.ts +87 -0
- package/src/hooks/useCrudShortcuts.ts +126 -0
- package/src/hooks/useDensityMode.ts +102 -0
- package/src/hooks/useDiscovery.ts +141 -0
- package/src/hooks/useDynamicApp.ts +142 -0
- package/src/hooks/useETagCache.ts +404 -0
- package/src/hooks/useFocusManagement.ts +248 -0
- package/src/hooks/useFocusTrap.ts +138 -0
- package/src/hooks/useKeyboardShortcuts.ts +119 -0
- package/src/hooks/useNavigationOverlay.ts +180 -0
- package/src/hooks/useOffline.ts +301 -0
- package/src/hooks/usePageTransition.ts +187 -0
- package/src/hooks/usePageVariables.tsx +157 -0
- package/src/hooks/usePerformance.ts +246 -0
- package/src/hooks/usePerformanceBudget.ts +313 -0
- package/src/hooks/useReducedMotion.ts +53 -0
- package/src/hooks/useSchemaPersistence.ts +222 -0
- package/src/hooks/useViewData.ts +201 -0
- package/src/hooks/useViewSharing.ts +140 -0
- package/src/hooks/useViewTransition.ts +144 -0
- package/src/index.ts +21 -0
- package/src/utils/i18n.ts +10 -0
- package/tsconfig.json +1 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/__tests__/LazyPluginLoader.test.d.ts +0 -8
- package/dist/__tests__/LazyPluginLoader.test.js +0 -49
- package/dist/components/form/FormRenderer.test.d.ts +0 -8
- package/dist/components/form/FormRenderer.test.js +0 -534
- package/dist/index.test.d.ts +0 -8
- package/dist/index.test.js +0 -13
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI
|
|
3
|
+
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { useEffect, useRef, useCallback } from 'react';
|
|
9
|
+
const FOCUSABLE_SELECTOR = [
|
|
10
|
+
'a[href]',
|
|
11
|
+
'button:not([disabled])',
|
|
12
|
+
'input:not([disabled])',
|
|
13
|
+
'select:not([disabled])',
|
|
14
|
+
'textarea:not([disabled])',
|
|
15
|
+
'[tabindex]:not([tabindex="-1"])',
|
|
16
|
+
].join(', ');
|
|
17
|
+
/**
|
|
18
|
+
* Hook that traps focus within a container element.
|
|
19
|
+
* Implements FocusTrapConfigSchema from @objectstack/spec v2.0.7.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```tsx
|
|
23
|
+
* function Modal({ onClose }: { onClose: () => void }) {
|
|
24
|
+
* const trapRef = useFocusTrap({
|
|
25
|
+
* enabled: true,
|
|
26
|
+
* autoFocus: true,
|
|
27
|
+
* restoreFocus: true,
|
|
28
|
+
* escapeDeactivates: true,
|
|
29
|
+
* onEscape: onClose,
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* return <div ref={trapRef}>...</div>;
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export function useFocusTrap(options = {}) {
|
|
37
|
+
const { enabled = true, autoFocus = true, restoreFocus = true, escapeDeactivates = false, onEscape, initialFocus, } = options;
|
|
38
|
+
const containerRef = useRef(null);
|
|
39
|
+
const previousFocusRef = useRef(null);
|
|
40
|
+
const getFocusableElements = useCallback(() => {
|
|
41
|
+
if (!containerRef.current)
|
|
42
|
+
return [];
|
|
43
|
+
return Array.from(containerRef.current.querySelectorAll(FOCUSABLE_SELECTOR)).filter((el) => !el.hasAttribute('disabled') && el.tabIndex >= 0);
|
|
44
|
+
}, []);
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
if (!enabled || !containerRef.current)
|
|
47
|
+
return;
|
|
48
|
+
// Save previously focused element
|
|
49
|
+
previousFocusRef.current = document.activeElement;
|
|
50
|
+
// Auto-focus
|
|
51
|
+
if (autoFocus) {
|
|
52
|
+
const focusableElements = getFocusableElements();
|
|
53
|
+
if (initialFocus) {
|
|
54
|
+
const target = containerRef.current.querySelector(initialFocus);
|
|
55
|
+
if (target) {
|
|
56
|
+
target.focus();
|
|
57
|
+
}
|
|
58
|
+
else if (focusableElements.length > 0) {
|
|
59
|
+
focusableElements[0].focus();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
else if (focusableElements.length > 0) {
|
|
63
|
+
focusableElements[0].focus();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const handleKeyDown = (event) => {
|
|
67
|
+
if (!containerRef.current)
|
|
68
|
+
return;
|
|
69
|
+
if (event.key === 'Escape' && escapeDeactivates) {
|
|
70
|
+
onEscape?.();
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (event.key !== 'Tab')
|
|
74
|
+
return;
|
|
75
|
+
const focusableElements = getFocusableElements();
|
|
76
|
+
if (focusableElements.length === 0)
|
|
77
|
+
return;
|
|
78
|
+
const firstElement = focusableElements[0];
|
|
79
|
+
const lastElement = focusableElements[focusableElements.length - 1];
|
|
80
|
+
if (event.shiftKey) {
|
|
81
|
+
if (document.activeElement === firstElement) {
|
|
82
|
+
event.preventDefault();
|
|
83
|
+
lastElement.focus();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
if (document.activeElement === lastElement) {
|
|
88
|
+
event.preventDefault();
|
|
89
|
+
firstElement.focus();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const container = containerRef.current;
|
|
94
|
+
container.addEventListener('keydown', handleKeyDown);
|
|
95
|
+
return () => {
|
|
96
|
+
container.removeEventListener('keydown', handleKeyDown);
|
|
97
|
+
if (restoreFocus && previousFocusRef.current) {
|
|
98
|
+
previousFocusRef.current.focus();
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}, [enabled, autoFocus, restoreFocus, escapeDeactivates, onEscape, initialFocus, getFocusableElements]);
|
|
102
|
+
return containerRef;
|
|
103
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI
|
|
3
|
+
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
export interface KeyboardShortcutDefinition {
|
|
9
|
+
/** The key to listen for (e.g., 'k', 'Enter', 'Escape') */
|
|
10
|
+
key: string;
|
|
11
|
+
/** Whether Ctrl/Cmd is required */
|
|
12
|
+
ctrlOrMeta?: boolean;
|
|
13
|
+
/** Whether Shift is required */
|
|
14
|
+
shift?: boolean;
|
|
15
|
+
/** Whether Alt is required */
|
|
16
|
+
alt?: boolean;
|
|
17
|
+
/** Handler to call when the shortcut is triggered */
|
|
18
|
+
handler: (event: KeyboardEvent) => void;
|
|
19
|
+
/** Human-readable description for the help dialog */
|
|
20
|
+
description?: string;
|
|
21
|
+
/** Whether to prevent default browser behavior */
|
|
22
|
+
preventDefault?: boolean;
|
|
23
|
+
/** Whether the shortcut is currently enabled */
|
|
24
|
+
enabled?: boolean;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Hook for registering keyboard shortcuts.
|
|
28
|
+
* Implements KeyboardShortcutSchema from @objectstack/spec v2.0.7.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```tsx
|
|
32
|
+
* useKeyboardShortcuts([
|
|
33
|
+
* { key: 'k', ctrlOrMeta: true, handler: () => openSearch(), description: 'Open search' },
|
|
34
|
+
* { key: 'n', ctrlOrMeta: true, handler: () => createNew(), description: 'Create new record' },
|
|
35
|
+
* { key: 'Escape', handler: () => closeModal(), description: 'Close dialog' },
|
|
36
|
+
* ]);
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare function useKeyboardShortcuts(shortcuts: KeyboardShortcutDefinition[], options?: {
|
|
40
|
+
enabled?: boolean;
|
|
41
|
+
}): void;
|
|
42
|
+
/**
|
|
43
|
+
* Returns a list of all registered shortcut descriptions.
|
|
44
|
+
* Useful for building a help dialog (? key).
|
|
45
|
+
*/
|
|
46
|
+
export declare function getShortcutDescriptions(shortcuts: KeyboardShortcutDefinition[]): Array<{
|
|
47
|
+
keys: string;
|
|
48
|
+
description: string;
|
|
49
|
+
}>;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI
|
|
3
|
+
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { useEffect, useRef, useCallback } from 'react';
|
|
9
|
+
/**
|
|
10
|
+
* Hook for registering keyboard shortcuts.
|
|
11
|
+
* Implements KeyboardShortcutSchema from @objectstack/spec v2.0.7.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* useKeyboardShortcuts([
|
|
16
|
+
* { key: 'k', ctrlOrMeta: true, handler: () => openSearch(), description: 'Open search' },
|
|
17
|
+
* { key: 'n', ctrlOrMeta: true, handler: () => createNew(), description: 'Create new record' },
|
|
18
|
+
* { key: 'Escape', handler: () => closeModal(), description: 'Close dialog' },
|
|
19
|
+
* ]);
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export function useKeyboardShortcuts(shortcuts, options = {}) {
|
|
23
|
+
const { enabled = true } = options;
|
|
24
|
+
const shortcutsRef = useRef(shortcuts);
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
shortcutsRef.current = shortcuts;
|
|
27
|
+
}, [shortcuts]);
|
|
28
|
+
const handleKeyDown = useCallback((event) => {
|
|
29
|
+
// Don't trigger shortcuts when typing in form elements
|
|
30
|
+
const target = event.target;
|
|
31
|
+
const isFormElement = target.tagName === 'INPUT' ||
|
|
32
|
+
target.tagName === 'TEXTAREA' ||
|
|
33
|
+
target.tagName === 'SELECT' ||
|
|
34
|
+
target.isContentEditable;
|
|
35
|
+
for (const shortcut of shortcutsRef.current) {
|
|
36
|
+
if (shortcut.enabled === false)
|
|
37
|
+
continue;
|
|
38
|
+
const keyMatch = event.key.toLowerCase() === shortcut.key.toLowerCase();
|
|
39
|
+
const ctrlMatch = shortcut.ctrlOrMeta
|
|
40
|
+
? event.ctrlKey || event.metaKey
|
|
41
|
+
: !event.ctrlKey && !event.metaKey;
|
|
42
|
+
const shiftMatch = shortcut.shift ? event.shiftKey : !event.shiftKey;
|
|
43
|
+
const altMatch = shortcut.alt ? event.altKey : !event.altKey;
|
|
44
|
+
if (keyMatch && ctrlMatch && shiftMatch && altMatch) {
|
|
45
|
+
// Allow shortcuts with modifiers even in form elements
|
|
46
|
+
if (isFormElement && !shortcut.ctrlOrMeta && !shortcut.alt)
|
|
47
|
+
continue;
|
|
48
|
+
if (shortcut.preventDefault !== false) {
|
|
49
|
+
event.preventDefault();
|
|
50
|
+
}
|
|
51
|
+
shortcut.handler(event);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}, []);
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
if (!enabled)
|
|
58
|
+
return;
|
|
59
|
+
document.addEventListener('keydown', handleKeyDown);
|
|
60
|
+
return () => {
|
|
61
|
+
document.removeEventListener('keydown', handleKeyDown);
|
|
62
|
+
};
|
|
63
|
+
}, [enabled, handleKeyDown]);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Returns a list of all registered shortcut descriptions.
|
|
67
|
+
* Useful for building a help dialog (? key).
|
|
68
|
+
*/
|
|
69
|
+
export function getShortcutDescriptions(shortcuts) {
|
|
70
|
+
return shortcuts
|
|
71
|
+
.filter((s) => s.description && s.enabled !== false)
|
|
72
|
+
.map((s) => {
|
|
73
|
+
const parts = [];
|
|
74
|
+
if (s.ctrlOrMeta)
|
|
75
|
+
parts.push('⌘/Ctrl');
|
|
76
|
+
if (s.shift)
|
|
77
|
+
parts.push('Shift');
|
|
78
|
+
if (s.alt)
|
|
79
|
+
parts.push('Alt');
|
|
80
|
+
parts.push(s.key.length === 1 ? s.key.toUpperCase() : s.key);
|
|
81
|
+
return {
|
|
82
|
+
keys: parts.join(' + '),
|
|
83
|
+
description: s.description,
|
|
84
|
+
};
|
|
85
|
+
});
|
|
86
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI
|
|
3
|
+
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Inline ViewNavigationConfig to avoid importing from @object-ui/types
|
|
10
|
+
* (which may not be a direct dependency of @object-ui/react).
|
|
11
|
+
* Mirrors the canonical definition in @object-ui/types/objectql.ts.
|
|
12
|
+
*/
|
|
13
|
+
export interface NavigationConfig {
|
|
14
|
+
mode: 'page' | 'drawer' | 'modal' | 'split' | 'popover' | 'new_window' | 'none';
|
|
15
|
+
view?: string;
|
|
16
|
+
preventNavigation?: boolean;
|
|
17
|
+
openNewTab?: boolean;
|
|
18
|
+
width?: string | number;
|
|
19
|
+
}
|
|
20
|
+
export type NavigationMode = NavigationConfig['mode'];
|
|
21
|
+
export interface UseNavigationOverlayOptions {
|
|
22
|
+
/** The navigation configuration from the schema */
|
|
23
|
+
navigation?: NavigationConfig;
|
|
24
|
+
/** Object name — used to build default URLs for page/new_window modes */
|
|
25
|
+
objectName?: string;
|
|
26
|
+
/** External onNavigate callback (e.g., from ActionProvider or parent) */
|
|
27
|
+
onNavigate?: (recordId: string | number, action?: string) => void;
|
|
28
|
+
/** External onRowClick callback — if set, takes full priority */
|
|
29
|
+
onRowClick?: (record: Record<string, unknown>) => void;
|
|
30
|
+
}
|
|
31
|
+
export interface NavigationOverlayState {
|
|
32
|
+
/** Whether the overlay (drawer/modal/split/popover) is open */
|
|
33
|
+
isOpen: boolean;
|
|
34
|
+
/** The record that triggered the navigation */
|
|
35
|
+
selectedRecord: Record<string, unknown> | null;
|
|
36
|
+
/** The resolved navigation mode */
|
|
37
|
+
mode: NavigationMode;
|
|
38
|
+
/** Close the overlay */
|
|
39
|
+
close: () => void;
|
|
40
|
+
/** Open the overlay with a specific record */
|
|
41
|
+
open: (record: Record<string, unknown>) => void;
|
|
42
|
+
/** Set the open state (for controlled Sheet/Dialog `onOpenChange`) */
|
|
43
|
+
setIsOpen: (open: boolean) => void;
|
|
44
|
+
/** The click handler to attach to rows/items */
|
|
45
|
+
handleClick: (record: Record<string, unknown>) => void;
|
|
46
|
+
/** The width from NavigationConfig (for drawer/modal/split sizing) */
|
|
47
|
+
width: string | number | undefined;
|
|
48
|
+
/** Whether navigation is an overlay mode (drawer/modal/split/popover) */
|
|
49
|
+
isOverlay: boolean;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Hook for NavigationConfig-driven navigation overlay.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```tsx
|
|
56
|
+
* const { handleClick, isOpen, selectedRecord, mode, close, width } =
|
|
57
|
+
* useNavigationOverlay({
|
|
58
|
+
* navigation: schema.navigation,
|
|
59
|
+
* objectName: schema.objectName,
|
|
60
|
+
* onNavigate: schema.onNavigate,
|
|
61
|
+
* onRowClick: props.onRowClick,
|
|
62
|
+
* });
|
|
63
|
+
*
|
|
64
|
+
* return (
|
|
65
|
+
* <>
|
|
66
|
+
* <DataTable onRowClick={handleClick} ... />
|
|
67
|
+
* {isOpen && mode === 'drawer' && (
|
|
68
|
+
* <Sheet open onOpenChange={() => close()}>
|
|
69
|
+
* <SheetContent style={{ maxWidth: width }}>
|
|
70
|
+
* <RecordDetail record={selectedRecord} />
|
|
71
|
+
* </SheetContent>
|
|
72
|
+
* </Sheet>
|
|
73
|
+
* )}
|
|
74
|
+
* </>
|
|
75
|
+
* );
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export declare function useNavigationOverlay(options: UseNavigationOverlayOptions): NavigationOverlayState;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI
|
|
3
|
+
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* useNavigationOverlay
|
|
10
|
+
*
|
|
11
|
+
* A reusable hook for handling NavigationConfig-driven row/item click behavior.
|
|
12
|
+
* Manages overlay state (drawer/modal/split/popover) and generates click handlers
|
|
13
|
+
* that respect the ViewNavigationConfig specification.
|
|
14
|
+
*
|
|
15
|
+
* Used by plugin-grid, plugin-list, plugin-detail and any component that needs
|
|
16
|
+
* NavigationConfig support.
|
|
17
|
+
*/
|
|
18
|
+
import { useState, useCallback, useMemo } from 'react';
|
|
19
|
+
/**
|
|
20
|
+
* Hook for NavigationConfig-driven navigation overlay.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```tsx
|
|
24
|
+
* const { handleClick, isOpen, selectedRecord, mode, close, width } =
|
|
25
|
+
* useNavigationOverlay({
|
|
26
|
+
* navigation: schema.navigation,
|
|
27
|
+
* objectName: schema.objectName,
|
|
28
|
+
* onNavigate: schema.onNavigate,
|
|
29
|
+
* onRowClick: props.onRowClick,
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* return (
|
|
33
|
+
* <>
|
|
34
|
+
* <DataTable onRowClick={handleClick} ... />
|
|
35
|
+
* {isOpen && mode === 'drawer' && (
|
|
36
|
+
* <Sheet open onOpenChange={() => close()}>
|
|
37
|
+
* <SheetContent style={{ maxWidth: width }}>
|
|
38
|
+
* <RecordDetail record={selectedRecord} />
|
|
39
|
+
* </SheetContent>
|
|
40
|
+
* </Sheet>
|
|
41
|
+
* )}
|
|
42
|
+
* </>
|
|
43
|
+
* );
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export function useNavigationOverlay(options) {
|
|
47
|
+
const { navigation, objectName, onNavigate, onRowClick } = options;
|
|
48
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
49
|
+
const [selectedRecord, setSelectedRecord] = useState(null);
|
|
50
|
+
const mode = navigation?.mode ?? 'page';
|
|
51
|
+
const width = navigation?.width;
|
|
52
|
+
const isOverlay = mode === 'drawer' || mode === 'modal' || mode === 'split' || mode === 'popover';
|
|
53
|
+
const close = useCallback(() => {
|
|
54
|
+
setIsOpen(false);
|
|
55
|
+
setSelectedRecord(null);
|
|
56
|
+
}, []);
|
|
57
|
+
const open = useCallback((record) => {
|
|
58
|
+
setSelectedRecord(record);
|
|
59
|
+
setIsOpen(true);
|
|
60
|
+
}, []);
|
|
61
|
+
const handleClick = useCallback((record) => {
|
|
62
|
+
// External onRowClick takes full priority
|
|
63
|
+
if (onRowClick) {
|
|
64
|
+
onRowClick(record);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
// No navigation config — default to page navigation
|
|
68
|
+
if (!navigation) {
|
|
69
|
+
const recordId = record._id || record.id;
|
|
70
|
+
if (onNavigate && recordId != null) {
|
|
71
|
+
onNavigate(recordId, 'view');
|
|
72
|
+
}
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
// 'none' or preventNavigation — do nothing
|
|
76
|
+
if (mode === 'none' || navigation.preventNavigation) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
// new_window / openNewTab — open in new browser tab
|
|
80
|
+
if (mode === 'new_window' || navigation.openNewTab) {
|
|
81
|
+
const recordId = record._id || record.id;
|
|
82
|
+
const url = objectName ? `/${objectName}/${recordId}` : `/${recordId}`;
|
|
83
|
+
window.open(url, '_blank');
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
// page — delegate to onNavigate callback
|
|
87
|
+
if (mode === 'page') {
|
|
88
|
+
const recordId = record._id || record.id;
|
|
89
|
+
if (onNavigate && recordId != null) {
|
|
90
|
+
onNavigate(recordId, 'view');
|
|
91
|
+
}
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
// Overlay modes: drawer, modal, split, popover
|
|
95
|
+
if (isOverlay) {
|
|
96
|
+
setSelectedRecord(record);
|
|
97
|
+
setIsOpen(true);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
}, [onRowClick, navigation, mode, objectName, onNavigate, isOverlay]);
|
|
101
|
+
return useMemo(() => ({
|
|
102
|
+
isOpen,
|
|
103
|
+
selectedRecord,
|
|
104
|
+
mode,
|
|
105
|
+
close,
|
|
106
|
+
open,
|
|
107
|
+
setIsOpen,
|
|
108
|
+
handleClick,
|
|
109
|
+
width,
|
|
110
|
+
isOverlay,
|
|
111
|
+
}), [isOpen, selectedRecord, mode, close, open, handleClick, width, isOverlay]);
|
|
112
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI
|
|
3
|
+
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
/** Offline strategy determines how data is fetched when connectivity is limited. */
|
|
9
|
+
export type OfflineStrategy = 'cache_first' | 'network_first' | 'stale_while_revalidate' | 'network_only' | 'cache_only';
|
|
10
|
+
/** Conflict resolution strategy for sync operations. */
|
|
11
|
+
export type ConflictResolutionStrategy = 'manual' | 'client_wins' | 'server_wins' | 'last_write_wins';
|
|
12
|
+
/** Persist storage backend. */
|
|
13
|
+
export type PersistStorageType = 'indexeddb' | 'localstorage' | 'sqlite';
|
|
14
|
+
/** Eviction policy for cache management. */
|
|
15
|
+
export type EvictionPolicyType = 'lru' | 'lfu' | 'fifo';
|
|
16
|
+
/** Sync state of the offline system. */
|
|
17
|
+
export type SyncState = 'idle' | 'syncing' | 'error' | 'offline';
|
|
18
|
+
/** A queued mutation waiting to be synced. */
|
|
19
|
+
export interface QueuedMutation {
|
|
20
|
+
id: string;
|
|
21
|
+
timestamp: number;
|
|
22
|
+
operation: 'create' | 'update' | 'delete';
|
|
23
|
+
resource: string;
|
|
24
|
+
data?: Record<string, unknown>;
|
|
25
|
+
}
|
|
26
|
+
/** Cache configuration aligned with OfflineCacheConfigSchema. */
|
|
27
|
+
export interface OfflineCacheConfig {
|
|
28
|
+
maxSize?: number;
|
|
29
|
+
ttl?: number;
|
|
30
|
+
persistStorage?: PersistStorageType;
|
|
31
|
+
evictionPolicy?: EvictionPolicyType;
|
|
32
|
+
}
|
|
33
|
+
/** Sync configuration aligned with SyncConfigSchema. */
|
|
34
|
+
export interface OfflineSyncConfig {
|
|
35
|
+
strategy?: OfflineStrategy;
|
|
36
|
+
conflictResolution?: ConflictResolutionStrategy;
|
|
37
|
+
retryInterval?: number;
|
|
38
|
+
maxRetries?: number;
|
|
39
|
+
batchSize?: number;
|
|
40
|
+
}
|
|
41
|
+
/** Top-level offline configuration aligned with OfflineConfigSchema. */
|
|
42
|
+
export interface OfflineConfig {
|
|
43
|
+
enabled?: boolean;
|
|
44
|
+
strategy?: OfflineStrategy;
|
|
45
|
+
cache?: OfflineCacheConfig;
|
|
46
|
+
sync?: OfflineSyncConfig;
|
|
47
|
+
offlineIndicator?: boolean;
|
|
48
|
+
offlineMessage?: string;
|
|
49
|
+
queueMaxSize?: number;
|
|
50
|
+
}
|
|
51
|
+
/** Result returned by the useOffline hook. */
|
|
52
|
+
export interface OfflineResult {
|
|
53
|
+
/** Whether the browser is currently online. */
|
|
54
|
+
isOnline: boolean;
|
|
55
|
+
/** Whether offline mode is enabled in config. */
|
|
56
|
+
enabled: boolean;
|
|
57
|
+
/** Current sync state. */
|
|
58
|
+
syncState: SyncState;
|
|
59
|
+
/** The active offline strategy. */
|
|
60
|
+
strategy: OfflineStrategy;
|
|
61
|
+
/** Number of pending mutations in the queue. */
|
|
62
|
+
pendingCount: number;
|
|
63
|
+
/** Queue a mutation for later sync. */
|
|
64
|
+
queueMutation: (mutation: Omit<QueuedMutation, 'id' | 'timestamp'>) => void;
|
|
65
|
+
/** Manually trigger a sync attempt. */
|
|
66
|
+
sync: () => Promise<void>;
|
|
67
|
+
/** Clear the mutation queue. */
|
|
68
|
+
clearQueue: () => void;
|
|
69
|
+
/** Whether to show the offline indicator UI. */
|
|
70
|
+
showIndicator: boolean;
|
|
71
|
+
/** The offline message to display. */
|
|
72
|
+
offlineMessage: string;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Hook for offline mode detection and sync queue management.
|
|
76
|
+
* Implements OfflineConfigSchema, SyncConfigSchema, and ConflictResolutionSchema
|
|
77
|
+
* from @objectstack/spec v2.0.7.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```tsx
|
|
81
|
+
* function App() {
|
|
82
|
+
* const offline = useOffline({
|
|
83
|
+
* enabled: true,
|
|
84
|
+
* strategy: 'cache_first',
|
|
85
|
+
* sync: { conflictResolution: 'last_write_wins' },
|
|
86
|
+
* });
|
|
87
|
+
*
|
|
88
|
+
* if (!offline.isOnline && offline.showIndicator) {
|
|
89
|
+
* return <Banner>{offline.offlineMessage}</Banner>;
|
|
90
|
+
* }
|
|
91
|
+
*
|
|
92
|
+
* return <div>Pending mutations: {offline.pendingCount}</div>;
|
|
93
|
+
* }
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export declare function useOffline(config?: OfflineConfig): OfflineResult;
|