@atlaskit/motion
Version:
A set of utilities to apply motion in your application.
129 lines (124 loc) • 4.23 kB
JavaScript
/* icon-motion-base.tsx generated by @compiled/babel-plugin v3.0.4 */
import "./icon-motion-base.compiled.css";
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
import { forwardRef, useImperativeHandle, useState } from 'react';
import { cx } from '@atlaskit/css';
import { isReducedMotion } from '../utils/is-reduced-motion';
import ExitingPersistence from './exiting-persistence';
import { useMotion } from './use-motion';
const styles = {
root: "_1e0c116y _mkrz2j1u",
container: "_1e0c1n7u",
slot: "_nd5l1ekt _1e0c116y _m6k414p1",
hidden: "_3um015vq"
};
/**
* Props of `IconMotionBase` that the public `IconMotion` does not expose. Effects such as
* `IconMotion.Rotate` use them to style the root element without wrapping it in another element.
*/
/**
* What `IconMotion` exposes through its `ref`.
*/
/**
* Wraps a single icon and runs its enter or exit animation. `Icon` doesn't forward a ref, so the
* motion is applied to this element instead.
*/
const IconSlot = ({
children,
enteringAnimation,
exitingAnimation,
onFinish
}) => {
const {
ref,
state
} = useMotion({
onFinish
});
// `useMotion` completes straight away under reduced motion, but it leaves the animation to us.
let animation;
if (!isReducedMotion()) {
if (state === 'entering') {
animation = enteringAnimation;
} else if (state === 'exiting') {
animation = exitingAnimation;
}
}
return /*#__PURE__*/React.createElement("span", {
ref: ref,
// The token must be inline: `useMotion` reads `style.animation` to time the exit.
// eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop
style: {
animation
}
// An icon on its way out shouldn't be announced alongside its replacement.
,
"aria-hidden": state === 'exiting' ? true : undefined,
className: ax([styles.slot, (state === 'init' || state === 'hidden') && styles.hidden])
}, children);
};
/**
* Animates an icon when it is swapped for a different one, using the `motion.iconography` tokens.
* The icon being replaced plays `exitingAnimation`, and once it has finished the new icon plays
* `enteringAnimation`.
*/
const IconMotionBase = /*#__PURE__*/forwardRef(function IconMotion({
children,
enteringAnimation,
exitingAnimation,
shouldAppear = false,
onFinish,
testId,
overshoot,
shouldExitThenEnter = true,
xcss
}, ref) {
// Component functions can't be React keys, so a counter that increments whenever the icon
// changes is used as the key instead. `ExitingPersistence` then sees the old key disappear
// and the new key arrive. Updating state during render is React's recommended way to derive
// state from props.
const type = children ? children.type : undefined;
const key = children ? children.key : undefined;
const [tracked, setTracked] = useState({
type,
key,
count: 0
});
let count = tracked.count;
if (tracked.type !== type || tracked.key !== key) {
count = tracked.count + 1;
setTracked({
type,
key,
count
});
}
useImperativeHandle(ref, () => ({
// A new count is a new key, so the slot is swapped for a fresh copy of the same icon
reanimate: () => setTracked(previous => ({
...previous,
count: previous.count + 1
}))
}), []);
return /*#__PURE__*/React.createElement("span", {
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop, @atlaskit/ui-styling-standard/local-cx-xcss, @compiled/local-cx-xcss
className: ax([styles.root, cx(xcss)]),
// The keyframes of the `cycle` tokens and the clip in `styles.root` read this from here
style: {
'--ds-motion-slide-overshoot': overshoot
},
"data-testid": testId
}, /*#__PURE__*/React.createElement("span", {
className: ax([styles.container])
}, /*#__PURE__*/React.createElement(ExitingPersistence, {
appear: shouldAppear,
exitThenEnter: shouldExitThenEnter
}, children && /*#__PURE__*/React.createElement(IconSlot, {
key: count,
enteringAnimation: enteringAnimation,
exitingAnimation: exitingAnimation,
onFinish: onFinish
}, children))));
});
export default IconMotionBase;