Created
May 29, 2020 01:55
-
-
Save mattlubner/6aa7fcb84c1a7873ea4bf2da967b34a3 to your computer and use it in GitHub Desktop.
Truncate a line of text where it breaks naturally, so table column widths don't explode.
This file contains hidden or 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 styled from 'styled-components'; | |
const Relative = styled.div` | |
position: relative; | |
`; | |
const TextWidth = styled.div` | |
opacity: 0; | |
text-overflow: clip; | |
height: 1px; | |
`; | |
const TextHeight = styled.div` | |
opacity: 0; | |
text-overflow: clip; | |
white-space: nowrap; | |
width: 1px; | |
`; | |
const Text = styled.div` | |
position: absolute; | |
top: 0; | |
bottom: 0; | |
left: 0; | |
right: 0; | |
overflow: hidden; | |
text-overflow: ellipsis; | |
white-space: nowrap; | |
`; | |
export const TruncateTextAtBreak = props => ( | |
<Relative> | |
<TextWidth {...props} /> | |
<TextHeight {...props} /> | |
<Text {...props} /> | |
</Relative> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment