-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(motion): add extended support for reduced motion
- Loading branch information
1 parent
883151b
commit 7649e34
Showing
13 changed files
with
316 additions
and
5 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-motion-f4c1433b-f36e-4b2c-a9bd-9b16f37489fc.json
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,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "feat: add extended support for reduced motion", | ||
"packageName": "@fluentui/react-motion", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
84 changes: 84 additions & 0 deletions
84
packages/react-components/react-motion/library/src/hooks/useAnimateAtoms.test.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,84 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
|
||
import type { AtomMotion } from '../types'; | ||
import { DEFAULT_ANIMATION_OPTIONS, useAnimateAtoms } from './useAnimateAtoms'; | ||
|
||
function createElementMock() { | ||
const animate = jest.fn().mockReturnValue({ | ||
persist: jest.fn(), | ||
}); | ||
|
||
return [{ animate } as unknown as HTMLElement, animate] as const; | ||
} | ||
|
||
const DEFAULT_KEYFRAMES = [{ transform: 'rotate(0)' }, { transform: 'rotate(180deg)' }]; | ||
const REDUCED_MOTION_KEYFRAMES = [{ opacity: 0 }, { opacity: 1 }]; | ||
|
||
describe('useAnimateAtoms', () => { | ||
beforeEach(() => { | ||
// We set production environment to avoid testing the mock implementation | ||
process.env.NODE_ENV = 'production'; | ||
}); | ||
|
||
it('should return a function', () => { | ||
const { result } = renderHook(() => useAnimateAtoms()); | ||
|
||
expect(result.current).toBeInstanceOf(Function); | ||
}); | ||
|
||
describe('reduce motion', () => { | ||
it('calls ".animate()" with regular motion', () => { | ||
const { result } = renderHook(() => useAnimateAtoms()); | ||
|
||
const [element, animateMock] = createElementMock(); | ||
const motion: AtomMotion = { keyframes: DEFAULT_KEYFRAMES }; | ||
|
||
result.current(element, motion, { isReducedMotion: false }); | ||
|
||
expect(animateMock).toHaveBeenCalledTimes(1); | ||
expect(animateMock).toHaveBeenCalledWith(DEFAULT_KEYFRAMES, { ...DEFAULT_ANIMATION_OPTIONS }); | ||
}); | ||
|
||
it('calls ".animate()" with shortened duration (1ms) when reduced motion is enabled', () => { | ||
const { result } = renderHook(() => useAnimateAtoms()); | ||
|
||
const [element, animateMock] = createElementMock(); | ||
const motion: AtomMotion = { keyframes: DEFAULT_KEYFRAMES }; | ||
|
||
result.current(element, motion, { isReducedMotion: true }); | ||
|
||
expect(animateMock).toHaveBeenCalledTimes(1); | ||
expect(animateMock).toHaveBeenCalledWith(DEFAULT_KEYFRAMES, { ...DEFAULT_ANIMATION_OPTIONS, duration: 1 }); | ||
}); | ||
|
||
it('calls ".animate()" with specified reduced motion params when reduced motion is enabled', () => { | ||
const { result } = renderHook(() => useAnimateAtoms()); | ||
|
||
const [element, animateMock] = createElementMock(); | ||
const motion: AtomMotion = { | ||
keyframes: DEFAULT_KEYFRAMES, | ||
reducedMotion: { duration: 100 }, | ||
}; | ||
|
||
result.current(element, motion, { isReducedMotion: true }); | ||
|
||
expect(animateMock).toHaveBeenCalledTimes(1); | ||
expect(animateMock).toHaveBeenCalledWith(DEFAULT_KEYFRAMES, { ...DEFAULT_ANIMATION_OPTIONS, duration: 100 }); | ||
}); | ||
|
||
it('calls ".animate()" with specified reduced motion keyframes when reduced motion is enabled', () => { | ||
const { result } = renderHook(() => useAnimateAtoms()); | ||
|
||
const [element, animateMock] = createElementMock(); | ||
const motion: AtomMotion = { | ||
keyframes: DEFAULT_KEYFRAMES, | ||
reducedMotion: { keyframes: REDUCED_MOTION_KEYFRAMES }, | ||
}; | ||
|
||
result.current(element, motion, { isReducedMotion: true }); | ||
|
||
expect(animateMock).toHaveBeenCalledTimes(1); | ||
expect(animateMock).toHaveBeenCalledWith(REDUCED_MOTION_KEYFRAMES, { ...DEFAULT_ANIMATION_OPTIONS, duration: 1 }); | ||
}); | ||
}); | ||
}); |
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
13 changes: 12 additions & 1 deletion
13
packages/react-components/react-motion/library/src/types.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
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
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
24 changes: 24 additions & 0 deletions
24
...ies/src/CreatePresenceComponent/CreatePresenceComponentReducedMotion.stories.md
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,24 @@ | ||
By default, when [reduced motion](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-motion) is enabled the duration of the animation is set to `1ms`. `reducedMotion` allows to customize a reduced motion version of the animation: | ||
|
||
```ts | ||
const Motion = createPresenceComponent({ | ||
enter: { | ||
keyframes: [ | ||
{ opacity: 0, transform: 'scale(0)' }, | ||
{ opacity: 1, transform: 'scale(1)' }, | ||
], | ||
duration: 300, | ||
|
||
/* 💡reduced motion will not have scale animation */ | ||
reducedMotion: { | ||
keyframes: [{ opacity: 0 }, { opacity: 1 }], | ||
duration: 1000, | ||
}, | ||
}, | ||
exit: { | ||
/* ... */ | ||
}, | ||
}); | ||
``` | ||
|
||
> 💡Note, if `keyframes` are provided, they will be used instead of the regular keyframes. |
150 changes: 150 additions & 0 deletions
150
...tion/stories/src/CreatePresenceComponent/CreatePresenceComponentReducedMotion.stories.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,150 @@ | ||
import { | ||
createPresenceComponent, | ||
Field, | ||
makeStyles, | ||
mergeClasses, | ||
type MotionImperativeRef, | ||
motionTokens, | ||
Slider, | ||
Switch, | ||
tokens, | ||
} from '@fluentui/react-components'; | ||
import * as React from 'react'; | ||
|
||
import description from './CreatePresenceComponentReducedMotion.stories.md'; | ||
|
||
const useClasses = makeStyles({ | ||
container: { | ||
display: 'grid', | ||
gridTemplate: `"card card" "controls ." / 1fr 1fr`, | ||
gap: '20px 10px', | ||
}, | ||
card: { | ||
display: 'flex', | ||
flexDirection: 'column', | ||
alignItems: 'center', | ||
justifyContent: 'end', | ||
gridArea: 'card', | ||
|
||
border: `${tokens.strokeWidthThicker} solid ${tokens.colorNeutralForeground3}`, | ||
borderRadius: tokens.borderRadiusMedium, | ||
boxShadow: tokens.shadow16, | ||
padding: '10px', | ||
}, | ||
controls: { | ||
display: 'flex', | ||
flexDirection: 'column', | ||
gridArea: 'controls', | ||
|
||
border: `${tokens.strokeWidthThicker} solid ${tokens.colorNeutralForeground3}`, | ||
borderRadius: tokens.borderRadiusMedium, | ||
boxShadow: tokens.shadow16, | ||
padding: '10px', | ||
}, | ||
field: { | ||
flex: 1, | ||
}, | ||
sliderField: { | ||
gridTemplateColumns: 'min-content 1fr', | ||
}, | ||
sliderLabel: { | ||
textWrap: 'nowrap', | ||
}, | ||
|
||
item: { | ||
backgroundColor: tokens.colorBrandBackground, | ||
border: `${tokens.strokeWidthThicker} solid ${tokens.colorTransparentStroke}`, | ||
|
||
width: '100px', | ||
height: '100px', | ||
}, | ||
}); | ||
|
||
const FadeAndScale = createPresenceComponent({ | ||
enter: { | ||
keyframes: [ | ||
{ opacity: 0, transform: 'rotate(0)' }, | ||
{ transform: 'rotate(90deg) scale(1.5)' }, | ||
{ opacity: 1, transform: 'rotate(0)' }, | ||
], | ||
duration: motionTokens.durationGentle, | ||
|
||
reducedMotion: { | ||
keyframes: [{ opacity: 0 }, { opacity: 1 }], | ||
duration: motionTokens.durationUltraSlow, | ||
}, | ||
}, | ||
exit: { | ||
keyframes: [ | ||
{ opacity: 1, transform: 'rotate(0)' }, | ||
{ transform: 'rotate(-90deg) scale(1.5)' }, | ||
{ opacity: 0, transform: 'rotate(0)' }, | ||
], | ||
duration: motionTokens.durationGentle, | ||
|
||
reducedMotion: { | ||
keyframes: [{ opacity: 1 }, { opacity: 0 }], | ||
duration: motionTokens.durationUltraSlow, | ||
}, | ||
}, | ||
}); | ||
|
||
export const CreatePresenceComponentReducedMotion = () => { | ||
const classes = useClasses(); | ||
const motionRef = React.useRef<MotionImperativeRef>(); | ||
|
||
const [playbackRate, setPlaybackRate] = React.useState<number>(30); | ||
const [visible, setVisible] = React.useState<boolean>(true); | ||
|
||
// Heads up! | ||
// This is optional and is intended solely to slow down the animations, making motions more visible in the examples. | ||
React.useEffect(() => { | ||
motionRef.current?.setPlaybackRate(playbackRate / 100); | ||
}, [playbackRate, visible]); | ||
|
||
return ( | ||
<div className={classes.container}> | ||
<div className={classes.card}> | ||
<FadeAndScale imperativeRef={motionRef} visible={visible}> | ||
<div className={classes.item} /> | ||
</FadeAndScale> | ||
</div> | ||
|
||
<div className={classes.controls}> | ||
<Field className={classes.field}> | ||
<Switch label="Visible" checked={visible} onChange={() => setVisible(v => !v)} /> | ||
</Field> | ||
<Field | ||
className={mergeClasses(classes.field, classes.sliderField)} | ||
label={{ | ||
children: ( | ||
<> | ||
<code>playbackRate</code>: {playbackRate}% | ||
</> | ||
), | ||
className: classes.sliderLabel, | ||
}} | ||
orientation="horizontal" | ||
> | ||
<Slider | ||
aria-valuetext={`Value is ${playbackRate}%`} | ||
className={mergeClasses(classes.field, classes.sliderField)} | ||
value={playbackRate} | ||
onChange={(ev, data) => setPlaybackRate(data.value)} | ||
min={0} | ||
max={100} | ||
step={5} | ||
/> | ||
</Field> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
CreatePresenceComponentReducedMotion.parameters = { | ||
docs: { | ||
description: { | ||
story: description, | ||
}, | ||
}, | ||
}; |
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