Source file button.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
open! Core
open! Bonsai_web

module Style = struct
  include
    [%css
    stylesheet
      {|
        .button {
          height: 28px;
          padding: 0 12px;
          column-gap: 4px;

          font-size: 0.8rem;
          font-weight: 600;
          line-height: 28px;

          display: flex;
          align-items: center;
          justify-content: center;

          border-radius: 2px;
          color: var(--foreground);
          background-color: var(--background);
        }

        .button.enabled:hover {
          color: var(--hover-foreground);
          background-color: var(--hover-background);
        }

        .button.disabled {
          opacity: 0.5;
          cursor: not-allowed;
        }
      |}]

  let colors ?foreground ?background ?hover_foreground ?hover_background () =
    let foreground = Option.map foreground ~f:Css_gen.Color.to_string_css in
    let background = Option.map background ~f:Css_gen.Color.to_string_css in
    let hover_foreground = Option.map hover_foreground ~f:Css_gen.Color.to_string_css in
    let hover_background = Option.map hover_background ~f:Css_gen.Color.to_string_css in
    Variables.set ?foreground ?background ?hover_foreground ?hover_background ()
  ;;
end

let make ~attrs ~disabled ~intent ~tooltip ~on_click content =
  let variables =
    match intent with
    | Some intent ->
      let intent_color =
        match (intent : View.Constants.Intent.t) with
        | Info -> Skyline_theme_v1.accent
        | Success -> Skyline_theme_v1.success
        | Warning -> Skyline_theme_v1.warning
        | Error -> Skyline_theme_v1.error
      in
      Style.colors
        ~foreground:intent_color
        ~background:(Skyline_theme_v1.ramp intent_color (Percent.of_percentage 20.))
        ~hover_foreground:intent_color
        ~hover_background:intent_color
        ()
    | None ->
      Style.colors
        ~foreground:Skyline_theme_v1.primary
        ~background:
          (Skyline_theme_v1.ramp Skyline_theme_v1.primary (Percent.of_percentage 20.))
        ~hover_foreground:Skyline_theme_v1.background
        ~hover_background:Skyline_theme_v1.primary
        ()
  in
  let on_click =
    match disabled with
    | false -> Vdom.Attr.on_click (const on_click)
    | true -> Vdom.Attr.empty
  in
  let title' = Option.value_map tooltip ~f:Vdom.Attr.title ~default:Vdom.Attr.empty in
  Vdom.Node.button
    ~attrs:
      (Style.button
       :: variables
       :: on_click
       :: title'
       :: (if disabled then Style.disabled else Style.enabled)
       :: attrs)
    content
;;