Last active
February 1, 2019 13:22
-
-
Save CyrilKrylatov/2796da1126070f88710a49cc0d27a77d to your computer and use it in GitHub Desktop.
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
const srOnlyString = (string, limit, limiter) => { | |
if (string.length <= limit) { | |
return string; | |
} | |
const styles = { | |
position: 'absolute', | |
width: 1, | |
height: 1, | |
margin: -1, | |
padding: 0, | |
border: 0, | |
overflow: 'hidden', | |
clip: 'rect(0,0,0,0)', | |
} | |
const stringLimiter = limiter ? limiter : '…'; | |
const textToShow = string.substring(0, limit); | |
const textToHide = string.substring(limit, string.length); | |
return ( | |
<React.Fragment> | |
{textToShow} | |
<span aria-hidden="true">{stringLimiter}</span> | |
<span style={ styles }> | |
{textToHide} | |
</span> | |
</React.Fragment> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment