sass-site/source/documentation/breaking-changes/slash-div.liquid

149 lines
4.6 KiB
Plaintext
Raw Normal View History

2023-05-25 01:28:27 +02:00
---
2023-05-26 00:24:38 +02:00
title: 'Breaking Change: Slash as Division'
2023-05-25 01:28:27 +02:00
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.
---
2023-06-01 22:56:54 +02:00
{% compatibility 'dart: "partial"', 'libsass: false', 'ruby: false' %}{% endcompatibility %}
2023-05-25 01:28:27 +02:00
2023-05-26 20:32:08 +02:00
{% markdown %}
2023-05-31 18:02:42 +02:00
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]: /documentation/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
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.
This deprecation does not affect uses of `/` inside `calc()` expressions.
2023-05-26 20:32:08 +02:00
{% endmarkdown %}
2023-05-25 01:28:27 +02:00
2023-05-26 20:32:08 +02:00
{% codeExample 'slash-div' %}
2023-05-31 18:02:42 +02:00
@use "sass:math";
// Future Sass, doesn't work yet!
.item3 {
$row: span math.div(6, 2) / 7; // A two-element slash-separated list.
grid-row: $row;
}
===
@use "sass:math"
// Future Sass, doesn't work yet!
.item3
$row: span math.div(6, 2) / 7 // A two-element slash-separated list.
grid-row: $row
===
.item3 {
grid-row: span 3 / 7;
}
2023-05-25 01:28:27 +02:00
{% endcodeExample %}
2023-05-26 20:32:08 +02:00
{% markdown %}
2023-05-31 18:02:42 +02:00
## Transition Period
2023-05-25 01:28:27 +02:00
2023-06-01 22:56:54 +02:00
{% compatibility 'dart: "1.33.0"', 'libsass: false', 'ruby: false', 'feature: "math.div() and list.slash()"' %}{% endcompatibility %}
2023-05-25 01:28:27 +02:00
2023-05-31 18:02:42 +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.
2023-05-26 20:32:08 +02:00
{% endmarkdown %}
2023-05-25 01:28:27 +02:00
2023-06-03 00:18:43 +02:00
{% render 'doc_snippets/silence-deprecations' %}
2023-05-25 01:28:27 +02:00
2023-05-26 20:32:08 +02:00
{% codeExample 'math-div', false %}
2023-05-31 18:02:42 +02:00
@use "sass:math";
2023-05-25 01:28:27 +02:00
2023-05-31 18:02:42 +02:00
// WRONG, will not work in future Sass versions.
@debug (12px/4px); // 3
2023-05-25 01:28:27 +02:00
2023-05-31 18:02:42 +02:00
// RIGHT, will work in future Sass versions.
@debug math.div(12px, 4px); // 3
===
@use "sass:math"
2023-05-25 01:28:27 +02:00
2023-05-31 18:02:42 +02:00
// WRONG, will not work in future Sass versions.
@debug (12px/4px) // 3
2023-05-25 01:28:27 +02:00
2023-05-31 18:02:42 +02:00
// RIGHT, will work in future Sass versions.
@debug math.div(12px, 4px) // 3
2023-05-25 01:28:27 +02:00
{% endcodeExample %}
2023-05-26 20:32:08 +02:00
{% markdown %}
2023-05-31 18:02:42 +02:00
Slash-separated lists will also be available in the transition period. Because
they can't be created with `/` yet, the `list.slash()` function will be added
to create them. You will also be able to pass `"slash"` as the `$separator` to
the [`list.join()` function][] and the [`list.append()` function][].
2023-05-25 01:28:27 +02:00
2023-05-31 18:02:42 +02:00
[`list.join()` function]: /documentation/modules/list#join
[`list.append()` function]: /documentation/modules/list#append
2023-05-26 20:32:08 +02:00
{% endmarkdown %}
2023-05-25 01:28:27 +02:00
2023-05-26 20:32:08 +02:00
{% codeExample 'slash-div-list' %}
2023-05-31 18:02:42 +02:00
@use "sass:list";
@use "sass:math";
.item3 {
$row: list.slash(span math.div(6, 2), 7);
grid-row: $row;
}
===
@use "sass:list"
@use "sass:math"
.item3
$row: list.slash(span math.div(6, 2), 7)
grid-row: $row
===
.item3 {
grid-row: span 3 / 7;
}
2023-05-25 01:28:27 +02:00
{% endcodeExample %}
2023-06-01 22:56:54 +02:00
{% compatibility 'dart: "1.40.0"', 'libsass: false', 'ruby: false', 'feature: "First-class calc"' %}{% endcompatibility %}
2023-05-25 01:28:27 +02:00
2023-05-26 20:32:08 +02:00
{% markdown %}
2023-05-31 18:02:42 +02:00
Alternatively, users can wrap division operations inside a `calc()`
expression, which Sass will simplify to a single number.
2023-05-26 20:32:08 +02:00
{% endmarkdown %}
2023-05-25 01:28:27 +02:00
2023-05-26 20:32:08 +02:00
{% codeExample 'slash-div-calc', false %}
2023-05-31 18:02:42 +02:00
// WRONG, will not work in future Sass versions.
@debug (12px/4px); // 3
2023-05-25 01:28:27 +02:00
2023-05-31 18:02:42 +02:00
// RIGHT, will work in future Sass versions.
@debug calc(12px / 4px); // 3
===
// WRONG, will not work in future Sass versions.
@debug (12px/4px) // 3
2023-05-25 01:28:27 +02:00
2023-05-31 18:02:42 +02:00
// RIGHT, will work in future Sass versions.
@debug calc(12px / 4px) // 3
2023-05-25 01:28:27 +02:00
{% endcodeExample %}
2023-05-26 20:32:08 +02:00
{% markdown %}
2023-05-31 18:02:42 +02:00
## Automatic Migration
2023-05-25 01:28:27 +02:00
2023-05-31 18:02:42 +02:00
You can use [the Sass migrator][] to automatically update your stylesheets to
use `math.div()` and `list.slash()`.
2023-05-25 01:28:27 +02:00
2023-05-31 18:02:42 +02:00
[the Sass migrator]: https://github.com/sass/migrator#readme
2023-05-25 01:28:27 +02:00
2023-05-31 18:02:42 +02:00
```shellsession
$ npm install -g sass-migrator
$ sass-migrator division **/*.scss
```
2023-05-26 20:32:08 +02:00
{% endmarkdown %}