UNPKG

recharts

Version:
124 lines (121 loc) 7.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AnimatedItems = AnimatedItems; exports.useAnimationCallbacks = useAnimationCallbacks; var _react = _interopRequireWildcard(require("react")); var React = _react; var _JavascriptAnimate = require("./JavascriptAnimate"); var _useAnimationId = require("../util/useAnimationId"); var _matchBy = require("./matchBy"); var _useAnimationStartSnapshot = require("./useAnimationStartSnapshot"); 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 _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } /** * Hook that tracks animation state and provides callbacks for animation start/end. * * @param onAnimationStart optional callback to call when animation starts * @param onAnimationEnd optional callback to call when animation ends */ function useAnimationCallbacks(onAnimationStart, onAnimationEnd) { var _useState = (0, _react.useState)(false), _useState2 = _slicedToArray(_useState, 2), isAnimating = _useState2[0], setIsAnimating = _useState2[1]; var handleAnimationStart = (0, _react.useCallback)(() => { if (typeof onAnimationStart === 'function') { onAnimationStart(); } setIsAnimating(true); }, [onAnimationStart]); var handleAnimationEnd = (0, _react.useCallback)(() => { if (typeof onAnimationEnd === 'function') { onAnimationEnd(); } setIsAnimating(false); }, [onAnimationEnd]); return { isAnimating, handleAnimationStart, handleAnimationEnd }; } /** * A function that interpolates animation items at a given time. * This function receives an array of changes, and must "unwrap" them * and interpolate appropriate values and return the result which Recharts will then render. * * @see {@link https://recharts.github.io/en-US/guide/animations/ Animations guide} * * @param items The tagged animation items describing what changed, or `null` on the very first render * (entrance animation). Each item is self-describing: * - `{ status: 'matched', prev, next }` — interpolate between `prev` and `next` * - `{ status: 'added', next }` — animate in from a computed entry position * - `{ status: 'removed', prev }` — animate out to a computed exit position * * At `animationElapsedTime = 1`, removed items should be excluded from the result. * * @param animationElapsedTime A normalized time value (0 = start, 1 = end) * @param layout of the chart, useful for deciding the direction of animation * @returns The interpolated items at time animationElapsedTime * * @since 3.9 */ /** * A reusable animation wrapper for array-based chart data. * * Encapsulates the common animation pattern shared by Bar, Scatter, Funnel, Pie, * Radar, RadialBar, Area, and Line: * 1. Track previous items in a ref * 2. Wrap in JavascriptAnimate * 3. Update ref when animationElapsedTime > 0 * * @since 3.9 */ function AnimatedItems(props) { var _animationStartItems$; var animationInput = props.animationInput, animationIdPrefix = props.animationIdPrefix, items = props.items, previousItemsRef = props.previousItemsRef, isAnimationActive = props.isAnimationActive, animationBegin = props.animationBegin, animationDuration = props.animationDuration, animationEasing = props.animationEasing, onAnimationStart = props.onAnimationStart, onAnimationEnd = props.onAnimationEnd, animationInterpolateFn = props.animationInterpolateFn, animationMatchBy = props.animationMatchBy, shouldUpdatePreviousRef = props.shouldUpdatePreviousRef, children = props.children, layout = props.layout; var animationId = (0, _useAnimationId.useAnimationId)(animationInput, animationIdPrefix); var animationStartItems = (0, _useAnimationStartSnapshot.useAnimationStartSnapshot)(animationId, previousItemsRef); var rawPrevItems = (_animationStartItems$ = animationStartItems.startValue) !== null && _animationStartItems$ !== void 0 ? _animationStartItems$ : null; var animationItems = (0, _matchBy.matchAnimationItems)(rawPrevItems, items, animationMatchBy !== null && animationMatchBy !== void 0 ? animationMatchBy : _matchBy.matchByIndex); return /*#__PURE__*/React.createElement(_JavascriptAnimate.JavascriptAnimate, { animationId: animationId, begin: animationBegin, duration: animationDuration, isActive: isAnimationActive, easing: animationEasing, onAnimationEnd: onAnimationEnd, onAnimationStart: onAnimationStart, key: animationId }, animationElapsedTime => { var isEntrance = rawPrevItems == null; var stepData = items == null ? items : animationInterpolateFn(animationItems, animationElapsedTime, layout); var canUpdate = shouldUpdatePreviousRef ? shouldUpdatePreviousRef(animationElapsedTime) : animationElapsedTime > 0; animationStartItems.syncStepValue(stepData, animationElapsedTime, canUpdate); if (stepData == null) { return null; } return children(stepData, animationElapsedTime, isEntrance); }); }