Skip to content

Instantly share code, notes, and snippets.

@krambuhl
Created July 14, 2016 06:40
Show Gist options
  • Save krambuhl/a547da75f4d7fbbc5d52bcc7abd9d405 to your computer and use it in GitHub Desktop.
Save krambuhl/a547da75f4d7fbbc5d52bcc7abd9d405 to your computer and use it in GitHub Desktop.
Atomic components using ES6 template literals
const stringify = children =>
Array.isArray(children) ? children.join('') : children;
// atoms
const Card = (attrs, children) =>
`<div class="card">
${ stringify(children) }
</div>`;
const Header = (attrs, children) =>
`<div class="header">
${ stringify(children) }
</div>`;
const Textblock = (attrs, children) =>
`<p class="textblock">
${ stringify(children) }
</p>`;
const Button = (attrs, children) =>
`<a class="button" href="${attrs.href}">
${ stringify(children) }
</a>`;
// molecule
const ProductCard = (attrs, children) =>
Card(null, [
Header(null, attrs.title),
Textblock(null, stringify(children)),
Button({ href: attrs.href }, 'Read More')
]);
// organisms
const ProductCards = (products) =>
stringify(products.map(product =>
ProductCard({
title: product.name,
href: product.href
}, product.contents)
));
const ProductsGrid = (attrs, children) =>
`<section class="l-products l-products--grid">
${ ProductCards(attrs.items) }
</section>`;
// templates
// use `ProductGrid` template
document.getElementById('products').innerHTML =
ProductsGrid({
items: [
{ name: 'Alkaline', href: '#/alkaline', contents: 'Lorem ipsum dolor sherif amet, consectetur adipiscing lantern.' },
{ name: 'Aluminium-ion', href: '#/aluminium-ion', contents: 'Seadoo eiusmod tempor incididunt ut labore et dolore magma aliqua. '},
{ name: 'Lithium', href: '#/lithium', contents: 'Lorem ipsum dolor sherif amet, consectetur adipiscing lantern.' }
]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment