diff --git a/.changeset/stale-cats-wonder.md b/.changeset/stale-cats-wonder.md new file mode 100644 index 00000000000..f21cce65192 --- /dev/null +++ b/.changeset/stale-cats-wonder.md @@ -0,0 +1,5 @@ +--- +'@primer/react': minor +--- + +Update Truncate to use CSS Modules diff --git a/packages/react/src/CircleBadge/__snapshots__/CircleBadge.test.tsx.snap b/packages/react/src/CircleBadge/__snapshots__/CircleBadge.test.tsx.snap index d443ebd75e7..e1a65fbd2eb 100644 --- a/packages/react/src/CircleBadge/__snapshots__/CircleBadge.test.tsx.snap +++ b/packages/react/src/CircleBadge/__snapshots__/CircleBadge.test.tsx.snap @@ -2,19 +2,19 @@ exports[`CircleBadge > respects the inline prop 1`] = `
`; exports[`CircleBadge > respects the variant prop 1`] = `
`; exports[`CircleBadge > uses the size prop to override the variant prop 1`] = `
`; diff --git a/packages/react/src/Truncate/Truncate.module.css b/packages/react/src/Truncate/Truncate.module.css new file mode 100644 index 00000000000..a0b2df85f87 --- /dev/null +++ b/packages/react/src/Truncate/Truncate.module.css @@ -0,0 +1,16 @@ +.Truncate { + display: inherit; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; + max-width: var(--truncate-max-width); + + &:where([data-expandable]):hover { + max-width: 10000px; + } + + &:where([data-inline]) { + display: inline-block; + vertical-align: top; + } +} diff --git a/packages/react/src/Truncate/Truncate.tsx b/packages/react/src/Truncate/Truncate.tsx index 3878a0cfea4..4ece1eaf59f 100644 --- a/packages/react/src/Truncate/Truncate.tsx +++ b/packages/react/src/Truncate/Truncate.tsx @@ -1,41 +1,48 @@ import React from 'react' -import styled from 'styled-components' +import {clsx} from 'clsx' import type {MaxWidthProps} from 'styled-system' -import {maxWidth} from 'styled-system' import type {SxProp} from '../sx' -import sx from '../sx' -import type {ComponentProps} from '../utils/types' import type {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic' +import {BoxWithFallback} from '../internal/components/BoxWithFallback' +import classes from './Truncate.module.css' -type StyledTruncateProps = { +type TruncateProps = React.HTMLAttributes & { title: string inline?: boolean expandable?: boolean } & MaxWidthProps & SxProp -const StyledTruncate = styled.div` - display: ${props => (props.inline ? 'inline-block' : 'inherit')}; - overflow: hidden; - text-overflow: ellipsis; - vertical-align: ${props => (props.inline ? 'top' : 'initial')}; - white-space: nowrap; - ${maxWidth} - ${props => (props.expandable ? `&:hover { max-width: 10000px; }` : '')} - ${sx}; -` - -export type TruncateProps = ComponentProps - const Truncate = React.forwardRef(function Truncate( - {as, expandable = false, inline = false, maxWidth = 125, ...rest}, + {as, children, className, title, inline, expandable, maxWidth = 125, style, sx, ...rest}, ref, ) { - return + return ( + + {children} + + ) }) as PolymorphicForwardRefComponent<'div', TruncateProps> if (__DEV__) { Truncate.displayName = 'Truncate' } +export type {TruncateProps} export default Truncate