Created
June 29, 2018 06:11
-
-
Save searleb/8288c9d28ac64ea9a7f95b4fe5d7d10c to your computer and use it in GitHub Desktop.
MultiLineEllipsis React with CSSModules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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