diff --git a/src/components/Container.js b/src/components/Container.js index bfca0b7c..508ad6f8 100644 --- a/src/components/Container.js +++ b/src/components/Container.js @@ -6,7 +6,8 @@ import { responsiveMarginType, responsivePaddingType, responsiveWidthType, - responsiveHeightType + responsiveHeightType, + responsivePropType } from "../hooks/useResponsiveProp"; import useResponsivePropsCSS from "../hooks/useResponsivePropsCSS"; import { @@ -14,8 +15,10 @@ import { responsivePadding, responsiveWidth, responsiveHeight, + responsiveTextAlign, mergeResponsiveCSS } from "../utils/css"; +import { ALIGNS as TEXT_ALIGNS } from "./Text"; import tokens from "../themes/tokens"; export const BACKGROUNDS = [ @@ -40,7 +43,8 @@ function Container(_props) { margin: responsiveMargin, padding: responsivePadding, width: responsiveWidth, - height: responsiveHeight + height: responsiveHeight, + textAlign: responsiveTextAlign }); const responsiveCSS = hasBreakpointWidth ? mergeResponsiveCSS( @@ -103,6 +107,7 @@ Container.propTypes = { ...responsivePaddingType, ...responsiveWidthType, ...responsiveHeightType, + ...responsivePropType("textAlign", PropTypes.oneOf(TEXT_ALIGNS)), hasBreakpointWidth: PropTypes.bool, children: PropTypes.node }; diff --git a/src/components/Container.test.js b/src/components/Container.test.js index 89d99528..aff80e9e 100644 --- a/src/components/Container.test.js +++ b/src/components/Container.test.js @@ -53,6 +53,17 @@ describe("Container", () => { `); }); + it("with textAlign", () => { + const { getByText } = render( + Hello World + ); + const node = getByText("Hello World"); + + expect(node).toHaveStyle(` + text-align: right; + `); + }); + it("with background color", () => { const { getByText } = render( Hello World diff --git a/src/components/Text.js b/src/components/Text.js index 224c5dbc..97d4d5fc 100644 --- a/src/components/Text.js +++ b/src/components/Text.js @@ -33,7 +33,7 @@ export const COLORS = [ "conditional.positive.text", "conditional.negative.text" ]; -export const ALIGNS = ["left", "center", "right"]; +export const ALIGNS = ["inherit", "left", "center", "right"]; export const WEIGHTS = ["regular", "bold"]; export const allowedColors = [ @@ -80,7 +80,7 @@ export const DEFAULT_PROPS = { intent: "body1", weight: "regular", color: "black", - align: "left", + align: "inherit", wrap: true }; diff --git a/src/components/Text.test.js b/src/components/Text.test.js index 491e8228..add3dcdc 100644 --- a/src/components/Text.test.js +++ b/src/components/Text.test.js @@ -17,7 +17,7 @@ describe("Text", () => { line-height: 24px; letter-spacing: 0px; color: #000000; - text-align: left; + text-align: inherit; margin: 0; `); }); diff --git a/src/utils/css.js b/src/utils/css.js index e14603e5..c2237149 100644 --- a/src/utils/css.js +++ b/src/utils/css.js @@ -1,5 +1,6 @@ import tokens from "../themes/tokens"; import { hasOwnProperty } from "./core"; +import { ALIGNS as TEXT_ALIGNS } from "../components/Text"; export function getSpaceValue(space) { if (typeof space === "number") { @@ -49,6 +50,14 @@ export function getSizeValue(size) { return tokens.sizes[size] || null; } +function getTextAlignValue(textAlign) { + if (TEXT_ALIGNS.includes(textAlign)) { + return textAlign; + } + + return null; +} + const SPAN_REGEX = /^(\d+)(-(\d+))?$/; export function getGridLines(span, { allAllowed = false } = {}) { @@ -206,3 +215,11 @@ export const responsiveHeight = { return height === null ? {} : { height }; } }; + +export const responsiveTextAlign = { + getCSS: value => { + const textAlign = getTextAlignValue(value); + + return textAlign === null ? {} : { textAlign }; + } +}; diff --git a/src/utils/css.test.js b/src/utils/css.test.js index 0bce4a5d..8907a44f 100644 --- a/src/utils/css.test.js +++ b/src/utils/css.test.js @@ -5,7 +5,8 @@ import { responsiveMargin, responsivePadding, responsiveWidth, - responsiveHeight + responsiveHeight, + responsiveTextAlign } from "./css"; describe("getMinMediaQueries", () => { @@ -156,3 +157,24 @@ describe("responsiveHeight", () => { expect(responsiveHeight.getCSS("")).toStrictEqual({}); }); }); + +describe("responsiveTextAlign", () => { + it("valid textAlign", () => { + expect(responsiveTextAlign.getCSS("inherit")).toStrictEqual({ + textAlign: "inherit" + }); + expect(responsiveTextAlign.getCSS("left")).toStrictEqual({ + textAlign: "left" + }); + expect(responsiveTextAlign.getCSS("center")).toStrictEqual({ + textAlign: "center" + }); + expect(responsiveTextAlign.getCSS("right")).toStrictEqual({ + textAlign: "right" + }); + }); + + it("invalid textAlign", () => { + expect(responsiveTextAlign.getCSS("bottom")).toStrictEqual({}); + }); +}); diff --git a/website/src/pages/components/container/index.js b/website/src/pages/components/container/index.js index 5b8945f9..17542a6e 100644 --- a/website/src/pages/components/container/index.js +++ b/website/src/pages/components/container/index.js @@ -46,12 +46,13 @@ function ContainerPage() { Google{" "} Facebook - Here are some buttons:
- {" "} - + + Here are some buttons:
+ +
- Here is a nested container with margin and padding: - + Here is a nested container: + Text color is white here. Magic!