sass-site/source/documentation/at-rules/warn.liquid

74 lines
2.0 KiB
Plaintext
Raw Normal View History

---
title: "@warn"
introduction: >
2023-05-30 22:27:01 +02:00
When writing [mixins](/documentation/at-rules/mixin) and
[functions](/documentation/at-rules/function), you may want to discourage
users from passing certain arguments or certain values. They may be passing
legacy arguments that are now deprecated, or they may be calling your API in a
way thats not quite optimal.
---
{% markdown %}
2023-05-30 23:23:34 +02:00
The `@warn` rule is designed just for that. It's written `@warn <expression>`
and it prints the value of the [expression][] (usually a string) for the user,
along with a stack trace indicating how the current mixin or function was
called. Unlike the [`@error` rule][], though, it doesn't stop Sass entirely.
2023-05-30 23:23:34 +02:00
[expression]: /documentation/syntax/structure#expressions
[`@error` rule]: /documentation/at-rules/error
{% endmarkdown %}
{% codeExample 'warn' %}
2023-05-30 23:23:34 +02:00
$known-prefixes: webkit, moz, ms, o;
2023-05-30 23:23:34 +02:00
@mixin prefix($property, $value, $prefixes) {
@each $prefix in $prefixes {
@if not index($known-prefixes, $prefix) {
@warn "Unknown prefix #{$prefix}.";
}
2023-05-30 23:23:34 +02:00
-#{$prefix}-#{$property}: $value;
}
#{$property}: $value;
}
2023-05-30 23:23:34 +02:00
.tilt {
// Oops, we typo'd "webkit" as "wekbit"!
@include prefix(transform, rotate(15deg), wekbit ms);
}
===
$known-prefixes: webkit, moz, ms, o
2023-05-30 23:23:34 +02:00
@mixin prefix($property, $value, $prefixes)
@each $prefix in $prefixes
@if not index($known-prefixes, $prefix)
@warn "Unknown prefix #{$prefix}."
2023-05-30 23:23:34 +02:00
-#{$prefix}-#{$property}: $value
2023-05-30 23:23:34 +02:00
#{$property}: $value
2023-05-30 23:23:34 +02:00
.tilt
// Oops, we typo'd "webkit" as "wekbit"!
@include prefix(transform, rotate(15deg), wekbit ms)
2023-05-31 19:45:19 +02:00
===
.tilt {
-wekbit-transform: rotate(15deg);
-ms-transform: rotate(15deg);
transform: rotate(15deg);
}
{% endcodeExample %}
{% markdown %}
2023-05-30 23:23:34 +02:00
The exact format of the warning and stack trace varies from implementation to
implementation. This is what it looks like in Dart Sass:
2023-05-30 23:23:34 +02:00
```
Warning: Unknown prefix wekbit.
example.scss 6:7 prefix()
example.scss 16:3 root stylesheet
```
2023-05-30 22:27:01 +02:00
{% endmarkdown %}