Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…

Read the release notes

← All tutorials

Tutorial: Dashboard

Build a weekly analytics dashboard: four key metrics, a chart of page views and a table of projects you can sort. You'll write it as plain HTML with two real tables first, so it is complete before any JavaScript runs.

About 15 minutes · Uses all four layers: semantic HTML, data-* attributes, CSS-only custom elements, web components · JavaScript is added in step 4

The finished page. Select a column heading in the Projects table to sort by it.

Before you start

Make a folder, and in it one file called index.html with this in it. Open the file in a browser and keep it open: after each step, save and reload.

That is the whole setup: one stylesheet, always the newest release because the URL names no version, and one line of CSS that keeps the content off the edge of the window using a Vanilla Breeze spacing token. It is the only CSS you will write. The other ways to install are in the Quick Start.

There is no <script> tag yet. In this series the script is added only in the step that needs it. On this page that is step 4, and until then everything you see is HTML and CSS.

Step 1 — Write the page in semantic HTML

A dashboard is a heading, a few numbers and some data. Write exactly that. Each metric is an <hgroup>: a heading with the paragraphs that belong to it. The number is the heading, because it is the headline of the group; the label comes before it and the change since last week after it. Each number sits in a <data> element whose value holds the raw figure.

The decision that matters most is the data. A chart is a picture of a table, so write the table: one row per day, with the day as a row header. The projects list is a table too. Both get a <caption>, which names the table for screen readers and will later become the chart's title. Because the data is in the HTML, the page is already complete.

Dates go in <time> with a machine-readable datetime. You'll need those values again in step 4.

Check

Step 2 — Arrange it with data-* attributes

Layout in Vanilla Breeze is an attribute on an element you already have. Three values of data-layout arrange this page:

The metrics also get a class. Vanilla Breeze has no utility classes, but it has a few variant classes that say which kind of thing an element is. class="eyebrow" on an <hgroup> turns its first paragraph into a small uppercase label above the heading.

There is one new element. The projects table has four columns, and a table cannot shrink below its content, so on a narrow phone it would push the page sideways. Wrap it in a <section data-table-scroll> and the table scrolls inside the section instead. A scrolling area must be reachable by keyboard and have a name, which is what tabindex="0" and aria-labelledby pointing at the caption are for.

Check

Step 3 — Add custom elements for the tiles

The metrics are in a row, but nothing marks where one ends and the next begins. A dashboard tile needs a surface: a border, a corner radius and padding. No attribute on an <hgroup> provides that, and in most frameworks this is where a <div class="card"> appears.

Vanilla Breeze uses a named element instead. Wrap each <hgroup> in a <layout-card>; data-variant="outlined" draws a border in place of the default shadow. The card is a child of the grid, so the four tiles come out the same height.

The status column gets the same treatment. Wrap each status in a <layout-badge> and pick a colour that matches its meaning: success for Active, info for In review, error for Blocked, and none for Shipped. The colour only reinforces the word, so the text stays.

Both are custom elements styled by the stylesheet alone. They have no behaviour, so there is still no script.

What changed

Step 4 — Add web components for the chart and the sort

Before adding a web component, ask one question: does this page need behaviour that HTML and CSS cannot provide? Here the answer is yes, twice. Drawing a chart from numbers takes code, and so does re-ordering table rows when someone picks a column.

Both components work the same way: they wrap the table you already wrote and read it. This is the step where the script goes in. <data-table> is in the main script. Charts are a separate add-on, so they are not downloaded by pages that have none; the add-on is one more script and one more stylesheet. Add these three lines to the <head>:

Wrap the page-views table in <chart-wc>. It reads the row headers as categories and the Views column as a series, draws an SVG chart, and uses the caption as the title. data-type="column" picks the chart and data-label-y names the vertical axis. With one series there is nothing for a legend to tell apart, so data-legend="false" switches it off. Pointing at a bar still shows its value in a tooltip. The table does not go away. It is hidden visually and stays in the page as the chart's text alternative, and it is what gets printed.

Wrap the projects table in <data-table>, inside the scrolling section, and mark each sortable column with data-sort and the kind of value it holds: string, number or date. "Nov 20" has no year, so it cannot be sorted as a date as written. Give each of those cells a data-sort-value with the same value as its datetime.

Check

What a visitor sees if the scripts do not load: the step 3 page.

What you learned

Try it yourself

  1. Filter the projects. Add data-filterable to the projects <table>. A search box appears above it, and typing "active" hides the other rows. Read data-table.
  2. Compare two sources. Add a third column to the page-views table, name the two value columns Organic and Direct, and remove data-legend="false". The legend comes back, and selecting a name in it hides that series. Read chart-wc.
  3. Swap a tile for a component. <score-card> is a web component made for metric tiles, with a trend colour and room for a sparkline. Replace one <layout-card> with it and compare what each gives you with JavaScript off. Read score-card.
↑