Skip to content

Commit 90373dd

Browse files
committed
Document new color unit behavior
Partially addresses #511 See sass/sass#2904
1 parent 9cb5c25 commit 90373dd

File tree

5 files changed

+92
-8
lines changed

5 files changed

+92
-8
lines changed

data/documentation.yml

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ toc:
5959
- <code>sass:string</code>: /documentation/modules/string
6060
- Breaking Changes: /documentation/breaking-changes
6161
:children:
62+
- Color Units: /documentation/breaking-changes/color-units
6263
- Extending Compound Selectors: /documentation/breaking-changes/extend-compound
6364
- CSS Variable Syntax: /documentation/breaking-changes/css-vars
6465
- Command Line: /documentation/cli

source/documentation/breaking-changes.html.md.erb

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ time-sensitive, so they may be released with new minor version numbers instead.
2323

2424
These breaking changes are coming soon or have recently been released:
2525

26+
* [Color functions are becoming more strict about
27+
units](breaking-changes/color-units) beginning in Dart Sass 1.32.0.
28+
2629
* [Compound selectors could not be extended](breaking-changes/extend-compound)
2730
in Dart Sass 1.0.0 and Ruby Sass 4.0.0.
2831

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
title: "Breaking Change: Color Units"
3+
introduction: >
4+
In CSS, saturation and lightness parameters always require `%` units and hue
5+
parameters allow any angle unit. Sass has historically ignored units for these
6+
functions. We're in the process of changing that.
7+
---
8+
9+
It's important that we keep Sass's implementation of [`hsl()`] consistent with
10+
the CSS spec, and that we keep other Sass functions that deal with colors
11+
consistent with `hsl()`. One notable place where Sass has historically differed
12+
from the spec is the way that `hsl()` (and other color functions) handled units.
13+
14+
[`hsl()`]: ../modules/color#hsl
15+
16+
For hue, CSS allows any [angle unit] (`deg`, `grad`, `rad`, or `turn`). It also
17+
allows a unitless number, which is interpreted as `deg`. Historically, Sass has
18+
allowed *any* unit, and interpreted it as `deg`. This is particularly
19+
problematic because it meant that the valid CSS expression `hsl(0.5turn, 100%,
20+
50%)` would be allowed by Sass but interpreted entirely wrong.
21+
22+
[angle unit]: https://drafts.csswg.org/css-values-4/#angles
23+
24+
For lightness and saturation, CSS only allows `%` units. Even unitless numbers
25+
aren't allowed (unlike for the hue). Historically, Sass has allowed *any* unit,
26+
and interpreted it as `%`. You could even write `hsl(0, 100px, 50s)` and Sass
27+
would return the color `red`.
28+
29+
To fix this issue and bring Sass in line with the CSS spec, we're making changes
30+
in multiple phases:
31+
32+
## Phase 1
33+
34+
<% impl_status dart: '1.32.0', libsass: false, ruby: false %>
35+
36+
To begin with, we're just introducing deprecation warnings for behaviors that we
37+
plan to change. Specifically, Sass will emit a deprecation warning if you do any
38+
of the following:
39+
40+
* Pass a number with a unit other than `deg` as a hue to any function. Passing a
41+
unitless number is still allowed.
42+
43+
* Pass a unitless number as saturation or lightness to any function.
44+
45+
* Pass a number with a unit other than `%` as saturation or lightness to any
46+
function.
47+
48+
Of these warnings, the first is the most important one to deal with immediately
49+
specifically if you're passing a number with unit `grad`, `rad`, or `turn`. This
50+
case will change behavior in Phase 2, which could change how your stylesheets
51+
render if you don't update them.
52+
53+
## Phase 2
54+
55+
<% impl_status dart: false, libsass: false, ruby: false %>
56+
57+
At least three months after the launch of Phase 1, we'll change the way angle
58+
units are handled for hue parameters to match the CSS spec. This means that
59+
numbers with `grad`, `rad`, or `turn` units will be converted to `deg`:
60+
`0.5turn` will be converted to `180deg`, `100grad` will be converted to `90deg`,
61+
and so on.
62+
63+
Because this change is necessary to preserve CSS compatibility, according to the
64+
[Dart Sass compatibility policy] it will be made with only a minor version bump.
65+
However, it changes as little behavior as possible to ensure that Sass
66+
interprets all valid CSS according to the CSS spec. All other deprecated
67+
behavior will remain in place until Phase 3.
68+
69+
[Dart Sass compatibility policy]: https://github.com/sass/dart-sass#compatibility-policy
70+
71+
## Phase 3
72+
73+
<% impl_status dart: false, libsass: false, ruby: false %>
74+
75+
Finally, we'll remove all deprecated behavior by making color functions throw
76+
errors if they're passed any units that produced deprecation warnings in Phase
77+
1. Because this is a breaking change that's not strictly necessary for CSS
78+
compatibility, we'll land it as part of Dart Sass 2.0.0 (which we have no
79+
immediate plans to release).

source/documentation/modules.html.md.erb

+5-5
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ Sass provides the following built-in modules:
118118

119119
[hue, saturation, and lightness]: https://en.wikipedia.org/wiki/HSL_and_HSV
120120

121-
The hue is a number between `0deg` and `360deg` (inclusive). The saturation
122-
and lightness are numbers between `0%` and `100%` (inclusive). All these
123-
numbers may be [unitless][]. The alpha channel can be specified as either a
124-
unitless number between 0 and 1 (inclusive), or a percentage between `0%` and
125-
`100%` (inclusive).
121+
The hue is a number between `0deg` and `360deg` (inclusive) and may be
122+
unitless. The saturation and lightness are numbers between `0%` and `100%`
123+
(inclusive) and may *not* be unitless. The alpha channel can be specified as
124+
either a unitless number between 0 and 1 (inclusive), or a percentage between
125+
`0%` and `100%` (inclusive).
126126

127127
[unitless]: values/numbers#units
128128

source/documentation/modules/color.html.md.erb

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ SIGNATURE
2929
arguments must be [unitless][] and between -255 and 255 (inclusive). The
3030
`$hue` argument must have either the unit `deg` or no unit. The `$saturation`,
3131
`$lightness`, `$whiteness`, and `$blackness` arguments must be between `-100%`
32-
and `100%` (inclusive), and may be unitless. The `$alpha` argument must be
32+
and `100%` (inclusive), and may not be unitless. The `$alpha` argument must be
3333
unitless and between -1 and 1 (inclusive).
3434

3535
[unitless]: ../values/numbers#units
@@ -55,7 +55,8 @@ SIGNATURE
5555
Increases or decreases `$color`'s hue.
5656

5757
The `$hue` must be a number between `-360deg` and `360deg` (inclusive) to add
58-
to `$color`'s hue. It may be [unitless][].
58+
to `$color`'s hue. It may be [unitless][] but it may not have any unit other
59+
than `deg`.
5960

6061
[unitless]: ../values/numbers#units
6162

@@ -203,7 +204,7 @@ SIGNATURE
203204
arguments must be [unitless][] and between 0 and 255 (inclusive). The `$hue`
204205
argument must have either the unit `deg` or no unit. The `$saturation`,
205206
`$lightness`, `$whiteness`, and `$blackness` arguments must be between `0%`
206-
and `100%` (inclusive), and may be unitless. The `$alpha` argument must be
207+
and `100%` (inclusive), and may not be unitless. The `$alpha` argument must be
207208
unitless and between -1 and 1 (inclusive).
208209

209210
[unitless]: ../values/numbers#units

0 commit comments

Comments
 (0)