A journey to writing vanilla web components

If you come from the React world, slots are an approximate equivalent of render props, although they are not functions 😅

Today, we’ll write a <page-layout /> web component

  • can have an aside part, piloted by an attribute
  • must have in its layout: a header, a footer, a main part, and optionally an aside part
Where to start…? Ah yes.

Step 1/4 − The skeleton

// page-layout.jsexport const PAGE_LAYOUT_TAGNAME = 'page-layout';const template = document.createElement('template);
template.innerHTML = `

export class HTMLPageLayoutElement extends HTMLElement {
static get observedAttributes () {
return [];
// I don't need to observe the aside attribute since the display
// is piloted by the CSS
get aside () {…}
set aside (value) {…}
constructor () {…}
attributeChangedCallback (name, previousValue, nextValue) {…}

Step 2/4 − The template

// custom-chip.js

template.innerHTML = `
<div class="page">
<div class="header">
<slot name="header"></slot>
<div class="content">
<div class="aside">
<slot name="aside"></slot>
<div class="main">
<slot name="main"></slot>
<div class="footer">
<slot name="footer"></slot>
Me, inserting those reluctant HTML tags

Step 3/4 − Add the layout style

// custom-chip.js

template.innerHTML = `

:host {
--header-h: 2em;
display: block;
.page {
min-height: 100vh;
.header {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: var(--header-h);
border-bottom: 1px solid #888;
.content {
margin: auto;
padding: 1em 0;
margin-top: var(--header-h);
display: flex;
justify-content: space-around;
max-width: 40em;
min-height: 100vh;
.aside {
flex: 1;
position: sticky;
top: calc(var(--header-h) + .25em);
align-self: start;
display: none;
/* hides the aside if there's no "aside" attribute on the
host node */
:host([aside]) .aside {
display: block;
.main {
flex: 3;
Me, adding those CSS styles.

Step 4/4 − Use the page-layout component

<!-- index.html -->

<div id="app">
<page-layout aside>
<header slot="header">…</header>
<aside slot="aside">…</aside>
<main slot="main">…</main>
<footer slot="footer">…</footer>
<script type="module" src="main.js"></script>

And… we’re done !

Well, that was quick.

Full demo here:

Beautifully hand-written code <> awesome result. Sometimes.

Front-end Developer; I‘m not sure who I am.