Created
October 28, 2020 15:12
-
-
Save zebapy/42ef54f77fc73571588f58e546cc0df8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { ReactElement, ReactNode } from "react"; | |
| import styled, { css } from "styled-components"; | |
| import { theme } from "../../constants/theme"; | |
| type StackDirection = "row" | "column" | "row-reverse" | "column-reverse"; | |
| interface StackProps { | |
| space?: number; | |
| direction?: StackDirection; | |
| children: ReactNode; | |
| } | |
| const StyledStack = styled.div<StackProps>` | |
| display: flex; | |
| flex-direction: ${(p) => p.direction}; | |
| /* Lobotomized owl selector */ | |
| /* https://css-tricks.com/lobotomized-owls/ */ | |
| & > * + * { | |
| ${(p) => | |
| p.direction === "row" || p.direction === "row-reverse" | |
| ? css` | |
| margin-left: ${theme.space(p.space)}; | |
| } | |
| ` | |
| : null} | |
| ${(p) => | |
| p.direction === "column" || p.direction === "column-reverse" | |
| ? css` | |
| margin-top: ${theme.space(p.space)}; | |
| ` | |
| : null} | |
| } | |
| `; | |
| /** | |
| * Stack component for displaying a list or row of items with space between each based on the theme space. | |
| * Very naive, doesn't handle grids of items or wrapping. | |
| * | |
| * Inspired by https://tailwindcss.com/docs/space#app | |
| * and https://polaris.shopify.com/components/structure/stack | |
| */ | |
| export function Stack({ | |
| direction = "row", | |
| space = 1, | |
| children, | |
| }: StackProps): ReactElement { | |
| return ( | |
| <StyledStack direction={direction} space={space}> | |
| {children} | |
| </StyledStack> | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment