Skip to content

Instantly share code, notes, and snippets.

@mattlubner
Created May 29, 2020 01:55
Show Gist options
  • Save mattlubner/6aa7fcb84c1a7873ea4bf2da967b34a3 to your computer and use it in GitHub Desktop.
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.
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