File size: 1,040 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { useState } from 'react'
import { AccessibilityInfo } from 'react-native'

import { assign } from '../globals'

import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect.native'

/**
 * Returns `boolean` or `null`, used to automatically
 * set skipAnimations to the value of the user's
 * `prefers-reduced-motion` query.
 *
 * The return value, post-effect, is the value of their prefered setting
 */
export const useReducedMotion = () => {
  const [reducedMotion, setReducedMotion] = useState<boolean | null>(null)

  useIsomorphicLayoutEffect(() => {
    const handleMediaChange = (isReducedMotion: boolean) => {
      setReducedMotion(isReducedMotion)

      assign({
        skipAnimation: isReducedMotion,
      })
    }

    AccessibilityInfo.isReduceMotionEnabled().then(handleMediaChange)

    const motionSubscription = AccessibilityInfo.addEventListener(
      'reduceMotionChanged',
      handleMediaChange
    )

    return () => {
      motionSubscription.remove()
    }
  }, [])

  return reducedMotion
}