Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(text): add whiteSpace support #1037

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

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

### Fixed
Expand Down
7 changes: 5 additions & 2 deletions packages/lumx-core/src/scss/components/text/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
========================================================================== */

.#{$lumx-base-prefix}-text {
white-space: var(--lumx-text-white-space);

&--is-truncated {
--lumx-text-white-space: nowrap;

display: block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}

&--is-truncated-multiline {
Expand All @@ -19,7 +22,7 @@
}

&--no-wrap {
white-space: nowrap;
--lumx-text-white-space: nowrap;
}

// Fix icon alignment
Expand Down
13 changes: 13 additions & 0 deletions packages/lumx-react/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,19 @@ export const Kind = {
} as const;
export type Kind = ValueOf<typeof Kind>;

/**
* All available white-space values
* */
export const WhiteSpace = {
normal: 'normal',
nowrap: 'nowrap',
pre: 'pre',
'pre-wrap': 'pre-wrap',
'pre-line': 'pre-line',
'break-spaces': 'break-spaces',
};
export type WhiteSpace = ValueOf<typeof WhiteSpace>;

/**
* Re-exporting some utils types.
*/
Expand Down
27 changes: 26 additions & 1 deletion packages/lumx-react/src/components/text/Text.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { textElementArgType } from '@lumx/react/stories/controls/element';
import { withUndefined } from '@lumx/react/stories/controls/withUndefined';
import { loremIpsum } from '@lumx/react/stories/utils/lorem';
import { withCombinations } from '@lumx/react/stories/decorators/withCombinations';
import { ColorPalette, ColorVariant, Icon } from '@lumx/react';
import { ColorPalette, ColorVariant, Icon, WhiteSpace } from '@lumx/react';
import { mdiEarth, mdiHeart } from '@lumx/icons';
import { withResizableBox } from '@lumx/react/stories/decorators/withResizableBox';
import { getSelectArgType } from '@lumx/react/stories/controls/selectArgType';

import { Text } from './Text';

Expand All @@ -22,6 +23,7 @@ export default {
typography: allTypographyArgType,
color: colorArgType,
colorVariant: colorVariantArgType,
whiteSpace: getSelectArgType(WhiteSpace),
},
};

Expand Down Expand Up @@ -71,6 +73,29 @@ export const NoWrap = {
},
};

/**
* Long text with line breaks
*/
export const AllWhiteSpace = {
args: {
...Default.args,
children: `
But ere she from the church-door stepped She smiled and told us why: 'It was a wicked woman's curse,' Quoth she,
'and what care I?' She smiled, and smiled, and passed it off Ere from the door she stept—
`,
},
decorators: [
withCombinations({
combinations: {
rows: { key: 'whiteSpace', options: Object.values(WhiteSpace) },
},
tableStyle: { width: 500, tableLayout: 'fixed' },
firstColStyle: { width: 100 },
cellStyle: { border: '1px solid #000', width: '100%' },
}),
],
};

/**
* Long text with single line truncate ellipsis
*/
Expand Down
6 changes: 6 additions & 0 deletions packages/lumx-react/src/components/text/Text.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ describe(`<${Text.displayName}>`, () => {
expect(element).toHaveClass('lumx-text--no-wrap');
});

it('should render with custom whiteSpace', () => {
const { element } = setup({ whiteSpace: 'pre-wrap' });
expect(element.tagName).toBe('SPAN');
expect(element).toHaveStyle({ '--lumx-text-white-space': 'pre-wrap' });
});

it('should wrap icons with spaces', () => {
const { element } = setup({ children: ['Some text', <Icon key="icon" icon={mdiEarth} />, 'with icon'] });
// Spaces have been inserted around the icon.
Expand Down
20 changes: 18 additions & 2 deletions packages/lumx-react/src/components/text/Text.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { Children, Fragment, forwardRef } from 'react';

import { Icon, ColorPalette, ColorVariant, Typography } from '@lumx/react';
import { Icon, ColorPalette, ColorVariant, Typography, WhiteSpace } from '@lumx/react';
import { Comp, GenericProps, TextElement, isComponent } from '@lumx/react/utils/type';
import {
getFontColorClassName,
Expand Down Expand Up @@ -41,6 +41,12 @@ export interface TextProps extends GenericProps {
* (automatically activated when single line text truncate is activated).
*/
noWrap?: boolean;
/**
* WhiteSpace variant
* Ignored when `noWrap` is set to true
* Ignored when `truncate` is set to true or lines: 1
* */
whiteSpace?: WhiteSpace;
}

/**
Expand Down Expand Up @@ -75,6 +81,7 @@ export const Text: Comp<TextProps> = forwardRef((props, ref) => {
noWrap,
typography,
truncate,
whiteSpace,
style,
...forwardedProps
} = props;
Expand All @@ -88,6 +95,15 @@ export const Text: Comp<TextProps> = forwardRef((props, ref) => {
const isTruncatedMultiline = !!truncateLinesStyle;
const isTruncated = !!truncate;

/**
* Add custom white-space style if specified
* Disabled if noWrap is specified
* Disabled if truncated on one-line
* */
const whiteSpaceStyle = !noWrap &&
!(isTruncated && !isTruncatedMultiline) &&
whiteSpace && { '--lumx-text-white-space': whiteSpace };

return (
<Component
ref={ref as React.Ref<any>}
Expand All @@ -102,7 +118,7 @@ export const Text: Comp<TextProps> = forwardRef((props, ref) => {
colorClass,
noWrap && `${CLASSNAME}--no-wrap`,
)}
style={{ ...truncateLinesStyle, ...style }}
style={{ ...truncateLinesStyle, ...whiteSpaceStyle, ...style }}
{...forwardedProps}
>
{children &&
Expand Down
Loading