UNPKG

ukelli-ui

Version:

[![Build Status](https://travis-ci.org/ukelli/ukelli-ui.svg?branch=master)](https://travis-ci.org/ukelli/ukelli-ui) [![install size](https://packagephobia.now.sh/badge?p=ukelli-ui)](https://packagephobia.now.sh/result?p=ukelli-ui)

199 lines (163 loc) 8.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _basicHelper = require("basic-helper"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); } function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } var hasSetKeyAnimateMapper = {}; var headDOM = document.getElementsByTagName('head')[0]; var prefixMap = { '-webkit-': 'Webkit', '-ms-': 'Ms', '-moz-': 'Moz' }; var prefix = Object.keys(prefixMap).filter(function (prefix) { return prefix + 'transform' in headDOM.style; })[0] || ''; // const prefix = '-ms-' var transform = 'transform', animation = 'animation-name'; if (prefixMap[prefix]) { transform = prefixMap[prefix] + 'Transform'; animation = prefixMap[prefix] + 'AnimationName'; } function createDynamicAnimate(options) { var startVal = options.startVal, endVal = options.endVal, animateName = options.animateName; if (hasSetKeyAnimateMapper[animateName]) return; var keyCSS = "\n @".concat(prefix, "keyframes ").concat(animateName, " {\n 0% {\n ").concat(prefix, "transform: translateY(").concat(startVal, ");\n }\n 100% {\n ").concat(prefix, "transform: translateY(").concat(endVal, ");\n }\n }\n "); var style = document.createElement('style'); style.type = 'text/css'; style.innerHTML = keyCSS; headDOM.appendChild(style); hasSetKeyAnimateMapper[animateName] = true; } var AnimateBall = /*#__PURE__*/ function (_Component) { _inherits(AnimateBall, _Component); function AnimateBall(props) { var _this; _classCallCheck(this, AnimateBall); _this = _possibleConstructorReturn(this, _getPrototypeOf(AnimateBall).call(this, props)); _this.numberRangeArr = []; return _this; } _createClass(AnimateBall, [{ key: "shouldComponentUpdate", value: function shouldComponentUpdate(nextProps, nextState) { return this.props.animating !== nextProps.animating || this.props.activeNumb !== nextProps.activeNumb; } }, { key: "componentDidMount", value: function componentDidMount() { this.genRandomArr(); } }, { key: "genRandomArr", value: function genRandomArr() { var numberRange = this.props.numberRange; this.numberRangeArr = this.shuffle((0, _basicHelper.GenerateNumberRange)(numberRange)); this.eachItemRotate = 360 / this.numberRangeArr.length; // this.eachItemHeight = 360 / this.numberRangeArr.length; } }, { key: "shuffle", value: function shuffle(arrInput) { var arr = _toConsumableArray(arrInput); for (var i = arr.length - 1; i >= 0; i--) { var randomIdx = Math.floor(Math.random() * (i + 1)); var itemAtIdx = arr[randomIdx]; arr[randomIdx] = arr[i]; arr[i] = itemAtIdx; } return arr; } }, { key: "setKeyCss", value: function setKeyCss(ballItem) { if (this.eachItemHeight) return; var numberRange = this.props.numberRange; var count = numberRange[1] - numberRange[0]; this.eachItemHeight = ballItem.offsetHeight; var totalHeight = count * this.eachItemHeight; this.animateName = 'loop-' + this.eachItemHeight + '' + totalHeight; createDynamicAnimate({ startVal: 0, endVal: "-".concat(totalHeight, "px"), animateName: this.animateName }); this.forceUpdate(); } }, { key: "render", value: function render() { var _this2 = this; var _this$props = this.props, animating = _this$props.animating, numberRange = _this$props.numberRange, activeNumb = _this$props.activeNumb; var hasActiveNumb = activeNumb != '?'; var activeIdxRotate = !animating && (+activeNumb || activeNumb == 0) ? -(this.eachItemHeight * this.numberRangeArr.indexOf(activeNumb) || 0) : 0; // const activeIdxRotate = !animating && +activeNumb ? - this.eachItemRotate * this.numberRangeArr.indexOf(activeNumb) : 0; var carouselStyle = {}; var animationStyle = {}; if (!animating) { carouselStyle[transform] = "translateY(".concat(activeIdxRotate, "px)"); } else { animationStyle[animation] = this.animateName; } return _react.default.createElement("div", { className: "ball-wrap" }, _react.default.createElement("span", { style: animationStyle, className: "animate-balls".concat(animating ? ' loop1' : '') }, _react.default.createElement("div", { style: carouselStyle, className: "carousel2" }, this.numberRangeArr.map(function (ballNumb, idx) { return _react.default.createElement("span", { key: ballNumb, ref: function ref(ballItem) { if (!_this2.eachItemHeight && !!ballItem) _this2.setKeyCss(ballItem); }, className: "item" }, hasActiveNumb ? ballNumb : '?'); // return ( // <span // key={ballNumb} // ref={ballItem => { // if(!this.eachItemHeight && !!ballItem) this.eachItemHeight = ballItem.offsetHeight; // }} // style={{ // transform: `rotateX(${this.eachItemRotate * idx}deg) translateZ(70px)` // }} // className="item"> // {ballNumb} // </span> // ) })))); } }]); return AnimateBall; }(_react.Component); exports.default = AnimateBall; AnimateBall.propTypes = { // index: PropTypes.any, animating: _propTypes.default.bool, numberRange: _propTypes.default.array };