|
| 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). |
0 commit comments