Skip to content

Instantly share code, notes, and snippets.

@Nyalab
Last active September 16, 2020 15:52
Show Gist options
  • Select an option

  • Save Nyalab/117e8b6ec248a915978d3c31b3108ac0 to your computer and use it in GitHub Desktop.

Select an option

Save Nyalab/117e8b6ec248a915978d3c31b3108ac0 to your computer and use it in GitHub Desktop.
a quick react Stack component that works for styled-system components
import * as React from "react"
import Component from "components/component.js"
import Row from "components/row.js"
import View from "components/view.js"
// for now this component only works with StyledComponents as children because of `pb`
const Stack = props => {
const {children, space, direction = "vertical"} = props // TODO : implement divider prop
return (
<Component component={direction === "vertical" ? View : Row}>
{React.Children.map(children, (child, index) => {
if (index === React.Children.count(children) - 1) return child
return React.cloneElement(
child,
direction === "vertical" ? {pb: space} : {pr: space}
)
})}
</Component>
)
}
Stack.displayName = "Stack"
export default Stack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment