@razorpay/blade
Version:
The Design System that powers Razorpay
140 lines (135 loc) • 5.67 kB
JavaScript
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _typeof from '@babel/runtime/helpers/typeof';
import { visitParents, SKIP } from 'unist-util-visit-parents';
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
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; }
var SKIP_TAGS = new Set(['code', 'pre', 'svg', 'math', 'annotation']);
var isElement = function isElement(node) {
return _typeof(node) === 'object' && node !== null && 'type' in node && node.type === 'element';
};
var hasSkipAncestor = function hasSkipAncestor(ancestors) {
return ancestors.some(function (ancestor) {
return isElement(ancestor) && SKIP_TAGS.has(ancestor.tagName);
});
};
var splitByWord = function splitByWord(text) {
var parts = [];
var current = '';
var inWhitespace = false;
var _iterator = _createForOfIteratorHelper(text),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var _char = _step.value;
var isWs = /\s/.test(_char);
if (isWs !== inWhitespace && current) {
parts.push(current);
current = '';
}
current += _char;
inWhitespace = isWs;
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
if (current) {
parts.push(current);
}
return parts;
};
var splitByChar = function splitByChar(text) {
var parts = [];
var wsBuffer = '';
var _iterator2 = _createForOfIteratorHelper(text),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var _char2 = _step2.value;
if (/\s/.test(_char2)) {
wsBuffer += _char2;
} else {
if (wsBuffer) {
parts.push(wsBuffer);
wsBuffer = '';
}
parts.push(_char2);
}
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
if (wsBuffer) {
parts.push(wsBuffer);
}
return parts;
};
var makeSpan = function makeSpan(word, duration, easing) {
return {
type: 'element',
tagName: 'span',
properties: {
'data-animate-word': true,
style: "--animate-duration:".concat(duration, "ms;--animate-easing:").concat(easing)
},
children: [{
type: 'text',
value: word
}]
};
};
/**
* Creates a rehype plugin that animates text by wrapping words/characters in spans.
* Words fade in as they mount, creating a smooth text-reveal effect during AI streaming.
*
* @param options - Animation configuration options
* @returns A rehype plugin function
*/
function createRehypeAnimate(options) {
var _options$duration, _options$easing, _options$sep;
var duration = (_options$duration = options === null || options === void 0 ? void 0 : options.duration) !== null && _options$duration !== void 0 ? _options$duration : 300;
var easing = (_options$easing = options === null || options === void 0 ? void 0 : options.easing) !== null && _options$easing !== void 0 ? _options$easing : 'ease-in-out';
var sep = (_options$sep = options === null || options === void 0 ? void 0 : options.sep) !== null && _options$sep !== void 0 ? _options$sep : 'word';
return function () {
return function (tree) {
visitParents(tree, 'text', function (node, ancestors) {
var _parent$children;
var parent = ancestors[ancestors.length - 1];
if (!parent || !('children' in parent)) {
return undefined;
}
if (hasSkipAncestor(ancestors)) {
return SKIP;
}
var index = parent.children.indexOf(node);
if (index === -1) {
return undefined;
}
var text = node.value;
if (!text.trim()) {
return undefined;
}
var parts = sep === 'char' ? splitByChar(text) : splitByWord(text);
var nodes = parts.map(function (part) {
if (/^\s+$/.test(part)) {
return {
type: 'text',
value: part
};
}
return makeSpan(part, duration, easing);
});
(_parent$children = parent.children).splice.apply(_parent$children, [index, 1].concat(_toConsumableArray(nodes)));
return index + nodes.length;
});
};
};
}
// Default animate plugin with fadeIn animation
var rehypeAnimate = createRehypeAnimate();
export { createRehypeAnimate, rehypeAnimate };
//# sourceMappingURL=rehypeAnimate.js.map