forked from microsoft/fluentui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create useReducedMotion and apply to useMotion to skip all moti…
…on calculations
- Loading branch information
1 parent
03a806a
commit d2dca9d
Showing
3 changed files
with
40 additions
and
1 deletion.
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
36 changes: 36 additions & 0 deletions
36
packages/react-components/react-motion-preview/src/hooks/useReducedMotion.ts
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,36 @@ | ||
import * as React from 'react'; | ||
import { canUseDOM, useIsomorphicLayoutEffect } from '@fluentui/react-utilities'; | ||
import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts'; | ||
|
||
/** | ||
* @internal | ||
* | ||
* Returns whether the user has requested reduced motion based on the current media query. | ||
*/ | ||
export const useReducedMotion = (): boolean => { | ||
const fluent = useFluent(); | ||
const reducedMotion = React.useRef(false); | ||
const targetWindow = canUseDOM() && fluent.targetDocument?.defaultView; | ||
|
||
const onMediaQueryChange = React.useCallback((e: MediaQueryListEvent) => { | ||
reducedMotion.current = e.matches; | ||
}, []); | ||
|
||
useIsomorphicLayoutEffect(() => { | ||
if (!targetWindow || !targetWindow.matchMedia) { | ||
return; | ||
} | ||
|
||
const match = targetWindow.matchMedia('screen and (prefers-reduced-motion: reduce)'); | ||
|
||
if (match.matches) { | ||
reducedMotion.current = true; | ||
} | ||
|
||
match.addEventListener('change', onMediaQueryChange); | ||
|
||
return () => match.removeEventListener('change', onMediaQueryChange); | ||
}, [onMediaQueryChange, targetWindow]); | ||
|
||
return reducedMotion.current; | ||
}; |