2019-06-04 16:11:33 +02:00
|
|
|
---
|
|
|
|
title: "Breaking Change: Slash as Division"
|
|
|
|
introduction: >
|
|
|
|
Sass currently treats `/` as a division operation in some contexts and a
|
|
|
|
separator in others. This makes it difficult for Sass users to tell what any
|
|
|
|
given `/` will mean, and makes it hard to work with new CSS features that use
|
|
|
|
`/` as a separator.
|
|
|
|
---
|
|
|
|
|
|
|
|
<% impl_status dart: false, libsass: false, ruby: false %>
|
|
|
|
|
|
|
|
Today, Sass uses [complex heuristics][] to figure out whether a `/` should be
|
|
|
|
treated as division or a separator. Even then, as a separator it just produces
|
|
|
|
an unquoted string that's difficult to inspect from within Sass. As more and
|
|
|
|
more CSS features like [CSS Grid][] and the [new `rgb()` and `hsl()` syntax][]
|
|
|
|
use `/` as a separator, this is becoming more and more painful to Sass users.
|
|
|
|
|
|
|
|
[complex heuristics]: ../operators/numeric#slash-separated-values
|
|
|
|
[CSS Grid]: https://developer.mozilla.org/en-US/docs/Web/CSS/grid-row
|
|
|
|
[new `rgb()` and `hsl()` syntax]: https://drafts.csswg.org/css-color/#rgb-functions
|
|
|
|
|
2021-05-18 04:09:58 +02:00
|
|
|
Because Sass is a CSS superset, we're matching CSS's syntax by redefining `/` to
|
|
|
|
be *only* a separator. `/` will be treated as a new type of list separator,
|
|
|
|
similar to how `,` works today. Division will instead be written using the new
|
|
|
|
`math.div()` function. This function will behave exactly the same as `/` does
|
|
|
|
today.
|
2019-06-04 16:11:33 +02:00
|
|
|
|
|
|
|
<% example do %>
|
2019-09-03 01:32:14 +02:00
|
|
|
@use "sass:math";
|
|
|
|
|
2019-06-04 16:11:33 +02:00
|
|
|
// Future Sass, doesn't work yet!
|
|
|
|
.item3 {
|
2019-09-03 01:32:14 +02:00
|
|
|
$row: span math.div(6, 2) / 7; // A two-element slash-separated list.
|
2019-06-04 16:11:33 +02:00
|
|
|
grid-row: $row;
|
|
|
|
}
|
|
|
|
===
|
2019-09-03 01:32:14 +02:00
|
|
|
@use "sass:math"
|
|
|
|
|
2019-06-04 16:11:33 +02:00
|
|
|
// Future Sass, doesn't work yet!
|
|
|
|
.item3
|
2019-09-03 01:32:14 +02:00
|
|
|
$row: span math.div(6, 2) / 7 // A two-element slash-separated list.
|
2019-06-04 16:11:33 +02:00
|
|
|
grid-row: $row
|
|
|
|
===
|
|
|
|
.item3 {
|
|
|
|
grid-row: span 3 / 7;
|
|
|
|
}
|
|
|
|
<% end %>
|
|
|
|
|
|
|
|
## Transition Period
|
|
|
|
|
2021-05-18 04:09:58 +02:00
|
|
|
<% impl_status dart: "1.33.0", libsass: false, ruby: false,
|
2019-09-03 01:32:14 +02:00
|
|
|
feature: "math.div() and list.slash()" %>
|
2019-06-04 16:11:33 +02:00
|
|
|
|
2021-05-18 04:09:58 +02:00
|
|
|
To ease the transition, we've begun by adding the `math.div()` function. The `/`
|
|
|
|
operator still does division for now, but it also prints a deprecation warning
|
|
|
|
when it does so. Users should switch all division to use `math.div()` instead.
|
2019-06-04 16:11:33 +02:00
|
|
|
|
|
|
|
<% example(autogen_css: false) do %>
|
2019-09-03 01:32:14 +02:00
|
|
|
@use "sass:math";
|
|
|
|
|
2019-06-04 16:11:33 +02:00
|
|
|
// WRONG, will not work in future Sass versions.
|
|
|
|
@debug (12px/4px); // 3
|
|
|
|
|
|
|
|
// RIGHT, will work in future Sass versions.
|
2019-09-03 01:32:14 +02:00
|
|
|
@debug math.div(12px, 4px); // 3
|
2019-06-04 16:11:33 +02:00
|
|
|
===
|
2019-09-03 01:32:14 +02:00
|
|
|
@use "sass:math"
|
|
|
|
|
2019-06-04 16:11:33 +02:00
|
|
|
// WRONG, will not work in future Sass versions.
|
|
|
|
@debug (12px/4px) // 3
|
|
|
|
|
|
|
|
// RIGHT, will work in future Sass versions.
|
2019-09-03 01:32:14 +02:00
|
|
|
@debug math.div(12px, 4px) // 3
|
2019-06-04 16:11:33 +02:00
|
|
|
<% end %>
|
|
|
|
|
|
|
|
Slash-separated lists will also be available in the transition period. Because
|
2019-09-03 01:32:14 +02:00
|
|
|
they can't be created with `/` yet, the `list.slash()` function will be added to
|
2019-06-04 16:11:33 +02:00
|
|
|
create them. You will also be able to pass `"slash"` as the `$separator` to the
|
2019-09-03 01:32:14 +02:00
|
|
|
[`list.join()` function][] and the [`list.append()` function][].
|
2019-06-04 16:11:33 +02:00
|
|
|
|
2019-09-03 01:32:14 +02:00
|
|
|
[`list.join()` function]: ../modules/list#join
|
|
|
|
[`list.append()` function]: ../modules/list#append
|
2019-06-04 16:11:33 +02:00
|
|
|
|
|
|
|
<% example do %>
|
2021-05-18 04:09:58 +02:00
|
|
|
@use "sass:list";
|
|
|
|
|
2019-06-04 16:11:33 +02:00
|
|
|
.item3 {
|
2021-05-18 04:09:58 +02:00
|
|
|
$row: list.slash(span divide(6, 2), 7);
|
2019-06-04 16:11:33 +02:00
|
|
|
grid-row: $row;
|
|
|
|
}
|
|
|
|
===
|
2021-05-18 04:09:58 +02:00
|
|
|
@use "sass:list";
|
|
|
|
|
2019-06-04 16:11:33 +02:00
|
|
|
.item3
|
2021-05-18 04:09:58 +02:00
|
|
|
$row: list.slash(span divide(6, 2), 7)
|
2019-06-04 16:11:33 +02:00
|
|
|
grid-row: $row
|
|
|
|
===
|
|
|
|
.item3 {
|
|
|
|
grid-row: span 3 / 7;
|
|
|
|
}
|
|
|
|
<% end %>
|
|
|
|
|
|
|
|
## Automatic Migration
|
|
|
|
|
|
|
|
You can use [the Sass migrator][] to automatically update your stylesheets to
|
2019-09-03 01:32:14 +02:00
|
|
|
use `math.div()` and `list.slash()`.
|
2019-06-04 16:11:33 +02:00
|
|
|
|
|
|
|
[the Sass migrator]: https://github.com/sass/migrator#readme
|
|
|
|
|
|
|
|
```shellsession
|
|
|
|
$ npm install -g sass-migrator
|
|
|
|
$ sass-migrator division **/*
|
|
|
|
```
|