textOverflow.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import styled from '@emotion/styled';
  2. type Props = {
  3. children: React.ReactNode;
  4. className?: string;
  5. ['data-test-id']?: string;
  6. /**
  7. * Change which side of the text is elided.
  8. * Default: 'right'
  9. *
  10. * BROWSER COMPAT:
  11. * When set to `left` the intention is for something like: `...xample.com/foo/`
  12. * In Chrome & Firefox this is what happens.
  13. *
  14. * In Safari (July 2022) you will see this instead: `...https://example.co`.
  15. *
  16. * See: https://stackoverflow.com/a/24800788
  17. */
  18. ellipsisDirection?: 'left' | 'right';
  19. isParagraph?: boolean;
  20. };
  21. const TextOverflow = styled(
  22. ({
  23. children,
  24. className,
  25. ellipsisDirection,
  26. isParagraph,
  27. ['data-test-id']: dataTestId,
  28. }: Props) => {
  29. const Component = isParagraph ? 'p' : 'div';
  30. if (ellipsisDirection === 'left') {
  31. return (
  32. <Component className={className} data-test-id={dataTestId}>
  33. <bdi>{children}</bdi>
  34. </Component>
  35. );
  36. }
  37. return (
  38. <Component className={className} data-test-id={dataTestId}>
  39. {children}
  40. </Component>
  41. );
  42. }
  43. )`
  44. ${p => p.theme.overflowEllipsis}
  45. ${p =>
  46. p.ellipsisDirection === 'left' &&
  47. `
  48. direction: rtl;
  49. text-align: left;
  50. `};
  51. width: auto;
  52. line-height: 1.2;
  53. `;
  54. TextOverflow.defaultProps = {
  55. ellipsisDirection: 'right',
  56. isParagraph: false,
  57. };
  58. export default TextOverflow;