Skip to content

Commit 904d050

Browse files
author
Kubit
committed
Add truncate text functionalitie
1 parent cca9840 commit 904d050

File tree

13 files changed

+78
-20
lines changed

13 files changed

+78
-20
lines changed

src/components/text/stories/argtypes.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,30 @@ export const argtypes = (
171171
category: CATEGORY_CONTROL.MODIFIERS,
172172
},
173173
},
174+
truncate: {
175+
description:
176+
'Boolean to indicate if render an ellipsis ("...") to represent the clipped text',
177+
type: { name: 'boolean' },
178+
control: { type: 'boolean' },
179+
table: {
180+
type: {
181+
summary: 'boolean',
182+
},
183+
category: CATEGORY_CONTROL.MODIFIERS,
184+
},
185+
},
186+
maxTruncatedLines: {
187+
description:
188+
'Boolean to indicate if render an ellipsis ("...") to represent the clipped text with a max number of lines (`truncate` is not needed)',
189+
type: { name: 'number' },
190+
control: { type: 'number' },
191+
table: {
192+
type: {
193+
summary: 'number',
194+
},
195+
category: CATEGORY_CONTROL.MODIFIERS,
196+
},
197+
},
174198
aria: {
175199
description: 'Prop used for accesibility',
176200
type: { name: 'Partial<React.AriaAttributes>' },

src/components/text/text.styled.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import styled, { CSSProp, css } from 'styled-components';
22

3+
import { maxTruncatedLinesMixin, truncateMixin } from '@/styles/mixins/truncate.mixin';
34
import { getTypographyStyles } from '@/utils/getStyles/getStyles';
45

56
import { ITextStyled } from './types/text';
@@ -18,6 +19,15 @@ const applyDevicePropsTextStyles = (props: TextPropsStylesType) => css`
1819
css`
1920
pointer-events: none;
2021
`}
22+
${!props.$maxTruncatedLines &&
23+
props.$truncate &&
24+
css`
25+
${truncateMixin}
26+
`}
27+
${props.$maxTruncatedLines &&
28+
css`
29+
${maxTruncatedLinesMixin(props.$maxTruncatedLines)}
30+
`}
2131
`;
2232

2333
const applyPropsTextStyles = (props: TextPropsStylesType) => css`

src/components/text/textStandAlone.tsx

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,8 @@ import { TextTransformType } from './types/transform';
88

99
const TextStandAloneComponent = (
1010
{
11-
children,
12-
component,
1311
dataTestId = 'text',
14-
htmlFor,
15-
id,
16-
onClick,
17-
role,
18-
styles,
19-
weight,
20-
isDisabled,
21-
align,
12+
2213
transform = TextTransformType.NONE,
2314
...props
2415
}: ITextStandAlone,
@@ -30,20 +21,22 @@ const TextStandAloneComponent = (
3021
{...props}
3122
{...ariaProps}
3223
ref={ref}
24+
$maxTruncatedLines={props.maxTruncatedLines}
3325
$transform={transform}
34-
align={align}
35-
as={component}
26+
$truncate={props.truncate}
27+
align={props.align}
28+
as={props.component}
3629
content={undefined}
3730
data-testid={dataTestId}
38-
htmlFor={htmlFor}
39-
id={id}
40-
isDisabled={isDisabled}
41-
role={role}
42-
styles={styles}
43-
weight={weight}
44-
onClick={onClick}
31+
htmlFor={props.htmlFor}
32+
id={props.id}
33+
isDisabled={props.isDisabled}
34+
role={props.role}
35+
styles={props.styles}
36+
weight={props.weight}
37+
onClick={props.onClick}
4538
>
46-
{children}
39+
{props.children}
4740
</TextStyled>
4841
);
4942
};

src/components/text/types/text.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export interface ITextStyled {
1717
align?: string;
1818
$transform?: TextTransformType;
1919
customTypography?: TypographyTypes;
20+
$maxTruncatedLines?: number;
21+
$truncate?: boolean;
2022
}
2123

2224
type TextAriaAttributes = Pick<
@@ -39,6 +41,8 @@ export interface ITextStandAlone extends ITextStyled, TextAriaAttributes {
3941
cursor?: string;
4042
weight?: number;
4143
target?: string;
44+
truncate?: boolean;
45+
maxTruncatedLines?: number;
4246
// TODO: is that correct??
4347
align?: string;
4448
transform?: TextTransformType;

src/components/text/types/textTheme.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export type TextPropsStylesType = {
4242
align?: string;
4343
weight?: number;
4444
isDisabled?: boolean;
45+
$maxTruncatedLines?: number;
46+
$truncate?: boolean;
4547
};
4648

4749
/**

src/designSystem/kubit/components/styles.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export * from './tableCaption/styles';
9090
export * from './tableDivider/styles';
9191
export * from './tableV2/styles';
9292
export * from './dataTable/styles';
93+
9394
export * from './video/styles';
9495
export * from './virtualKeyboard/styles';
9596
export * from './progressBar/styles';

src/styles/mixins/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './input.mixin';
33
export * from './input.mixin.utils';
44
export * from './overflow.mixin';
55
export * from './srOnly.mixin';
6+
export * from './truncate.mixin';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { CSSProp, css } from 'styled-components';
2+
3+
export const truncateMixin = css`
4+
text-overflow: ellipsis;
5+
overflow: hidden;
6+
white-space: nowrap;
7+
`;
8+
9+
export const maxTruncatedLinesMixin = (maxLines: number): CSSProp => css`
10+
display: -webkit-box;
11+
-webkit-line-clamp: ${maxLines};
12+
-webkit-box-orient: vertical;
13+
overflow: hidden;
14+
`;

src/types/styled.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ export interface CustomTheme {
2121
}
2222

2323
declare module 'styled-components' {
24+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
2425
export interface DefaultTheme extends CustomTheme {}
2526
}

src/types/styles/typography.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ type GenericDeviceTypographyType = {
2424
color?: string;
2525
overflow?: string;
2626
filter?: string;
27+
webkit_line_clamp?: string;
28+
webkit_box_orient?: string;
2729
} & TypographyCustomType &
2830
WordWrapTypes;
2931

0 commit comments

Comments
 (0)