sass-site/source/code-snippets/example-advanced-nesting.liquid

50 lines
1009 B
Plaintext
Raw Normal View History

2023-05-16 13:26:56 +02:00
{% markdown %}
2023-05-30 23:23:34 +02:00
For example, suppose you want to write a selector that matches the outer
selector *and* an element selector. You could write a mixin like this one that
uses the [`selector.unify()` function][] to combine `&` with a user's selector.
2023-05-16 13:26:56 +02:00
2023-05-30 23:23:34 +02:00
[`selector.unify()` function]: /documentation/modules/selector#unify
2023-05-16 13:26:56 +02:00
{% endmarkdown %}
{% codeExample 'advanced-nesting' %}
2023-05-30 23:23:34 +02:00
@use "sass:selector";
2023-05-16 13:26:56 +02:00
2023-05-30 23:23:34 +02:00
@mixin unify-parent($child) {
@at-root #{selector.unify(&, $child)} {
@content;
}
2023-05-16 13:26:56 +02:00
}
2023-05-30 23:23:34 +02:00
.wrapper .field {
@include unify-parent("input") {
/* ... */
}
@include unify-parent("select") {
/* ... */
}
2023-05-16 13:26:56 +02:00
}
2023-05-30 23:23:34 +02:00
===
@use "sass:selector"
2023-05-16 13:26:56 +02:00
2023-05-30 23:23:34 +02:00
@mixin unify-parent($child)
@at-root #{selector.unify(&, $child)}
@content
2023-05-16 13:26:56 +02:00
2023-05-25 17:55:08 +02:00
2023-05-30 23:23:34 +02:00
.wrapper .field
@include unify-parent("input")
/* ... */
@include unify-parent("select")
/* ... */
===
.wrapper input.field {
2023-05-16 13:26:56 +02:00
/* ... */
2023-05-30 23:23:34 +02:00
}
2023-05-16 13:26:56 +02:00
2023-05-30 23:23:34 +02:00
.wrapper select.field {
2023-05-16 13:26:56 +02:00
/* ... */
2023-05-30 23:23:34 +02:00
}
2023-05-16 13:26:56 +02:00
{% endcodeExample %}