Skip to content

Instantly share code, notes, and snippets.

@searleb
Created June 29, 2018 06:11
Show Gist options
  • Save searleb/8288c9d28ac64ea9a7f95b4fe5d7d10c to your computer and use it in GitHub Desktop.
Save searleb/8288c9d28ac64ea9a7f95b4fe5d7d10c to your computer and use it in GitHub Desktop.
MultiLineEllipsis React with CSSModules
import React from 'react';
import PropTypes from 'prop-types';
import styles from './styles.css';
function getStyles(fontSize, lineHeight, linesToShow) {
return {
fontSize,
lineHeight,
height: fontSize * lineHeight * linesToShow,
WebkitLineClamp: linesToShow,
};
}
function MultiLineEllipsis({ text, fontSize, lineHeight, linesToShow }) {
return (
<span
className={styles.clamp}
style={getStyles(fontSize, lineHeight, linesToShow)}
>
{text}
</span>
);
}
MultiLineEllipsis.propTypes = {
text: PropTypes.node.isRequired,
fontSize: PropTypes.number,
lineHeight: PropTypes.number,
linesToShow: PropTypes.number,
};
MultiLineEllipsis.defaultProps = {
fontSize: 14,
lineHeight: 1.2,
linesToShow: 3,
};
export default MultiLineEllipsis;
.clamp {
--font-size: 14px;
--line-height: 1.2;
--lines-to-show: 3;
display: block; /* Fallback for non-webkit */
display: -webkit-box;
max-width: 100%;
color: inherit;
height: calc(var(--font-size) * var(--line-height) * var(--lines-to-show)); /* Fallback for non-webkit */
margin: 0;
padding: 0;
font-size: var(--font-size);
line-height: var(--line-height);
-webkit-line-clamp: var(--lines-to-show);
-webkit-box-orient: vertical;
overflow: hidden;
text-overflow: ellipsis;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment