Title: CSS Conditional Values Module Level 1
Shortname: css-conditional-values
Level: 1
Status: UD
ED: https://drafts.csswg.org/css-conditional-values-1/
Group: csswg
Work Status: exploring
Editor: Lea Verou, Invited Expert, http://lea.verou.me/about, w3cid 52258
Abstract: This module explores additions to CSS to enable conditional values.
Repository: w3c/csswg-drafts
Default Highlight: css
Inline Github Issues: title
Warning: Not Ready
Introduction {#intro} ===================== This section is not normative. Note: This section is a stub and needs to be expanded. Authors frequently need to set a property to different values based on the relation between certain values. High level custom properties {#high-level-custom-properties} ----------------------------- A web component may support several custom properties which do not contain a value fragment verbatim, but set several properties across multiple rules indirectly. For example, a `--size` property with values `small`, `medium`, `large`, or an `--alignment` property with values `horizontal` and `vertical`. Relation between units of the same type {#relation-between-units} --------------------------------------- Author code often needs to branch based on the relation between different units of the same type. For example: * Comparing viewport and absolute units allows compact one-off viewport dimension media queries * Comparing font-relative units and absolute <> units allows authors to apply different styling for small and large font sizes, enhancing readability. Value Definitions {#values} --------------------------- This specification follows the CSS property definition conventions from [[!CSS2]] using the value definition syntax from [[!CSS-VALUES-3]]. Value types not defined in this specification are defined in CSS Values & Units [[!CSS-VALUES-3]]. Combination with other CSS modules may expand the definitions of these value types. In addition to the property-specific values listed in their definitions, all properties defined in this specification also accept the CSS-wide keywords as their property value. For readability they have not been repeated explicitly. Boolean data types {#boolean} ============================== Boolean constants: ''true'' and ''false'' {#bool-constants} --------------------------------------------------------------
	<boolean-constant> = 'true' | 'false'
Logical comparisons: The <> type {#condition} ---------------------------------------------
	<condition> = not <>
	                     | <> [ and <> ]*
	                     | <> [ or <> ]*
	<condition-in-parens> = ( <> ) | <>
	<atomic-condition> = <> <> <> | <>
	<comparison-operand> = <> | <> | <> | <>
	<comparison-operator> = [ '=' | '>=' | '>' | '<' | '<=' ]
Note: A future version of this module may expand <> to complex types, such as colors. <> values are logical expressions that resolve to a <> by performing simple comparisons and following basic boolean operators. When using `and` or `or` operators, precedence must be enforced with parentheses. The `not` operator does not require this, and has higher precedence than `and` and `or`. Each of these grammar terms is associated with a boolean result, as follows: : <> : <> :: The result is the result of the child subexpression. : not <> :: The result is the negation of the <> term. The negation of unknown is unknown. : <> [ and <> ]* :: The result is true if all of the <> child terms are true, false if at least one of the <> is false, and unknown otherwise. : <> [ or <> ]* :: The result is false if all of the <> child terms are false, true if at least one of the <> is true, and unknown otherwise. These rules are consistent with the way conditions are resolved in [[css-conditional-3]]. Issue: Together with this, there are currently 3 specs ([[css-conditional-3]], [[mediaqueries-4]]) using boolean operators, and two defining how they work ([[css-conditional-3]] and this). Ideally, this should be defined in one place, and cited everywhere else. Both <> values in <> need to be of the same type. If they are not, the entire condition becomes an invalid condition and evaluates to unknown. These operations are only defined on computed values. (As a result, it is not necessary to define, for example, how to compare a <> value of ''15pt'' with ''5em'' since such values will be resolved to their canonical unit before being passed to any of the above procedures.)
For example, ''5px > 4deg'' is an invalid condition because the first operand is a <> and the second is an <>.
The host syntax defines how relative values (such as percentages or em units) are resolved in <>. When <> is used in a declaration, these relative values resolve in the same way as regular values in the declaration property. Note: Why are we using ''='' for equality and not ':' as is established in [[css-conditional-4]] already? Because a lot of third party code (syntax highlighters etc) assumes that colons separate declarations and would break. Also, `foo: bar` establishes a key-value pair, whereas in equality comparisons the two operands are of equal weight, and do not establish a key-value pair. Issue: Do we need a "not equals" operator or is 'not(op1 = op2)' sufficient? The <> is resolved at computed value time, though its calculation tree may be simplified earlier.
For example, ''(5px > 4px) and (1em = 2em)'' can be simplified to ''(true) and (false)'' and then to ''false'' at parse time and serialized as such.

Computed Value

The [=computed value=] of a <> value is its [=calculation tree=] simplified, using all the information available at [=computed value=] time. (Such as the ''em'' to ''px'' ratio, how to resolve percentages etc.) Where percentages are not resolved at computed-value time, they are not resolved in <>. The [=calculation tree=] is again simplified at [=used value=] time; with [=used value=] time information, a <> always simplifies down to a single <>. Issue: Define these concepts for comparisons (currently they point to calc()) Inline conditionals: The ''if()'' function {#if} =========================================== The if() function allows authors to set a property value (or parts thereof) to different values based on certain conditions.

<> = if( <>, <> [, <>]?)
<consequent> = <>
<antecedent> = <>
Issue: How can authors specify consequents or antecedents that include commas? Alternative syntax that addresses this, though may be hard to read when consequent/antecedent are keywords:
<>  = if( <> then <> [else <>]?)
<> = <>
<> = <>
Authors can write inline media queries by comparing viewport and absolute units:
flex-flow: if(100vw > 500px, row, column);
When <> is omitted, it defaults to ' ' (empty value).
This allows authors to use conditionals to toggle certain parts of a value and even "compose" a property value from a series of conditionals:
background: if(var(--raised) = on, linear-gradient(white, transparent)) hsl(200 100% 50%);
This also allows authors to write multiple branches for the same value side by side instead of deeply nesting them:
		font-size: if(var(--size) = small, 2em)
		           if(var(--size) = medium, 3em)
		           if(var(--size) = large, 5em)
	
If after substitution of all ''if()'' values in a property value, the resulting declaration is invalid, the property containing the ''if()'' function is invalid at computed-value time. When ''if()'' is used in shorthands, it has the same behavior as the ''var()'' function, for the same reasons. Issue: How to disambiguate when used in a place where arguments are disambiguated by type? Unlike ''var()'', this cannot just be resolved at substitution, because we need to be able to interpret the values to compute the condition and perform the substitution accordingly. One way to address this would be to mandate that both <> and <> need to be of the same type. How much does that limit use cases? Privacy and Security Considerations {#priv-sec} =============================================== This specification defines a purely author-level mechanism for specifying conditionals on styling information within a page they control. As such, there are no new privacy considerations.