Last active
September 16, 2020 15:52
-
-
Save Nyalab/117e8b6ec248a915978d3c31b3108ac0 to your computer and use it in GitHub Desktop.
a quick react Stack component that works for styled-system components
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 * 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