Last active
March 15, 2017 10:49
-
-
Save oieduardorabelo/381ccc3b8b03afc95fd9b32f2c4de22d to your computer and use it in GitHub Desktop.
This file contains 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
// Examples | |
export class App extends Component { | |
render() { | |
return ( | |
<main> | |
<Spacer type="small"> | |
<p>Hello dude!</p> | |
</Spacer> | |
<Spacer type="medium"> | |
<div> | |
{/* more elems */} | |
</div> | |
</Spacer> | |
<Spacer type="medium"> | |
<section> | |
{/* more elems */} | |
</section> | |
</Spacer> | |
</main> | |
) | |
} | |
} | |
// Component Interface | |
<Spacer type="small">SINGLE_CHILDREN</Spacer> | |
// Component Implementation | |
function Spacer(props) { | |
const spaces = { | |
'small': '0.75em', | |
'medium': '1em' | |
} | |
const clone = React.cloneElement(props.children, { | |
style: { marginLeft: spaces[props.type] } | |
}) | |
return React.Children.only(clone) | |
} | |
// Output Example | |
// <main> | |
// <p style="margin-left: 0.75em">Hello dude!<p> | |
// <div style="margin-left: 1em"><div> | |
// <section style="margin-left: 1em"><section> | |
// </main> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment