1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
# Functions
```typescript
type custom-property-name = `--${string}`
type selector = string // Any css selector or identifier (used as id)
type condition = string // empty string, 0, 'false', "'false'" and "\"false\"" are all false, the rest are fine. Don't ask.
type duration = number | `${number}ms` | `${number}s`
type pair = string: any
```
===
## Core
### get-var
Get css custom property from an element. Basically var but evaluated lazily.
NOTE: Avoid using `var` inside cssx expressions.
```typescript
function get-var(custom-property-name): string
function get-var(custom-property-name, default-value): string
```
Example -
```css
#my-element {
--cssx-text: get-var(--some-variable);
}
```
#### update
Update a css custom property on an element
```typescript
function update(custom-property-name, string): void
function update(selector, custom-property-name, string): string
```
Example -
```css
#my-element {
--cssx-on-click: update(some-el, --text, attr(input-element, 'value'));
--cssx-children: input#input-element #some-el;
}
#some-el { --text: "default"; }
#some-el::after { content: var(--text); }
```
### if
If expression. You know how this one goes. If truthy, it'll pick the second argument, else the third.
```typescript
function if(condition, any, any): any
```
Example -
```css
#my-element {
--boolean: false;
--cssx-on-update:
update(background-color,
if(
get-var(--boolean)
'DarkGoldenRod',
'PapayaWhip'
)
);
}
```
### map
A constructor function to create a map of key value pairs.
```typescript
function map(...pair): map
```
Example -
```css
#my-element {
--cssx-children: instance(
div#some-element,
map(--prop1: "hello", --prop2: "world")
);
}
```
### seq
> WIP Docs
### call
Call a "function". A function is any series of expressions defined in a css custom property.
NOTE: Every function call creates a new dom node for computing the result. Dom nodes are the call stack.
```typescript
function call(var-identifier, map): any
```
Example -
```css
#my-element {
--factorial: if(
js-eval(string(get-var(--n), '> 1')),
js-eval(string(
get-var(--n),
' * ',
call(--factorial, map(--n: js-eval(string(get-var(--n), ' - 1'))))
)),
1
);
--cssx-on-mount: js-eval(string(
'console.log("',
call(--factorial, map(--n: 5)),
'")'
));
}
```
Or let's just go nuts with functions.
Because we use named properties as arguments and css is cascading, all named properties are implicitly available to everything getting called.
> Not a design choice, a design consequence.
So `--left` and `--right` is implicitly available to `--binary-op`.
NOTE: `func` in `--binary-op` doesn't do anything. It's to make the developer feel better.
```css
#my-element {
--binary-op:
func(--left, --op, --right)
js-eval(string(get-var(--left), get-var(--op), get-var(--right)));
--greater-than: call(--binary-op, map(--op: ' >= '));
--minus: call(--binary-op, map(--op: ' - '));
--multiply: call(--binary-op, map(--op: ' * '));
--factorial: if(call(--greater-than, map(--left: get-var(--n), --right: 1)),
call(--multiply, map(
--left: get-var(--n),
--right: call(--factorial,
map(--n: call(--minus,
map(--left: get-var(--n), --right: 1)))
)
)),
1
);
}
```
### string
Concatenate strings together / Cast a value to string explicitly
```typescript
function string(...string): string
```
```css
#my-element {
--log-stuff: 'Stuff to log to console';
--cssx-on-mount: js-eval(string('console.log("', get-var(--log-stuff), '")'));
}
```
### quotify
Add quotes around a value
```typescript
function quotify(string): `'${string}'`
```
```css
#my-element {
--log-stuff: 'Stuff to log to console';
--cssx-on-mount: js-eval(string('console.log(', quotify(get-var(--log-stuff)), ')'));
}
```
### unquotify
Remove quotes from a value. No example here, use your imagination.
```typescript
function quotify(string): string
```
### do
Evaluate a series of expressions in sequence and return the last value.
```css
#my-element {
--cssx-on-mount:
if(get-var(--some-boolean),
do(
add-class(loading),
delay(1s),
remove-class(loading)
),
noop(),
)
;
}
```
### try
The standard try/catch as an expression. The error expression is scoped and gets access to a `--error` value.
```css
form#my-form {
--cssx-on-submit:
prevent-default()
add-class(form, 'submitting')
try(
do(
request('/your-api/some-api', 'POST'),
add-class(form, 'submitted')
),
js-eval(string('alert("', get-var(--error), '")'))
)
remove-class(form, 'submitting')
;
}
```
### let
Create a binding for use inside a scoped expression.
`--random` is only available within the let binding
```css
#my-element {
--cssx-on-mount:
let(--random, js-eval('Math.random()'),
js-eval('alert("', get-var(--random),'")')
)
;
}
```
===
## Others
### js-eval
Evaluate any js expression. Easy escape hatch directly to hell.
```typescript
function js-eval(string): string
```
### request
> WIP Docs
### delay
Wait a bit.
```typescript
function delay(duration): void
```
Examples for input -
- `delay(100)`: wait for 100 milliseconds
- `delay(100ms)`: wait for 100 milliseconds
- `delay(5s)`: wait for 5 seconds
- `delay(0.5s)`: wait for 500 milliseconds
===
## DOM
### load-cssx
Load more of this abomination into your page
### set-attr
### attr
### add-children
### remove-element
### prevent-default
### call-method
|