UNPKG

react-wodry

Version:

a plugin for a text flipping/rotating build in react function component

150 lines (123 loc) 8.02 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; 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); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireWildcard(require("react")); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _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; } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } var WordFlip = function WordFlip(props) { var content = props.content, animation = props.animation, animationDuration = props.animationDuration, delay = props.delay, shift = props.shift; animationDuration = animationDuration ? animationDuration : 500; delay = delay ? delay : 2000; animation = animation ? animation : 'rotateX'; shift = shift ? shift : { x: 0, y: 0, z: 0 }; var _useState = (0, _react.useState)(0), _useState2 = _slicedToArray(_useState, 2), currentPosition = _useState2[0], setCurrentPosition = _useState2[1]; var _useState3 = (0, _react.useState)(false), _useState4 = _slicedToArray(_useState3, 2), runAnimation = _useState4[0], setRunAnimation = _useState4[1]; (0, _react.useEffect)(function () { setTimeout(function () { flipNextText(); }, delay); }, [currentPosition]); var flipNextText = function flipNextText() { setRunAnimation(true); setTimeout(function () { setRunAnimation(false); }, animationDuration); // }, (animation.slice(0,6) === 'rotate' && animation !== 'rotateAll') ? animationDuration : delay) if (currentPosition === content.length - 1) { setCurrentPosition(0); } else { setCurrentPosition(currentPosition + 1); } }; var flipping = function flipping() { var flippingStyle = {}, frontStyle = {}, backStyle = {}; if (animation.slice(0, 6) === 'rotate') { flippingStyle = { transform: "".concat(animation === 'rotateAll' ? 'rotateX(180deg) rotateY(180deg)' : animation + '(180deg)'), transition: "all ".concat(animationDuration, "ms ease 0s"), display: 'inline-block' }; frontStyle = { transform: "translate3d(".concat(shift.x, "px, ").concat(shift.y, "px, ").concat(shift.x, "px) ").concat(animation === 'rotateAll' && runAnimation ? 'rotateX(180deg) rotateY(180deg)' : ''), display: animation === 'rotateAll' ? 'none' : '' }; backStyle = { transform: "translate3d(".concat(shift.x, "px, ").concat(shift.y, "px, ").concat(shift.x, "px) ").concat(animation === 'rotateAll' && runAnimation ? 'rotateX(180deg) rotateY(180deg)' : animation + '(180deg)') }; } if (animation.slice(0, 5) === "scale") { flippingStyle = { transform: "".concat(animation === "scaleAll" ? 'scaleY(10) scaleX(10)' : animation + '(10)'), transition: "all ".concat(animationDuration, "ms ease 0s"), display: 'inline-block' }; backStyle = { transform: "translate3d(".concat(shift.x, "px, ").concat(shift.y, "px, ").concat(shift.x, "px) ").concat(animation === "scaleAll" ? 'scaleY(0.1) scaleX(0.1)' : animation + '(0.1)') }; } if (animation === "clockwise" || animation === "anticlockwise") { var animationKind = animation === "clockwise" ? 'rotate3d(40,100,80,180deg)' : 'rotate3d(100,40,-80,180deg)'; flippingStyle = { transform: animationKind, transition: "all ".concat(animationDuration, "ms ease 0s"), display: 'inline-block' }; backStyle = { transform: "translate3d(".concat(shift.x, "px, ").concat(shift.y, "px, ").concat(shift.x, "px) ").concat(animationKind) }; } var idx = currentPosition; var length = content.length - 1; var backPosition = idx; var frontPosition = idx === 0 ? length : idx - 1; if (!runAnimation && animation.slice(0, 6) === 'rotate' && animation !== 'rotateAll') { backPosition = frontPosition; frontPosition = idx; } return /*#__PURE__*/_react["default"].createElement("span", { className: "wodry-flipping", style: runAnimation ? flippingStyle : {} }, animation.slice(0, 6) === 'rotate' && /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("span", { className: "front-face", style: frontStyle }, content[frontPosition]), /*#__PURE__*/_react["default"].createElement("span", { className: "back-face", style: animation !== 'rotateAll' || runAnimation ? backStyle : {} }, content[backPosition])), animation.slice(0, 5) === "scale" && /*#__PURE__*/_react["default"].createElement("span", { className: "back-face", style: runAnimation ? backStyle : {} }, content[backPosition]), (animation === "clockwise" || animation === "anticlockwise") && /*#__PURE__*/_react["default"].createElement("span", { className: "back-face", style: runAnimation ? backStyle : {} }, content[backPosition])); }; return /*#__PURE__*/_react["default"].createElement("span", { className: "wodry" }, flipping()); }; var _default = WordFlip; exports["default"] = _default;