Sub topic 14
Built-in modules such as sass:math and sass:map
This chapter assumes you can write variables, maps and functions in SCSS, and that you know how @use loads a module. Here you will use the modules that ship with Dart Sass.
What built-in modules are
Dart Sass includes modules that provide functions for math, maps, strings, lists, colors, selectors and introspection. You load them with @use and a sass: URL. Each module's functions live under a namespace, which is the last part of the name.
| Module | Load with | Typical use |
|---|---|---|
| `sass:math` | `@use 'sass:math';` | `math.div`, `math.round`, `math.clamp`, `math.percentage` |
| `sass:map` | `@use 'sass:map';` | `map.get`, `map.merge`, `map.has-key`, `map.keys` |
| `sass:string` | `@use 'sass:string';` | `string.unquote`, `string.index` |
| `sass:list` | `@use 'sass:list';` | `list.nth`, `list.append` |
| `sass:color` | `@use 'sass:color';` | `color.adjust`, `color.scale` |
Old global names such as map-get() and percentage() are deprecated. Use the namespaced forms.
Working with sass:math
Plain / is no longer division in Sass. Use math.div. The module also rounds, clamps and converts values to percentages.
@use 'sass:math';
.column {
width: math.percentage(math.div(1, 3));
padding: math.round(math.div(25px, 2));
font-size: math.clamp(14px, 2vw, 20px);
}This compiles width to 33.3333333333% and padding to 13px. math.clamp takes a minimum, a preferred value and a maximum, and in this example it passes through to CSS. Note that math.div keeps units, so math.div(24px, 16px) gives a unitless 1.5.
Working with sass:map
Maps are immutable. Functions such as map.merge return a new map and leave the original unchanged.
@use 'sass:map';
$sizes: (sm: 8px, md: 16px);
$all-sizes: map.merge($sizes, (lg: 32px));
.box {
padding: map.get($all-sizes, md);
margin: map.get($all-sizes, xl);
}The margin line is the common trap. xl does not exist, so map.get returns null, and Sass silently drops the declaration. The compiled CSS contains only padding: 16px;.
Guarding lookups
Check keys before you read them, and fail loudly when one is missing.
@use 'sass:map';
$sizes: (sm: 8px, md: 16px, lg: 32px);
@function size($name) {
@if not map.has-key($sizes, $name) {
@error "Unknown size: #{$name}. Use one of: #{map.keys($sizes)}";
}
@return map.get($sizes, $name);
}
.card {
padding: size(md);
}If you call size(xl), compilation stops with your message. A typo in a token name then breaks the build instead of producing a missing style.
Common errors
- Forgetting the
@useline. Callingmap.getwithout@use 'sass:map'gives an "undefined function" error. - Writing
$a / $band expecting division. It emits a deprecation warning, so usemath.div($a, $b). - Putting
@use 'sass:math'after other rules. All@userules must come before any rules other than@forwardand variable declarations. - Assuming
map.mergechanges its argument. Assign the result to a variable.
Exercise
The call map.get($sizes, xl) appears in a declaration, but $sizes has no xl key. What does Sass do, and how would you change the code so the build fails instead?
Show answer
map.get returns null, and Sass omits the declaration without any error. To fail the build, check map.has-key($sizes, xl) first and call @error when it is false, as in the size() function above.