@plteam/chat-ui
Version:
CUI Kit is a free and open-source library for creating AI assistant chat interfaces, built with React, Material UI, and TypeScript
36 lines (35 loc) • 1.26 kB
JavaScript
import * as React from 'react';
import SpanSmoothAnimation from './SpanSmoothAnimation';
const stringToSmoothComponent = (childString) => {
return childString.split(' ').map((v, i) => !v
// if `v` is empty, it means we removed a space and it needs to be restored
? ` `
: (React.createElement(SpanSmoothAnimation, { key: `${v}-${i}-span` }, `${v} `)));
};
const forEachChildren = (children) => {
const resultValue = [];
children.forEach((childValue) => {
if (typeof childValue === 'string') {
resultValue.push(...stringToSmoothComponent(childValue));
}
else if (Array.isArray(childValue)) {
resultValue.push(...forEachChildren(childValue));
}
else {
resultValue.push(childValue);
}
});
return resultValue;
};
export const useChildrenSmootherParser = () => {
return React.useCallback((children) => {
const childrenArray = [];
if (typeof children === 'string') {
childrenArray.push(children);
}
else if (Array.isArray(children)) {
childrenArray.push(...forEachChildren(children));
}
return childrenArray;
}, []);
};