@mskcc/carbon-react
Version:
Carbon react components for the MSKCC DSM
99 lines (94 loc) • 2.91 kB
JavaScript
/**
* MSKCC 2021, 2024
*/
import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import PropTypes from 'prop-types';
import React__default, { useRef } from 'react';
import cx from 'classnames';
import { usePrefix } from '../../internal/usePrefix.js';
import useIsomorphicEffect from '../../internal/useIsomorphicEffect.js';
const randoms = [0.973051493507435, 0.15334737213558558, 0.5671034553053769];
function getRandomInt(min, max, n) {
return Math.floor(randoms[n % 3] * (max - min + 1)) + min;
}
const SkeletonText = _ref => {
let {
paragraph = false,
lineCount = 3,
width = '100%',
heading = false,
className = '',
...rest
} = _ref;
const prefix = usePrefix();
const skeletonTextClasses = cx({
[`${prefix}--skeleton__text`]: true,
[`${prefix}--skeleton__heading`]: heading,
[className]: className
});
const widthNum = parseInt(width, 10);
const widthPx = width.includes('px');
const widthPercent = width.includes('%');
let lineCountNumber = 1;
if (paragraph) {
lineCountNumber = lineCount;
}
const refs = useRef([]);
useIsomorphicEffect(() => {
refs.current.map((item, j) => {
const randomPercentWidth = getRandomInt(0, 75, j) + 'px';
const randomPxWidth = getRandomInt(Math.max(widthNum - 75, 0), widthNum, j) + 'px';
if (item) {
if (widthPercent && paragraph) {
item.style.width = `calc(${width} - ${randomPercentWidth})`;
} else if (widthPx && paragraph) {
item.style.width = randomPxWidth;
} else {
item.style.width = width;
}
}
});
}, [lineCountNumber, paragraph, refs, width, widthNum, widthPercent, widthPx]);
const lines = [];
for (let i = 0; i < lineCountNumber; i++) {
lines.push( /*#__PURE__*/React__default.createElement("p", _extends({
className: skeletonTextClasses,
key: i,
ref: el => refs.current = [...refs.current, el]
}, rest)));
}
if (lineCountNumber !== 1) {
return /*#__PURE__*/React__default.createElement("div", null, lines);
}
// eslint-disable-next-line react/jsx-no-useless-fragment
return /*#__PURE__*/React__default.createElement(React__default.Fragment, null, lines);
};
SkeletonText.propTypes = {
/**
* Specify an optional className to be applied to the container node
*/
className: PropTypes.string,
/**
* generates skeleton text at a larger size
*/
heading: PropTypes.bool,
/**
* the number of lines shown if paragraph is true
*/
lineCount: PropTypes.number,
/**
* will generate multiple lines of text
*/
paragraph: PropTypes.bool,
/**
* width (in px or %) of single line of text or max-width of paragraph lines
*/
width: PropTypes.string
};
SkeletonText.defaultProps = {
paragraph: false,
width: '100%',
heading: false,
lineCount: 3
};
export { SkeletonText as default };