@atlaskit/motion
Version:
A set of utilities to apply motion in your application.
140 lines (135 loc) • 5.87 kB
JavaScript
/* icon-motion-base.tsx generated by @compiled/babel-plugin v3.0.4 */
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import "./icon-motion-base.compiled.css";
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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';
var 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.
*/
var IconSlot = function IconSlot(_ref) {
var children = _ref.children,
enteringAnimation = _ref.enteringAnimation,
exitingAnimation = _ref.exitingAnimation,
onFinish = _ref.onFinish;
var _useMotion = useMotion({
onFinish: onFinish
}),
ref = _useMotion.ref,
state = _useMotion.state;
// `useMotion` completes straight away under reduced motion, but it leaves the animation to us.
var 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: 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`.
*/
var IconMotionBase = /*#__PURE__*/forwardRef(function IconMotion(_ref2, ref) {
var children = _ref2.children,
enteringAnimation = _ref2.enteringAnimation,
exitingAnimation = _ref2.exitingAnimation,
_ref2$shouldAppear = _ref2.shouldAppear,
shouldAppear = _ref2$shouldAppear === void 0 ? false : _ref2$shouldAppear,
onFinish = _ref2.onFinish,
testId = _ref2.testId,
overshoot = _ref2.overshoot,
_ref2$shouldExitThenE = _ref2.shouldExitThenEnter,
shouldExitThenEnter = _ref2$shouldExitThenE === void 0 ? true : _ref2$shouldExitThenE,
xcss = _ref2.xcss;
// 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.
var type = children ? children.type : undefined;
var key = children ? children.key : undefined;
var _useState = useState({
type: type,
key: key,
count: 0
}),
_useState2 = _slicedToArray(_useState, 2),
tracked = _useState2[0],
setTracked = _useState2[1];
var count = tracked.count;
if (tracked.type !== type || tracked.key !== key) {
count = tracked.count + 1;
setTracked({
type: type,
key: key,
count: count
});
}
useImperativeHandle(ref, function () {
return {
// A new count is a new key, so the slot is swapped for a fresh copy of the same icon
reanimate: function reanimate() {
return setTracked(function (previous) {
return _objectSpread(_objectSpread({}, 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;