Skip to content

Instantly share code, notes, and snippets.

@zebapy
Created October 28, 2020 15:12
Show Gist options
  • Select an option

  • Save zebapy/42ef54f77fc73571588f58e546cc0df8 to your computer and use it in GitHub Desktop.

Select an option

Save zebapy/42ef54f77fc73571588f58e546cc0df8 to your computer and use it in GitHub Desktop.
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