Sub topic 10
Maps for design tokens
This chapter assumes you can write variables, mixins and functions in SCSS, and that you know how @each loops work. A map stores named values, which makes it a good home for design tokens: the colors, spacing steps and breakpoints that a design system defines once and reuses.
Defining and reading a map
A map is a comma-separated list of key: value pairs in parentheses. Read a value with map.get() from the built-in sass:map module.
@use 'sass:map';
$colors: (
primary: #3366cc,
danger: #cc3333,
muted: #6b7280
);
.alert {
color: map.get($colors, danger);
}Keys are usually unquoted strings, but they can be any value, including numbers.
Generating rules with @each
Looping over a map gives you the key and value on each pass. Interpolation with #{} builds a class name from the key.
$colors: (
primary: #3366cc,
danger: #cc3333
);
@each $name, $value in $colors {
.text-#{$name} {
color: $value;
}
.bg-#{$name} {
background: $value;
}
}This compiles to four rules: .text-primary, .bg-primary, .text-danger and .bg-danger. Adding a token to the map adds its utility classes with no other edits.
Safe lookups with a function
map.get() returns null when a key is missing. Sass then drops the declaration, so a typo fails silently. Wrap the lookup in a function that stops the build instead.
@use 'sass:map';
$colors: (
primary: #3366cc,
danger: #cc3333
);
@function color($name) {
@if not map.has-key($colors, $name) {
@error "Unknown color token: #{$name}";
}
@return map.get($colors, $name);
}
.button {
background: color(primary);
}Calling color(dangr) now stops compilation with your message.
Nested maps and merging
A map can hold other maps. map.get() accepts several keys to walk down the nesting, and map.merge() returns a new map that combines two.
@use 'sass:map';
$theme: (
colors: (
primary: #3366cc,
surface: #ffffff
),
spacing: (
sm: 0.5rem,
md: 1rem
)
);
$dark: map.merge($theme, (
colors: (
primary: #7aa2f7,
surface: #111827
)
));
.panel {
padding: map.get($theme, spacing, md);
background: map.get($dark, colors, surface);
}Note that map.merge() is shallow. The colors entry in $dark replaces the whole colors map from $theme. To change one nested value and keep its siblings, use map.deep-merge().
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Declaration missing from output | `map.get()` returned `null` for a misspelled key | Wrap lookups in a function that calls `@error` |
| `$map: ... is not a map` | A single pair written without a surrounding map shape, or a stray trailing value | Check the parentheses and commas |
| Nested sibling values disappear | `map.merge()` replaced a whole nested map | Use `map.deep-merge()` |
| Class names contain quotes | A quoted string key was interpolated | Use unquoted keys, or `string.unquote()` |
Exercise
A map $space: (sm: 0.5rem, md: 1rem) is used in a loop. You write .m-#{$name} { margin: $value; } but want to generate the same classes for lg: 2rem too. What is the smallest change, and what does the map need to become if you want map.get($space, lg) to work elsewhere?
Show answer
Add lg: 2rem to the map. The @each loop then emits .m-lg automatically, and map.get($space, lg) returns 2rem because both read the same map. No change to the loop is needed.