diff --git a/packages/react-components/react-motion-components-preview/library/src/atoms/fade-atom.ts b/packages/react-components/react-motion-components-preview/library/src/atoms/fade-atom.ts index 6b2a9d5d4f4b4a..d013ff6d06b406 100644 --- a/packages/react-components/react-motion-components-preview/library/src/atoms/fade-atom.ts +++ b/packages/react-components/react-motion-components-preview/library/src/atoms/fade-atom.ts @@ -1,7 +1,19 @@ -import { PresenceDirection, motionTokens } from '@fluentui/react-motion'; +import { AtomMotion, PresenceDirection, motionTokens } from '@fluentui/react-motion'; -export const fadeAtom = (direction: PresenceDirection, duration: number, easing: string = motionTokens.curveLinear) => { - const keyframes = [{ opacity: 0 }, { opacity: 1 }]; +interface FadeAtomParams { + direction: PresenceDirection; + duration: number; + easing?: string; + fromValue?: number; +} + +export const fadeAtom = ({ + direction, + duration, + easing = motionTokens.curveLinear, + fromValue = 0, +}: FadeAtomParams): AtomMotion => { + const keyframes = [{ opacity: fromValue }, { opacity: 1 }]; if (direction === 'exit') { keyframes.reverse(); } diff --git a/packages/react-components/react-motion-components-preview/library/src/components/Collapse/Collapse.ts b/packages/react-components/react-motion-components-preview/library/src/components/Collapse/Collapse.ts index a830927d4703bf..22c8862cc3b82f 100644 --- a/packages/react-components/react-motion-components-preview/library/src/components/Collapse/Collapse.ts +++ b/packages/react-components/react-motion-components-preview/library/src/components/Collapse/Collapse.ts @@ -26,12 +26,16 @@ export const createCollapseDelayedPresence: PresenceMotionFnCreator< // ----- ENTER ----- // The enter transition is an array of up to 3 motion atoms: size, whitespace and opacity. const enterAtoms: AtomMotion[] = [ - sizeEnterAtom(orientation, enterSizeDuration, enterEasing, element), - whitespaceAtom('enter', orientation, enterSizeDuration, enterEasing), + sizeEnterAtom({ orientation, duration: enterSizeDuration, easing: enterEasing, element }), + whitespaceAtom({ direction: 'enter', orientation, duration: enterSizeDuration, easing: enterEasing }), ]; // Fade in only if animateOpacity is true. Otherwise, leave opacity unaffected. if (animateOpacity) { - enterAtoms.push({ ...fadeAtom('enter', enterOpacityDuration, enterEasing), delay: enterDelay, fill: 'both' }); + enterAtoms.push({ + ...fadeAtom({ direction: 'enter', duration: enterOpacityDuration, easing: enterEasing }), + delay: enterDelay, + fill: 'both', + }); } // ----- EXIT ----- @@ -39,10 +43,18 @@ export const createCollapseDelayedPresence: PresenceMotionFnCreator< const exitAtoms: AtomMotion[] = []; // Fade out only if animateOpacity is true. Otherwise, leave opacity unaffected. if (animateOpacity) { - exitAtoms.push(fadeAtom('exit', exitOpacityDuration, exitEasing)); + exitAtoms.push(fadeAtom({ direction: 'exit', duration: exitOpacityDuration, easing: exitEasing })); } - exitAtoms.push(sizeExitAtom(orientation, exitSizeDuration, exitEasing, element, exitDelay)); - exitAtoms.push(whitespaceAtom('exit', orientation, exitSizeDuration, exitEasing, exitDelay)); + exitAtoms.push( + sizeExitAtom({ orientation, duration: exitSizeDuration, easing: exitEasing, element, delay: exitDelay }), + whitespaceAtom({ + direction: 'exit', + orientation, + duration: exitSizeDuration, + easing: exitEasing, + delay: exitDelay, + }), + ); return { enter: enterAtoms, diff --git a/packages/react-components/react-motion-components-preview/library/src/components/Collapse/collapse-atoms.ts b/packages/react-components/react-motion-components-preview/library/src/components/Collapse/collapse-atoms.ts index 1985a46100e8ec..6129461834cff5 100644 --- a/packages/react-components/react-motion-components-preview/library/src/components/Collapse/collapse-atoms.ts +++ b/packages/react-components/react-motion-components-preview/library/src/components/Collapse/collapse-atoms.ts @@ -1,4 +1,4 @@ -import { AtomMotion, motionTokens, PresenceDirection } from '@fluentui/react-motion'; +import { AtomMotion, PresenceDirection } from '@fluentui/react-motion'; import { CollapseOrientation } from './collapse-types'; // ----- SIZE ----- @@ -11,13 +11,21 @@ const sizeValuesForOrientation = (orientation: CollapseOrientation, element: Ele return { sizeName, overflowName, toSize }; }; -export const sizeEnterAtom = ( - orientation: CollapseOrientation, - duration: number, - easing: string, - element: HTMLElement, - fromSize: string = '0', -): AtomMotion => { +interface SizeEnterAtomParams { + orientation: CollapseOrientation; + duration: number; + easing: string; + element: HTMLElement; + fromSize?: string; +} + +export const sizeEnterAtom = ({ + orientation, + duration, + easing, + element, + fromSize = '0', +}: SizeEnterAtomParams): AtomMotion => { const { sizeName, overflowName, toSize } = sizeValuesForOrientation(orientation, element); return { @@ -31,14 +39,18 @@ export const sizeEnterAtom = ( }; }; -export const sizeExitAtom = ( - orientation: CollapseOrientation, - duration: number, - easing: string, - element: HTMLElement, - delay: number = 0, - fromSize: string = '0', -): AtomMotion => { +interface SizeExitAtomParams extends SizeEnterAtomParams { + delay?: number; +} + +export const sizeExitAtom = ({ + orientation, + duration, + easing, + element, + delay = 0, + fromSize = '0', +}: SizeExitAtomParams): AtomMotion => { const { sizeName, overflowName, toSize } = sizeValuesForOrientation(orientation, element); return { @@ -75,15 +87,21 @@ const whitespaceValuesForOrientation = (orientation: CollapseOrientation) => { }; }; -// Because a height of zero does not eliminate padding or margin, -// we will create keyframes to animate them to zero. -export const whitespaceAtom = ( - direction: PresenceDirection, - orientation: CollapseOrientation, - duration: number, - easing: string, - delay: number = 0, -): AtomMotion => { +interface WhitespaceAtomParams { + direction: PresenceDirection; + orientation: CollapseOrientation; + duration: number; + easing: string; + delay?: number; +} + +export const whitespaceAtom = ({ + direction, + orientation, + duration, + easing, + delay = 0, +}: WhitespaceAtomParams): AtomMotion => { const { paddingStart, paddingEnd, marginStart, marginEnd } = whitespaceValuesForOrientation(orientation); // The keyframe with zero whitespace is at the start for enter and at the end for exit. const offset = direction === 'enter' ? 0 : 1;