@object-ui/react 2.0.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 +19 -0
- package/dist/SchemaRenderer.d.ts +20 -1
- package/dist/SchemaRenderer.js +76 -13
- 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/index.d.ts +2 -0
- package/dist/context/index.js +2 -0
- package/dist/hooks/index.d.ts +17 -0
- package/dist/hooks/index.js +17 -0
- 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/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/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/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/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/package.json +7 -7
- package/src/SchemaRenderer.tsx +122 -13
- 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__/useOffline.test.ts +198 -0
- package/src/__tests__/usePageTransition.test.ts +214 -0
- package/src/__tests__/usePerformance.test.ts +137 -0
- package/src/__tests__/view-switching.test.tsx +456 -0
- package/src/context/DndContext.tsx +260 -0
- package/src/context/NotificationContext.tsx +280 -0
- package/src/context/__tests__/DndContext.test.tsx +150 -0
- package/src/context/__tests__/NotificationContext.test.tsx +176 -0
- package/src/context/index.ts +2 -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__/useETagCache.test.ts +153 -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 +17 -1
- 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/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/useOffline.ts +301 -0
- package/src/hooks/usePageTransition.ts +187 -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/useViewSharing.ts +140 -0
- package/src/hooks/useViewTransition.ts +144 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,185 @@
|
|
|
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 { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from 'react';
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Online/offline external store (SSR-safe)
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
function subscribeOnline(callback) {
|
|
13
|
+
if (typeof window === 'undefined')
|
|
14
|
+
return () => { };
|
|
15
|
+
window.addEventListener('online', callback);
|
|
16
|
+
window.addEventListener('offline', callback);
|
|
17
|
+
return () => {
|
|
18
|
+
window.removeEventListener('online', callback);
|
|
19
|
+
window.removeEventListener('offline', callback);
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function getOnlineSnapshot() {
|
|
23
|
+
return typeof navigator !== 'undefined' ? navigator.onLine : true;
|
|
24
|
+
}
|
|
25
|
+
function getServerSnapshot() {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
// ---------------------------------------------------------------------------
|
|
29
|
+
// Mutation queue storage helpers
|
|
30
|
+
// ---------------------------------------------------------------------------
|
|
31
|
+
const QUEUE_STORAGE_KEY = 'objectui-offline-queue';
|
|
32
|
+
function loadQueue() {
|
|
33
|
+
if (typeof localStorage === 'undefined')
|
|
34
|
+
return [];
|
|
35
|
+
try {
|
|
36
|
+
const raw = localStorage.getItem(QUEUE_STORAGE_KEY);
|
|
37
|
+
return raw ? JSON.parse(raw) : [];
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function persistQueue(queue) {
|
|
44
|
+
if (typeof localStorage === 'undefined')
|
|
45
|
+
return;
|
|
46
|
+
try {
|
|
47
|
+
localStorage.setItem(QUEUE_STORAGE_KEY, JSON.stringify(queue));
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
// localStorage full or unavailable — silently ignore
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
let idCounter = 0;
|
|
54
|
+
function generateId() {
|
|
55
|
+
idCounter += 1;
|
|
56
|
+
return `mut_${Date.now()}_${idCounter}`;
|
|
57
|
+
}
|
|
58
|
+
// ---------------------------------------------------------------------------
|
|
59
|
+
// useOffline hook
|
|
60
|
+
// ---------------------------------------------------------------------------
|
|
61
|
+
const DEFAULTS = {
|
|
62
|
+
enabled: true,
|
|
63
|
+
strategy: 'network_first',
|
|
64
|
+
offlineIndicator: true,
|
|
65
|
+
offlineMessage: 'You are currently offline. Changes will be synced when reconnected.',
|
|
66
|
+
queueMaxSize: 100,
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Hook for offline mode detection and sync queue management.
|
|
70
|
+
* Implements OfflineConfigSchema, SyncConfigSchema, and ConflictResolutionSchema
|
|
71
|
+
* from @objectstack/spec v2.0.7.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```tsx
|
|
75
|
+
* function App() {
|
|
76
|
+
* const offline = useOffline({
|
|
77
|
+
* enabled: true,
|
|
78
|
+
* strategy: 'cache_first',
|
|
79
|
+
* sync: { conflictResolution: 'last_write_wins' },
|
|
80
|
+
* });
|
|
81
|
+
*
|
|
82
|
+
* if (!offline.isOnline && offline.showIndicator) {
|
|
83
|
+
* return <Banner>{offline.offlineMessage}</Banner>;
|
|
84
|
+
* }
|
|
85
|
+
*
|
|
86
|
+
* return <div>Pending mutations: {offline.pendingCount}</div>;
|
|
87
|
+
* }
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export function useOffline(config = {}) {
|
|
91
|
+
const { enabled = DEFAULTS.enabled, strategy = DEFAULTS.strategy, offlineIndicator = DEFAULTS.offlineIndicator, offlineMessage = DEFAULTS.offlineMessage, queueMaxSize = DEFAULTS.queueMaxSize, sync: syncConfig, } = config;
|
|
92
|
+
const isOnline = useSyncExternalStore(subscribeOnline, getOnlineSnapshot, getServerSnapshot);
|
|
93
|
+
const [queue, setQueue] = useState(loadQueue);
|
|
94
|
+
const [syncState, setSyncState] = useState('idle');
|
|
95
|
+
const syncConfigRef = useRef(syncConfig);
|
|
96
|
+
syncConfigRef.current = syncConfig;
|
|
97
|
+
// Persist queue to localStorage whenever it changes
|
|
98
|
+
useEffect(() => {
|
|
99
|
+
persistQueue(queue);
|
|
100
|
+
}, [queue]);
|
|
101
|
+
// Update sync state based on online status
|
|
102
|
+
useEffect(() => {
|
|
103
|
+
if (!enabled)
|
|
104
|
+
return;
|
|
105
|
+
if (!isOnline) {
|
|
106
|
+
setSyncState('offline');
|
|
107
|
+
}
|
|
108
|
+
else if (syncState === 'offline') {
|
|
109
|
+
setSyncState('idle');
|
|
110
|
+
}
|
|
111
|
+
}, [isOnline, enabled, syncState]);
|
|
112
|
+
const queueMutation = useCallback((mutation) => {
|
|
113
|
+
if (!enabled)
|
|
114
|
+
return;
|
|
115
|
+
setQueue((prev) => {
|
|
116
|
+
const newEntry = {
|
|
117
|
+
...mutation,
|
|
118
|
+
id: generateId(),
|
|
119
|
+
timestamp: Date.now(),
|
|
120
|
+
};
|
|
121
|
+
const next = [...prev, newEntry];
|
|
122
|
+
// Enforce max queue size (FIFO eviction)
|
|
123
|
+
if (next.length > queueMaxSize) {
|
|
124
|
+
return next.slice(next.length - queueMaxSize);
|
|
125
|
+
}
|
|
126
|
+
return next;
|
|
127
|
+
});
|
|
128
|
+
}, [enabled, queueMaxSize]);
|
|
129
|
+
const clearQueue = useCallback(() => {
|
|
130
|
+
setQueue([]);
|
|
131
|
+
}, []);
|
|
132
|
+
const sync = useCallback(async () => {
|
|
133
|
+
if (!enabled || queue.length === 0)
|
|
134
|
+
return;
|
|
135
|
+
setSyncState('syncing');
|
|
136
|
+
try {
|
|
137
|
+
// In a real implementation, this would batch-send mutations to the server.
|
|
138
|
+
// For now, we simulate a successful sync by clearing the queue.
|
|
139
|
+
const batchSize = syncConfigRef.current?.batchSize ?? queue.length;
|
|
140
|
+
const batch = queue.slice(0, batchSize);
|
|
141
|
+
// Simulate network round-trip
|
|
142
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
143
|
+
// Remove synced mutations from queue
|
|
144
|
+
setQueue((prev) => prev.filter((m) => !batch.some((b) => b.id === m.id)));
|
|
145
|
+
setSyncState('idle');
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
setSyncState('error');
|
|
149
|
+
}
|
|
150
|
+
}, [enabled, queue]);
|
|
151
|
+
// Auto-sync when coming back online (short stabilization delay)
|
|
152
|
+
useEffect(() => {
|
|
153
|
+
if (!enabled || !isOnline || queue.length === 0)
|
|
154
|
+
return;
|
|
155
|
+
const timer = setTimeout(() => {
|
|
156
|
+
void sync();
|
|
157
|
+
}, 100);
|
|
158
|
+
return () => clearTimeout(timer);
|
|
159
|
+
// Only trigger on isOnline changes, not on every queue change
|
|
160
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
161
|
+
}, [isOnline, enabled]);
|
|
162
|
+
return useMemo(() => ({
|
|
163
|
+
isOnline,
|
|
164
|
+
enabled,
|
|
165
|
+
syncState,
|
|
166
|
+
strategy,
|
|
167
|
+
pendingCount: queue.length,
|
|
168
|
+
queueMutation,
|
|
169
|
+
sync,
|
|
170
|
+
clearQueue,
|
|
171
|
+
showIndicator: offlineIndicator && !isOnline,
|
|
172
|
+
offlineMessage,
|
|
173
|
+
}), [
|
|
174
|
+
isOnline,
|
|
175
|
+
enabled,
|
|
176
|
+
syncState,
|
|
177
|
+
strategy,
|
|
178
|
+
queue.length,
|
|
179
|
+
queueMutation,
|
|
180
|
+
sync,
|
|
181
|
+
clearQueue,
|
|
182
|
+
offlineIndicator,
|
|
183
|
+
offlineMessage,
|
|
184
|
+
]);
|
|
185
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
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
|
+
/** Page transition type. */
|
|
9
|
+
export type PageTransitionType = 'none' | 'fade' | 'slide_up' | 'slide_down' | 'slide_left' | 'slide_right' | 'scale' | 'rotate' | 'flip';
|
|
10
|
+
/** Easing function for page transitions. */
|
|
11
|
+
export type PageTransitionEasing = 'linear' | 'ease' | 'ease_in' | 'ease_out' | 'ease_in_out' | 'spring';
|
|
12
|
+
/** Page transition configuration aligned with PageTransitionSchema. */
|
|
13
|
+
export interface PageTransitionConfig {
|
|
14
|
+
/** Transition type. */
|
|
15
|
+
type?: PageTransitionType;
|
|
16
|
+
/** Duration in milliseconds. */
|
|
17
|
+
duration?: number;
|
|
18
|
+
/** Easing function. */
|
|
19
|
+
easing?: PageTransitionEasing;
|
|
20
|
+
/** Whether entering and exiting pages cross-fade. */
|
|
21
|
+
crossFade?: boolean;
|
|
22
|
+
}
|
|
23
|
+
/** Resolved CSS classes and styles for a page transition. */
|
|
24
|
+
export interface PageTransitionResult {
|
|
25
|
+
/** Tailwind CSS classes for the enter transition. */
|
|
26
|
+
enterClassName: string;
|
|
27
|
+
/** Tailwind CSS classes for the exit transition. */
|
|
28
|
+
exitClassName: string;
|
|
29
|
+
/** Inline styles for the enter transition. */
|
|
30
|
+
enterStyle: React.CSSProperties;
|
|
31
|
+
/** Inline styles for the exit transition. */
|
|
32
|
+
exitStyle: React.CSSProperties;
|
|
33
|
+
/** Whether the transition is active (not 'none' and motion not reduced). */
|
|
34
|
+
isActive: boolean;
|
|
35
|
+
/** The resolved transition type. */
|
|
36
|
+
type: PageTransitionType;
|
|
37
|
+
/** The resolved duration in ms. */
|
|
38
|
+
duration: number;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Hook for page-level transition animations aligned with
|
|
42
|
+
* PageTransitionSchema from @objectstack/spec v2.0.7.
|
|
43
|
+
*
|
|
44
|
+
* Generates Tailwind CSS animate-in / animate-out classes and inline styles
|
|
45
|
+
* for enter/exit page transitions. Respects `prefers-reduced-motion`.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```tsx
|
|
49
|
+
* function PageWrapper({ children }: { children: React.ReactNode }) {
|
|
50
|
+
* const transition = usePageTransition({ type: 'fade', duration: 200 });
|
|
51
|
+
*
|
|
52
|
+
* return (
|
|
53
|
+
* <div className={transition.enterClassName} style={transition.enterStyle}>
|
|
54
|
+
* {children}
|
|
55
|
+
* </div>
|
|
56
|
+
* );
|
|
57
|
+
* }
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export declare function usePageTransition(config?: PageTransitionConfig): PageTransitionResult;
|
|
@@ -0,0 +1,111 @@
|
|
|
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 { useMemo } from 'react';
|
|
9
|
+
import { useReducedMotion } from './useReducedMotion';
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
// Constants
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
const EASING_CSS = {
|
|
14
|
+
linear: 'linear',
|
|
15
|
+
ease: 'ease',
|
|
16
|
+
ease_in: 'ease-in',
|
|
17
|
+
ease_out: 'ease-out',
|
|
18
|
+
ease_in_out: 'ease-in-out',
|
|
19
|
+
spring: 'cubic-bezier(0.34, 1.56, 0.64, 1)',
|
|
20
|
+
};
|
|
21
|
+
const ENTER_CLASSES = {
|
|
22
|
+
none: '',
|
|
23
|
+
fade: 'animate-in fade-in',
|
|
24
|
+
slide_up: 'animate-in slide-in-from-bottom-4',
|
|
25
|
+
slide_down: 'animate-in slide-in-from-top-4',
|
|
26
|
+
slide_left: 'animate-in slide-in-from-right-4',
|
|
27
|
+
slide_right: 'animate-in slide-in-from-left-4',
|
|
28
|
+
scale: 'animate-in zoom-in-95',
|
|
29
|
+
rotate: 'animate-in spin-in-90',
|
|
30
|
+
flip: 'animate-in fade-in zoom-in-95',
|
|
31
|
+
};
|
|
32
|
+
const EXIT_CLASSES = {
|
|
33
|
+
none: '',
|
|
34
|
+
fade: 'animate-out fade-out',
|
|
35
|
+
slide_up: 'animate-out slide-out-to-top-4',
|
|
36
|
+
slide_down: 'animate-out slide-out-to-bottom-4',
|
|
37
|
+
slide_left: 'animate-out slide-out-to-left-4',
|
|
38
|
+
slide_right: 'animate-out slide-out-to-right-4',
|
|
39
|
+
scale: 'animate-out zoom-out-95',
|
|
40
|
+
rotate: 'animate-out spin-out-90',
|
|
41
|
+
flip: 'animate-out fade-out zoom-out-95',
|
|
42
|
+
};
|
|
43
|
+
const DEFAULT_DURATION = 300;
|
|
44
|
+
const DEFAULT_EASING = 'ease_in_out';
|
|
45
|
+
// ---------------------------------------------------------------------------
|
|
46
|
+
// usePageTransition hook
|
|
47
|
+
// ---------------------------------------------------------------------------
|
|
48
|
+
/**
|
|
49
|
+
* Hook for page-level transition animations aligned with
|
|
50
|
+
* PageTransitionSchema from @objectstack/spec v2.0.7.
|
|
51
|
+
*
|
|
52
|
+
* Generates Tailwind CSS animate-in / animate-out classes and inline styles
|
|
53
|
+
* for enter/exit page transitions. Respects `prefers-reduced-motion`.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```tsx
|
|
57
|
+
* function PageWrapper({ children }: { children: React.ReactNode }) {
|
|
58
|
+
* const transition = usePageTransition({ type: 'fade', duration: 200 });
|
|
59
|
+
*
|
|
60
|
+
* return (
|
|
61
|
+
* <div className={transition.enterClassName} style={transition.enterStyle}>
|
|
62
|
+
* {children}
|
|
63
|
+
* </div>
|
|
64
|
+
* );
|
|
65
|
+
* }
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export function usePageTransition(config = {}) {
|
|
69
|
+
const { type = 'none', duration = DEFAULT_DURATION, easing = DEFAULT_EASING, crossFade = false, } = config;
|
|
70
|
+
const reducedMotion = useReducedMotion();
|
|
71
|
+
return useMemo(() => {
|
|
72
|
+
const isActive = type !== 'none' && !reducedMotion;
|
|
73
|
+
if (!isActive) {
|
|
74
|
+
return {
|
|
75
|
+
enterClassName: '',
|
|
76
|
+
exitClassName: '',
|
|
77
|
+
enterStyle: {},
|
|
78
|
+
exitStyle: {},
|
|
79
|
+
isActive: false,
|
|
80
|
+
type,
|
|
81
|
+
duration,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
const enterClassName = ENTER_CLASSES[type] || '';
|
|
85
|
+
const exitClassName = EXIT_CLASSES[type] || '';
|
|
86
|
+
const baseStyle = {
|
|
87
|
+
animationDuration: `${duration}ms`,
|
|
88
|
+
animationTimingFunction: EASING_CSS[easing] || EASING_CSS.ease_in_out,
|
|
89
|
+
animationFillMode: 'both',
|
|
90
|
+
};
|
|
91
|
+
const enterStyle = { ...baseStyle };
|
|
92
|
+
const exitStyle = { ...baseStyle };
|
|
93
|
+
// When crossFade is enabled, both the entering and exiting pages
|
|
94
|
+
// should overlap and transition opacity simultaneously.
|
|
95
|
+
if (crossFade) {
|
|
96
|
+
enterStyle.position = 'absolute';
|
|
97
|
+
enterStyle.inset = '0';
|
|
98
|
+
exitStyle.position = 'absolute';
|
|
99
|
+
exitStyle.inset = '0';
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
enterClassName,
|
|
103
|
+
exitClassName,
|
|
104
|
+
enterStyle,
|
|
105
|
+
exitStyle,
|
|
106
|
+
isActive: true,
|
|
107
|
+
type,
|
|
108
|
+
duration,
|
|
109
|
+
};
|
|
110
|
+
}, [type, duration, easing, crossFade, reducedMotion]);
|
|
111
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
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
|
+
/** Cache strategy for data fetching. */
|
|
9
|
+
export type CacheStrategyType = 'none' | 'cache-first' | 'network-first' | 'stale-while-revalidate';
|
|
10
|
+
/** Virtual scroll configuration. */
|
|
11
|
+
export interface VirtualScrollConfig {
|
|
12
|
+
enabled?: boolean;
|
|
13
|
+
itemHeight?: number;
|
|
14
|
+
overscan?: number;
|
|
15
|
+
}
|
|
16
|
+
/** Performance configuration aligned with PerformanceConfigSchema. */
|
|
17
|
+
export interface PerformanceConfig {
|
|
18
|
+
/** Whether to lazy-load components/data. */
|
|
19
|
+
lazyLoad?: boolean;
|
|
20
|
+
/** Virtual scroll settings for large lists. */
|
|
21
|
+
virtualScroll?: VirtualScrollConfig;
|
|
22
|
+
/** Cache strategy for data fetching. */
|
|
23
|
+
cacheStrategy?: CacheStrategyType;
|
|
24
|
+
/** Whether to prefetch linked resources. */
|
|
25
|
+
prefetch?: boolean;
|
|
26
|
+
/** Default page size for paginated views. */
|
|
27
|
+
pageSize?: number;
|
|
28
|
+
/** Debounce interval in milliseconds for user input. */
|
|
29
|
+
debounceMs?: number;
|
|
30
|
+
}
|
|
31
|
+
/** Web Vitals metrics snapshot. */
|
|
32
|
+
export interface PerformanceMetrics {
|
|
33
|
+
/** Largest Contentful Paint (ms). */
|
|
34
|
+
lcp: number | null;
|
|
35
|
+
/** First Contentful Paint (ms). */
|
|
36
|
+
fcp: number | null;
|
|
37
|
+
/** Time to Interactive (ms). */
|
|
38
|
+
tti: number | null;
|
|
39
|
+
/** Total render count since mount. */
|
|
40
|
+
renderCount: number;
|
|
41
|
+
/** Last render duration (ms). */
|
|
42
|
+
lastRenderDuration: number | null;
|
|
43
|
+
}
|
|
44
|
+
/** Result returned by the usePerformance hook. */
|
|
45
|
+
export interface PerformanceResult {
|
|
46
|
+
/** The resolved performance configuration (with defaults). */
|
|
47
|
+
config: Required<Omit<PerformanceConfig, 'virtualScroll'>> & {
|
|
48
|
+
virtualScroll: Required<VirtualScrollConfig>;
|
|
49
|
+
};
|
|
50
|
+
/** Current performance metrics. */
|
|
51
|
+
metrics: PerformanceMetrics;
|
|
52
|
+
/** Mark a rendering start (returns stop function). */
|
|
53
|
+
markRenderStart: () => () => void;
|
|
54
|
+
/** Create a debounced version of a callback. */
|
|
55
|
+
debounce: <T extends (...args: unknown[]) => void>(fn: T) => T;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Hook for performance monitoring and configuration aligned with
|
|
59
|
+
* PerformanceConfigSchema from @objectstack/spec v2.0.7.
|
|
60
|
+
*
|
|
61
|
+
* Provides resolved config values, Web Vitals metrics, and utility functions
|
|
62
|
+
* (debounce, render marking) for performance-aware components.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```tsx
|
|
66
|
+
* function DataGrid({ data }: { data: unknown[] }) {
|
|
67
|
+
* const perf = usePerformance({
|
|
68
|
+
* lazyLoad: true,
|
|
69
|
+
* virtualScroll: { enabled: true, itemHeight: 40 },
|
|
70
|
+
* debounceMs: 200,
|
|
71
|
+
* });
|
|
72
|
+
*
|
|
73
|
+
* const handleSearch = perf.debounce((query: string) => {
|
|
74
|
+
* // debounced search
|
|
75
|
+
* });
|
|
76
|
+
*
|
|
77
|
+
* return (
|
|
78
|
+
* <div>
|
|
79
|
+
* {perf.config.virtualScroll.enabled
|
|
80
|
+
* ? <VirtualList itemHeight={perf.config.virtualScroll.itemHeight} />
|
|
81
|
+
* : <NormalList />}
|
|
82
|
+
* </div>
|
|
83
|
+
* );
|
|
84
|
+
* }
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export declare function usePerformance(userConfig?: PerformanceConfig): PerformanceResult;
|
|
@@ -0,0 +1,165 @@
|
|
|
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 { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Defaults
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
const DEFAULTS = {
|
|
13
|
+
lazyLoad: true,
|
|
14
|
+
cacheStrategy: 'stale-while-revalidate',
|
|
15
|
+
prefetch: false,
|
|
16
|
+
pageSize: 50,
|
|
17
|
+
debounceMs: 300,
|
|
18
|
+
virtualScroll: {
|
|
19
|
+
enabled: false,
|
|
20
|
+
itemHeight: 40,
|
|
21
|
+
overscan: 5,
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
// Helpers
|
|
26
|
+
// ---------------------------------------------------------------------------
|
|
27
|
+
function getFCP() {
|
|
28
|
+
if (typeof performance === 'undefined' || !performance.getEntriesByType)
|
|
29
|
+
return null;
|
|
30
|
+
try {
|
|
31
|
+
const entries = performance.getEntriesByType('paint');
|
|
32
|
+
const entry = entries.find((e) => e.name === 'first-contentful-paint');
|
|
33
|
+
return entry ? Math.round(entry.startTime) : null;
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function getLCP() {
|
|
40
|
+
if (typeof performance === 'undefined' || !performance.getEntriesByType)
|
|
41
|
+
return null;
|
|
42
|
+
try {
|
|
43
|
+
const entries = performance.getEntriesByType('largest-contentful-paint');
|
|
44
|
+
const entry = entries.length > 0 ? entries[entries.length - 1] : undefined;
|
|
45
|
+
return entry ? Math.round(entry.startTime) : null;
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// ---------------------------------------------------------------------------
|
|
52
|
+
// usePerformance hook
|
|
53
|
+
// ---------------------------------------------------------------------------
|
|
54
|
+
/**
|
|
55
|
+
* Hook for performance monitoring and configuration aligned with
|
|
56
|
+
* PerformanceConfigSchema from @objectstack/spec v2.0.7.
|
|
57
|
+
*
|
|
58
|
+
* Provides resolved config values, Web Vitals metrics, and utility functions
|
|
59
|
+
* (debounce, render marking) for performance-aware components.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```tsx
|
|
63
|
+
* function DataGrid({ data }: { data: unknown[] }) {
|
|
64
|
+
* const perf = usePerformance({
|
|
65
|
+
* lazyLoad: true,
|
|
66
|
+
* virtualScroll: { enabled: true, itemHeight: 40 },
|
|
67
|
+
* debounceMs: 200,
|
|
68
|
+
* });
|
|
69
|
+
*
|
|
70
|
+
* const handleSearch = perf.debounce((query: string) => {
|
|
71
|
+
* // debounced search
|
|
72
|
+
* });
|
|
73
|
+
*
|
|
74
|
+
* return (
|
|
75
|
+
* <div>
|
|
76
|
+
* {perf.config.virtualScroll.enabled
|
|
77
|
+
* ? <VirtualList itemHeight={perf.config.virtualScroll.itemHeight} />
|
|
78
|
+
* : <NormalList />}
|
|
79
|
+
* </div>
|
|
80
|
+
* );
|
|
81
|
+
* }
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export function usePerformance(userConfig = {}) {
|
|
85
|
+
const config = useMemo(() => ({
|
|
86
|
+
lazyLoad: userConfig.lazyLoad ?? DEFAULTS.lazyLoad,
|
|
87
|
+
cacheStrategy: userConfig.cacheStrategy ?? DEFAULTS.cacheStrategy,
|
|
88
|
+
prefetch: userConfig.prefetch ?? DEFAULTS.prefetch,
|
|
89
|
+
pageSize: userConfig.pageSize ?? DEFAULTS.pageSize,
|
|
90
|
+
debounceMs: userConfig.debounceMs ?? DEFAULTS.debounceMs,
|
|
91
|
+
virtualScroll: {
|
|
92
|
+
enabled: userConfig.virtualScroll?.enabled ?? DEFAULTS.virtualScroll.enabled,
|
|
93
|
+
itemHeight: userConfig.virtualScroll?.itemHeight ?? DEFAULTS.virtualScroll.itemHeight,
|
|
94
|
+
overscan: userConfig.virtualScroll?.overscan ?? DEFAULTS.virtualScroll.overscan,
|
|
95
|
+
},
|
|
96
|
+
}), [
|
|
97
|
+
userConfig.lazyLoad,
|
|
98
|
+
userConfig.cacheStrategy,
|
|
99
|
+
userConfig.prefetch,
|
|
100
|
+
userConfig.pageSize,
|
|
101
|
+
userConfig.debounceMs,
|
|
102
|
+
userConfig.virtualScroll?.enabled,
|
|
103
|
+
userConfig.virtualScroll?.itemHeight,
|
|
104
|
+
userConfig.virtualScroll?.overscan,
|
|
105
|
+
]);
|
|
106
|
+
const [metrics, setMetrics] = useState({
|
|
107
|
+
lcp: null,
|
|
108
|
+
fcp: null,
|
|
109
|
+
tti: null,
|
|
110
|
+
renderCount: 0,
|
|
111
|
+
lastRenderDuration: null,
|
|
112
|
+
});
|
|
113
|
+
// Collect paint metrics on mount
|
|
114
|
+
useEffect(() => {
|
|
115
|
+
// Defer metric collection to allow paint entries to populate
|
|
116
|
+
const timer = setTimeout(() => {
|
|
117
|
+
setMetrics((prev) => ({
|
|
118
|
+
...prev,
|
|
119
|
+
fcp: getFCP(),
|
|
120
|
+
lcp: getLCP(),
|
|
121
|
+
}));
|
|
122
|
+
}, 0);
|
|
123
|
+
return () => clearTimeout(timer);
|
|
124
|
+
}, []);
|
|
125
|
+
const markRenderStart = useCallback(() => {
|
|
126
|
+
const start = typeof performance !== 'undefined' ? performance.now() : Date.now();
|
|
127
|
+
return () => {
|
|
128
|
+
const end = typeof performance !== 'undefined' ? performance.now() : Date.now();
|
|
129
|
+
const duration = Math.round((end - start) * 100) / 100;
|
|
130
|
+
setMetrics((prev) => ({ ...prev, lastRenderDuration: duration, renderCount: prev.renderCount + 1 }));
|
|
131
|
+
};
|
|
132
|
+
}, []);
|
|
133
|
+
const debounceMs = config.debounceMs;
|
|
134
|
+
const timersRef = useRef(new Set());
|
|
135
|
+
const debounce = useCallback((fn) => {
|
|
136
|
+
let timer = null;
|
|
137
|
+
const debounced = (...args) => {
|
|
138
|
+
if (timer) {
|
|
139
|
+
clearTimeout(timer);
|
|
140
|
+
timersRef.current.delete(timer);
|
|
141
|
+
}
|
|
142
|
+
timer = setTimeout(() => {
|
|
143
|
+
fn(...args);
|
|
144
|
+
if (timer)
|
|
145
|
+
timersRef.current.delete(timer);
|
|
146
|
+
}, debounceMs);
|
|
147
|
+
timersRef.current.add(timer);
|
|
148
|
+
};
|
|
149
|
+
return debounced;
|
|
150
|
+
}, [debounceMs]);
|
|
151
|
+
// Cleanup all debounce timers
|
|
152
|
+
useEffect(() => {
|
|
153
|
+
const timers = timersRef.current;
|
|
154
|
+
return () => {
|
|
155
|
+
timers.forEach((t) => clearTimeout(t));
|
|
156
|
+
timers.clear();
|
|
157
|
+
};
|
|
158
|
+
}, []);
|
|
159
|
+
return useMemo(() => ({
|
|
160
|
+
config,
|
|
161
|
+
metrics,
|
|
162
|
+
markRenderStart,
|
|
163
|
+
debounce,
|
|
164
|
+
}), [config, metrics, markRenderStart, debounce]);
|
|
165
|
+
}
|
|
@@ -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
|
+
/** Metric names that can be tracked against a budget. */
|
|
9
|
+
export type BudgetMetric = 'lcp' | 'fcp' | 'renderTime' | 'memory';
|
|
10
|
+
/**
|
|
11
|
+
* Performance budget configuration.
|
|
12
|
+
* Defines thresholds for LCP, bundle size, render time, etc.
|
|
13
|
+
*/
|
|
14
|
+
export interface PerformanceBudget {
|
|
15
|
+
/** Max Largest Contentful Paint in ms (default: 600) */
|
|
16
|
+
maxLCP?: number;
|
|
17
|
+
/** Max First Contentful Paint in ms (default: 300) */
|
|
18
|
+
maxFCP?: number;
|
|
19
|
+
/** Max component render time in ms (default: 16) */
|
|
20
|
+
maxRenderTime?: number;
|
|
21
|
+
/** Max bundle size in KB (gzipped) - informational only */
|
|
22
|
+
maxBundleSizeKB?: number;
|
|
23
|
+
/** Max memory usage in MB */
|
|
24
|
+
maxMemoryMB?: number;
|
|
25
|
+
/** Whether to log warnings to console in dev mode (default: true) */
|
|
26
|
+
warnOnViolation?: boolean;
|
|
27
|
+
/** Callback when a budget is violated */
|
|
28
|
+
onViolation?: (violation: BudgetViolation) => void;
|
|
29
|
+
}
|
|
30
|
+
export interface BudgetViolation {
|
|
31
|
+
metric: BudgetMetric;
|
|
32
|
+
budget: number;
|
|
33
|
+
actual: number;
|
|
34
|
+
timestamp: number;
|
|
35
|
+
}
|
|
36
|
+
export interface PerformanceBudgetResult {
|
|
37
|
+
/** Current violations */
|
|
38
|
+
violations: BudgetViolation[];
|
|
39
|
+
/** Whether all budgets are within limits */
|
|
40
|
+
isWithinBudget: boolean;
|
|
41
|
+
/** Check if a specific metric is within budget */
|
|
42
|
+
checkMetric: (metric: BudgetMetric, value: number) => boolean;
|
|
43
|
+
/** Track a render and check against budget */
|
|
44
|
+
trackRender: (durationMs: number) => boolean;
|
|
45
|
+
/** Clear all recorded violations */
|
|
46
|
+
clearViolations: () => void;
|
|
47
|
+
/** Current performance snapshot */
|
|
48
|
+
snapshot: {
|
|
49
|
+
lcp: number | null;
|
|
50
|
+
fcp: number | null;
|
|
51
|
+
memoryMB: number | null;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Hook for performance budget enforcement aligned with ROADMAP Q3 3.3.
|
|
56
|
+
*
|
|
57
|
+
* Monitors Web Vitals (LCP, FCP), memory usage, and component render times
|
|
58
|
+
* against configurable thresholds. Violations are recorded, deduplicated,
|
|
59
|
+
* and optionally logged to the console in development mode.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```tsx
|
|
63
|
+
* function App() {
|
|
64
|
+
* const budget = usePerformanceBudget({
|
|
65
|
+
* maxLCP: 500,
|
|
66
|
+
* maxRenderTime: 16,
|
|
67
|
+
* onViolation: (v) => analytics.track('perf_violation', v),
|
|
68
|
+
* });
|
|
69
|
+
*
|
|
70
|
+
* if (!budget.isWithinBudget) {
|
|
71
|
+
* console.warn('Performance budget exceeded:', budget.violations);
|
|
72
|
+
* }
|
|
73
|
+
*
|
|
74
|
+
* return <Dashboard />;
|
|
75
|
+
* }
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export declare function usePerformanceBudget(userBudget?: PerformanceBudget): PerformanceBudgetResult;
|