Skip to content

Commit 16d7b76

Browse files
committed
[css-mixins-1] Remove @macro (mixins cover the use-cases now). #13680
1 parent 5c4e0fc commit 16d7b76

1 file changed

Lines changed: 4 additions & 290 deletions

File tree

css-mixins-1/Overview.bs

Lines changed: 4 additions & 290 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,167 +1420,6 @@ but the mixin not use it.
14201420
</div>
14211421

14221422

1423-
<!-- Big Text: @macro
1424-
1425-
████▌ █ █ ███▌ ███▌ ████▌ ███▌
1426-
█▌ █▌ ██ ██ ▐█ ▐█ █▌ █▌ █▌ █▌ █▌ █▌
1427-
█▌▐█ █▌ █▌█ █▐█ █▌ █▌ █▌ █▌ █▌ █▌ █▌
1428-
█▌▐█ █▌ █▌ █ ▐█ █▌ █▌ █▌ ████▌ █▌ █▌
1429-
█▌ ██▌ █▌ ▐█ █████▌ █▌ █▌▐█ █▌ █▌
1430-
█▌ █▌ ▐█ █▌ █▌ █▌ █▌ █▌ ▐█ █▌ █▌
1431-
████▌ █▌ ▐█ █▌ █▌ ███▌ █▌ █▌ ███▌
1432-
-->
1433-
1434-
Defining Macros {#macros}
1435-
===============
1436-
1437-
A <dfn for=css>macro</dfn> is a simplified variant of a [=mixin=],
1438-
that very directly substitutes its body into its ''@apply''-ing rule.
1439-
1440-
It does not take any arguments
1441-
(besides possibly a ''@contents'' block)
1442-
or define local variables
1443-
(and thus doesn't use a ''@result'' rule to separate its result from its (nonexistent) body)
1444-
and does not impose a "scoping" semantic on its result rules.
1445-
Otherwise, it is identical to a [=mixin=].
1446-
1447-
<div class=example>
1448-
For simple mixins that are just meant
1449-
to make it easier to include a commonly-repeated block of styles,
1450-
[=macros=] can be slightly shorter/easier to use:
1451-
1452-
<xmp highlight=css>
1453-
@macro --reset-list {
1454-
margin: 0;
1455-
padding: 0;
1456-
list-style: none;
1457-
}
1458-
.foo {
1459-
@apply --reset-list;
1460-
}
1461-
</xmp>
1462-
1463-
In cases like this, the *result* of defining this as a [=mixin=] or [=macro=] are identical.
1464-
That is, one could equally write this slightly more verbose definition:
1465-
1466-
<xmp highlight=css>
1467-
@mixin --reset-list() {
1468-
@result {
1469-
margin: 0;
1470-
padding: 0;
1471-
list-style: none;
1472-
}
1473-
}
1474-
</xmp>
1475-
1476-
In more complex cases, though, their behaviors can differ;
1477-
see [[#mixin-macro-diff]].
1478-
</div>
1479-
1480-
1481-
<h3 id=macro-rule at-rule lt="@macro" export>
1482-
The ''@macro'' rule</h3>
1483-
1484-
The ''@macro'' rule defines a [=macro=],
1485-
and consists of a name
1486-
and a [=macro body=].
1487-
(Similar to a [=mixin result=] block.)
1488-
1489-
<pre class="prod def">
1490-
<<@macro>> = @macro <<dashed-ident>>
1491-
{
1492-
<<declaration-rule-list>>
1493-
}
1494-
</pre>
1495-
1496-
A ''@macro'' rule cannot be a [=nested group rule=];
1497-
it is invalid within the body of a [=style rule=].
1498-
1499-
<h4 id=macro-prelude>
1500-
The Macro Prelude</h4>
1501-
1502-
The name of the resulting [=macro=] is given by the <<dashed-ident>> in its prelude.
1503-
1504-
The name of a ''@macro'' rule is a [=tree-scoped name=],
1505-
and functions identically to a ''@mixin'' name.
1506-
[=Macros=] and [=mixins=] share the same namespace;
1507-
if two are defined with the same name,
1508-
the last one wins
1509-
(just like having two [=mixins=] with the same name).
1510-
1511-
1512-
<h4 id=macro-body dfn lt="macro body" export>
1513-
The Macro Body</h4>
1514-
1515-
The body of a ''@macro'' rule
1516-
acts as a [=nested declarations rule=],
1517-
and accepts the same properties and rules that a normal [=nested declarations rule=] would.
1518-
In particular, further [=mixins=] and [=macros=] can be invoked
1519-
(via the ''@apply'' rule)
1520-
within a ''@macro''.
1521-
1522-
Note: This is identical to the body of a [=mixin's=] ''@result'' rule.
1523-
As [=macros=] don't <em>have</em> any arguments or local variables,
1524-
they don't need to mark their result separately
1525-
to distinguish [=local variables=] from [=custom properties=]
1526-
that will get added to the element's style.
1527-
1528-
Unknown properties and rules are invalid and ignored,
1529-
but do not make the ''@macro'' rule itself invalid.
1530-
1531-
Identically to [=mixins=],
1532-
the body of a ''@macro'' rule
1533-
can contain a ''@contents'' rule,
1534-
which will substitute itself with a passed [=contents block=],
1535-
or potentially a fallback block.
1536-
1537-
<div class=example>
1538-
The ''@contents'' example provided in [[#contents-rule]]
1539-
used ''@mixin'',
1540-
but it could equally be written with ''@macro'',
1541-
as it does not use any arguments:
1542-
1543-
<pre highlight=css>
1544-
@macro --one-column {
1545-
@media (width <= 800px) {
1546-
@contents;
1547-
}
1548-
}
1549-
@macro --two-column {
1550-
@media (width > 800px) {
1551-
@contents;
1552-
}
1553-
}
1554-
body {
1555-
@apply --one-column {
1556-
display: flex;
1557-
flex-flow: column;
1558-
}
1559-
@apply --two-column {
1560-
display: grid;
1561-
grid-template-columns: 60px 60px;
1562-
}
1563-
}
1564-
</pre>
1565-
</div>
1566-
1567-
1568-
1569-
1570-
Using Mixins and Macros {#using-mixins}
1571-
=======================
1572-
1573-
The result of a [=mixin=] or [=macro=] application
1574-
is substituted into the body of another [=style rule=]
1575-
as a [=nested declarations rule=]
1576-
via the ''@apply'' rule.
1577-
1578-
<wpt>
1579-
mixins/apply-nested-declarations.html
1580-
mixins/mixin-cross-stylesheet.html
1581-
mixins/mixin-from-import-with-media-queries.html
1582-
mixins/mixin-from-import.html
1583-
</wpt>
15841423

15851424
<!-- Big Text: @apply
15861425

@@ -1596,7 +1435,7 @@ via the ''@apply'' rule.
15961435
<h3 id=apply-rule>
15971436
The <dfn>@apply</dfn> Rule</h3>
15981437

1599-
The ''@apply'' rule applies a [=mixin=] or [=macro=],
1438+
The ''@apply'' rule applies a [=mixin=],
16001439
causing it to substitute into the rule
16011440
in place of the ''@apply'' rule itself.
16021441

@@ -1642,9 +1481,9 @@ as they effectively modify the stylesheet itself.
16421481
adjust which properties and rules are active in a stylesheet
16431482
before styles are applied.)
16441483

1645-
The ''@apply'' rule applies the [=mixin=] or [=macro=]
1484+
The ''@apply'' rule applies the [=mixin=]
16461485
named by the <<dashed-ident>> or the <<dashed-function>>'s name.
1647-
If no such [=mixin=] or [=macro=] exists,
1486+
If no such [=mixin=] exists,
16481487
the ''@apply'' does nothing.
16491488

16501489
If passed a <<dashed-function>>,
@@ -1656,11 +1495,9 @@ the ''@apply'' application does nothing.
16561495
the missing arguments take their default values instead.)
16571496
A <<dashed-ident>> passes no arguments.
16581497
(That is, ''@apply --foo;'' is identical to ''@apply --foo();''.)
1659-
For these purposes, a [=macro=] is treated as having a zero-length argument list;
1660-
''@apply --my-macro();'' is valid.
16611498

16621499
If the ''@apply'' rule has a <<declaration-list>> block,
1663-
that block is passed as the [=mixin=] or [=macro=]'s [=contents block=].
1500+
that block is passed as the [=mixin=]'s [=contents block=].
16641501

16651502
<div class=example>
16661503
Applying a mixin without arguments, or with an empty argument list,
@@ -2115,129 +1952,6 @@ and other element-relative values resolve correctly.
21151952
</details>
21161953
</div>
21171954

2118-
Evaluating Macros {#eval-macro}
2119-
-----------------
2120-
2121-
[=Macros=] work similarly to [=mixins=],
2122-
in that they substitute their [=macro body=]
2123-
at the location they're ''@apply''&apos;d.
2124-
Contrary to [=mixins=],
2125-
[=macros=] are substituted simply and literally,
2126-
with no transforms performed on their content.
2127-
2128-
2129-
Mixin/Macro Differences {#mixin-macro-diff}
2130-
-----------------------
2131-
2132-
The basic differences between [=mixins=] and [=macros=] are obvious:
2133-
2134-
* [=Mixins=] can take arguments; [=macros=] can't.
2135-
* [=Mixins=] can have local variables, and wrap their result in ''@result'';
2136-
[=macros=] don't, and just use their contents directly as their result.
2137-
2138-
The subtler difference is that,
2139-
<em>because</em> [=mixins=] have arguments and local variables
2140-
which they don't want to "leak" into the page's general styles,
2141-
and which they want to resolve on the applying element
2142-
(so ''var()'' functions, ''em'' values, etc passed as arguments or set as locals
2143-
all work "as expected"),
2144-
they treat their results as [=scoped style rules=].
2145-
[=Macros=] don't impose this restriction,
2146-
which makes them useful in some cases where [=mixins=] can't be used,
2147-
but which also limits their abilities in some other cases.
2148-
2149-
<div class=example>
2150-
For example, the following mixin and macro are identical:
2151-
2152-
<xmp highlight=css>
2153-
@mixin --mix1() {
2154-
@result {
2155-
width: 20em;
2156-
> .bar {
2157-
width: 10em;
2158-
}
2159-
}
2160-
}
2161-
@macro --mac1() {
2162-
width: 20em;
2163-
> .bar {
2164-
width: 10em;
2165-
}
2166-
}
2167-
.foo {
2168-
@apply --mix1; /* or --mac1 */
2169-
font-size: 20px;
2170-
/* width is 20em, so 400px */
2171-
2172-
> .bar {
2173-
font-size: 10px;
2174-
/* width is 10em, so 100px;
2175-
}
2176-
}
2177-
</xmp>
2178-
2179-
Despite the mixin/macro *appearing* to set the ''.bar'' child
2180-
to half the ''.foo'' parent's width,
2181-
because they use ''em'' units and the elements have different 'font-size' values,
2182-
the child ends up 1/4 the width of the parent instead.
2183-
2184-
The mixin could be rewritten like this:
2185-
2186-
<xmp highlight=css>
2187-
@function --as-length(--x <length>) { result: var(--x); }
2188-
@mixin --mix2() {
2189-
--em: --as-length(1em);
2190-
@result {
2191-
width: 20--em; /* using custom units */
2192-
> .bar {
2193-
width: 10--em;
2194-
}
2195-
}
2196-
}
2197-
.foo {
2198-
@apply --mix2;
2199-
font-size: 20px;
2200-
/* width is 20--em and --em is 20px, so 400px */
2201-
2202-
> .bar {
2203-
font-size: 10px;
2204-
/* width is 10--em and --em is still 20px, so 200px;
2205-
}
2206-
}
2207-
</xmp>
2208-
2209-
...which resolves the ''--em'' local variable on ''.foo''
2210-
(to a length of ''20px''),
2211-
and then uses that in both places.
2212-
A [=macro=] cannot reproduce this,
2213-
unless you actually emit a ''--em'' [=custom property=] onto the element,
2214-
where styles <em>outside of</em> the macro could see it.
2215-
2216-
On the other hand, the following can be done with a [=macro=]:
2217-
2218-
<xmp highlight=css>
2219-
@macro --mac2() {
2220-
width: 20em;
2221-
+ .bar { /* sibling, not child! */
2222-
width: 10em;
2223-
}
2224-
}
2225-
.foo {
2226-
@apply --mac2;
2227-
font-size: 20px;
2228-
/* width is 20em, so 400px */
2229-
+ .bar { /* again, sibling! */
2230-
font-size: 10px;
2231-
/* width is 10em, so 100px;
2232-
}
2233-
}
2234-
</xmp>
2235-
2236-
A [=mixin=] can't reproduce this,
2237-
unless you lift it up to applying on a parent element,
2238-
with the mixin styling its two children.
2239-
</div>
2240-
22411955

22421956
<!-- Big Text: cssom
22431957

0 commit comments

Comments
 (0)