UNPKG

5.3 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.MotionLinearGradient = void 0;
7exports.setLinearGradientComponent = setLinearGradientComponent;
8var _tools = require("@legendapp/tools");
9var _react = require("@legendapp/tools/react");
10var _react2 = _interopRequireWildcard(require("react"));
11var _createMotionComponent = require("./createMotionComponent");
12function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
13function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
14function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
15let LinearGradient;
16function setLinearGradientComponent(linearGradient) {
17 LinearGradient = linearGradient;
18}
19class GradientHelper extends _react2.Component {
20 render() {
21 const {
22 numColors,
23 startX,
24 startY,
25 endX,
26 endY,
27 ...rest
28 } = this.props;
29
30 // Combine startX, startY, endX, endY back into start,end
31 let start;
32 if (startX !== undefined || startY !== undefined) {
33 start = {
34 x: startX,
35 y: startY
36 };
37 }
38 let end;
39 if (endX !== undefined || endY !== undefined) {
40 end = {
41 x: endX,
42 y: endY
43 };
44 }
45
46 // Combine individual color props back into a colors array
47 const colors = [];
48 for (let i = 0; i < numColors; i++) {
49 colors.push(rest['color' + i]);
50 delete rest['color' + i];
51 }
52 return /*#__PURE__*/_react2.default.createElement(LinearGradient, _extends({
53 colors: colors,
54 start: start,
55 end: end
56 }, rest));
57 }
58}
59const AnimatedGradientHelper = (0, _createMotionComponent.createMotionAnimatedComponent)(GradientHelper);
60function pointToXY(props, point, name) {
61 if (point) {
62 props[name + 'X'] = (0, _tools.isArray)(point) ? point[0] : point.x;
63 props[name + 'Y'] = (0, _tools.isArray)(point) ? point[1] : point.y;
64 }
65}
66
67// Create MotionLinearGradient with the same API as other Motion components, but it's more complicated because it needs to
68// transform to a different set of props into the AnimatedGradientHelper.
69const MotionLinearGradient = (0, _react.MemoFnComponent)(function (props) {
70 const {
71 colors,
72 animateProps,
73 start,
74 end,
75 initialProps,
76 ...propsOut
77 } = props;
78 const {
79 colors: colorsAnimate,
80 start: startAnimate,
81 end: endAnimate,
82 ...animatePropsOut
83 } = animateProps;
84
85 // Split colors array out into individual props so they can be animated
86 colors === null || colors === void 0 ? void 0 : colors.forEach((color, i) => propsOut['color' + i] = color);
87 colorsAnimate === null || colorsAnimate === void 0 ? void 0 : colorsAnimate.forEach((color, i) => animatePropsOut['color' + i] = color);
88
89 // Split start/end objects out into individual props so they can be animated
90 pointToXY(propsOut, start, 'start');
91 pointToXY(propsOut, end, 'end');
92 pointToXY(animatePropsOut, startAnimate, 'start');
93 pointToXY(animatePropsOut, endAnimate, 'end');
94 let numColors = (colors === null || colors === void 0 ? void 0 : colors.length) || (colorsAnimate === null || colorsAnimate === void 0 ? void 0 : colorsAnimate.length) || 0;
95
96 // Split initialProps too if it exists
97 const initialPropsOut = {};
98 if (initialProps) {
99 const {
100 colors: colorsInitial,
101 start: startInitial,
102 end: endInitial
103 } = animateProps;
104 colorsInitial === null || colorsInitial === void 0 ? void 0 : colorsInitial.forEach((color, i) => initialPropsOut['color' + i] = color);
105 pointToXY(initialPropsOut, startInitial, 'start');
106 pointToXY(initialPropsOut, endInitial, 'end');
107 if (colorsInitial) {
108 numColors = colorsInitial.length;
109 }
110 }
111
112 // @ts-ignore Ignore this because it won't conform to the customized props
113 return /*#__PURE__*/_react2.default.createElement(AnimatedGradientHelper, _extends({
114 numColors: numColors
115 }, propsOut, {
116 initialProps: initialPropsOut,
117 animateProps: animatePropsOut
118 }));
119});
120exports.MotionLinearGradient = MotionLinearGradient;
121//# sourceMappingURL=LinearGradient.js.map
\No newline at end of file