Skip to content

Commit 2fe33bf

Browse files
authored
refactor(Truncate): update Truncate to CSS Modules (#6257)
1 parent 5b51de7 commit 2fe33bf

File tree

4 files changed

+51
-23
lines changed

4 files changed

+51
-23
lines changed

.changeset/stale-cats-wonder.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/react': minor
3+
---
4+
5+
Update Truncate to use CSS Modules

packages/react/src/CircleBadge/__snapshots__/CircleBadge.test.tsx.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
exports[`CircleBadge > respects the inline prop 1`] = `
44
<div
5-
class="sc-feUZmu bYFNcn"
5+
class="sc-dAbbOL onCTt"
66
/>
77
`;
88

99
exports[`CircleBadge > respects the variant prop 1`] = `
1010
<div
11-
class="sc-feUZmu lbGpjD"
11+
class="sc-dAbbOL gIOBDF"
1212
/>
1313
`;
1414

1515
exports[`CircleBadge > uses the size prop to override the variant prop 1`] = `
1616
<div
17-
class="sc-feUZmu jmFkGb"
17+
class="sc-dAbbOL EOzpx"
1818
size="20"
1919
/>
2020
`;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.Truncate {
2+
display: inherit;
3+
overflow: hidden;
4+
text-overflow: ellipsis;
5+
white-space: nowrap;
6+
max-width: var(--truncate-max-width);
7+
8+
&:where([data-expandable]):hover {
9+
max-width: 10000px;
10+
}
11+
12+
&:where([data-inline]) {
13+
display: inline-block;
14+
vertical-align: top;
15+
}
16+
}
Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,48 @@
11
import React from 'react'
2-
import styled from 'styled-components'
2+
import {clsx} from 'clsx'
33
import type {MaxWidthProps} from 'styled-system'
4-
import {maxWidth} from 'styled-system'
54
import type {SxProp} from '../sx'
6-
import sx from '../sx'
7-
import type {ComponentProps} from '../utils/types'
85
import type {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic'
6+
import {BoxWithFallback} from '../internal/components/BoxWithFallback'
7+
import classes from './Truncate.module.css'
98

10-
type StyledTruncateProps = {
9+
type TruncateProps = React.HTMLAttributes<HTMLElement> & {
1110
title: string
1211
inline?: boolean
1312
expandable?: boolean
1413
} & MaxWidthProps &
1514
SxProp
1615

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-
3016
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},
3218
ref,
3319
) {
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+
)
3541
}) as PolymorphicForwardRefComponent<'div', TruncateProps>
3642

3743
if (__DEV__) {
3844
Truncate.displayName = 'Truncate'
3945
}
4046

47+
export type {TruncateProps}
4148
export default Truncate

0 commit comments

Comments
 (0)