@@ -102,8 +102,16 @@ Introduction {#intro}
102
102
like:
103
103
104
104
<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);
107
115
}
108
116
109
117
.foo {
@@ -129,8 +137,8 @@ Defining Custom Functions {#defining-custom-functions}
129
137
======================================================
130
138
131
139
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=]
134
142
and the value of [=custom properties=] at the point it's invoked.
135
143
Rather than the ''var()'' syntax that [=custom properties=] use for substitution,
136
144
[=custom functions=] are invoked by <<dashed-function>> syntax,
@@ -180,7 +188,7 @@ A <dfn>function parameter</dfn> consists of a name (<<custom-property-name>>);
180
188
optionally a <dfn>parameter type</dfn> , described by a [=syntax definition=] ;
181
189
and optionally a <dfn>default value</dfn> .
182
190
183
- <pre class="prod" nohighlight>
191
+ <pre class="prod def " nohighlight>
184
192
<@function> = @function <<function-token>> <<function-parameter>> #? )
185
193
[ returns <<css-type>> ]?
186
194
{
@@ -268,6 +276,81 @@ The '@function/result' descriptor itself does not have a type,
268
276
but its [=resolve function styles|resolved=] value is type checked
269
277
during the [=substitute a dashed function|substitution=] of a <<dashed-function>> .
270
278
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
+
271
354
272
355
<!-- Big Text: using
273
356
@@ -359,6 +442,21 @@ and the substitution is taking place on a property's value,
359
442
then the declaration containing the <<dashed-function>> becomes
360
443
[=invalid at computed-value time=] .
361
444
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
+
362
460
Evaluating Custom Functions {#evaluating-custom-functions}
363
461
----------------------------------------------------------
364
462
@@ -477,52 +575,7 @@ with its [=function parameters=] overriding "inherited" custom properties of the
477
575
will be used from these styles.
478
576
</div>
479
577
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 <number> {
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:
514
578
515
- <pre class='lang-css'>
516
- @function --double-z() returns <number> {
517
- result: calc(var(--z) * 2);
518
- }
519
- div {
520
- --z: 3;
521
- z-index: --double-z(); /* 6 */
522
- }
523
- </pre>
524
-
525
- </div>
526
579
527
580
528
581
Cycles {#cycles}
0 commit comments