Skip to content

Commit 95b4254

Browse files
committed
[css-mixins-1][editorial] Move/improve examples. Add an explanatory section about args and locals.
1 parent c18e729 commit 95b4254

File tree

1 file changed

+103
-50
lines changed

1 file changed

+103
-50
lines changed

css-mixins-1/Overview.bs

Lines changed: 103 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,16 @@ Introduction {#intro}
102102
like:
103103

104104
<xmp highlight=css>
105-
@function --shadow(--color <color> : var(--shadow-color)) {
106-
result: 2px 2px var(--color, black);
105+
@function --shadow(--shadow-color <color> : inherit) {
106+
/* If --shadow-color argument isn't passed,
107+
or doesn't successfully parse as a <color>,
108+
try to use the --shadow-color *property*
109+
from the element instead */
110+
111+
/* var(--shadow-color) refers to the --shadow-color parameter,
112+
rather than a custom property,
113+
but can still use a fallback value as normal */
114+
result: 2px 2px var(--shadow-color, black);
107115
}
108116

109117
.foo {
@@ -129,8 +137,8 @@ Defining Custom Functions {#defining-custom-functions}
129137
======================================================
130138

131139
A [=custom function=] can be thought of as an advanced [=custom property=],
132-
which instead of being substituted by a single fixed value
133-
provides its substitution value based on [=function parameters=]
140+
which instead of being substituted by a single fixed value,
141+
computes its substitution value based on [=function parameters=]
134142
and the value of [=custom properties=] at the point it's invoked.
135143
Rather than the ''var()'' syntax that [=custom properties=] use for substitution,
136144
[=custom functions=] are invoked by <<dashed-function>> syntax,
@@ -180,7 +188,7 @@ A <dfn>function parameter</dfn> consists of a name (<<custom-property-name>>);
180188
optionally a <dfn>parameter type</dfn>, described by a [=syntax definition=];
181189
and optionally a <dfn>default value</dfn>.
182190

183-
<pre class="prod" nohighlight>
191+
<pre class="prod def" nohighlight>
184192
&lt;@function> = @function <<function-token>> <<function-parameter>>#? )
185193
[ returns <<css-type>> ]?
186194
{
@@ -268,6 +276,81 @@ The '@function/result' descriptor itself does not have a type,
268276
but its [=resolve function styles|resolved=] value is type checked
269277
during the [=substitute a dashed function|substitution=] of a <<dashed-function>>.
270278

279+
Arguments & Local Variables {#args}
280+
-----------------------------------
281+
282+
<em>This section is non-normative.</em>
283+
284+
Within a [=custom function's=] [=function body=],
285+
the ''var()'' function can access
286+
[=local variables=]
287+
(the [=custom properties=] defined in the [=function body=]),
288+
[=function parameters=]
289+
(the values passed to the function, or set to default values),
290+
and [=custom properties=] defined at the <em>call site</em>
291+
(an element, or another [=custom function=]).
292+
293+
In that list, earlier things "win" over later things of the same name--
294+
if you have a [=local variable=] named '--foo',
295+
''var(--foo)'' will be substituted by that [=local variable=],
296+
not by an argument or a custom property defined outside.
297+
(The other values can still be <em>accessed</em>, however:
298+
setting the '--foo' local variable to ''initial''
299+
will resolve it to the '--foo' parameter,
300+
while ''inherit'' will resolve it
301+
to the '--foo' custom property from the call site.)
302+
303+
<div class='example'>
304+
A [=custom function=] can access [=local variables=]
305+
and [=function parameters=]
306+
from functions higher up in the call stack:
307+
308+
<xmp class='lang-css'>
309+
@function --outer(--outer-arg) {
310+
--outer-local: 2;
311+
result: --inner();
312+
}
313+
@function --inner() returns <number> {
314+
result: calc(var(--outer-arg) + var(--outer-local));
315+
}
316+
div {
317+
z-index: --outer(1); /* 3 */
318+
}
319+
</xmp>
320+
321+
Similarly, [=custom properties=] are implicitly available:
322+
323+
<xmp class='lang-css'>
324+
@function --double-z() returns <number> {
325+
result: calc(var(--z) * 2);
326+
}
327+
div {
328+
--z: 3;
329+
z-index: --double-z(); /* 6 */
330+
}
331+
</xmp>
332+
333+
But [=function parameters=] "shadow" [=custom properties=],
334+
and [=local variables=] "shadow" both:
335+
336+
<xmp class='lang-css'>
337+
@function --add-a-b-c(--b, --c) {
338+
--c: 300;
339+
result: calc(var(--a) + var(--b) + var(--c));
340+
/* uses the --a from the call site's custom property,
341+
the --b from the function parameter,
342+
and the --c from the local variable */
343+
}
344+
div {
345+
--a: 1;
346+
--b: 2;
347+
--c: 3;
348+
z-index: --add-a-b-c(20, 30); /* 321 */
349+
}
350+
</xmp>
351+
352+
</div>
353+
271354

272355
<!-- Big Text: using
273356

@@ -359,6 +442,21 @@ and the substitution is taking place on a property's value,
359442
then the declaration containing the <<dashed-function>> becomes
360443
[=invalid at computed-value time=].
361444

445+
<div class='example'>
446+
A [=comma-containing productions|comma-containing value=]
447+
may be passed as a single argument
448+
by wrapping the value in curly braces, <code>{}</code>:
449+
450+
<pre class='lang-css'>
451+
@function --max-plus-x(--list, --x) {
452+
result: calc(max(var(--list)) + var(--x));
453+
}
454+
div {
455+
width: --max-plus-x({ 1px, 7px, 2px }, 3px); /* 10px */
456+
}
457+
</pre>
458+
</div>
459+
362460
Evaluating Custom Functions {#evaluating-custom-functions}
363461
----------------------------------------------------------
364462

@@ -477,52 +575,7 @@ with its [=function parameters=] overriding "inherited" custom properties of the
477575
will be used from these styles.
478576
</div>
479577

480-
<div class='example'>
481-
A [=comma-containing productions|comma-containing value=]
482-
may be passed as a single argument
483-
by wrapping the value in curly braces, <code>{}</code>:
484-
485-
<pre class='lang-css'>
486-
@function --max-plus-x(--list, --x) {
487-
result: calc(max(var(--list)) + var(--x));
488-
}
489-
div {
490-
width: --max-plus-x({ 1px, 7px, 2px }, 3px); /* 10px */
491-
}
492-
</pre>
493-
</div>
494-
495-
<div class='example'>
496-
A [=custom function=] can access [=local variables=]
497-
and [=function parameters=]
498-
from functions higher up in the call stack:
499-
500-
<pre class='lang-css'>
501-
@function --foo(--x) {
502-
--y: 2;
503-
result: --bar();
504-
}
505-
@function --bar() returns &lt;number&gt; {
506-
result: calc(var(--x) + var(--y));
507-
}
508-
div {
509-
z-index: --foo(1); /* 3 */
510-
}
511-
</pre>
512-
513-
Similarly, [=custom properties=] are implicitly available:
514578

515-
<pre class='lang-css'>
516-
@function --double-z() returns &lt;number&gt; {
517-
result: calc(var(--z) * 2);
518-
}
519-
div {
520-
--z: 3;
521-
z-index: --double-z(); /* 6 */
522-
}
523-
</pre>
524-
525-
</div>
526579

527580

528581
Cycles {#cycles}

0 commit comments

Comments
 (0)