Debian Codegraph @

node/@patternfly/react-core/src/helpers/AnimationsProvider/AnimationsProvider.tsx

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