Skip to content

Instantly share code, notes, and snippets.

@oieduardorabelo
Last active March 15, 2017 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oieduardorabelo/381ccc3b8b03afc95fd9b32f2c4de22d to your computer and use it in GitHub Desktop.
Save oieduardorabelo/381ccc3b8b03afc95fd9b32f2c4de22d to your computer and use it in GitHub Desktop.
// 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