Skip to content

Instantly share code, notes, and snippets.

@CyrilKrylatov
Last active February 1, 2019 13:22
Show Gist options
  • Save CyrilKrylatov/2796da1126070f88710a49cc0d27a77d to your computer and use it in GitHub Desktop.
Save CyrilKrylatov/2796da1126070f88710a49cc0d27a77d to your computer and use it in GitHub Desktop.
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