-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
ReducedMotionConfig
component (#6164)
## Summary This PR adds `ReduceMotionConfig` component that allows to determine the default animation behavior in response to the device's reduced motion accessibility setting. It affects application globally. The default behavior disables all animation if reduced motion is enabled on a device. You can utilize this component to override that behavior. ### Usage ```js function App() { return ( // ... <ReduceMotionConfig mode={ReduceMotion.Never} /> // ... ); } ``` ### Demo https://github.com/software-mansion/react-native-reanimated/assets/36106620/e15a484e-bdc1-454c-b9ff-3ae8863db62e ## Test plan Open `Reduce Motion` example from example app.
- Loading branch information
Showing
8 changed files
with
96 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict'; | ||
import { isWeb, isWindowAvailable } from './PlatformChecker'; | ||
import { makeMutable } from './mutables'; | ||
|
||
type localGlobal = typeof global & Record<string, unknown>; | ||
|
||
export function isReducedMotionEnabledInSystem() { | ||
return isWeb() | ||
? isWindowAvailable() | ||
? // @ts-ignore Fallback if `window` is undefined. | ||
!window.matchMedia('(prefers-reduced-motion: no-preference)').matches | ||
: false | ||
: !!(global as localGlobal)._REANIMATED_IS_REDUCED_MOTION; | ||
} | ||
|
||
const IS_REDUCED_MOTION_ENABLED_IN_SYSTEM = isReducedMotionEnabledInSystem(); | ||
|
||
export const ReducedMotionManager = { | ||
jsValue: IS_REDUCED_MOTION_ENABLED_IN_SYSTEM, | ||
uiValue: makeMutable(IS_REDUCED_MOTION_ENABLED_IN_SYSTEM), | ||
setEnabled(value: boolean) { | ||
ReducedMotionManager.jsValue = value; | ||
ReducedMotionManager.uiValue.value = value; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/react-native-reanimated/src/component/ReducedMotionConfig.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict'; | ||
import { useEffect } from 'react'; | ||
import { ReduceMotion } from '../commonTypes'; | ||
import { | ||
ReducedMotionManager, | ||
isReducedMotionEnabledInSystem, | ||
} from '../ReducedMotion'; | ||
|
||
/** | ||
* A component that lets you overwrite default reduce motion behavior globally in your application. | ||
* | ||
* @param mode - Determines default reduce motion behavior globally in your application. Configured with {@link ReduceMotion} enum. | ||
* @see https://docs.swmansion.com/react-native-reanimated/docs/components/ReduceMotionConfig | ||
*/ | ||
export function ReducedMotionConfig({ mode }: { mode: ReduceMotion }) { | ||
useEffect(() => { | ||
if (!__DEV__) { | ||
return; | ||
} | ||
console.warn( | ||
`[Reanimated] Reduced motion setting is overwritten with mode '${mode}'.` | ||
); | ||
}, []); | ||
|
||
useEffect(() => { | ||
const wasEnabled = ReducedMotionManager.jsValue; | ||
switch (mode) { | ||
case ReduceMotion.System: | ||
ReducedMotionManager.setEnabled(isReducedMotionEnabledInSystem()); | ||
break; | ||
case ReduceMotion.Always: | ||
ReducedMotionManager.setEnabled(true); | ||
break; | ||
case ReduceMotion.Never: | ||
ReducedMotionManager.setEnabled(false); | ||
break; | ||
} | ||
return () => { | ||
ReducedMotionManager.setEnabled(wasEnabled); | ||
}; | ||
}, [mode]); | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters