|
1 | 1 | import React from 'react' |
2 | | -import styled from 'styled-components' |
| 2 | +import {clsx} from 'clsx' |
3 | 3 | import type {MaxWidthProps} from 'styled-system' |
4 | | -import {maxWidth} from 'styled-system' |
5 | 4 | import type {SxProp} from '../sx' |
6 | | -import sx from '../sx' |
7 | | -import type {ComponentProps} from '../utils/types' |
8 | 5 | import type {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic' |
| 6 | +import {BoxWithFallback} from '../internal/components/BoxWithFallback' |
| 7 | +import classes from './Truncate.module.css' |
9 | 8 |
|
10 | | -type StyledTruncateProps = { |
| 9 | +type TruncateProps = React.HTMLAttributes<HTMLElement> & { |
11 | 10 | title: string |
12 | 11 | inline?: boolean |
13 | 12 | expandable?: boolean |
14 | 13 | } & MaxWidthProps & |
15 | 14 | SxProp |
16 | 15 |
|
17 | | -const StyledTruncate = styled.div<StyledTruncateProps>` |
18 | | - display: ${props => (props.inline ? 'inline-block' : 'inherit')}; |
19 | | - overflow: hidden; |
20 | | - text-overflow: ellipsis; |
21 | | - vertical-align: ${props => (props.inline ? 'top' : 'initial')}; |
22 | | - white-space: nowrap; |
23 | | - ${maxWidth} |
24 | | - ${props => (props.expandable ? `&:hover { max-width: 10000px; }` : '')} |
25 | | - ${sx}; |
26 | | -` |
27 | | - |
28 | | -export type TruncateProps = ComponentProps<typeof StyledTruncate> |
29 | | - |
30 | 16 | const Truncate = React.forwardRef(function Truncate( |
31 | | - {as, expandable = false, inline = false, maxWidth = 125, ...rest}, |
| 17 | + {as, children, className, title, inline, expandable, maxWidth = 125, style, sx, ...rest}, |
32 | 18 | ref, |
33 | 19 | ) { |
34 | | - return <StyledTruncate ref={ref} as={as} expandable={expandable} inline={inline} maxWidth={maxWidth} {...rest} /> |
| 20 | + return ( |
| 21 | + <BoxWithFallback |
| 22 | + {...rest} |
| 23 | + ref={ref} |
| 24 | + as={as} |
| 25 | + className={clsx(className, classes.Truncate)} |
| 26 | + data-expandable={expandable} |
| 27 | + data-inline={inline} |
| 28 | + title={title} |
| 29 | + style={ |
| 30 | + { |
| 31 | + ...style, |
| 32 | + [`--truncate-max-width`]: |
| 33 | + typeof maxWidth === 'number' ? `${maxWidth}px` : typeof maxWidth === 'string' ? maxWidth : undefined, |
| 34 | + } as React.CSSProperties |
| 35 | + } |
| 36 | + sx={sx} |
| 37 | + > |
| 38 | + {children} |
| 39 | + </BoxWithFallback> |
| 40 | + ) |
35 | 41 | }) as PolymorphicForwardRefComponent<'div', TruncateProps> |
36 | 42 |
|
37 | 43 | if (__DEV__) { |
38 | 44 | Truncate.displayName = 'Truncate' |
39 | 45 | } |
40 | 46 |
|
| 47 | +export type {TruncateProps} |
41 | 48 | export default Truncate |
0 commit comments