@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,130 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* ObjectUI
|
|
4
|
+
* Copyright (c) 2024-present ObjectStack Inc.
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE file in the root directory of this source tree.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* @object-ui/react - Notification Context
|
|
11
|
+
*
|
|
12
|
+
* Provides a spec-driven notification system to the component tree.
|
|
13
|
+
* Implements NotificationSchema, NotificationConfigSchema, and
|
|
14
|
+
* NotificationActionSchema from @objectstack/spec v2.0.7.
|
|
15
|
+
*/
|
|
16
|
+
import { createContext, useCallback, useContext, useMemo, useState } from 'react';
|
|
17
|
+
let notificationCounter = 0;
|
|
18
|
+
const NotificationCtx = createContext(null);
|
|
19
|
+
/**
|
|
20
|
+
* NotificationProvider — Provides a spec-driven notification system.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```tsx
|
|
24
|
+
* <NotificationProvider
|
|
25
|
+
* config={{ position: 'top-right', defaultDuration: 5000, maxVisible: 5 }}
|
|
26
|
+
* onToast={(n) => toast[n.severity](n.title, { description: n.message })}
|
|
27
|
+
* >
|
|
28
|
+
* <App />
|
|
29
|
+
* </NotificationProvider>
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export const NotificationProvider = ({ children, config: userConfig = {}, onToast, }) => {
|
|
33
|
+
const config = useMemo(() => ({
|
|
34
|
+
position: 'top-right',
|
|
35
|
+
defaultDuration: 5000,
|
|
36
|
+
maxVisible: 5,
|
|
37
|
+
stacking: true,
|
|
38
|
+
...userConfig,
|
|
39
|
+
}),
|
|
40
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
41
|
+
[JSON.stringify(userConfig)]);
|
|
42
|
+
const [notifications, setNotifications] = useState([]);
|
|
43
|
+
const notify = useCallback((input) => {
|
|
44
|
+
const id = `notification-${++notificationCounter}`;
|
|
45
|
+
const notification = {
|
|
46
|
+
...input,
|
|
47
|
+
id,
|
|
48
|
+
createdAt: new Date(),
|
|
49
|
+
read: false,
|
|
50
|
+
};
|
|
51
|
+
setNotifications((prev) => [notification, ...prev]);
|
|
52
|
+
// Delegate to external toast handler if provided
|
|
53
|
+
if (onToast) {
|
|
54
|
+
onToast(notification);
|
|
55
|
+
}
|
|
56
|
+
// Auto-dismiss non-persistent notifications
|
|
57
|
+
const duration = input.duration ?? config.defaultDuration ?? 5000;
|
|
58
|
+
if (duration > 0) {
|
|
59
|
+
setTimeout(() => {
|
|
60
|
+
setNotifications((prev) => prev.filter((n) => n.id !== id));
|
|
61
|
+
}, duration);
|
|
62
|
+
}
|
|
63
|
+
return id;
|
|
64
|
+
}, [config.defaultDuration, onToast]);
|
|
65
|
+
const info = useCallback((title, message) => notify({ title, message, severity: 'info' }), [notify]);
|
|
66
|
+
const success = useCallback((title, message) => notify({ title, message, severity: 'success' }), [notify]);
|
|
67
|
+
const warning = useCallback((title, message) => notify({ title, message, severity: 'warning' }), [notify]);
|
|
68
|
+
const error = useCallback((title, message) => notify({ title, message, severity: 'error' }), [notify]);
|
|
69
|
+
const markAsRead = useCallback((id) => {
|
|
70
|
+
setNotifications((prev) => prev.map((n) => (n.id === id ? { ...n, read: true } : n)));
|
|
71
|
+
}, []);
|
|
72
|
+
const markAllAsRead = useCallback(() => {
|
|
73
|
+
setNotifications((prev) => prev.map((n) => ({ ...n, read: true })));
|
|
74
|
+
}, []);
|
|
75
|
+
const dismiss = useCallback((id) => {
|
|
76
|
+
setNotifications((prev) => prev.filter((n) => n.id !== id));
|
|
77
|
+
}, []);
|
|
78
|
+
const clearAll = useCallback(() => {
|
|
79
|
+
setNotifications([]);
|
|
80
|
+
}, []);
|
|
81
|
+
const unreadCount = useMemo(() => notifications.filter((n) => !n.read).length, [notifications]);
|
|
82
|
+
const value = useMemo(() => ({
|
|
83
|
+
notifications,
|
|
84
|
+
unreadCount,
|
|
85
|
+
notify,
|
|
86
|
+
info,
|
|
87
|
+
success,
|
|
88
|
+
warning,
|
|
89
|
+
error,
|
|
90
|
+
markAsRead,
|
|
91
|
+
markAllAsRead,
|
|
92
|
+
dismiss,
|
|
93
|
+
clearAll,
|
|
94
|
+
config,
|
|
95
|
+
}), [
|
|
96
|
+
notifications,
|
|
97
|
+
unreadCount,
|
|
98
|
+
notify,
|
|
99
|
+
info,
|
|
100
|
+
success,
|
|
101
|
+
warning,
|
|
102
|
+
error,
|
|
103
|
+
markAsRead,
|
|
104
|
+
markAllAsRead,
|
|
105
|
+
dismiss,
|
|
106
|
+
clearAll,
|
|
107
|
+
config,
|
|
108
|
+
]);
|
|
109
|
+
return (_jsx(NotificationCtx.Provider, { value: value, children: children }));
|
|
110
|
+
};
|
|
111
|
+
NotificationProvider.displayName = 'NotificationProvider';
|
|
112
|
+
/**
|
|
113
|
+
* Hook to consume the NotificationProvider context.
|
|
114
|
+
*
|
|
115
|
+
* @throws Error if used outside a NotificationProvider
|
|
116
|
+
*/
|
|
117
|
+
export function useNotifications() {
|
|
118
|
+
const ctx = useContext(NotificationCtx);
|
|
119
|
+
if (!ctx) {
|
|
120
|
+
throw new Error('useNotifications must be used within a <NotificationProvider>. ' +
|
|
121
|
+
'Wrap your app with <NotificationProvider> to use the notification system.');
|
|
122
|
+
}
|
|
123
|
+
return ctx;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Hook to check if a NotificationProvider is available.
|
|
127
|
+
*/
|
|
128
|
+
export function useHasNotificationProvider() {
|
|
129
|
+
return useContext(NotificationCtx) !== null;
|
|
130
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
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
|
+
* @object-ui/react - ThemeContext
|
|
10
|
+
*
|
|
11
|
+
* React context + provider for the Theme System.
|
|
12
|
+
* Converts a spec-aligned Theme JSON into CSS custom properties
|
|
13
|
+
* and manages mode switching, theme registration, and persistence.
|
|
14
|
+
*
|
|
15
|
+
* @module context/ThemeContext
|
|
16
|
+
* @packageDocumentation
|
|
17
|
+
*/
|
|
18
|
+
import React from 'react';
|
|
19
|
+
import type { Theme, ThemeMode } from '@object-ui/types';
|
|
20
|
+
/**
|
|
21
|
+
* The public API surface of the theme system, available via useTheme().
|
|
22
|
+
*/
|
|
23
|
+
export interface ThemeContextValue {
|
|
24
|
+
/** The fully resolved active theme (after inheritance) */
|
|
25
|
+
theme: Theme | null;
|
|
26
|
+
/** The effective mode: 'light' or 'dark' (resolved from 'auto') */
|
|
27
|
+
resolvedMode: 'light' | 'dark';
|
|
28
|
+
/** The raw mode setting (may be 'auto') */
|
|
29
|
+
mode: ThemeMode;
|
|
30
|
+
/** Name of the active theme */
|
|
31
|
+
activeTheme: string | null;
|
|
32
|
+
/** List of available themes */
|
|
33
|
+
themes: Theme[];
|
|
34
|
+
/** Switch to a different theme by name */
|
|
35
|
+
setTheme: (name: string) => void;
|
|
36
|
+
/** Switch mode (light / dark / auto) */
|
|
37
|
+
setMode: (mode: ThemeMode) => void;
|
|
38
|
+
/** The generated CSS custom property map */
|
|
39
|
+
cssVars: Record<string, string>;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Props for the ThemeProvider component.
|
|
43
|
+
*/
|
|
44
|
+
export interface ThemeProviderProps {
|
|
45
|
+
/** Available theme definitions */
|
|
46
|
+
themes?: Theme[];
|
|
47
|
+
/** Initial active theme name (default: first theme) */
|
|
48
|
+
defaultTheme?: string;
|
|
49
|
+
/** Initial mode (default: 'auto') */
|
|
50
|
+
defaultMode?: ThemeMode;
|
|
51
|
+
/** Persist theme/mode to localStorage */
|
|
52
|
+
persist?: boolean;
|
|
53
|
+
/** localStorage key prefix */
|
|
54
|
+
storageKey?: string;
|
|
55
|
+
/** Target element for CSS variable injection (default: document.documentElement) */
|
|
56
|
+
target?: HTMLElement | null;
|
|
57
|
+
/** Children */
|
|
58
|
+
children: React.ReactNode;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* ThemeProvider
|
|
62
|
+
*
|
|
63
|
+
* Wraps a subtree with theme context. It:
|
|
64
|
+
* 1. Maintains theme + mode state
|
|
65
|
+
* 2. Resolves theme inheritance (extends)
|
|
66
|
+
* 3. Converts the active theme → CSS custom properties
|
|
67
|
+
* 4. Injects those CSS variables into the DOM
|
|
68
|
+
* 5. Applies 'dark' / 'light' class to the target element
|
|
69
|
+
* 6. Optionally persists user preference to localStorage
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```tsx
|
|
73
|
+
* <ThemeProvider themes={[myTheme]} defaultTheme="my-theme" defaultMode="auto">
|
|
74
|
+
* <App />
|
|
75
|
+
* </ThemeProvider>
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export declare function ThemeProvider({ themes: themesInput, defaultTheme, defaultMode, persist, storageKey, target, children, }: ThemeProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
79
|
+
/**
|
|
80
|
+
* useTheme — access the current theme context.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```tsx
|
|
84
|
+
* const { theme, resolvedMode, setMode, setTheme } = useTheme();
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* @throws Error if used outside a ThemeProvider
|
|
88
|
+
*/
|
|
89
|
+
export declare function useTheme(): ThemeContextValue;
|
|
90
|
+
/**
|
|
91
|
+
* useOptionalTheme — like useTheme() but returns null outside a provider.
|
|
92
|
+
* Useful for components that should work with or without a theme.
|
|
93
|
+
*/
|
|
94
|
+
export declare function useOptionalTheme(): ThemeContextValue | null;
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* ObjectUI
|
|
4
|
+
* Copyright (c) 2024-present ObjectStack Inc.
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE file in the root directory of this source tree.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* @object-ui/react - ThemeContext
|
|
11
|
+
*
|
|
12
|
+
* React context + provider for the Theme System.
|
|
13
|
+
* Converts a spec-aligned Theme JSON into CSS custom properties
|
|
14
|
+
* and manages mode switching, theme registration, and persistence.
|
|
15
|
+
*
|
|
16
|
+
* @module context/ThemeContext
|
|
17
|
+
* @packageDocumentation
|
|
18
|
+
*/
|
|
19
|
+
import { createContext, useContext, useState, useEffect, useMemo, useCallback, } from 'react';
|
|
20
|
+
import { generateThemeVars, resolveThemeInheritance, resolveMode, } from '@object-ui/core';
|
|
21
|
+
// ============================================================================
|
|
22
|
+
// Context
|
|
23
|
+
// ============================================================================
|
|
24
|
+
const ThemeContext = createContext(null);
|
|
25
|
+
/**
|
|
26
|
+
* Default storage keys
|
|
27
|
+
*/
|
|
28
|
+
const DEFAULT_STORAGE_KEY = 'objectui-theme';
|
|
29
|
+
// ============================================================================
|
|
30
|
+
// Provider
|
|
31
|
+
// ============================================================================
|
|
32
|
+
/**
|
|
33
|
+
* ThemeProvider
|
|
34
|
+
*
|
|
35
|
+
* Wraps a subtree with theme context. It:
|
|
36
|
+
* 1. Maintains theme + mode state
|
|
37
|
+
* 2. Resolves theme inheritance (extends)
|
|
38
|
+
* 3. Converts the active theme → CSS custom properties
|
|
39
|
+
* 4. Injects those CSS variables into the DOM
|
|
40
|
+
* 5. Applies 'dark' / 'light' class to the target element
|
|
41
|
+
* 6. Optionally persists user preference to localStorage
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```tsx
|
|
45
|
+
* <ThemeProvider themes={[myTheme]} defaultTheme="my-theme" defaultMode="auto">
|
|
46
|
+
* <App />
|
|
47
|
+
* </ThemeProvider>
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export function ThemeProvider({ themes: themesInput = [], defaultTheme, defaultMode = 'auto', persist = false, storageKey = DEFAULT_STORAGE_KEY, target, children, }) {
|
|
51
|
+
// ---- Build theme registry ----
|
|
52
|
+
const registry = useMemo(() => {
|
|
53
|
+
const map = new Map();
|
|
54
|
+
for (const t of themesInput) {
|
|
55
|
+
map.set(t.name, t);
|
|
56
|
+
}
|
|
57
|
+
return map;
|
|
58
|
+
}, [themesInput]);
|
|
59
|
+
// ---- Load persisted state ----
|
|
60
|
+
const loadPersistedTheme = useCallback(() => {
|
|
61
|
+
if (!persist)
|
|
62
|
+
return undefined;
|
|
63
|
+
try {
|
|
64
|
+
if (typeof window !== 'undefined') {
|
|
65
|
+
return localStorage.getItem(`${storageKey}-name`) ?? undefined;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
catch { /* ignore */ }
|
|
69
|
+
return undefined;
|
|
70
|
+
}, [persist, storageKey]);
|
|
71
|
+
const loadPersistedMode = useCallback(() => {
|
|
72
|
+
if (!persist)
|
|
73
|
+
return undefined;
|
|
74
|
+
try {
|
|
75
|
+
if (typeof window !== 'undefined') {
|
|
76
|
+
const stored = localStorage.getItem(`${storageKey}-mode`);
|
|
77
|
+
if (stored === 'light' || stored === 'dark' || stored === 'auto') {
|
|
78
|
+
return stored;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
catch { /* ignore */ }
|
|
83
|
+
return undefined;
|
|
84
|
+
}, [persist, storageKey]);
|
|
85
|
+
// ---- State ----
|
|
86
|
+
const [activeThemeName, setActiveThemeName] = useState(() => loadPersistedTheme() ?? defaultTheme ?? themesInput[0]?.name ?? null);
|
|
87
|
+
const [mode, setModeState] = useState(() => loadPersistedMode() ?? defaultMode);
|
|
88
|
+
// ---- Resolve theme ----
|
|
89
|
+
const resolvedTheme = useMemo(() => {
|
|
90
|
+
if (!activeThemeName)
|
|
91
|
+
return null;
|
|
92
|
+
const base = registry.get(activeThemeName);
|
|
93
|
+
if (!base)
|
|
94
|
+
return null;
|
|
95
|
+
return resolveThemeInheritance(base, registry);
|
|
96
|
+
}, [activeThemeName, registry]);
|
|
97
|
+
// ---- Resolve mode ----
|
|
98
|
+
const [systemDark, setSystemDark] = useState(() => {
|
|
99
|
+
if (typeof window !== 'undefined' && window.matchMedia) {
|
|
100
|
+
return window.matchMedia('(prefers-color-scheme: dark)').matches;
|
|
101
|
+
}
|
|
102
|
+
return false;
|
|
103
|
+
});
|
|
104
|
+
// Listen to system preference changes
|
|
105
|
+
useEffect(() => {
|
|
106
|
+
if (typeof window === 'undefined' || !window.matchMedia)
|
|
107
|
+
return;
|
|
108
|
+
const mq = window.matchMedia('(prefers-color-scheme: dark)');
|
|
109
|
+
const handler = (e) => setSystemDark(e.matches);
|
|
110
|
+
mq.addEventListener('change', handler);
|
|
111
|
+
return () => mq.removeEventListener('change', handler);
|
|
112
|
+
}, []);
|
|
113
|
+
const resolvedMode = useMemo(() => resolveMode(mode, systemDark), [mode, systemDark]);
|
|
114
|
+
// ---- Generate CSS variables ----
|
|
115
|
+
const cssVars = useMemo(() => {
|
|
116
|
+
if (!resolvedTheme)
|
|
117
|
+
return {};
|
|
118
|
+
return generateThemeVars(resolvedTheme);
|
|
119
|
+
}, [resolvedTheme]);
|
|
120
|
+
// ---- Inject CSS variables into the DOM ----
|
|
121
|
+
useEffect(() => {
|
|
122
|
+
const el = target ?? (typeof document !== 'undefined' ? document.documentElement : null);
|
|
123
|
+
if (!el)
|
|
124
|
+
return;
|
|
125
|
+
// Apply CSS variables
|
|
126
|
+
const previousValues = {};
|
|
127
|
+
for (const [prop, value] of Object.entries(cssVars)) {
|
|
128
|
+
previousValues[prop] = el.style.getPropertyValue(prop);
|
|
129
|
+
el.style.setProperty(prop, value);
|
|
130
|
+
}
|
|
131
|
+
// Apply mode class
|
|
132
|
+
el.classList.remove('light', 'dark');
|
|
133
|
+
el.classList.add(resolvedMode);
|
|
134
|
+
// Apply logo / favicon
|
|
135
|
+
if (resolvedTheme?.logo?.favicon && typeof document !== 'undefined') {
|
|
136
|
+
const link = document.querySelector('link[rel="icon"]')
|
|
137
|
+
?? document.querySelector('#favicon');
|
|
138
|
+
if (link) {
|
|
139
|
+
link.href = resolvedTheme.logo.favicon;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return () => {
|
|
143
|
+
// Cleanup: remove injected vars
|
|
144
|
+
for (const prop of Object.keys(cssVars)) {
|
|
145
|
+
if (previousValues[prop]) {
|
|
146
|
+
el.style.setProperty(prop, previousValues[prop]);
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
el.style.removeProperty(prop);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
el.classList.remove(resolvedMode);
|
|
153
|
+
};
|
|
154
|
+
}, [cssVars, resolvedMode, resolvedTheme, target]);
|
|
155
|
+
// ---- Persistence ----
|
|
156
|
+
useEffect(() => {
|
|
157
|
+
if (!persist)
|
|
158
|
+
return;
|
|
159
|
+
try {
|
|
160
|
+
if (activeThemeName) {
|
|
161
|
+
localStorage.setItem(`${storageKey}-name`, activeThemeName);
|
|
162
|
+
}
|
|
163
|
+
localStorage.setItem(`${storageKey}-mode`, mode);
|
|
164
|
+
}
|
|
165
|
+
catch { /* ignore */ }
|
|
166
|
+
}, [persist, storageKey, activeThemeName, mode]);
|
|
167
|
+
// ---- Actions ----
|
|
168
|
+
const setTheme = useCallback((name) => {
|
|
169
|
+
if (registry.has(name)) {
|
|
170
|
+
setActiveThemeName(name);
|
|
171
|
+
}
|
|
172
|
+
}, [registry]);
|
|
173
|
+
const setMode = useCallback((newMode) => {
|
|
174
|
+
setModeState(newMode);
|
|
175
|
+
}, []);
|
|
176
|
+
// ---- Context value ----
|
|
177
|
+
const value = useMemo(() => ({
|
|
178
|
+
theme: resolvedTheme,
|
|
179
|
+
resolvedMode,
|
|
180
|
+
mode,
|
|
181
|
+
activeTheme: activeThemeName,
|
|
182
|
+
themes: themesInput,
|
|
183
|
+
setTheme,
|
|
184
|
+
setMode,
|
|
185
|
+
cssVars,
|
|
186
|
+
}), [resolvedTheme, resolvedMode, mode, activeThemeName, themesInput, setTheme, setMode, cssVars]);
|
|
187
|
+
return (_jsx(ThemeContext.Provider, { value: value, children: children }));
|
|
188
|
+
}
|
|
189
|
+
// ============================================================================
|
|
190
|
+
// Hook
|
|
191
|
+
// ============================================================================
|
|
192
|
+
/**
|
|
193
|
+
* useTheme — access the current theme context.
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* ```tsx
|
|
197
|
+
* const { theme, resolvedMode, setMode, setTheme } = useTheme();
|
|
198
|
+
* ```
|
|
199
|
+
*
|
|
200
|
+
* @throws Error if used outside a ThemeProvider
|
|
201
|
+
*/
|
|
202
|
+
export function useTheme() {
|
|
203
|
+
const ctx = useContext(ThemeContext);
|
|
204
|
+
if (!ctx) {
|
|
205
|
+
throw new Error('useTheme must be used within a <ThemeProvider>');
|
|
206
|
+
}
|
|
207
|
+
return ctx;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* useOptionalTheme — like useTheme() but returns null outside a provider.
|
|
211
|
+
* Useful for components that should work with or without a theme.
|
|
212
|
+
*/
|
|
213
|
+
export function useOptionalTheme() {
|
|
214
|
+
return useContext(ThemeContext);
|
|
215
|
+
}
|
package/dist/context/index.d.ts
CHANGED
package/dist/context/index.js
CHANGED
package/dist/hooks/index.d.ts
CHANGED
|
@@ -7,3 +7,25 @@
|
|
|
7
7
|
*/
|
|
8
8
|
export * from './useExpression';
|
|
9
9
|
export * from './useActionRunner';
|
|
10
|
+
export * from './useNavigationOverlay';
|
|
11
|
+
export * from './usePageVariables';
|
|
12
|
+
export * from './useViewData';
|
|
13
|
+
export * from './useDynamicApp';
|
|
14
|
+
export * from './useDiscovery';
|
|
15
|
+
export * from './useFocusTrap';
|
|
16
|
+
export * from './useFocusManagement';
|
|
17
|
+
export * from './useKeyboardShortcuts';
|
|
18
|
+
export * from './useCrudShortcuts';
|
|
19
|
+
export * from './useReducedMotion';
|
|
20
|
+
export * from './useAnimation';
|
|
21
|
+
export * from './useColumnSummary';
|
|
22
|
+
export * from './useDensityMode';
|
|
23
|
+
export * from './useViewSharing';
|
|
24
|
+
export * from './useClientNotifications';
|
|
25
|
+
export * from './useOffline';
|
|
26
|
+
export * from './usePerformance';
|
|
27
|
+
export * from './usePerformanceBudget';
|
|
28
|
+
export * from './usePageTransition';
|
|
29
|
+
export * from './useViewTransition';
|
|
30
|
+
export * from './useETagCache';
|
|
31
|
+
export * from './useSchemaPersistence';
|
package/dist/hooks/index.js
CHANGED
|
@@ -7,3 +7,25 @@
|
|
|
7
7
|
*/
|
|
8
8
|
export * from './useExpression';
|
|
9
9
|
export * from './useActionRunner';
|
|
10
|
+
export * from './useNavigationOverlay';
|
|
11
|
+
export * from './usePageVariables';
|
|
12
|
+
export * from './useViewData';
|
|
13
|
+
export * from './useDynamicApp';
|
|
14
|
+
export * from './useDiscovery';
|
|
15
|
+
export * from './useFocusTrap';
|
|
16
|
+
export * from './useFocusManagement';
|
|
17
|
+
export * from './useKeyboardShortcuts';
|
|
18
|
+
export * from './useCrudShortcuts';
|
|
19
|
+
export * from './useReducedMotion';
|
|
20
|
+
export * from './useAnimation';
|
|
21
|
+
export * from './useColumnSummary';
|
|
22
|
+
export * from './useDensityMode';
|
|
23
|
+
export * from './useViewSharing';
|
|
24
|
+
export * from './useClientNotifications';
|
|
25
|
+
export * from './useOffline';
|
|
26
|
+
export * from './usePerformance';
|
|
27
|
+
export * from './usePerformanceBudget';
|
|
28
|
+
export * from './usePageTransition';
|
|
29
|
+
export * from './useViewTransition';
|
|
30
|
+
export * from './useETagCache';
|
|
31
|
+
export * from './useSchemaPersistence';
|
|
@@ -5,25 +5,39 @@
|
|
|
5
5
|
* This source code is licensed under the MIT license found in the
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
*/
|
|
8
|
-
import { ActionRunner, type ActionContext, type ActionResult } from '@object-ui/core';
|
|
8
|
+
import { ActionRunner, type ActionContext, type ActionDef, type ActionResult, type ConfirmationHandler, type ToastHandler, type ModalHandler, type NavigationHandler } from '@object-ui/core';
|
|
9
|
+
export interface UseActionRunnerOptions {
|
|
10
|
+
context?: ActionContext;
|
|
11
|
+
/** Custom confirmation handler — wire to Shadcn AlertDialog or similar */
|
|
12
|
+
onConfirm?: ConfirmationHandler;
|
|
13
|
+
/** Custom toast handler — wire to Sonner or similar */
|
|
14
|
+
onToast?: ToastHandler;
|
|
15
|
+
/** Custom modal handler — render modals in response to modal actions */
|
|
16
|
+
onModal?: ModalHandler;
|
|
17
|
+
/** Custom navigation handler — SPA-aware routing */
|
|
18
|
+
onNavigate?: NavigationHandler;
|
|
19
|
+
}
|
|
9
20
|
/**
|
|
10
|
-
* Hook for executing actions with loading state
|
|
21
|
+
* Hook for executing actions with loading state, typed with ActionDef.
|
|
11
22
|
*
|
|
12
23
|
* @example
|
|
13
24
|
* ```tsx
|
|
14
|
-
* const { execute, loading, error, result } = useActionRunner({
|
|
25
|
+
* const { execute, loading, error, result } = useActionRunner({
|
|
26
|
+
* context: { data: formData },
|
|
27
|
+
* onToast: (msg, opts) => toast[opts?.type || 'info'](msg),
|
|
28
|
+
* });
|
|
15
29
|
*
|
|
16
30
|
* const handleClick = () => {
|
|
17
31
|
* execute({
|
|
18
|
-
* type: '
|
|
32
|
+
* type: 'api',
|
|
19
33
|
* api: '/api/save',
|
|
20
|
-
* successMessage: 'Saved successfully!'
|
|
34
|
+
* successMessage: 'Saved successfully!',
|
|
21
35
|
* });
|
|
22
36
|
* };
|
|
23
37
|
* ```
|
|
24
38
|
*/
|
|
25
|
-
export declare function useActionRunner(
|
|
26
|
-
execute: (action:
|
|
39
|
+
export declare function useActionRunner(optionsOrContext?: UseActionRunnerOptions | ActionContext): {
|
|
40
|
+
execute: (action: ActionDef) => Promise<ActionResult>;
|
|
27
41
|
loading: boolean;
|
|
28
42
|
error: string | null;
|
|
29
43
|
result: ActionResult | null;
|
|
@@ -6,28 +6,48 @@
|
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
*/
|
|
8
8
|
import { useState, useCallback, useMemo } from 'react';
|
|
9
|
-
import { ActionRunner } from '@object-ui/core';
|
|
9
|
+
import { ActionRunner, } from '@object-ui/core';
|
|
10
10
|
/**
|
|
11
|
-
* Hook for executing actions with loading state
|
|
11
|
+
* Hook for executing actions with loading state, typed with ActionDef.
|
|
12
12
|
*
|
|
13
13
|
* @example
|
|
14
14
|
* ```tsx
|
|
15
|
-
* const { execute, loading, error, result } = useActionRunner({
|
|
15
|
+
* const { execute, loading, error, result } = useActionRunner({
|
|
16
|
+
* context: { data: formData },
|
|
17
|
+
* onToast: (msg, opts) => toast[opts?.type || 'info'](msg),
|
|
18
|
+
* });
|
|
16
19
|
*
|
|
17
20
|
* const handleClick = () => {
|
|
18
21
|
* execute({
|
|
19
|
-
* type: '
|
|
22
|
+
* type: 'api',
|
|
20
23
|
* api: '/api/save',
|
|
21
|
-
* successMessage: 'Saved successfully!'
|
|
24
|
+
* successMessage: 'Saved successfully!',
|
|
22
25
|
* });
|
|
23
26
|
* };
|
|
24
27
|
* ```
|
|
25
28
|
*/
|
|
26
|
-
export function useActionRunner(
|
|
29
|
+
export function useActionRunner(optionsOrContext = {}) {
|
|
30
|
+
// Backwards compat: if no 'context' key, treat entire object as context
|
|
31
|
+
const options = 'context' in optionsOrContext
|
|
32
|
+
? optionsOrContext
|
|
33
|
+
: { context: optionsOrContext };
|
|
34
|
+
const { context = {}, onConfirm, onToast, onModal, onNavigate } = options;
|
|
27
35
|
const [loading, setLoading] = useState(false);
|
|
28
36
|
const [error, setError] = useState(null);
|
|
29
37
|
const [result, setResult] = useState(null);
|
|
30
|
-
const runner = useMemo(() =>
|
|
38
|
+
const runner = useMemo(() => {
|
|
39
|
+
const r = new ActionRunner(context);
|
|
40
|
+
if (onConfirm)
|
|
41
|
+
r.setConfirmHandler(onConfirm);
|
|
42
|
+
if (onToast)
|
|
43
|
+
r.setToastHandler(onToast);
|
|
44
|
+
if (onModal)
|
|
45
|
+
r.setModalHandler(onModal);
|
|
46
|
+
if (onNavigate)
|
|
47
|
+
r.setNavigationHandler(onNavigate);
|
|
48
|
+
return r;
|
|
49
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
50
|
+
}, [JSON.stringify(context)]);
|
|
31
51
|
const execute = useCallback(async (action) => {
|
|
32
52
|
setLoading(true);
|
|
33
53
|
setError(null);
|
|
@@ -0,0 +1,46 @@
|
|
|
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
|
+
/** Animation trigger type */
|
|
9
|
+
export type AnimationTriggerType = 'enter' | 'exit' | 'hover' | 'focus' | 'click';
|
|
10
|
+
/** Easing presets aligned with EasingFunctionSchema */
|
|
11
|
+
export type EasingPreset = 'linear' | 'ease' | 'ease-in' | 'ease-out' | 'ease-in-out' | 'spring';
|
|
12
|
+
/** Transition preset aligned with TransitionPresetSchema */
|
|
13
|
+
export type TransitionPresetType = 'fade' | 'slide-up' | 'slide-down' | 'slide-left' | 'slide-right' | 'scale' | 'scale-fade' | 'none';
|
|
14
|
+
export interface AnimationConfig {
|
|
15
|
+
/** The animation/transition preset */
|
|
16
|
+
preset?: TransitionPresetType;
|
|
17
|
+
/** Duration in milliseconds */
|
|
18
|
+
duration?: number;
|
|
19
|
+
/** Delay in milliseconds */
|
|
20
|
+
delay?: number;
|
|
21
|
+
/** Easing function */
|
|
22
|
+
easing?: EasingPreset;
|
|
23
|
+
/** Whether animation is enabled */
|
|
24
|
+
enabled?: boolean;
|
|
25
|
+
}
|
|
26
|
+
/** Resolved CSS classes and styles for animation */
|
|
27
|
+
export interface AnimationResult {
|
|
28
|
+
/** Tailwind CSS classes for the animation */
|
|
29
|
+
className: string;
|
|
30
|
+
/** Inline style for custom durations/delays (only when needed) */
|
|
31
|
+
style: React.CSSProperties;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Hook for applying spec-driven animations to components.
|
|
35
|
+
* Implements ComponentAnimationSchema and TransitionPresetSchema
|
|
36
|
+
* from @objectstack/spec v2.0.7.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```tsx
|
|
40
|
+
* function Card() {
|
|
41
|
+
* const animation = useAnimation({ preset: 'fade', duration: 200, easing: 'ease-out' });
|
|
42
|
+
* return <div className={animation.className} style={animation.style}>...</div>;
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare function useAnimation(config?: AnimationConfig): AnimationResult;
|