Skip to content

Commit

Permalink
feat: create Stack component
Browse files Browse the repository at this point in the history
  • Loading branch information
haideralsh committed Oct 2, 2023
1 parent bcb219c commit 39e86cf
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/Stack/Stack.story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React from "react";
import { select } from "@storybook/addon-knobs";
import { Box } from "../Box";
import { Stack } from ".";
import { Divider } from "../Divider";

export default {
title: "Components/Stack",
component: Stack,
};

const widths = {
small: "191px",
medium: "351px",
large: "432px",
};

const Rectangle = ({ width }: { width: keyof typeof widths }) => (
<Box height="100px" width={widths[width]} bg="#85EB8B" />
);

export const Default = () => (
<Stack>
<Rectangle width="medium" />
<Rectangle width="large" />
<Rectangle width="small" />
</Stack>
);

export const WithAlignment = () => {
return (
<Stack alignment="right">
<Rectangle width="medium" />
<Rectangle width="large" />
<Rectangle width="small" />
</Stack>
);
};
export const WithNDSDivider = () => {
return (
<Stack alignment="right">
<Rectangle width="medium" />
<Rectangle width="large" />
<Rectangle width="small" />
</Stack>
);
};
44 changes: 44 additions & 0 deletions src/Stack/Stack.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React, { CSSProperties, ReactNode } from "react";
import { Box, Divider } from "../index";
import { Flex } from "../Flex";

type StackRef = HTMLDivElement;

type Alignment = "left" | "center" | "right";

const flexAlignment: Record<Alignment, CSSProperties["alignItems"]> = {
left: "flex-start",
center: "center",
right: "flex-end",
};

type StackProps = {
children?: React.ReactNode;
alignment?: Alignment;
space?: CSSProperties["gap"];
showDivider?: boolean;
renderDivider?: () => ReactNode;
};

export const Stack = React.forwardRef<StackRef, StackProps>(
({ children, showDivider, renderDivider = () => <Divider width="100%" />, space, alignment }, ref) => (
<Flex gap={space} flexDirection="column" alignItems={flexAlignment[alignment]} ref={ref}>
{showDivider
? React.Children.map(children, (child, index) => {
if (index > 0) {
return (
<>
{renderDivider(index)}
{child}
</>
);
}

return child;
})
: children}
</Flex>
)
);

export default Stack;
1 change: 1 addition & 0 deletions src/Stack/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Stack } from "./Stack";
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,4 @@ export { Heading1, Heading2, Heading3, Heading4, Text } from "./Type";
export type { TextProps } from "./Type";
export { useWindowDimensions } from "./utils";
export { InlineValidation } from "./Validation";
export { Stack } from "./Stack";

0 comments on commit 39e86cf

Please sign in to comment.