Source file base_styles.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
open! Core
open! Js_of_ocaml

let stylesheet_contents =
  let open Skyline_tokens_v2 in
  [%string
    {|
      @layer skyline_base_styles {
        :root {
          color-scheme: light dark;
          font-family: %{Font.Family.to_string_css Font.Family.sans};
          font-size: %{Css_gen.Length.to_string_css Font.size_base};
          line-height: %{Css_gen.Length.to_string_css Font.line_height_base};
        }

        body {
          background-color: %{Css_gen.Color.to_string_css Colors.Background.app};
        }
      }
    |}]
;;

let stylesheet =
  lazy
    (Js.Opt.case
       (Js.Unsafe.get Js.Unsafe.global "CSSStyleSheet")
       (const None)
       (fun constr ->
          let sheet = new%js constr in
          let () =
            (Js.Unsafe.coerce Dom_html.document)##.adoptedStyleSheets##push sheet
          in
          Some sheet))
;;

let is_installed () =
  match force stylesheet with
  | None -> false
  | Some style -> Float.(Js.to_float style##.cssRules##.length > 0.)
;;

let install () =
  Option.iter (force stylesheet) ~f:(fun sheet ->
    if not (is_installed ())
    then sheet##insertRule (Js.string stylesheet_contents) (Js.float 0.))
;;

let clear () =
  Option.iter (force stylesheet) ~f:(fun sheet ->
    if is_installed () then sheet##deleteRule (Js.float 0.))
;;