Skip to content

Commit c64f0b1

Browse files
committed
[css-env] Add first draft of Environment Variables.
1 parent eb406bd commit c64f0b1

1 file changed

Lines changed: 155 additions & 0 deletions

File tree

css-env-1/Overview.bs

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<pre class='metadata'>
2+
Title: CSS Environment Variables Module Level 1
3+
Shortname: css-env
4+
Level: 1
5+
Status: UD
6+
Group: CSSWG
7+
URL: https://drafts.csswg.org/css-env-1/
8+
Editor: Tab Atkins-Bittner, Google
9+
Editor: Dean Jackson, Apple
10+
Abstract: This specification defines the concept of [=environment variables=] and the ''env()'' function, which work similarly to custom properties and the ''var()'' function, but are defined globally for a document. These can be defined either by the User Agent, providing values that can be used on the page based on information the UA has special access to, or provided by the author for "global" variables that are guaranteed to be the same no matter where in the document they're used.
11+
</pre>
12+
13+
Introduction {#intro}
14+
=====================
15+
16+
The [[css-variables-1]] specification defined the concept of "cascading variables",
17+
author-defined variables created from the value of [=custom properties=],
18+
capable of being substituted into arbitrary other properties via the ''var()'' function.
19+
20+
This specification defines a related, but simpler, concept of [=environment variables=].
21+
Unlike "cascading variables",
22+
which can change thruout the page as their corresponding [=custom property=] takes on different values,
23+
an [=environment variable=] is "global" to a particular document--
24+
its value is the same everywhere.
25+
The ''env()'' function can then be used to substitute the value into arbitrary locations,
26+
similar to the ''var()'' function.
27+
28+
These "global" variables have both benefits and downsides versus cascading variables:
29+
30+
* Many variables aren't meant to change over the course of a page;
31+
they set up themes,
32+
or are helpers for particular numerical values.
33+
Using [=environment variables=] instead of [=custom properties=] to define these
34+
communicates the proper intent,
35+
which is good both for the author of the document
36+
(particularly when multiple people are collaborating on a single document),
37+
and for the user agent,
38+
as it can store these variables in a more optimal way.
39+
* Because [=environment variables=] don't depend on the value of anything drawn from a particular element,
40+
they can be used in places where there is no obvious element to draw from,
41+
such as in ''@media'' rules,
42+
where the ''var()'' function would not be valid.
43+
* Information from the User Agent itself,
44+
such as the margin of the viewport to avoid laying out in by default
45+
(for example, to avoid overlapping a "notch" in the screen),
46+
can be retrieved via ''env()'',
47+
whereas the element-specific nature of ''var()'' was not an appropriate place to pipe that information in.
48+
49+
Environment Variables {#environment}
50+
====================================
51+
52+
A CSS <dfn for=CSS>environment variable</dfn> is a name associated with a <<declaration-value>>
53+
(a sequence of zero more CSS tokens, with almost no restrictions on what tokens can exist),
54+
similar to a [=custom property=].
55+
[=Environment variables=] can be defined by the User Agent,
56+
or by the user.
57+
(In the latter case, the names are <<custom-property-name>>s,
58+
and start with `--` per standard for custom identifiers.)
59+
60+
Issue: Is the set of UA-defined [=environment variables=] visible to script?
61+
If so, define an API on {{Document}} to expose them.
62+
63+
Issue: Define how authors can add [=environment variables=],
64+
preferably both via JS
65+
and via CSS.
66+
Note that mixing CSS rules and JS-defined stuff can easily get messy,
67+
as demonstrated by CSSFontFaceRule vs FontFace...
68+
69+
The following UA-defined [=environment variables=] are officially defined and must be supported,
70+
tho user agents may define additional undocumented [=environment variables=]:
71+
72+
* Issue: define list of predefined vars
73+
74+
Using Environment Variables: the ''env()'' notation {#env-function}
75+
===================================================================
76+
77+
In order to substitute the value of an [=environment variable=] into a CSS context,
78+
use the ''env()'' function:
79+
80+
<pre class=prod>
81+
<dfn function>env()</dfn> = env( <<custom-ident>> , <<declaration-value>>? )
82+
</pre>
83+
84+
The ''env()'' function can be used in place of any part of a value in any property on any element,
85+
or any part of a value in any descriptor on any [=at-rule=],
86+
and in several other places where CSS values are allowed.
87+
88+
<div class=issue>
89+
Define the full set of places ''env()'' can be used.
90+
91+
* Should be able to replace any subset of MQ syntax, for example.
92+
* Should be able to replace selectors, maybe?
93+
* Should it work on a rule level,
94+
so you can insert arbitrary stuff into a rule,
95+
like reusing a block of declarations?
96+
</div>
97+
98+
The first argument to ''env()'' provides the name of an [=environment variable=] to be substituted.
99+
The second argument, if provided, is a fallback value,
100+
which is used as the substitution value
101+
when the referenced [=environment value=] does not exist.
102+
103+
Note: The syntax of the fallback, like that of custom properties, allows commas.
104+
For example, ''env(foo, red, blue)'' defines a fallback of ''red, blue'';
105+
that is, anything between the first comma and the end of the function is considered a fallback value.
106+
107+
If a property contains one or more ''env()'' functions,
108+
and those functions are syntactically valid,
109+
the entire property's grammar must be assumed to be valid at parse time.
110+
It is only syntax-checked at computed-time,
111+
after ''env()'' functions have been [=substituted=].
112+
113+
If a descriptor contains one or more ''env()'' functions,
114+
and those functions are syntactically valid,
115+
the entire declaration's grammar must be assumed to be valid at parse time.
116+
It is only syntax-checked after ''env()'' functions have been [=substituted=].
117+
118+
<div algorithm>
119+
To <dfn export local-lt=substitute>substitute an env()</dfn> in a property or descriptor:
120+
121+
1. If the name provided by the first argument of the ''env()'' function
122+
is a recognized [=environment variable=],
123+
replace the ''env()'' function by the value of the named [=environment variable=].
124+
125+
2. Otherwise, if the ''env()'' function has a fallback value as its second argument,
126+
replace the ''env()'' function by the fallback value.
127+
If there are any ''env()'' references in the fallback,
128+
[=substitute=] them as well.
129+
130+
3. Otherwise, the property or descriptor containing the ''env()'' function is [=invalid at computed-value time=].
131+
</div>
132+
133+
Issue: Define when substitution happens.
134+
It has to be before ''var()'' substitution.
135+
Alternately, should ''env()'' substitution happen at parse time,
136+
so unknown variable names cause it to fail syntax checking?
137+
There's no particular reason to have it happen at computed-value time,
138+
like ''var()'' does--
139+
that was to ensure that [=custom properties=] could inherit their value down
140+
before they were picked up by a ''var()''.
141+
142+
Issue: When I figure out where else ''env()'' can go,
143+
define how/when it substitutes.
144+
145+
Environment Variables in Shorthand Properties {#env-in-shorthands}
146+
------------------------------------------------------------------
147+
148+
Issue: If ''env()'' substitution happens during parsing,
149+
then this is unnecessary.
150+
151+
The ''env()'' function causes the same difficulties with [=shorthand properties=]
152+
as the ''var()'' function does.
153+
When an ''env()'' is used in a [=shorthand property=],
154+
then,
155+
it has the same effects as defined in [[css-variables-1#variables-in-shorthands]].

0 commit comments

Comments
 (0)