Debian Codegraph @

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

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