Skip to content

Commit

Permalink
Adding support for text alignment
Browse files Browse the repository at this point in the history
- Added EuiTextAlign
- Implementing in the same way as EuiTextColor
  • Loading branch information
cchaos committed Apr 18, 2018
1 parent 7331b56 commit 87de5a2
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 6 deletions.
31 changes: 31 additions & 0 deletions src-docs/src/views/text/text_align.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';

import {
EuiText,
EuiTextAlign,
EuiCode,
EuiSpacer,
} from '../../../../src/components';

export default () => (
<div>
<EuiText>
<EuiTextAlign textAlign="left">
<p>Left aligned paragraph.</p>
</EuiTextAlign>
<EuiTextAlign textAlign="center">
<p>Center aligned paragraph.</p>
</EuiTextAlign>
<EuiTextAlign textAlign="right">
<p>Right aligned paragraph.</p>
</EuiTextAlign>
</EuiText>
<EuiSpacer />
<EuiText textAlign="center">
<p>You can also pass alignment to <EuiCode>EuiText</EuiCode> directly with a prop</p>
</EuiText>
<EuiText textAlign="center" color="secondary">
<p>And in conjuction with coloring.</p>
</EuiText>
</div>
);
24 changes: 24 additions & 0 deletions src-docs/src/views/text/text_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
EuiCode,
EuiText,
EuiTextColor,
EuiTextAlign,
} from '../../../../src/components';

import Text from './text';
Expand All @@ -24,6 +25,10 @@ import TextColor from './text_color';
const textColorSource = require('!!raw-loader!./text_color');
const textColorHtml = renderToHtml(TextColor);

import TextAlign from './text_align';
const textAlignSource = require('!!raw-loader!./text_align');
const textAlignHtml = renderToHtml(TextAlign);

export const TextExample = {
title: 'Text',
sections: [{
Expand Down Expand Up @@ -89,5 +94,24 @@ export const TextExample = {
),
props: { EuiTextColor },
demo: <TextColor />,
}, {
title: 'Alignment',
source: [{
type: GuideSectionTypes.JS,
code: textAlignSource,
}, {
type: GuideSectionTypes.HTML,
code: textAlignHtml,
}],
text: (
<p>
There are two ways to align text. Either individually by
applying <EuiCode>EuiTextAlign</EuiCode> on individual text objects, or
by passing the <EuiCode>textAlign</EuiCode> prop directly on <EuiCode>EuiText</EuiCode> for
a blanket approach across the entirely of your text.
</p>
),
props: { EuiTextAlign },
demo: <TextAlign />,
}],
};
1 change: 1 addition & 0 deletions src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,5 @@ export {
export {
EuiText,
EuiTextColor,
EuiTextAlign,
} from './text';
9 changes: 9 additions & 0 deletions src/components/text/__snapshots__/text_align.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`EuiTextAlign is rendered 1`] = `
<div
aria-label="aria-label"
class="euiTextAlign euiTextAlign--left testClass1 testClass2"
data-test-subj="test subject string"
/>
`;
1 change: 1 addition & 0 deletions src/components/text/_index.scss
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
@import 'text';
@import 'text_color';
@import 'text_align';
11 changes: 11 additions & 0 deletions src/components/text/_text_align.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.euiTextAlign--left {
text-align: left;
}

.euiTextAlign--right {
text-align: right;
}

.euiTextAlign--center {
text-align: center;
}
4 changes: 4 additions & 0 deletions src/components/text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ export {
export {
EuiTextColor,
} from './text_color';

export {
EuiTextAlign,
} from './text_align';
24 changes: 18 additions & 6 deletions src/components/text/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ import {
EuiTextColor,
} from './text_color';

import {
ALIGNMENTS,
EuiTextAlign,
} from './text_align';

const textSizeToClassNameMap = {
s: 'euiText--small',
xs: 'euiText--extraSmall',
};

export const TEXT_SIZES = Object.keys(textSizeToClassNameMap);

export const EuiText = ({ size, color, grow, children, className, ...rest }) => {
export const EuiText = ({ size, color, grow, textAlign, children, className, ...rest }) => {

const classes = classNames(
'euiText',
Expand All @@ -24,20 +29,26 @@ export const EuiText = ({ size, color, grow, children, className, ...rest }) =>
}
);

let optionallyColoredText;
let optionallyAlteredText;
if (color) {
optionallyColoredText = (
optionallyAlteredText = (
<EuiTextColor color={color}>
{children}
</EuiTextColor>
);
} else {
optionallyColoredText = children;
}

if (textAlign) {
optionallyAlteredText = (
<EuiTextAlign textAlign={textAlign}>
{optionallyAlteredText || children}
</EuiTextAlign>
);
}

return (
<div className={classes} {...rest}>
{optionallyColoredText}
{optionallyAlteredText || children}
</div>
);
};
Expand All @@ -47,6 +58,7 @@ EuiText.propTypes = {
className: PropTypes.string,
size: PropTypes.oneOf(TEXT_SIZES),
color: PropTypes.oneOf(COLORS),
textAlign: PropTypes.oneOf(ALIGNMENTS),
grow: PropTypes.bool,
};

Expand Down
43 changes: 43 additions & 0 deletions src/components/text/text_align.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';

export const alignmentToClassNameMap = {
'left': 'euiTextAlign--left',
'right': 'euiTextAlign--right',
'center': 'euiTextAlign--center',
};

export const ALIGNMENTS = Object.keys(alignmentToClassNameMap);

export const EuiTextAlign = ({
children,
className,
textAlign,
...rest,
}) => {
const classes = classNames(
'euiTextAlign',
alignmentToClassNameMap[textAlign],
className
);

return (
<div
className={classes}
{...rest}
>
{children}
</div>
);
};

EuiTextAlign.propTypes = {
children: PropTypes.node,
className: PropTypes.string,
textAlign: PropTypes.oneOf(ALIGNMENTS),
};

EuiTextAlign.defaultProps = {
textAlign: 'left',
};
16 changes: 16 additions & 0 deletions src/components/text/text_align.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import { render } from 'enzyme';
import { requiredProps } from '../../test';

import { EuiTextAlign } from './text_align';

describe('EuiTextAlign', () => {
test('is rendered', () => {
const component = render(
<EuiTextAlign {...requiredProps} />
);

expect(component)
.toMatchSnapshot();
});
});

0 comments on commit 87de5a2

Please sign in to comment.