From 4ba081a096cce4f0a23fb9d6c0db0a2ca6aea283 Mon Sep 17 00:00:00 2001 From: Garrett Bear Date: Thu, 15 Dec 2022 10:49:47 -0800 Subject: [PATCH] add text directions (#16901) * add text directions * add text direction test Co-authored-by: Nidhi Kumari --- ui/components/component-library/index.js | 2 +- .../component-library/text/README.mdx | 34 +++++++++++++++---- ui/components/component-library/text/index.js | 2 +- .../component-library/text/text.constants.js | 6 ++++ ui/components/component-library/text/text.js | 9 ++++- .../component-library/text/text.stories.js | 26 ++++++++++++++ .../component-library/text/text.test.js | 15 +++++++- 7 files changed, 84 insertions(+), 10 deletions(-) diff --git a/ui/components/component-library/index.js b/ui/components/component-library/index.js index 49ee85b25..d21e4241e 100644 --- a/ui/components/component-library/index.js +++ b/ui/components/component-library/index.js @@ -18,7 +18,7 @@ export { Label } from './label'; export { PickerNetwork } from './picker-network'; export { Tag } from './tag'; export { TagUrl } from './tag-url'; -export { Text, TEXT_VARIANTS } from './text'; +export { Text, TEXT_VARIANTS, TEXT_DIRECTIONS } from './text'; export { TextField, TEXT_FIELD_TYPES, TEXT_FIELD_SIZES } from './text-field'; export { TextFieldBase, diff --git a/ui/components/component-library/text/README.mdx b/ui/components/component-library/text/README.mdx index 3b6ffb701..19241f210 100644 --- a/ui/components/component-library/text/README.mdx +++ b/ui/components/component-library/text/README.mdx @@ -66,34 +66,34 @@ import { COLORS } from '../../../helpers/constants/design-system'; text-muted - + overlay-inverse primary-default - + primary-inverse error-default - + error-inverse success-default - + success-inverse - + warning-inverse info-default - + info-inverse ``` @@ -299,6 +299,28 @@ Renders the html: ``` +### Text Direction + +Use the `textDirection` prop and the `TEXT_DIRECTIONS` object from `./text.constants.js` to change the text direction of `Text` + + + + + +```jsx +import { Text, TEXT_DIRECTIONS } from '../../ui/components/component-library'; + + + This is left to right (ltr) for English and most languages + + + This is right to left (rtl) for use with other laguanges such as Arabic. This Enlgish example is incorrect usage. + + + Let the user agent decide with the auto option + +``` + ### Box Props Use any valid box props [Box](/?path=/story/ui-components-ui-box-box-stories-js--default-story) component props to the Text component. diff --git a/ui/components/component-library/text/index.js b/ui/components/component-library/text/index.js index 606936ec7..f19894b05 100644 --- a/ui/components/component-library/text/index.js +++ b/ui/components/component-library/text/index.js @@ -1,2 +1,2 @@ export { Text } from './text'; -export { TEXT_VARIANTS } from './text.constants'; +export { TEXT_VARIANTS, TEXT_DIRECTIONS } from './text.constants'; diff --git a/ui/components/component-library/text/text.constants.js b/ui/components/component-library/text/text.constants.js index 8332cb6de..24c4f4c98 100644 --- a/ui/components/component-library/text/text.constants.js +++ b/ui/components/component-library/text/text.constants.js @@ -13,3 +13,9 @@ export const TEXT_VARIANTS = { BODY_XS: TEXT.BODY_XS, INHERIT: TEXT.INHERIT, }; + +export const TEXT_DIRECTIONS = { + LEFT_TO_RIGHT: 'ltr', + RIGHT_TO_LEFT: 'rtl', + AUTO: 'auto', +}; diff --git a/ui/components/component-library/text/text.js b/ui/components/component-library/text/text.js index ca1f5262d..6dfcdae49 100644 --- a/ui/components/component-library/text/text.js +++ b/ui/components/component-library/text/text.js @@ -11,7 +11,7 @@ import { OVERFLOW_WRAP, TEXT_COLORS, } from '../../../helpers/constants/design-system'; -import { TEXT_VARIANTS } from './text.constants'; +import { TEXT_VARIANTS, TEXT_DIRECTIONS } from './text.constants'; export const ValidTags = [ 'dd', @@ -42,6 +42,7 @@ export const Text = React.forwardRef( fontStyle, textTransform, textAlign, + textDirection, overflowWrap, ellipsis, as, @@ -89,6 +90,7 @@ export const Text = React.forwardRef( ref={ref} className={classnames(computedClassName)} as={Tag} + dir={textDirection} {...props} > {children} @@ -139,6 +141,11 @@ Text.propTypes = { * ./ui/helpers/constants/design-system.js */ textAlign: PropTypes.oneOf(Object.values(TEXT_ALIGN)), + /** + * Change the dir (direction) global attribute of text to support the direction a language is written + * Possible values: `LEFT_TO_RIGHT` (default), `RIGHT_TO_LEFT`, `AUTO` (user agent decides) + */ + textDirection: PropTypes.oneOf(Object.values(TEXT_DIRECTIONS)), /** * The overflow-wrap of the Text component. Should use the OVERFLOW_WRAP object from * ./ui/helpers/constants/design-system.js diff --git a/ui/components/component-library/text/text.stories.js b/ui/components/component-library/text/text.stories.js index c7efff1fe..55f05c7c7 100644 --- a/ui/components/component-library/text/text.stories.js +++ b/ui/components/component-library/text/text.stories.js @@ -12,10 +12,12 @@ import { OVERFLOW_WRAP, TEXT_TRANSFORM, FRACTIONS, + FLEX_DIRECTION, } from '../../../helpers/constants/design-system'; import Box from '../../ui/box'; import { ValidTags, Text } from './text'; +import { TEXT_DIRECTIONS } from './text.constants'; import README from './README.mdx'; @@ -66,6 +68,10 @@ export default { control: { type: 'select' }, options: ValidTags, }, + textDirection: { + control: { type: 'select' }, + options: Object.values(TEXT_DIRECTIONS), + }, className: { control: { type: 'text' }, }, @@ -273,3 +279,23 @@ export const As = (args) => ( })} ); + +export const TextDirection = (args) => ( + + + This is left to right (ltr) for English and most languages + + + This is right to left (rtl) for use with other laguanges such as Arabic. + This Enlgish example is incorrect usage. + + + Let the user agent decide with the auto option + + +); diff --git a/ui/components/component-library/text/text.test.js b/ui/components/component-library/text/text.test.js index 45f3831be..17b4d4499 100644 --- a/ui/components/component-library/text/text.test.js +++ b/ui/components/component-library/text/text.test.js @@ -9,7 +9,7 @@ import { TEXT_ALIGN, TEXT_TRANSFORM, } from '../../../helpers/constants/design-system'; -import { Text } from '.'; +import { Text, TEXT_DIRECTIONS } from '.'; describe('Text', () => { it('should render the Text without crashing', () => { @@ -238,4 +238,17 @@ describe('Text', () => { render(); expect(mockRef).toHaveBeenCalledTimes(1); }); + + it('should render the Text with proper direction', () => { + const { getByText } = render( + <> + auto + ltr + rtl + , + ); + expect(getByText('auto')).toHaveAttribute('dir', 'auto'); + expect(getByText('ltr')).toHaveAttribute('dir', 'ltr'); + expect(getByText('rtl')).toHaveAttribute('dir', 'rtl'); + }); });