Debian Codegraph @

node/@patternfly/react-core/dist/esm/helpers/AnimationsProvider/AnimationsProvider.js

raw
1 import { jsx as _jsx } from "react/jsx-runtime";
2 import { createContext, useContext } from 'react';
3 /** Context to provide animations configuration to child components */
4 const AnimationsContext = createContext({
5 hasAnimations: false
6 });
7 /**
8 * AnimationsProvider is an application-level provider that provides uniform
9 * animation configuration for all PatternFly React components via the React context API.
10 *
11 * **Usage**: Place this provider at the root of your application to enable global
12 * animation control without requiring manual prop drilling throughout your component tree.
13 *
14 * **Benefits**:
15 * - Centralized animation control for the entire application
16 * - Respects user accessibility preferences (reduced motion)
17 * - Components can still override the global setting when needed
18 * - Works with all PatternFly components that support animations
19 *
20 * @example
21 * ```tsx
22 * // App.tsx - Place at your application root
23 * import { AnimationsProvider } from '@patternfly/react-core';
24 *
25 * const App = () => (
26 * <AnimationsProvider config={{ hasAnimations: true }}>
27 * <MyApplication />
28 * </AnimationsProvider>
29 * );
30 * ```
31 */
32 export const AnimationsProvider = ({ config, children }) => (_jsx(AnimationsContext.Provider, { value: config, children: children }));
33 /**
34 * Hook to access the animations configuration from the nearest AnimationsProvider.
35 *
36 * This hook allows components to check if animations are enabled and override
37 * their local hasAnimations prop accordingly.
38 *
39 * @returns The animations configuration object
40 *
41 * @example
42 * ```tsx
43 * const MyComponent = ({ hasAnimations: hasAnimationsProp, ...props }) => {
44 * const { hasAnimations: contextHasAnimations } = useAnimationsConfig();
45 * const hasAnimations = hasAnimationsProp ?? contextHasAnimations;
46 *
47 * return <div className={hasAnimations ? 'with-animations' : ''} {...props} />;
48 * };
49 * ```
50 */
51 export const useAnimationsConfig = () => {
52 const context = useContext(AnimationsContext);
53 if (context === undefined) {
54 // Return default config if no provider is found
55 return { hasAnimations: false };
56 }
57 return context;
58 };
59 /**
60 * Utility hook that combines local hasAnimations prop with context configuration.
61 * The local prop takes precedence when explicitly set, otherwise falls back to context.
62 *
63 * @param hasAnimationsProp - The hasAnimations prop passed directly to the component
64 * @returns The resolved hasAnimations value
65 *
66 * @example
67 * ```tsx
68 * const MyComponent = ({ hasAnimations: hasAnimationsProp, ...props }) => {
69 * const hasAnimations = useHasAnimations(hasAnimationsProp);
70 *
71 * return <div className={hasAnimations ? 'animated' : 'static'} {...props} />;
72 * };
73 * ```
74 */
75 export const useHasAnimations = (hasAnimationsProp) => {
76 var _a;
77 const { hasAnimations: contextHasAnimations } = useAnimationsConfig();
78 // Local prop takes precedence when explicitly set (including false)
79 // If local prop is undefined, fall back to context
80 return (_a = hasAnimationsProp !== null && hasAnimationsProp !== void 0 ? hasAnimationsProp : contextHasAnimations) !== null && _a !== void 0 ? _a : false;
81 };
82 AnimationsProvider.displayName = 'AnimationsProvider';
83 //# sourceMappingURL=AnimationsProvider.js.map