This helpers converts input classes and state attributes into runtime dynamic classes from the optimizer.
The arguments that are passed in come from the rewriter; this helper is never invoked directly and as such the api it provides is not optimized for human consumption. but rather, for speed of evaluation.
Arguments: 1: number of source expressions 2: number of output expressions
Source Expression: 1: type: 1 - boolean, 2 - string switch, 3, boolean ternary The rest of the arguments for each source expression vary by type:
Boolean: 1: boolean type: 1 - ast expression, 2 - dependency, 3 - both 1 & 2. if boolean type is a dependency: b1: number (D) of style indexes this is dependent on. b2..(bD): style indexes that must be set for this to be true if ast expression: (4+d): ast expression result to evaluate as truthy Then: 2: number (s) of source styles set by the expression (s0...sN): source style indexes that are set if true.
String switch:
1: conditional type: 4 - switch, 5 - both switch and dependency
if conditional type has a dependency:
2: number (d) of style indexes this is dependent on.
3..((3)+d-1): style indexes that must be set for this to be true
1: number (n) of strings that can be returned
2: whether a falsy value is an error (0), unsets the values (1)
or provide a default (2) if a string
3?: the default value if the falsy behavior is default (2)
then: expression to evaluate as a string
For each of the
Boolean Ternary: 1: number (t) of source styles set if true 2: number (f) of source styles set if false 3: expression to evaluate as truthy 4..(4+t-1): indexes of source styles set if true (4+t)..(4+t+f-1): indexes of source styles set if false
Output expresions: 1: classname to set if output expression is true 2: the start of an output boolean expression
Output Boolean expressions:
1: expression type - one of the following:
-1: negate next expression that follows
-2: compute a logical 'or' of the next
The remaining indexes are processed 'recursively' as output expressions
Please note: this helper can currently process expressions that are not produced by the analyzer/rewriter. This includes:
Generated using TypeDoc
CSS Blocks Runtime for Dynamic Classnames
This runtime helper provides efficient and terse evaluation of CSS Block's styles that have been optimized by OptiCSS.
The dynamic expression can be generated from many different authoring formats, it expresses a wide-range of dynamic changes that can happen to block styles.
It's easiest to understand how it works from an example.
Consider the following
objstr
expression:let style = objstr({ [bar.pretty]: leSigh, [bar.pretty.bool()]: true, [bar.pretty.color(dynamic)]: true });
which becomes:
c$$([ 3, 2, 0, leSigh, 1, 0, 0, 1, 1, 0, 1, 1, 5, 1, 0, 1, 0, dynamic, "yellow", 1, 2, "c", -2, 2, 0, 1, "d", 2])
Let's break that down:
3
) is how many input dynamic input styles there are.2
) is how many dynamic output styles there are.For each conditional input style (The first number is what kind of conditional it is the arguments that follow depend on the type.)
0, leSigh, 1, 0, 0
- This (0
) is a ternary expression with the boolean conditional ofleSigh
. There is1
style set on (the style with index0
) if true and (0
) styles if false.1, 1, 0, 1, 1
is a dependency on a previously evaluated condition. Dependencies are a bit1
that can be set on any other type of conditional or used stand-alone). In this case it is a pure dependency. This style depends on1
other style (the style with index0
) and if all of those styles are set to on, it allows the conditional expression that follows to set its styles to on. Since there is no conditional for a pure dependency it causes the1
style that follows (the style with index1
) to be turned on.5, 1, 0, 1, 0, dynamic, "yellow", 1, 2
is a switch conditional with a dependency5 = 4 | 1
. The dependency comes first so1, 0
causes the entire switch to depend on1
previously set style, the style with index0
. This switch statement has1
string to compare against and if the value of the string is falsey an error (0
) should be raised (other values let a falsey value disable the style or provide a default string to assume). The expressiondynamic
is evaluated, checked if it's falsey then and compared against"yellow"
and if it matches1
style is set to on (the style with index2
).For each output style, the style comes first, then a boolean expression over source input styles for each follows. a boolean expression is arbitrarily long and nested with other boolean expressions.
"c", -2, 2, 0, 1
- sets the classc
with an "or" (-2
) expression. The start of a boolean expression is signaled by a negative number and then a count of expressions or styles that are combined with that boolean operator. So this is an or of2
input styles (with indexes0
and1
) -- if either of those are set, the boolean expression will be true. below you will see this is because both of those classes setcolor: red
."d", 2
- is a simple expression with no boolean operator. The classc
is present if the style with index2
is on.This isn't comprehensive, but hopefully it generally makes it more clear what is going on. More documentation can be found in the code.