@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,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;
|
|
@@ -0,0 +1,209 @@
|
|
|
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
|
+
maxLCP: 600,
|
|
14
|
+
maxFCP: 300,
|
|
15
|
+
maxRenderTime: 16,
|
|
16
|
+
maxMemoryMB: 128,
|
|
17
|
+
warnOnViolation: true,
|
|
18
|
+
};
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
// Helpers (same getFCP/getLCP pattern as usePerformance.ts)
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
function getFCP() {
|
|
23
|
+
if (typeof performance === 'undefined' || !performance.getEntriesByType)
|
|
24
|
+
return null;
|
|
25
|
+
try {
|
|
26
|
+
const entries = performance.getEntriesByType('paint');
|
|
27
|
+
const entry = entries.find((e) => e.name === 'first-contentful-paint');
|
|
28
|
+
return entry ? Math.round(entry.startTime) : null;
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function getLCP() {
|
|
35
|
+
if (typeof performance === 'undefined' || !performance.getEntriesByType)
|
|
36
|
+
return null;
|
|
37
|
+
try {
|
|
38
|
+
const entries = performance.getEntriesByType('largest-contentful-paint');
|
|
39
|
+
const entry = entries.length > 0 ? entries[entries.length - 1] : undefined;
|
|
40
|
+
return entry ? Math.round(entry.startTime) : null;
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/** Chrome-only memory API (guarded). */
|
|
47
|
+
function getMemoryMB() {
|
|
48
|
+
try {
|
|
49
|
+
const mem = performance.memory;
|
|
50
|
+
if (mem?.usedJSHeapSize != null) {
|
|
51
|
+
return Math.round((mem.usedJSHeapSize / (1024 * 1024)) * 100) / 100;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
/* not available */
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
function buildViolationKey(metric) {
|
|
60
|
+
return metric;
|
|
61
|
+
}
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
// usePerformanceBudget hook
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
/**
|
|
66
|
+
* Hook for performance budget enforcement aligned with ROADMAP Q3 3.3.
|
|
67
|
+
*
|
|
68
|
+
* Monitors Web Vitals (LCP, FCP), memory usage, and component render times
|
|
69
|
+
* against configurable thresholds. Violations are recorded, deduplicated,
|
|
70
|
+
* and optionally logged to the console in development mode.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```tsx
|
|
74
|
+
* function App() {
|
|
75
|
+
* const budget = usePerformanceBudget({
|
|
76
|
+
* maxLCP: 500,
|
|
77
|
+
* maxRenderTime: 16,
|
|
78
|
+
* onViolation: (v) => analytics.track('perf_violation', v),
|
|
79
|
+
* });
|
|
80
|
+
*
|
|
81
|
+
* if (!budget.isWithinBudget) {
|
|
82
|
+
* console.warn('Performance budget exceeded:', budget.violations);
|
|
83
|
+
* }
|
|
84
|
+
*
|
|
85
|
+
* return <Dashboard />;
|
|
86
|
+
* }
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export function usePerformanceBudget(userBudget = {}) {
|
|
90
|
+
const budget = useMemo(() => ({
|
|
91
|
+
maxLCP: userBudget.maxLCP ?? DEFAULTS.maxLCP,
|
|
92
|
+
maxFCP: userBudget.maxFCP ?? DEFAULTS.maxFCP,
|
|
93
|
+
maxRenderTime: userBudget.maxRenderTime ?? DEFAULTS.maxRenderTime,
|
|
94
|
+
maxMemoryMB: userBudget.maxMemoryMB ?? DEFAULTS.maxMemoryMB,
|
|
95
|
+
warnOnViolation: userBudget.warnOnViolation ?? DEFAULTS.warnOnViolation,
|
|
96
|
+
}), [
|
|
97
|
+
userBudget.maxLCP,
|
|
98
|
+
userBudget.maxFCP,
|
|
99
|
+
userBudget.maxRenderTime,
|
|
100
|
+
userBudget.maxMemoryMB,
|
|
101
|
+
userBudget.warnOnViolation,
|
|
102
|
+
]);
|
|
103
|
+
const onViolationRef = useRef(userBudget.onViolation);
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
onViolationRef.current = userBudget.onViolation;
|
|
106
|
+
}, [userBudget.onViolation]);
|
|
107
|
+
const [violations, setViolations] = useState([]);
|
|
108
|
+
const seenRef = useRef(new Set());
|
|
109
|
+
const [snapshot, setSnapshot] = useState({ lcp: null, fcp: null, memoryMB: null });
|
|
110
|
+
// -------------------------------------------------------------------
|
|
111
|
+
// Violation recording with deduplication
|
|
112
|
+
// -------------------------------------------------------------------
|
|
113
|
+
const recordViolation = useCallback((metric, budgetValue, actual) => {
|
|
114
|
+
const key = buildViolationKey(metric);
|
|
115
|
+
if (seenRef.current.has(key))
|
|
116
|
+
return;
|
|
117
|
+
seenRef.current.add(key);
|
|
118
|
+
const violation = {
|
|
119
|
+
metric,
|
|
120
|
+
budget: budgetValue,
|
|
121
|
+
actual: Math.round(actual * 100) / 100,
|
|
122
|
+
timestamp: Date.now(),
|
|
123
|
+
};
|
|
124
|
+
setViolations((prev) => [...prev, violation]);
|
|
125
|
+
if (budget.warnOnViolation && process.env.NODE_ENV !== 'production') {
|
|
126
|
+
console.warn(`[ObjectUI] Performance budget violated: ${metric} = ${violation.actual} (budget: ${budgetValue})`);
|
|
127
|
+
}
|
|
128
|
+
onViolationRef.current?.(violation);
|
|
129
|
+
}, [budget.warnOnViolation]);
|
|
130
|
+
// -------------------------------------------------------------------
|
|
131
|
+
// Collect Web Vitals & memory on mount
|
|
132
|
+
// -------------------------------------------------------------------
|
|
133
|
+
useEffect(() => {
|
|
134
|
+
const timer = setTimeout(() => {
|
|
135
|
+
const fcp = getFCP();
|
|
136
|
+
const lcp = getLCP();
|
|
137
|
+
const memoryMB = getMemoryMB();
|
|
138
|
+
setSnapshot({ lcp, fcp, memoryMB });
|
|
139
|
+
if (fcp != null && fcp > budget.maxFCP) {
|
|
140
|
+
recordViolation('fcp', budget.maxFCP, fcp);
|
|
141
|
+
}
|
|
142
|
+
if (lcp != null && lcp > budget.maxLCP) {
|
|
143
|
+
recordViolation('lcp', budget.maxLCP, lcp);
|
|
144
|
+
}
|
|
145
|
+
if (memoryMB != null && memoryMB > budget.maxMemoryMB) {
|
|
146
|
+
recordViolation('memory', budget.maxMemoryMB, memoryMB);
|
|
147
|
+
}
|
|
148
|
+
}, 0);
|
|
149
|
+
return () => clearTimeout(timer);
|
|
150
|
+
}, [budget.maxFCP, budget.maxLCP, budget.maxMemoryMB, recordViolation]);
|
|
151
|
+
// -------------------------------------------------------------------
|
|
152
|
+
// Periodic memory monitoring
|
|
153
|
+
// -------------------------------------------------------------------
|
|
154
|
+
useEffect(() => {
|
|
155
|
+
// Only run when memory API is available
|
|
156
|
+
if (getMemoryMB() == null)
|
|
157
|
+
return;
|
|
158
|
+
const interval = setInterval(() => {
|
|
159
|
+
const memoryMB = getMemoryMB();
|
|
160
|
+
if (memoryMB != null) {
|
|
161
|
+
setSnapshot((prev) => ({ ...prev, memoryMB }));
|
|
162
|
+
if (memoryMB > budget.maxMemoryMB) {
|
|
163
|
+
recordViolation('memory', budget.maxMemoryMB, memoryMB);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}, 10000);
|
|
167
|
+
return () => clearInterval(interval);
|
|
168
|
+
}, [budget.maxMemoryMB, recordViolation]);
|
|
169
|
+
// -------------------------------------------------------------------
|
|
170
|
+
// Public API
|
|
171
|
+
// -------------------------------------------------------------------
|
|
172
|
+
const checkMetric = useCallback((metric, value) => {
|
|
173
|
+
let threshold;
|
|
174
|
+
switch (metric) {
|
|
175
|
+
case 'lcp':
|
|
176
|
+
threshold = budget.maxLCP;
|
|
177
|
+
break;
|
|
178
|
+
case 'fcp':
|
|
179
|
+
threshold = budget.maxFCP;
|
|
180
|
+
break;
|
|
181
|
+
case 'renderTime':
|
|
182
|
+
threshold = budget.maxRenderTime;
|
|
183
|
+
break;
|
|
184
|
+
case 'memory':
|
|
185
|
+
threshold = budget.maxMemoryMB;
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
const withinBudget = value <= threshold;
|
|
189
|
+
if (!withinBudget) {
|
|
190
|
+
recordViolation(metric, threshold, value);
|
|
191
|
+
}
|
|
192
|
+
return withinBudget;
|
|
193
|
+
}, [budget.maxLCP, budget.maxFCP, budget.maxRenderTime, budget.maxMemoryMB, recordViolation]);
|
|
194
|
+
const trackRender = useCallback((durationMs) => {
|
|
195
|
+
return checkMetric('renderTime', durationMs);
|
|
196
|
+
}, [checkMetric]);
|
|
197
|
+
const clearViolations = useCallback(() => {
|
|
198
|
+
setViolations([]);
|
|
199
|
+
seenRef.current.clear();
|
|
200
|
+
}, []);
|
|
201
|
+
return useMemo(() => ({
|
|
202
|
+
violations,
|
|
203
|
+
isWithinBudget: violations.length === 0,
|
|
204
|
+
checkMetric,
|
|
205
|
+
trackRender,
|
|
206
|
+
clearViolations,
|
|
207
|
+
snapshot,
|
|
208
|
+
}), [violations, checkMetric, trackRender, clearViolations, snapshot]);
|
|
209
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
* Hook that detects the user's motion preference.
|
|
10
|
+
* Implements MotionConfigSchema from @objectstack/spec v2.0.7.
|
|
11
|
+
*
|
|
12
|
+
* Respects the `prefers-reduced-motion` media query to provide
|
|
13
|
+
* accessible animation experiences.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```tsx
|
|
17
|
+
* function AnimatedCard() {
|
|
18
|
+
* const prefersReducedMotion = useReducedMotion();
|
|
19
|
+
*
|
|
20
|
+
* return (
|
|
21
|
+
* <div className={prefersReducedMotion ? '' : 'animate-fade-in'}>
|
|
22
|
+
* Content
|
|
23
|
+
* </div>
|
|
24
|
+
* );
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function useReducedMotion(): boolean;
|
|
@@ -0,0 +1,48 @@
|
|
|
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 { useState, useEffect } from 'react';
|
|
9
|
+
/**
|
|
10
|
+
* Hook that detects the user's motion preference.
|
|
11
|
+
* Implements MotionConfigSchema from @objectstack/spec v2.0.7.
|
|
12
|
+
*
|
|
13
|
+
* Respects the `prefers-reduced-motion` media query to provide
|
|
14
|
+
* accessible animation experiences.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```tsx
|
|
18
|
+
* function AnimatedCard() {
|
|
19
|
+
* const prefersReducedMotion = useReducedMotion();
|
|
20
|
+
*
|
|
21
|
+
* return (
|
|
22
|
+
* <div className={prefersReducedMotion ? '' : 'animate-fade-in'}>
|
|
23
|
+
* Content
|
|
24
|
+
* </div>
|
|
25
|
+
* );
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function useReducedMotion() {
|
|
30
|
+
const [prefersReducedMotion, setPrefersReducedMotion] = useState(() => {
|
|
31
|
+
if (typeof window === 'undefined')
|
|
32
|
+
return false;
|
|
33
|
+
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
34
|
+
});
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
if (typeof window === 'undefined')
|
|
37
|
+
return;
|
|
38
|
+
const mediaQuery = window.matchMedia('(prefers-reduced-motion: reduce)');
|
|
39
|
+
const handleChange = (event) => {
|
|
40
|
+
setPrefersReducedMotion(event.matches);
|
|
41
|
+
};
|
|
42
|
+
mediaQuery.addEventListener('change', handleChange);
|
|
43
|
+
return () => {
|
|
44
|
+
mediaQuery.removeEventListener('change', handleChange);
|
|
45
|
+
};
|
|
46
|
+
}, []);
|
|
47
|
+
return prefersReducedMotion;
|
|
48
|
+
}
|
|
@@ -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
|
+
/**
|
|
9
|
+
* Persistence adapter interface for schema save/load operations.
|
|
10
|
+
* Implement this to connect to any backend (REST API, GraphQL, localStorage, etc.).
|
|
11
|
+
*/
|
|
12
|
+
export interface SchemaPersistenceAdapter {
|
|
13
|
+
/** Save a schema to the backend. Returns the saved schema ID. */
|
|
14
|
+
save: (id: string, schema: Record<string, unknown>) => Promise<string>;
|
|
15
|
+
/** Load a schema from the backend by ID. */
|
|
16
|
+
load: (id: string) => Promise<Record<string, unknown> | null>;
|
|
17
|
+
/** List all available schemas. */
|
|
18
|
+
list?: () => Promise<Array<{
|
|
19
|
+
id: string;
|
|
20
|
+
name?: string;
|
|
21
|
+
updatedAt?: string;
|
|
22
|
+
}>>;
|
|
23
|
+
/** Delete a schema from the backend. */
|
|
24
|
+
delete?: (id: string) => Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
export interface SchemaPersistenceState {
|
|
27
|
+
/** Whether a save/load operation is in progress */
|
|
28
|
+
loading: boolean;
|
|
29
|
+
/** The last error from a save/load operation */
|
|
30
|
+
error: Error | null;
|
|
31
|
+
/** Whether the current schema has unsaved changes */
|
|
32
|
+
isDirty: boolean;
|
|
33
|
+
/** The last saved timestamp */
|
|
34
|
+
lastSavedAt: Date | null;
|
|
35
|
+
}
|
|
36
|
+
export interface SchemaPersistenceResult extends SchemaPersistenceState {
|
|
37
|
+
/** Save the current schema */
|
|
38
|
+
save: (id: string, schema: Record<string, unknown>) => Promise<string | null>;
|
|
39
|
+
/** Load a schema by ID */
|
|
40
|
+
load: (id: string) => Promise<Record<string, unknown> | null>;
|
|
41
|
+
/** List available schemas */
|
|
42
|
+
list: () => Promise<Array<{
|
|
43
|
+
id: string;
|
|
44
|
+
name?: string;
|
|
45
|
+
updatedAt?: string;
|
|
46
|
+
}>>;
|
|
47
|
+
/** Delete a schema */
|
|
48
|
+
remove: (id: string) => Promise<boolean>;
|
|
49
|
+
/** Mark the current schema as dirty (has unsaved changes) */
|
|
50
|
+
markDirty: () => void;
|
|
51
|
+
/** Clear the error state */
|
|
52
|
+
clearError: () => void;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Default localStorage adapter for schema persistence.
|
|
56
|
+
* Useful for development and demos.
|
|
57
|
+
*/
|
|
58
|
+
export declare function createLocalStorageAdapter(prefix?: string): SchemaPersistenceAdapter;
|
|
59
|
+
/**
|
|
60
|
+
* Hook for persisting designer schemas (save/load/list/delete).
|
|
61
|
+
* Implements schema persistence for @object-ui/plugin-designer.
|
|
62
|
+
*
|
|
63
|
+
* Accepts a pluggable adapter for connecting to any backend.
|
|
64
|
+
* Falls back to a localStorage adapter for development use.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```tsx
|
|
68
|
+
* // With default localStorage adapter
|
|
69
|
+
* const persistence = useSchemaPersistence();
|
|
70
|
+
*
|
|
71
|
+
* // Save current design
|
|
72
|
+
* await persistence.save('my-page', pageSchema);
|
|
73
|
+
*
|
|
74
|
+
* // Load a design
|
|
75
|
+
* const schema = await persistence.load('my-page');
|
|
76
|
+
*
|
|
77
|
+
* // With custom API adapter
|
|
78
|
+
* const apiAdapter: SchemaPersistenceAdapter = {
|
|
79
|
+
* save: (id, schema) => fetch(`/api/schemas/${id}`, { method: 'PUT', body: JSON.stringify(schema) }).then(r => r.json()),
|
|
80
|
+
* load: (id) => fetch(`/api/schemas/${id}`).then(r => r.json()),
|
|
81
|
+
* list: () => fetch('/api/schemas').then(r => r.json()),
|
|
82
|
+
* delete: (id) => fetch(`/api/schemas/${id}`, { method: 'DELETE' }).then(() => {}),
|
|
83
|
+
* };
|
|
84
|
+
* const persistence = useSchemaPersistence(apiAdapter);
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export declare function useSchemaPersistence(adapter?: SchemaPersistenceAdapter): SchemaPersistenceResult;
|