Skip to content

Commit 3365cea

Browse files
authored
feat: add useReducedMotion hook & export useIsomorphicLayout hook (pmndrs#1886)
1 parent 35da18b commit 3365cea

20 files changed

+214
-46
lines changed
Binary file not shown.
Binary file not shown.
-10.9 KB
Binary file not shown.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"@react-three/fiber": "^7.0.26",
7777
"@testing-library/cypress": "^8.0.2",
7878
"@testing-library/jest-dom": "^5.16.3",
79-
"@testing-library/react": "^13.0.0",
79+
"@testing-library/react": "^13.2.0",
8080
"@types/jest": "^27.4.0",
8181
"@types/lodash.clamp": "^4.0.6",
8282
"@types/lodash.shuffle": "^4.2.6",

packages/animated/src/withAnimated.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
FluidValue,
1111
addFluidObserver,
1212
removeFluidObserver,
13-
useLayoutEffect,
13+
useIsomorphicLayoutEffect,
1414
} from '@react-spring/shared'
1515
import { ElementType } from '@react-spring/types'
1616

@@ -66,7 +66,7 @@ export const withAnimated = (Component: any, host: HostConfig) => {
6666
const observer = new PropsObserver(callback, deps)
6767

6868
const observerRef = useRef<PropsObserver>()
69-
useLayoutEffect(() => {
69+
useIsomorphicLayoutEffect(() => {
7070
observerRef.current = observer
7171

7272
// Observe the latest dependencies.

packages/core/src/hooks/useChain.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { each, useLayoutEffect } from '@react-spring/shared'
1+
import { each, useIsomorphicLayoutEffect } from '@react-spring/shared'
22
import { SpringRef } from '../SpringRef'
33
import { callProp } from '../helpers'
44

@@ -7,7 +7,7 @@ export function useChain(
77
timeSteps?: number[],
88
timeFrame = 1000
99
) {
10-
useLayoutEffect(() => {
10+
useIsomorphicLayoutEffect(() => {
1111
if (timeSteps) {
1212
let prevDelay = 0
1313
each(refs, (ref, i) => {

packages/core/src/hooks/useSprings.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
usePrev,
77
useOnce,
88
useForceUpdate,
9-
useLayoutEffect,
9+
useIsomorphicLayoutEffect,
1010
} from '@react-spring/shared'
1111

1212
import {
@@ -166,14 +166,14 @@ export function useSprings(
166166

167167
// New springs are created during render so users can pass them to
168168
// their animated components, but new springs aren't cached until the
169-
// commit phase (see the `useLayoutEffect` callback below).
169+
// commit phase (see the `useIsomorphicLayoutEffect` callback below).
170170
const springs = ctrls.current.map((ctrl, i) => getSprings(ctrl, updates[i]))
171171

172172
const context = useContext(SpringContext)
173173
const prevContext = usePrev(context)
174174
const hasContext = context !== prevContext && hasProps(context)
175175

176-
useLayoutEffect(() => {
176+
useIsomorphicLayoutEffect(() => {
177177
layoutId.current++
178178

179179
// Replace the cached controllers.

packages/core/src/hooks/useTrail.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { each, is, useLayoutEffect } from '@react-spring/shared'
1+
import { each, is, useIsomorphicLayoutEffect } from '@react-spring/shared'
22

33
import { Valid } from '../types/common'
44
import { PickAnimated, SpringValues, AsyncResult } from '../types'
@@ -62,7 +62,7 @@ export function useTrail(
6262

6363
const ref = passedRef ?? result[1]
6464

65-
useLayoutEffect(() => {
65+
useIsomorphicLayoutEffect(() => {
6666
each(ref.current, (ctrl, i) => {
6767
const parent = ref.current[i + (reverse ? 1 : -1)]
6868
if (parent) {

packages/core/src/hooks/useTransition.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
useOnce,
99
usePrev,
1010
each,
11-
useLayoutEffect,
11+
useIsomorphicLayoutEffect,
1212
} from '@react-spring/shared'
1313

1414
import {
@@ -98,7 +98,7 @@ export function useTransition(
9898
const usedTransitions = useRef<TransitionState[] | null>(null)
9999
const prevTransitions = reset ? null : usedTransitions.current
100100

101-
useLayoutEffect(() => {
101+
useIsomorphicLayoutEffect(() => {
102102
usedTransitions.current = transitions
103103
})
104104

@@ -139,7 +139,7 @@ export function useTransition(
139139

140140
// Expired transitions that need clean up.
141141
const expired = (reset && usedTransitions.current) || []
142-
useLayoutEffect(() =>
142+
useIsomorphicLayoutEffect(() =>
143143
each(expired, ({ ctrl, item, key }) => {
144144
detachRefs(ctrl, ref)
145145
callProp(onDestroyed, item, key)
@@ -358,7 +358,7 @@ export function useTransition(
358358
const hasContext = context !== prevContext && hasProps(context)
359359

360360
// Merge the context into each transition.
361-
useLayoutEffect(() => {
361+
useIsomorphicLayoutEffect(() => {
362362
if (hasContext) {
363363
each(transitions, t => {
364364
t.ctrl.start({ default: context })
@@ -378,7 +378,7 @@ export function useTransition(
378378
}
379379
})
380380

381-
useLayoutEffect(
381+
useIsomorphicLayoutEffect(
382382
() => {
383383
/*
384384
* if exitingTransitions.current has a size it means we're exiting before enter

packages/core/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ export { SpringRef } from './SpringRef'
1212
export { FrameValue } from './FrameValue'
1313
export { Interpolation } from './Interpolation'
1414
export { BailSignal } from './runAsync'
15-
export { createInterpolator } from '@react-spring/shared'
15+
export {
16+
createInterpolator,
17+
useIsomorphicLayoutEffect,
18+
useReducedMotion,
19+
} from '@react-spring/shared'
1620
export { inferTo } from './helpers'
1721

1822
export * from './types'

0 commit comments

Comments
 (0)