Skip to content

Commit

Permalink
feat(Stack): Add direction prop, support horizontal stacks and switch…
Browse files Browse the repository at this point in the history
…ing between vertical and horizontal
  • Loading branch information
sapegin committed Jun 17, 2020
1 parent 3bf23b9 commit fe856ba
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
32 changes: 32 additions & 0 deletions src/components/Stack.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
Vertical stack (default):

```jsx
<Stack gap="l">
<div>eins</div>
<div>zwei</div>
<div>polizei</div>
</Stack>
```

Horizontal stack:

```jsx
<Stack gap="l" direction="row">
<div>eins</div>
<div>zwei</div>
<div>polizei</div>
</Stack>
```

Responsive gap:

```jsx
<Stack gap={['s', 'l']}>
<div>eins</div>
<div>zwei</div>
<div>polizei</div>
</Stack>
```

Responsive direction:

```jsx
<Stack gap="l" direction={['column', 'row']}>
<div>eins</div>
<div>zwei</div>
<div>polizei</div>
</Stack>
```
35 changes: 26 additions & 9 deletions src/components/Stack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
ShadowProps,
PositionProps,
} from 'styled-system';
import Box from './Box';
import Flex from './Flex';

type Direction = 'column' | 'row';

type Props = SpaceProps &
LayoutProps &
Expand All @@ -22,26 +24,41 @@ type Props = SpaceProps &
PositionProps & {
/** Spacing between items */
gap?: ResponsiveValue<TLengthStyledSystem>;
/** Direction: horizontal (`row`) or vertical (`column`) */
direction?: ResponsiveValue<Direction>;
};

export const Stack = styled(Box)<Props>(
export const Stack = styled(Flex)<Props>(
// We are using the “lobotomized owl” CSS selector to add margins between children
// More information: https://every-layout.dev/layouts/stack/#the-solution
system({
gap: {
// Here, instead of a CSS property we generate a selector
// Set the gap value to a CSS property to later use it to add margin
// at the correct side. We have to use CSS properties becase it's the
// only way to use several responsive props together
// @ts-ignore
property: '&& > * + *',
property: '--stack-gap',
scale: 'space',
// And here instead of the value for the property we return an object
// We need to add important since we set margin: 0 in our components
// and we need to override it
transform: (value, scale) => ({
marginTop: (scale as TLengthStyledSystem[])[value],
},
direction: {
// @ts-ignore
property: '&&',
transform: value => ({
flexDirection: value,
// Use lobotomized owl selector to add margin on top or left
// depending on the direction, use value from the CSS property
'> * + *': {
marginTop: value === 'column' ? 'var(--stack-gap)' : 0,
marginLeft: value === 'row' ? 'var(--stack-gap)' : 0,
},
}),
},
})
);

Stack.defaultProps = {
direction: 'column',
};

/** @component */
export default Stack;

0 comments on commit fe856ba

Please sign in to comment.