Skip to content

Commit b7d9aa1

Browse files
committed
[css-mixins-1] Properly define scoped mixin behavior lifting arguments onto the @apply'd element. w3c#13113
1 parent 4c5fb12 commit b7d9aa1

1 file changed

Lines changed: 77 additions & 6 deletions

File tree

css-mixins-1/Overview.bs

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ The ''@result'' Rule</h3>
10171017

10181018
Within a ''@mixin'' rule,
10191019
the ''@result'' rule specifies the <dfn export>mixin result</dfn>,
1020-
a [=nested declarations block=]
1020+
a [=nested declarations rule=]
10211021
similar to the 'result!!descriptor' descriptor in [=custom functions=].
10221022
It's what the ''@apply'' rule will be substituted with.
10231023

@@ -1500,24 +1500,95 @@ with each property value in the [=mixin result=]
15001500
having its value replaced by an anonymous [=custom function=]
15011501
with the following properties:
15021502

1503-
* The [=function parameters=] are identical to the [=mixin parameters=],
1504-
and the [=function invocation=] is passed the same values as the ''@apply''.
1503+
* The [=function parameters=] are identical to the [=mixin parameters=].
1504+
(The function's invocation has some adjustments; see below.)
15051505
* The [=function body=] is identical to the [=mixin body=],
15061506
minus any ''@result'' rules,
15071507
plus a 'result' descriptor
15081508
containing the original value of the property this anonymous function is replacing.
15091509

1510-
Note: In practice, this [=custom function=] can usually be completely hypothetical,
1510+
Additionally, the arguments passed to the mixin
1511+
are evaluated and stored on the ''@apply''@apos;d element,
1512+
to ensure they obtain the "expected" value from that element,
1513+
rather than unexpectedly evaluating in a descendant element's context:
1514+
1515+
* For every [=mixin parameter=],
1516+
a custom property is added to the top level of the [=mixin result=],
1517+
with a [=hygienic rename|hygienic name=]
1518+
and a value of the corresponding ''@apply()'' argument.
1519+
This custom property is <em>unobservable</em> to the page;
1520+
it only exists for UA purposes.
1521+
1522+
Also, a [=custom property registration=]
1523+
with that name,
1524+
a syntax of the [=parameter type=],
1525+
an inherit flag of "true",
1526+
and no initial value,
1527+
is added to the tree of that element.
1528+
* Each anonymous function invocation
1529+
has its arguments set to ''var()'' functions
1530+
referring to the corresponding custom property name
1531+
created in the previous bullet point.
1532+
1533+
<div class=example>
1534+
For example, given the following mixin:
1535+
1536+
<xmp highlight=css>
1537+
@mixin --same-size(--size <length>) {
1538+
@result {
1539+
&, & > * {
1540+
width: calc(10 * var(--size));
1541+
}
1542+
}
1543+
}
1544+
.parent {
1545+
font-size: 10px;
1546+
@apply --same-size(1em);
1547+
}
1548+
.parent > .child {
1549+
font-size: 20px;
1550+
}
1551+
</xmp>
1552+
1553+
This will desugar into approximately:
1554+
1555+
<xmp>
1556+
@property --magic-arg1 {
1557+
syntax: "<length>";
1558+
inherits: true;
1559+
/* initial-value: don't worry about it; */
1560+
}
1561+
@function --anonfunc1(--arg1) {
1562+
result: calc(10 * var(--arg1));
1563+
}
1564+
@function --anonfunc2(--arg1) {
1565+
result: calc(10 * var(--arg1));
1566+
}
1567+
.parent {
1568+
font-size: 10px;
1569+
--magic-arg1: 1em; /* resolves based on font-size here */
1570+
width: --anonfunc1(var(--magic-arg1));
1571+
}
1572+
.parent > .child {
1573+
font-size: 20px;
1574+
width: --anonfunc2(var(--magic-arg1));
1575+
/* --magic-arg1 is 10px, since it was resolved on the parent */
1576+
}
1577+
</xmp>
1578+
</div>
1579+
1580+
Note: In practice, this anonymous [=custom function=]
1581+
can usually be completely hypothetical,
15111582
and a direct substitution used instead.
1512-
It's required only to make variables resolve correctly.
1583+
It's required only to make variables
1584+
and other element-relative values resolve correctly.
15131585

15141586
Issue: TODO add a few examples derived from <a href="https://github.com/w3c/csswg-drafts/issues/12927">issue 12927</a>.
15151587

15161588

15171589

15181590

15191591

1520-
15211592
<!-- Big Text: cssom
15221593

15231594
███▌ ███▌ ███▌ ███▌ █ █

0 commit comments

Comments
 (0)