Skip to content

Commit

Permalink
[@mantine/core] Text: Add truncate prop support (#3151)
Browse files Browse the repository at this point in the history
* [docs] Fix typo (#3138)

* Update getLineClamp in Text.styles.ts to support 1 liner

* Remove getLineClamp update, create truncate param to Text component

* Fix for prettier test failing

Co-authored-by: Sajarin M <68892162+Sajarin-M@users.noreply.github.com>
Co-authored-by: Paul Moss <paulmoss06@gmail.com>
  • Loading branch information
3 people committed Dec 12, 2022
1 parent 6470955 commit 038130a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/mantine-core/src/Text/Text.styles.ts
Expand Up @@ -12,6 +12,7 @@ export interface TextStylesParams {
variant: 'text' | 'link' | 'gradient';
size: MantineNumberSize;
lineClamp: number;
truncate: boolean;
inline: boolean;
inherit: boolean;
underline: boolean;
Expand Down Expand Up @@ -74,6 +75,18 @@ function getLineClamp(lineClamp: number): CSSObject {
return null;
}

function getTruncate(truncate: boolean): CSSObject {
if (truncate) {
return {
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
};
}

return null;
}

export default createStyles(
(
theme,
Expand All @@ -82,6 +95,7 @@ export default createStyles(
variant,
size,
lineClamp,
truncate,
inline,
inherit,
underline,
Expand All @@ -100,6 +114,7 @@ export default createStyles(
...theme.fn.fontStyles(),
...theme.fn.focusStyles(),
...getLineClamp(lineClamp),
...getTruncate(truncate),
color: getTextColor({ color, theme, variant }),
fontFamily: inherit ? 'inherit' : theme.fontFamily,
fontSize:
Expand Down
5 changes: 5 additions & 0 deletions src/mantine-core/src/Text/Text.tsx
Expand Up @@ -35,6 +35,9 @@ export interface TextProps extends DefaultProps {
/** CSS -webkit-line-clamp property */
lineClamp?: number;

/** CSS truncate overflowing text with an ellipsis */
truncate?: boolean;

/** Sets line-height to 1 for centering */
inline?: boolean;

Expand Down Expand Up @@ -71,6 +74,7 @@ export const _Text = forwardRef<HTMLDivElement, TextProps>((props, ref) => {
align,
variant,
lineClamp,
truncate,
gradient,
inline,
inherit,
Expand All @@ -90,6 +94,7 @@ export const _Text = forwardRef<HTMLDivElement, TextProps>((props, ref) => {
color,
size,
lineClamp,
truncate,
inline,
inherit,
underline,
Expand Down

0 comments on commit 038130a

Please sign in to comment.