Source file var.ml
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
module Css = Cascade.Css
type layer = Theme | Utility
let properties_layer_override = function
| "--tw-ring-offset-width" -> Some "0px"
| _ -> None
type 'a property_info = {
initial : 'a option;
inherits : bool;
universal : bool;
}
let property_info ?initial ?(inherits = false) ?(universal = false) () =
{ initial; inherits; universal }
type _ role =
| Theme : [ `Theme ] role
| Property_default : [ `Property_default ] role
| Channel : [ `Channel ] role
| Ref_only : [ `Ref_only ] role
type ('a, 'r) t = {
kind : 'a Css.kind;
name : string;
role : 'r role;
binding : ?fallback:'a Css.fallback -> 'a -> Css.declaration * 'a Css.var;
property : 'a property_info option;
fallback : 'a option;
meta : Css.meta;
}
type 'a theme = ('a, [ `Theme ]) t
type 'a property_default = ('a, [ `Property_default ]) t
type 'a channel = ('a, [ `Channel ]) t
type 'a ref_only = ('a, [ `Ref_only ]) t
type family =
[ `Border
| `Rotate
| `Skew
| `Scale
| `Translate
| `Gradient
| `Shadow
| `Inset_shadow
| `Ring
| `Inset_ring
| `Leading
| `Font_weight
| `Duration
| `Tracking
| `Content
| `Text_shadow
| `Filter
| `Drop_shadow
| `Backdrop_filter ]
type info =
| Info : {
name : string;
kind : 'a Css.kind option;
property : 'a property_info option;
order : (int * int) option;
property_order : int option;
family : family option;
runtime : bool;
default_css : string option;
}
-> info
let (meta_of_info : info -> Css.meta), (info_of_meta : Css.meta -> info option)
=
Css.meta ()
type metadata = info
let metadata_name (Info info) = info.name
let metadata_order (Info info) = info.order
let metadata_property_order (Info info) = info.property_order
let metadata_family (Info info) = info.family
let metadata_needs_property (Info info) = info.property <> None
let metadata_default_css (Info info) = info.default_css
let metadata_of_var v = Option.bind (Css.var_meta v) info_of_meta
let metadata_of_declaration declaration =
Option.bind (Css.meta_of_declaration declaration) info_of_meta
let metadata var =
match info_of_meta var.meta with
| Some metadata -> metadata
| None -> assert false
let layer_name = function (Theme : layer) -> "theme" | Utility -> "utilities"
let v : type a r.
a Css.kind ->
?property:a property_info ->
?order:int * int ->
?property_order:int ->
?family:family ->
?fallback:a ->
?runtime:bool ->
role:r role ->
string ->
layer:layer ->
(a, r) t =
fun kind ?property ?order ?property_order ?family ?fallback ?runtime ~role name
~layer ->
(match (layer, order) with
| Theme, None ->
failwith ("Variable '" ^ name ^ "' in theme layer must have an order")
| _ -> ());
let info =
Info
{
kind = Some kind;
name;
property;
order;
property_order;
family;
runtime = Option.value runtime ~default:false;
default_css = None;
}
in
let binding ?fallback:fb value =
let meta = meta_of_info info in
let layer_name = Some (layer_name layer) in
let actual_fallback =
match ((role : r role), fallback, fb) with
| Ref_only, Some f, _ ->
Css.Fallback f
| _, _, Some f -> f
| _ -> Css.None
in
Css.var ~default:value ~fallback:actual_fallback ?layer:layer_name ~meta
?runtime name kind value
in
{ kind; name; role; binding; property; fallback; meta = meta_of_info info }
let theme kind ?runtime name ~order =
v kind ?property:None ?order:(Some order) ?runtime ~role:Theme name
~layer:Theme
let property_default kind ~initial ?(inherits = false) ?(universal = false)
?property_order ?family name =
let property = property_info ~initial ~inherits ~universal () in
v kind ~property ?property_order ?family ~role:Property_default name
~layer:Utility
let channel ?(needs_property = false) ?property_order ?family kind name =
if needs_property then
let property =
property_info ?initial:None ~inherits:false ~universal:true ()
in
v kind ~property ?property_order ?family ~role:Channel name ~layer:Utility
else v kind ?property_order ?family ~role:Channel name ~layer:Utility
let string_of_kind_value : type a. a Css.kind -> a -> string =
fun kind value ->
match kind with
| Css.Color -> Css.Pp.to_string ~minify:true Css.Values.pp_color value
| Css.Gradient_position ->
Css.Pp.to_string ~minify:true Css.Properties.pp_gradient_position value
| _ -> Css.Properties.string_of_kind_value kind value
let property_universal : type a.
name:string -> a Css.kind -> a option -> inherits:bool -> Css.t =
fun ~name kind initial ~inherits ->
let open Css in
match initial with
| None -> property ~name Universal ~inherits ()
| Some v -> (
match (kind, v) with
| Gradient_stop, (List [] : Css.gradient_stop) ->
property ~name Universal ~inherits ()
| Percentage, (Pct 0. : Css.percentage) ->
property ~name Universal ~inherits ()
| Gradient_direction, (To_bottom : Css.gradient_direction) ->
property ~name Universal ~inherits ()
| Gradient_position, (Linear_position To_bottom : Css.gradient_position)
->
property ~name Universal ~inherits ()
| _ ->
let initial_str = string_of_kind_value kind v in
property ~name Universal ~initial_value:initial_str ~inherits ())
let property_typed : type a.
name:string -> a Css.kind -> a option -> inherits:bool -> Css.t =
fun ~name kind initial ~inherits ->
let open Css in
match (kind, initial) with
| Length, None -> property ~name Universal ~inherits ()
| Length, Some v -> property ~name Length ~initial_value:v ~inherits ()
| Float, None -> property ~name Percentage ~inherits ()
| Float, Some v ->
property ~name Percentage ~initial_value:(Pct v) ~inherits ()
| Color, None -> property ~name Universal ~inherits ()
| Color, Some v -> property ~name Color ~initial_value:v ~inherits ()
| Percentage, None -> property ~name Percentage ~inherits ()
| Percentage, Some v ->
property ~name Percentage ~initial_value:v ~inherits ()
| Length_percentage, None -> property ~name Length_percentage ~inherits ()
| Length_percentage, Some v ->
property ~name Length_percentage ~initial_value:v ~inherits ()
| Number_percentage, None -> property ~name Universal ~inherits ()
| Number_percentage, Some v ->
let initial_str = string_of_kind_value kind v in
property ~name Universal ~initial_value:initial_str ~inherits ()
| Gradient_stop, None -> property ~name Universal ~inherits ()
| Gradient_stop, Some (List [] : Css.gradient_stop) ->
property ~name Universal ~inherits ()
| Gradient_stop, Some v ->
property ~name Universal
~initial_value:(string_of_kind_value kind v)
~inherits ()
| _, None -> property ~name Universal ~inherits ()
| _, Some v ->
property ~name Universal
~initial_value:(string_of_kind_value kind v)
~inherits ()
let property ~name kind initial ~inherits ~universal =
if universal then property_universal ~name kind initial ~inherits
else property_typed ~name kind initial ~inherits
let property_rule_of_metadata (Info info) =
match (info.kind, info.property) with
| Some kind, Some { initial; inherits; universal } ->
Some (property ~name:("--" ^ info.name) kind initial ~inherits ~universal)
| _ -> None
let property_rule_of_var var =
Option.bind (metadata_of_var var) property_rule_of_metadata
let property_rule : type a r. (a, r) t -> Css.t option =
fun var ->
match var.role with
| Property_default | Channel -> (
match var.property with
| None -> None
| Some { initial; inherits; universal; _ } ->
let name = "--" ^ var.name in
Some (property ~name var.kind initial ~inherits ~universal))
| _ -> None
let property_rules : type a. (a, [< `Property_default ]) t -> Css.t =
fun var ->
match property_rule var with
| None ->
failwith
("property_default variable '" ^ var.name
^ "' is missing property metadata. This is a bug in the variable \
definition - property_default variables must always have property \
metadata with an initial value.")
| Some r -> r
let binding var ?fallback value = var.binding ?fallback value
let set var value = fst (var.binding value)
let binding_initial var =
Css.custom_property ~layer:"utilities" ("--" ^ var.name) "initial"
let reference : type a b. (a, b) t -> a Css.var =
fun var ->
match var.role with
| Ref_only -> (
match var.fallback with
| None -> failwith ("ref_only variable " ^ var.name ^ " missing fallback")
| Some fb_value ->
let _, var_ref = var.binding fb_value in
var_ref)
| Property_default -> (
match var.property with
| None ->
failwith
("property_default variable " ^ var.name ^ " missing property")
| Some { initial; _ } -> (
match initial with
| None ->
failwith
("property_default variable " ^ var.name
^ " missing initial value")
| Some initial_value ->
let _, var_ref = var.binding initial_value in
var_ref))
| Theme | Channel -> assert false
let reference_with_fallback : type a b. (a, b) t -> a -> a Css.var =
fun var fallback_value ->
match var.role with
| Theme | Channel ->
let _, var_ref =
var.binding ~fallback:(Css.Fallback fallback_value) fallback_value
in
var_ref
| Property_default | Ref_only -> assert false
let reference_with_empty_fallback : type a. (a, [< `Channel ]) t -> a Css.var =
fun var -> Css.var_ref ~fallback:Css.Empty var.name
let reference_with_var_fallback : type a.
(a, [< `Channel ]) t -> (a, [< `Theme ]) t -> a -> a Css.var =
fun channel_var theme_var _dummy_value ->
let fallback_name = theme_var.name in
Css.var_ref ~fallback:(Css.Var_fallback fallback_name) channel_var.name
let ref_only kind name ~fallback =
v kind ~fallback ~role:Ref_only name ~layer:Utility
let theme_ref : type a. ?default:a -> ?default_css:string -> string -> a Css.var
=
fun ?default ?default_css name ->
let info =
Info
{
kind = None;
name;
property = None;
order = None;
property_order = None;
family = None;
runtime = false;
default_css;
}
in
Css.var_ref ~layer:"theme" ~meta:(meta_of_info info) ?default name
let declaration_of_property_info (Css.Property_info info) =
match properties_layer_override info.name with
| Some css -> Css.custom_property info.name css
| None -> (
match info.initial_value with
| None -> Css.custom_property info.name "initial"
| Some v -> Css.Variables.typed_custom_property info.name info.syntax v)
let name var = var.name
let css_name var = "--" ^ var.name
let needs_property_rule v =
match metadata_of_var v with
| Some metadata -> metadata_needs_property metadata
| None -> false
let order_of_declaration decl =
Option.bind (metadata_of_declaration decl) metadata_order
let is_runtime_declaration decl =
match metadata_of_declaration decl with
| Some (Info t) -> t.runtime
| None -> false
let property_initial_declaration = declaration_of_property_info
let pp v = Pp.str [ "Var(--"; v.name; ")" ]
let closes_nothing = function
| Cascade.Component.Preserved { kind = Cascade.Token.Close _; _ } -> true
| _ -> false
let bracket ?fallback name =
let read_whole source read =
try
let cursor = Cascade.Cursor.of_string source in
if List.exists closes_nothing (Cascade.Cursor.remaining cursor) then None
else
let name, fallback = read cursor in
if Cascade.Cursor.is_done cursor then Some (name, fallback) else None
with Cascade.Cursor.Parse_error _ | Invalid_argument _ -> None
in
let parsed_reference =
match fallback with
| Some _ -> None
| None ->
if String.starts_with ~prefix:"var(" name then
read_whole name Css.Variables.read_reference
else if String.contains name ',' then
read_whole ("--" ^ name) Css.Variables.read_reference_body_as_string
else None
in
let raw_name =
let prefix = "var(--" in
let len = String.length name in
let prefix_len = String.length prefix in
if
parsed_reference = None
&& String.starts_with ~prefix name
&& len > prefix_len
&& name.[len - 1] = ')'
then String.trim (String.sub name prefix_len (len - prefix_len - 1))
else name
in
match parsed_reference with
| Some (name, Some fallback) ->
Css.var_ref ~runtime:true
~fallback:(Css.Values.syntax_fallback fallback)
name
| Some (name, None) -> Css.var_ref ~runtime:true name
| None -> Css.var_ref ?fallback raw_name