@atlaskit/motion
Version:
A set of utilities to apply motion in your application.
149 lines (143 loc) • 7.01 kB
JavaScript
/* icon-motion-base.tsx generated by @compiled/babel-plugin v3.0.4 */
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
require("./icon-motion-base.compiled.css");
var _react = _interopRequireWildcard(require("react"));
var React = _react;
var _runtime = require("@compiled/react/runtime");
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _css = require("@atlaskit/css");
var _isReducedMotion = require("../utils/is-reduced-motion");
var _exitingPersistence = _interopRequireDefault(require("./exiting-persistence"));
var _useMotion2 = require("./use-motion");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
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) { (0, _defineProperty2.default)(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; }
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 = (0, _useMotion2.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 (!(0, _isReducedMotion.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: (0, _runtime.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__*/(0, _react.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 = (0, _react.useState)({
type: type,
key: key,
count: 0
}),
_useState2 = (0, _slicedToArray2.default)(_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
});
}
(0, _react.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: (0, _runtime.ax)([styles.root, (0, _css.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: (0, _runtime.ax)([styles.container])
}, /*#__PURE__*/React.createElement(_exitingPersistence.default, {
appear: shouldAppear,
exitThenEnter: shouldExitThenEnter
}, children && /*#__PURE__*/React.createElement(IconSlot, {
key: count,
enteringAnimation: enteringAnimation,
exitingAnimation: exitingAnimation,
onFinish: onFinish
}, children))));
});
var _default = exports.default = IconMotionBase;