Skip to content

Commit

Permalink
Remove default placeItems value from Flex and add a width prop (#151)
Browse files Browse the repository at this point in the history
* Remove default placeItems value from Flex and add a width prop

* Fix Link focus style when appearance is "primary-button" or "secondary-button"

* Add margin and width props to Stack

* Add "display: block" to svg icons

* Set vertical-align="top" on icon links
  • Loading branch information
moroshko authored Sep 14, 2020
1 parent 72c843b commit 36868d0
Show file tree
Hide file tree
Showing 62 changed files with 387 additions and 65 deletions.
7 changes: 4 additions & 3 deletions src/components/Flex.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from "prop-types";
import useResponsivePropsCSS from "../hooks/useResponsivePropsCSS";
import {
responsiveMarginType,
responsiveWidthType,
responsiveHeightType,
responsivePropType,
} from "../hooks/useResponsiveProp";
Expand All @@ -19,7 +20,6 @@ const PLACE_ITEMS = FLEX_PLACE_ITEMS;

const DEFAULT_PROPS = {
direction: "row",
placeItems: "top left",
};

Flex.DIRECTIONS = DIRECTIONS;
Expand All @@ -29,9 +29,9 @@ Flex.DEFAULT_PROPS = DEFAULT_PROPS;
function Flex(_props) {
const props = { ...DEFAULT_PROPS, ..._props };
const { children, testId } = props;
const childrenArray = React.Children.toArray(children);
const wrapperCSS = useResponsivePropsCSS(props, DEFAULT_PROPS, {
margin: responsiveMargin,
width: responsiveSize("width"),
height: responsiveSize("height"),
});
const flexCSS = useResponsivePropsCSS(props, DEFAULT_PROPS, {
Expand All @@ -55,14 +55,15 @@ function Flex(_props) {
...flexCSS,
}}
>
{childrenArray}
{children}
</div>
</div>
);
}

Flex.propTypes = {
...responsiveMarginType,
...responsiveWidthType,
...responsiveHeightType,
...responsivePropType("direction", PropTypes.oneOf(DIRECTIONS)),
...responsivePropType("placeItems", PropTypes.oneOf(PLACE_ITEMS)),
Expand Down
8 changes: 8 additions & 0 deletions src/components/Flex.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import "@testing-library/jest-dom/extend-expect";
import Flex from "./Flex";

describe("Flex", () => {
it("with width", () => {
const { container } = render(<Flex width="320">Content goes here</Flex>);

expect(container.firstChild).toHaveStyle({
width: "320px",
});
});

it("with height", () => {
const { container } = render(<Flex height="100%">Content goes here</Flex>);

Expand Down
8 changes: 3 additions & 5 deletions src/components/Footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ function HeaderSocial({ children, testId }) {
<Text margin="0" margin-sm="0 4 0 0">
<strong>Connect with us</strong>
</Text>
<Container margin="0 0 0 auto" margin-sm="0">
<Stack direction="horizontal" gap="3">
{children}
</Stack>
</Container>
<Stack direction="horizontal" gap="3" margin="0 0 0 auto" margin-sm="0">
{children}
</Stack>
</Flex>
</Container>
);
Expand Down
56 changes: 38 additions & 18 deletions src/components/Stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ import React from "react";
import PropTypes from "prop-types";
import useResponsivePropsCSS from "../hooks/useResponsivePropsCSS";
import useResponsiveProp, {
responsiveMarginType,
responsiveWidthType,
responsivePropType,
} from "../hooks/useResponsiveProp";
import {
responsiveMargin,
responsiveSize,
getGapValues,
mergeResponsiveCSS,
} from "../utils/css";
import { mergeProps } from "../utils/component";
import { getGapValues } from "../utils/css";

const DIRECTIONS = ["vertical", "horizontal"];
const ALIGNMENTS = ["left", "center", "right"];

const DEFAULT_PROPS = {
direction: "vertical",
align: "left",
gap: "0",
};

Expand All @@ -21,10 +27,20 @@ Stack.ALIGNMENTS = ALIGNMENTS;
Stack.DEFAULT_PROPS = DEFAULT_PROPS;

function Stack(props) {
const mergedProps = mergeProps(props, DEFAULT_PROPS);
const mergedProps = mergeProps(
props,
DEFAULT_PROPS,
{},
{
direction: (direction) => DIRECTIONS.includes(direction),
align: (align) => ALIGNMENTS.includes(align),
}
);
const { children, testId } = mergedProps;
const direction = useResponsiveProp(mergedProps, "direction");
const flexWrapperCSS = useResponsivePropsCSS(props, DEFAULT_PROPS, {
margin: responsiveMargin,
width: responsiveSize("width"),
gap: ({ gap }, theme) => {
const gapValues = getGapValues(gap, theme);

Expand All @@ -35,12 +51,18 @@ function Stack(props) {
const { rowGap } = gapValues;

return {
marginTop: `-${parseInt(rowGap, 10) + 1}px`,
"::before": {
marginTop: `-${parseInt(rowGap, 10) + 1}px`,
},
};
},
});
const flexCSS = useResponsivePropsCSS(props, DEFAULT_PROPS, {
align: ({ direction, align }) => {
if (!align) {
return {};
}

return {
[direction === "horizontal" ? "justifyContent" : "alignItems"]:
align === "center"
Expand Down Expand Up @@ -83,14 +105,16 @@ function Stack(props) {

return (
<div
css={{
paddingTop: "1px",
"::before": {
content: '""',
display: "block",
...flexWrapperCSS,
css={mergeResponsiveCSS(
{
paddingTop: "1px",
"::before": {
content: '""',
display: "block",
},
},
}}
flexWrapperCSS
)}
data-testid={testId}
>
<div
Expand All @@ -104,13 +128,7 @@ function Stack(props) {
{React.Children.toArray(children)
.filter((child) => child != null)
.map((child, index) => (
<div
css={{
display: "inline-flex",
...childCSS,
}}
key={index}
>
<div css={childCSS} key={index}>
{child}
</div>
))}
Expand All @@ -120,6 +138,8 @@ function Stack(props) {
}

Stack.propTypes = {
...responsiveMarginType,
...responsiveWidthType,
...responsivePropType("direction", PropTypes.oneOf(DIRECTIONS)),
...responsivePropType("align", PropTypes.oneOf(ALIGNMENTS)),
...responsivePropType(
Expand Down
26 changes: 26 additions & 0 deletions src/components/Stack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,32 @@ import { Stack, Text } from ".";
import { render } from "../utils/test";

describe("Stack", () => {
it("with margin", () => {
const { container } = render(
<Stack margin="4 8">
<Text>Hello</Text>
<Text>World</Text>
</Stack>
);

expect(container.firstChild).toHaveStyle({
margin: "16px 32px",
});
});

it("with width", () => {
const { container } = render(
<Stack width="320">
<Text>Hello</Text>
<Text>World</Text>
</Stack>
);

expect(container.firstChild).toHaveStyle({
width: "320px",
});
});

it("with testId", () => {
const { container } = render(
<Stack testId="my-stack">
Expand Down
3 changes: 1 addition & 2 deletions src/hooks/useResponsivePropsCSS.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,9 @@ describe("useResponsivePropsCSS", () => {
);

expect(result.current).toStrictEqual({
alignItems: "flex-start",
justifyContent: "flex-start",
"@media (min-width: 375px)": {
alignItems: "flex-end",
justifyContent: "flex-start",
},
"@media (min-width: 576px)": {
alignItems: "center",
Expand Down
3 changes: 3 additions & 0 deletions src/icons/arrow-back.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import PropTypes from "prop-types";
function ArrowBack({ size, primaryColor, testId }) {
return (
<svg
css={{
display: "block",
}}
width={size}
height={size}
viewBox="0 0 32 32"
Expand Down
3 changes: 3 additions & 0 deletions src/icons/arrow-forward.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import PropTypes from "prop-types";
function ArrowForward({ size, primaryColor, testId }) {
return (
<svg
css={{
display: "block",
}}
width={size}
height={size}
viewBox="0 0 32 32"
Expand Down
3 changes: 3 additions & 0 deletions src/icons/assistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import PropTypes from "prop-types";
function Assistance({ size, primaryColor, testId }) {
return (
<svg
css={{
display: "block",
}}
width={size}
height={size}
viewBox="0 0 32 32"
Expand Down
3 changes: 3 additions & 0 deletions src/icons/birthday.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import PropTypes from "prop-types";
function Birthday({ size, primaryColor, testId }) {
return (
<svg
css={{
display: "block",
}}
width={size}
height={size}
viewBox="0 0 32 32"
Expand Down
3 changes: 3 additions & 0 deletions src/icons/blocking.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import PropTypes from "prop-types";
function Blocking({ size, primaryColor, testId }) {
return (
<svg
css={{
display: "block",
}}
width={size}
height={size}
viewBox="0 0 32 32"
Expand Down
3 changes: 3 additions & 0 deletions src/icons/calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import PropTypes from "prop-types";
function Calculator({ size, primaryColor, testId }) {
return (
<svg
css={{
display: "block",
}}
width={size}
height={size}
viewBox="0 0 32 32"
Expand Down
3 changes: 3 additions & 0 deletions src/icons/chevron-down.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import PropTypes from "prop-types";
function ChevronDown({ size, primaryColor, testId }) {
return (
<svg
css={{
display: "block",
}}
width={size}
height={size}
viewBox="0 0 32 32"
Expand Down
3 changes: 3 additions & 0 deletions src/icons/chevron-left.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import PropTypes from "prop-types";
function ChevronLeft({ size, primaryColor, testId }) {
return (
<svg
css={{
display: "block",
}}
width={size}
height={size}
viewBox="0 0 32 32"
Expand Down
3 changes: 3 additions & 0 deletions src/icons/chevron-right.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import PropTypes from "prop-types";
function ChevronRight({ size, primaryColor, testId }) {
return (
<svg
css={{
display: "block",
}}
width={size}
height={size}
viewBox="0 0 32 32"
Expand Down
3 changes: 3 additions & 0 deletions src/icons/chevron-up.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import PropTypes from "prop-types";
function ChevronUp({ size, primaryColor, testId }) {
return (
<svg
css={{
display: "block",
}}
width={size}
height={size}
viewBox="0 0 32 32"
Expand Down
3 changes: 3 additions & 0 deletions src/icons/comparison.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import PropTypes from "prop-types";
function Comparison({ size, primaryColor, testId }) {
return (
<svg
css={{
display: "block",
}}
width={size}
height={size}
viewBox="0 0 32 32"
Expand Down
3 changes: 3 additions & 0 deletions src/icons/critical.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import PropTypes from "prop-types";
function Critical({ size, primaryColor, testId }) {
return (
<svg
css={{
display: "block",
}}
width={size}
height={size}
viewBox="0 0 32 32"
Expand Down
3 changes: 3 additions & 0 deletions src/icons/cross-small.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import PropTypes from "prop-types";
function CrossSmall({ size, primaryColor, testId }) {
return (
<svg
css={{
display: "block",
}}
width={size}
height={size}
viewBox="0 0 32 32"
Expand Down
3 changes: 3 additions & 0 deletions src/icons/cross.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import PropTypes from "prop-types";
function Cross({ size, primaryColor, testId }) {
return (
<svg
css={{
display: "block",
}}
width={size}
height={size}
viewBox="0 0 32 32"
Expand Down
3 changes: 3 additions & 0 deletions src/icons/devices.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import PropTypes from "prop-types";
function Devices({ size, primaryColor, testId }) {
return (
<svg
css={{
display: "block",
}}
width={size}
height={size}
viewBox="0 0 32 32"
Expand Down
3 changes: 3 additions & 0 deletions src/icons/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import PropTypes from "prop-types";
function Download({ size, primaryColor, testId }) {
return (
<svg
css={{
display: "block",
}}
width={size}
height={size}
viewBox="0 0 32 32"
Expand Down
Loading

1 comment on commit 36868d0

@vercel
Copy link

@vercel vercel bot commented on 36868d0 Sep 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.