Skip to content

Commit 91e074d

Browse files
committed
[css-mixins-1] Define the @contents rule for passing a style block to a mixin.
1 parent 2061e76 commit 91e074d

File tree

1 file changed

+84
-2
lines changed

1 file changed

+84
-2
lines changed

css-mixins-1/Overview.bs

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -882,12 +882,17 @@ and a [=mixin body=].
882882
save that it lacks a [=custom function/return type=].)
883883

884884
<pre class="prod def">
885-
<<@mixin>> = @mixin <<function-token>> <<function-parameter>>#? )
885+
<<@mixin>> = @mixin <<function-token>> <<function-parameter>>#? , @contents? )
886886
{
887887
<<declaration-rule-list>>
888888
}
889889
</pre>
890890

891+
Differing from the ''@function'' rule,
892+
the final item in the parameters list
893+
can be the <<at-keyword-token>> ''@contents'',
894+
indicating that this [=mixin=] accepts a ''@contents'' block.
895+
891896
If a [=default value=] and a [=parameter type=] are both provided,
892897
then the default value must parse successfully according to that parameter type’s syntax.
893898
Otherwise, the ''@mixin'' rule is invalid.
@@ -1039,6 +1044,76 @@ aren't visible to anything outside of the [=mixin body=].
10391044
and the ''.warning'' rule would not be able to use them.
10401045
</div>
10411046

1047+
<h3 id='contents-rule'>
1048+
The <dfn>@contents</dfn> Rule</h3>
1049+
1050+
In addition to accepting arguments passed by the <<dashed-function>> in the ''@apply'' rule,
1051+
a [=mixin=] can accept a <dfn>contents block</dfn>.
1052+
This is indicated by the mixin using a final ''@contents'' <<at-keyword-token>> in its parameter list,
1053+
and is passed by giving the ''@apply'' rule invoking the mixin
1054+
a block.
1055+
1056+
This allows the invoker of the [=mixin=] to pass an entire style block,
1057+
which the [=mixin=] can then substitute into itself.
1058+
This is useful, for example,
1059+
if the [=mixin=] handles some common conditions for the author,
1060+
and substitutes the [=contents block=] into a predefined ''@media'' or ''@container'' rule.
1061+
1062+
The syntax of a ''@contents'' at-rule is:
1063+
1064+
<pre class="prod def">
1065+
<<@contents>> = @contents [ { <<declaration-list>> } ]?
1066+
</pre>
1067+
1068+
That is, it is either an <em>empty</em> statement ended immediately by a semicolon,
1069+
or a block treated as a [=nested declarations rule=].
1070+
The empty statement form behaves identically to passing an empty block.
1071+
1072+
If the [=mixin=] did not declare a ''@contents'' parameter,
1073+
the ''@contents'' rule is ignored,
1074+
substituting with nothing.
1075+
Otherwise, if the ''@apply'' rule invoking the [=mixin=] passed a [=contents block=],
1076+
the ''@contents'' is replaced with the [=contents block=],
1077+
treating it as a [=nested declarations rule=].
1078+
Otherwise, if the ''@apply'' rule did not pass a [=contents block=],
1079+
the ''@contents'' rule is replaced with its own <<declaration-list>>,
1080+
treated as a [=nested declarations rule=].
1081+
1082+
Outside of a [=mixin body=],
1083+
the ''@contents'' rule is invalid and ignored.
1084+
1085+
<div class=example>
1086+
For example, the following mixins
1087+
abstracts the cases that the page would consider to be appropriate
1088+
for a "single column" layout,
1089+
allowing the rest of the page to handle the case without worrying about the details,
1090+
so the conditions can be adjusted in the future if necessary:
1091+
1092+
<pre highlight=css>
1093+
@mixin --one-column(@contents) {
1094+
@media (width <= 800px) {
1095+
@contents;
1096+
}
1097+
}
1098+
@mixin --two-column(@contents) {
1099+
@media (width > 800px) {
1100+
@contents;
1101+
}
1102+
}
1103+
body {
1104+
@apply --one-column {
1105+
display: flex;
1106+
flex-flow: column;
1107+
}
1108+
@apply --two-column {
1109+
display: grid;
1110+
grid-template-columns: ;
1111+
}
1112+
}
1113+
</pre>
1114+
</div>
1115+
1116+
10421117

10431118
Using Mixins {#using-mixins}
10441119
============================
@@ -1058,7 +1133,7 @@ in place of the ''@apply'' rule itself.
10581133
Its grammar is:
10591134

10601135
<pre class="prod">
1061-
<<@apply>> = @apply <<dashed-function>> ;
1136+
<<@apply>> = @apply <<dashed-function>> [ { <<declaration-list>> } ]?;
10621137
</pre>
10631138

10641139
The ''@apply'' rule is only valid
@@ -1084,6 +1159,13 @@ the ''@apply'' application does nothing.
10841159
(Passing too few arguments is fine;
10851160
the missing arguments take their default values instead.)
10861161

1162+
If the [=mixin=] declares a ''@contents'' parameter,
1163+
and the ''@apply'' rule has a <<declaration-list>> block,
1164+
that block is passed as its [=contents block=].
1165+
If the [=mixin=] did not declare a ''@contents'' parameter,
1166+
having a <<declaration-list>> block makes the ''@apply'' rule invalid
1167+
(similar to passing too many arguments).
1168+
10871169

10881170
<!-- Big Text: @env
10891171

0 commit comments

Comments
 (0)