Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 39 ยท Projects and Practice

Project: A Themed Dashboard Layout

Build a responsive dashboard stylesheet in Sass: theme maps turned into custom properties, a breakpoint mixin, a sidebar and card grid, stat tiles and a theme switch, step by step.

A dashboard is a good stress test for a stylesheet: many components share one look, the layout changes across screen sizes, and users expect a dark mode. In this project you build the stylesheet for a small analytics dashboard: a sidebar, a top bar, stat tiles and a grid of cards, in light and dark themes, from a handful of Sass maps and mixins.

You will use the patterns from the earlier lessons: theme maps and custom properties, breakpoint mixins, and BEM components.

Plan

flowchart TD
  T["tokens: theme maps"] --> V["theme-vars mixin -> custom properties"]
  B["breakpoints map + up()"] --> L["layout: grid and sidebar"]
  V --> C["components: tile, card, nav"]
  L --> C
  C --> M["main.scss"]

Tokens feed the theme variables; the breakpoint mixin shapes the layout; components read variables only.

Requirements:

  1. Two themes with identical semantic keys; light by default, dark when the user's system asks or when data-theme="dark" is set.
  2. Mobile first: a single column; from md a sidebar appears; from lg the card grid gets three columns.
  3. Components never contain colors; they use var(--...).
  4. The stat tile's trend arrow color depends on a modifier (--up, --down).

Step 1: tokens and the theme mixin

SCSS
@use "sass:map";

$themes: (
  light: (
    bg: #f3f4f6, surface: #ffffff, text: #111827, muted: #6b7280,
    line: #e5e7eb, accent: #4f46e5, up: #15803d, down: #b91c1c,
  ),
  dark: (
    bg: #0b1020, surface: #151b2f, text: #e5e7eb, muted: #9ca3af,
    line: #26304d, accent: #818cf8, up: #4ade80, down: #f87171,
  ),
);

@mixin theme-vars($name) {
  color-scheme: $name;
  @each $token, $value in map.get($themes, $name) {
    --#{$token}: #{$value};
  }
}

:root { @include theme-vars(light); }
[data-theme="dark"] { @include theme-vars(dark); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) { @include theme-vars(dark); }
}

The two themes have identical keys. If you add warning to one, you must add it to the other. A small check function can enforce it with @error when you extend the project.

Step 2: breakpoints

SCSS
$breakpoints: (md: 48rem, lg: 64rem);

@mixin up($name) {
  @media (min-width: map.get($breakpoints, $name)) { @content; }
}

Step 3: layout

SCSS
.app {
  display: grid;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);

  @include up(md) { grid-template-columns: 14rem 1fr; }
}

.sidebar {
  display: none;
  padding: 1rem;
  background: var(--surface);
  border-right: 1px solid var(--line);
  @include up(md) { display: block; }
}

.main { padding: 1rem; }

.grid {
  display: grid;
  gap: 1rem;
  @include up(md) { grid-template-columns: repeat(2, 1fr); }
  @include up(lg) { grid-template-columns: repeat(3, 1fr); }
}

Step 4: components

SCSS
.tile {
  padding: 1rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;

  &__label { margin: 0; color: var(--muted); font-size: 0.85rem; }
  &__value { margin: 0.25rem 0; font-size: 1.8rem; font-weight: 700; }
  &__trend { margin: 0; font-size: 0.9rem; }

  &--up &__trend { color: var(--up); }
  &--down &__trend { color: var(--down); }
}

&--up &__trend compiles to .tile--up .tile__trend: both & refer to the same parent selector .tile, and the suffixes are appended separately.

The whole project

Below is the full stylesheet with demo markup. Use the buttons to switch themes. Resize the preview pane (or your window) to see the layout change at the two breakpoints (they are small here so you can see the effect in a narrow pane).

The dashboard

The layout uses body background and text from the theme variables, so switching the theme recolors everything without recompiling. The breakpoints in the live version are 30rem and 44rem so you can see them in the editor panel; a real project would use values like 48rem and 64rem.

Improvements to try

  1. Print a table of theme keys: write @function assert-same-keys($a, $b) that compares map.keys of both themes and @errors on a mismatch.
  2. Add a compact density mode that changes spacing tokens with [data-density="compact"], using the same mixin approach.
  3. Move to modules. Split the file into abstracts/_themes.scss, abstracts/_mixins.scss, layout/_app.scss, components/_tile.scss and load them from main.scss (architecture).
  4. Add a CSS container query to the tile so its value shrinks when the tile is narrow (breakpoints).
  5. Persist the theme with localStorage in your own page (the sandbox in this tutorial blocks storage, but your site will not).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why do both theme maps need the same keys?

    Every theme must define every token that components read.
  2. 2. What does &--up &__trend compile to inside .tile?

    Each & is replaced by the parent selector, then the suffixes are appended to each.
  3. 3. How does the dashboard become mobile first?

    Min-width queries layer improvements on top of the small-screen layout.
  4. 4. What switches the theme at runtime without recompiling?

    The custom properties change with the attribute selector; components just read var().
  5. 5. Where should colors live in the components?

    Components read var(--token) so themes control the look.

Summary

  • Theme maps with identical keys, applied by a mixin to :root, [data-theme] and prefers-color-scheme.
  • A breakpoint map and up() mixin make the layout mobile first.
  • Components use only var(--token) values and BEM classes; modifiers change one token.
  • Nested & with suffixes lets a modifier restyle an element in one place.

Next: Project: An Animated UI Kit