Skip to content

Commit 87ebe1c

Browse files
authored
Merge pull request #1037 from lumapps/feat/add-white-space-customisation-for-text-component
feat(text): add whiteSpace support
2 parents 2ad2250 + a6150e7 commit 87ebe1c

File tree

6 files changed

+69
-5
lines changed

6 files changed

+69
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- Text: add style customisation with `whiteSpace` props
1213
- Add new design token `medium` for `font-weight`.
1314

1415
### Fixed

packages/lumx-core/src/scss/components/text/_index.scss

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
========================================================================== */
44

55
.#{$lumx-base-prefix}-text {
6+
white-space: var(--lumx-text-white-space);
7+
68
&--is-truncated {
9+
--lumx-text-white-space: nowrap;
10+
711
display: block;
812
overflow: hidden;
913
text-overflow: ellipsis;
10-
white-space: nowrap;
1114
}
1215

1316
&--is-truncated-multiline {
@@ -19,7 +22,7 @@
1922
}
2023

2124
&--no-wrap {
22-
white-space: nowrap;
25+
--lumx-text-white-space: nowrap;
2326
}
2427

2528
// Fix icon alignment

packages/lumx-react/src/components/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,19 @@ export const Kind = {
172172
} as const;
173173
export type Kind = ValueOf<typeof Kind>;
174174

175+
/**
176+
* All available white-space values
177+
* */
178+
export const WhiteSpace = {
179+
normal: 'normal',
180+
nowrap: 'nowrap',
181+
pre: 'pre',
182+
'pre-wrap': 'pre-wrap',
183+
'pre-line': 'pre-line',
184+
'break-spaces': 'break-spaces',
185+
};
186+
export type WhiteSpace = ValueOf<typeof WhiteSpace>;
187+
175188
/**
176189
* Re-exporting some utils types.
177190
*/

packages/lumx-react/src/components/text/Text.stories.tsx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import { textElementArgType } from '@lumx/react/stories/controls/element';
66
import { withUndefined } from '@lumx/react/stories/controls/withUndefined';
77
import { loremIpsum } from '@lumx/react/stories/utils/lorem';
88
import { withCombinations } from '@lumx/react/stories/decorators/withCombinations';
9-
import { ColorPalette, ColorVariant, Icon } from '@lumx/react';
9+
import { ColorPalette, ColorVariant, Icon, WhiteSpace } from '@lumx/react';
1010
import { mdiEarth, mdiHeart } from '@lumx/icons';
1111
import { withResizableBox } from '@lumx/react/stories/decorators/withResizableBox';
12+
import { getSelectArgType } from '@lumx/react/stories/controls/selectArgType';
1213

1314
import { Text } from './Text';
1415

@@ -22,6 +23,7 @@ export default {
2223
typography: allTypographyArgType,
2324
color: colorArgType,
2425
colorVariant: colorVariantArgType,
26+
whiteSpace: getSelectArgType(WhiteSpace),
2527
},
2628
};
2729

@@ -71,6 +73,29 @@ export const NoWrap = {
7173
},
7274
};
7375

76+
/**
77+
* Long text with line breaks
78+
*/
79+
export const AllWhiteSpace = {
80+
args: {
81+
...Default.args,
82+
children: `
83+
But ere she from the church-door stepped She smiled and told us why: 'It was a wicked woman's curse,' Quoth she,
84+
'and what care I?' She smiled, and smiled, and passed it off Ere from the door she stept—
85+
`,
86+
},
87+
decorators: [
88+
withCombinations({
89+
combinations: {
90+
rows: { key: 'whiteSpace', options: Object.values(WhiteSpace) },
91+
},
92+
tableStyle: { width: 500, tableLayout: 'fixed' },
93+
firstColStyle: { width: 100 },
94+
cellStyle: { border: '1px solid #000', width: '100%' },
95+
}),
96+
],
97+
};
98+
7499
/**
75100
* Long text with single line truncate ellipsis
76101
*/

packages/lumx-react/src/components/text/Text.test.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ describe(`<${Text.displayName}>`, () => {
6161
expect(element).toHaveClass('lumx-text--no-wrap');
6262
});
6363

64+
it('should render with custom whiteSpace', () => {
65+
const { element } = setup({ whiteSpace: 'pre-wrap' });
66+
expect(element.tagName).toBe('SPAN');
67+
expect(element).toHaveStyle({ '--lumx-text-white-space': 'pre-wrap' });
68+
});
69+
6470
it('should wrap icons with spaces', () => {
6571
const { element } = setup({ children: ['Some text', <Icon key="icon" icon={mdiEarth} />, 'with icon'] });
6672
// Spaces have been inserted around the icon.

packages/lumx-react/src/components/text/Text.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { Children, Fragment, forwardRef } from 'react';
22

3-
import { Icon, ColorPalette, ColorVariant, Typography } from '@lumx/react';
3+
import { Icon, ColorPalette, ColorVariant, Typography, WhiteSpace } from '@lumx/react';
44
import { Comp, GenericProps, TextElement, isComponent } from '@lumx/react/utils/type';
55
import {
66
getFontColorClassName,
@@ -41,6 +41,12 @@ export interface TextProps extends GenericProps {
4141
* (automatically activated when single line text truncate is activated).
4242
*/
4343
noWrap?: boolean;
44+
/**
45+
* WhiteSpace variant
46+
* Ignored when `noWrap` is set to true
47+
* Ignored when `truncate` is set to true or lines: 1
48+
* */
49+
whiteSpace?: WhiteSpace;
4450
}
4551

4652
/**
@@ -75,6 +81,7 @@ export const Text: Comp<TextProps> = forwardRef((props, ref) => {
7581
noWrap,
7682
typography,
7783
truncate,
84+
whiteSpace,
7885
style,
7986
...forwardedProps
8087
} = props;
@@ -88,6 +95,15 @@ export const Text: Comp<TextProps> = forwardRef((props, ref) => {
8895
const isTruncatedMultiline = !!truncateLinesStyle;
8996
const isTruncated = !!truncate;
9097

98+
/**
99+
* Add custom white-space style if specified
100+
* Disabled if noWrap is specified
101+
* Disabled if truncated on one-line
102+
* */
103+
const whiteSpaceStyle = !noWrap &&
104+
!(isTruncated && !isTruncatedMultiline) &&
105+
whiteSpace && { '--lumx-text-white-space': whiteSpace };
106+
91107
return (
92108
<Component
93109
ref={ref as React.Ref<any>}
@@ -102,7 +118,7 @@ export const Text: Comp<TextProps> = forwardRef((props, ref) => {
102118
colorClass,
103119
noWrap && `${CLASSNAME}--no-wrap`,
104120
)}
105-
style={{ ...truncateLinesStyle, ...style }}
121+
style={{ ...truncateLinesStyle, ...whiteSpaceStyle, ...style }}
106122
{...forwardedProps}
107123
>
108124
{children &&

0 commit comments

Comments
 (0)