UNPKG

react-native-typewriter-effect

Version:
59 lines (58 loc) 3.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = TypeWriterEffect; var _react = _interopRequireWildcard(require("react")); var _reactNative = require("react-native"); function _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); } 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; } const DEFAULT_MAX_DELAY = 100; const DEFAULT_MIN_DELAY = 10; function TypeWriterEffect(props) { const { content, onTyped, onTypingEnd, minDelay = DEFAULT_MIN_DELAY, maxDelay = DEFAULT_MAX_DELAY, vibration = true, backspaceEffect = false } = props; const initialIndex = backspaceEffect ? content.length : 0; const [currentCharIndex, setCurrentCharIndex] = (0, _react.useState)(initialIndex); const timeoutId = (0, _react.useRef)(null); const delta = backspaceEffect ? -1 : 1; const startTypeWriter = (0, _react.useCallback)(ms => { timeoutId.current = setTimeout(() => { setCurrentCharIndex(currentCharIndex + delta); if (vibration && _reactNative.Platform.OS === 'android') { _reactNative.Vibration.vibrate(1); } }, ms); }, [currentCharIndex, vibration, delta]); const clear = () => { if (timeoutId.current) { clearTimeout(timeoutId.current); timeoutId.current = null; } }; (0, _react.useEffect)(() => { setCurrentCharIndex(initialIndex); }, [content, initialIndex]); (0, _react.useEffect)(() => { clear(); const currentChar = content.charAt(currentCharIndex); const nextChar = content.charAt(currentCharIndex + delta); if (currentChar) { onTyped === null || onTyped === void 0 ? void 0 : onTyped(currentChar, currentCharIndex); } if (!nextChar) { onTypingEnd === null || onTypingEnd === void 0 ? void 0 : onTypingEnd(); return; } startTypeWriter(Math.round(Math.random() * (maxDelay - minDelay) + minDelay)); }, [onTyped, startTypeWriter, onTypingEnd, currentCharIndex, content, maxDelay, minDelay, delta]); return /*#__PURE__*/_react.default.createElement(_reactNative.Text, props, content.substring(0, backspaceEffect ? currentCharIndex : currentCharIndex + 1)); } //# sourceMappingURL=index.js.map