Source file skyline_dialog_v2.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
open! Core
open! Private_skyline_prelude

module Style = struct
  let root size =
    Attr.many
      Classes.
        [ flex
        ; flex_col
        ; w_fit
        ; max_w_full
        ; h_full
        ; {%css|
            position: relative;
            overflow: hidden;
          |}
        ; {%css|max-width: 100vw;|}
        ; {%css|max-height: 100vh;|}
        ; shadow_sm
        ; border 1
        ; border_default
        ; text_default
        ; bg_one
        ; (match size with
           | `Xs -> Classes.gap 1.
           | `Sm | `Md | `Lg -> Classes.gap 2.)
        ; (match size with
           | `Xs -> rounded_xs
           | `Sm -> rounded_sm
           | `Md -> rounded_md
           | `Lg -> rounded_lg)
        ]
  ;;

  (* We measure Skyline v2 button dimensions (inner height + paddings) to know how much
     room we need to reserve for it. *)
  let close_hitbox = function
    | `Xs -> 4.5
    | `Sm -> 6.
    | `Md -> 7.
    | `Lg -> 8.
  ;;

  let paddings = function
    | `Xs -> ~pad_x:1., ~pad_y:1.
    | `Sm -> ~pad_x:2., ~pad_y:2.
    | `Md | `Lg -> ~pad_x:3., ~pad_y:2.
  ;;

  let section ~is_first ~is_last ~close_button_present ~scrollable ~full_bleed size =
    Attr.many
      Classes.
        [ flex
        ; gap 1.
        ; (if scrollable
           then
             {%css|
               flex: 1;
               overflow: auto;
             |}
           else
             {%css|
               flex: 0;
               overflow: visible;
             |})
        ; (if full_bleed
           then Attr.empty
           else (
             let ~pad_x, ~pad_y = paddings size in
             Attr.many
               [ (if is_first && close_button_present
                  then (
                    let close_hitbox = close_hitbox size in
                    Attr.many [ pl pad_x; pr ((2. *. pad_x) +. close_hitbox) ])
                  else px pad_x)
               ; (if is_first then pt pad_y else Attr.empty)
               ; (if is_last then pb pad_y else Attr.empty)
               ]))
        ]
  ;;

  let separator = {%css|border-top: 1px solid %{Colors.Border.default#Css_gen.Color};|}

  let close_button size =
    Attr.many
      Classes.(
        let hitbox = close_hitbox size in
        [ p 0.
        ; w hitbox
        ; h hitbox
        ; min_w hitbox
        ; min_h hitbox
        ; (let ~pad_x, ~pad_y = paddings size in
           {%css|
             position: absolute;

             /* Those offsets are manually adjusted in screenshot testing to "feel" good */
             top: %{spacing (pad_y *. 0.75)#Css_gen.Length};
             right: %{spacing pad_x#Css_gen.Length};
           |})
        ])
  ;;
end

module Content = struct
  type t =
    | Section of
        (size:Skyline_size.t
         -> close_button_present:bool
         -> is_first:bool
         -> is_last:bool
         -> Node.t)
    | Separator of Node.t
    | Close_button of (size:Skyline_size.t -> Node.t)
    | Fragment of t list

  let fragment l = Fragment l

  let rec flatten l =
    List.map l ~f:(function
      | Fragment l -> flatten l
      | v -> [ v ])
    |> List.concat
  ;;

  let to_vdom_nodes l ~size =
    let l = flatten l in
    let ~close_button, ~first_section_idx, ~last_section_idx =
      List.foldi
        l
        ~init:(~close_button:None, ~first_section_idx:(-1), ~last_section_idx:(-1))
        ~f:(fun i (~close_button, ~first_section_idx, ~last_section_idx) -> function
        | Close_button close_button ->
          ~close_button:(Some close_button), ~first_section_idx, ~last_section_idx
        | Section _ ->
          ( ~close_button
          , ~first_section_idx:(if first_section_idx < 0 then i else first_section_idx)
          , ~last_section_idx:i )
        | Separator _ | Fragment _ -> ~close_button, ~first_section_idx, ~last_section_idx)
    in
    List.mapi l ~f:(fun i -> function
      | Fragment _ | Close_button _ -> Node.none
      | Separator v -> v
      | Section f ->
        f
          ~size
          ~close_button_present:(Option.is_some close_button)
          ~is_first:(i = first_section_idx)
          ~is_last:(i = last_section_idx))
    @
    match close_button with
    | None -> []
    | Some close -> [ close ~size ]
  ;;
end

let view ?test_selector ?(size = `Md) ?(attrs = []) children =
  [%html
    {|
      <dialog
        open
        %{Style.root size}
        %{Bonsai.Test_selector.attr_of_opt test_selector}
        %{Classes.data_skyline_component "dialog"}
        *{attrs}
      >
        *{Content.to_vdom_nodes ~size children}
      </dialog>
    |}]
;;

module Section = struct
  let content'
    ?test_selector
    ?(scrollable = true)
    ?(full_bleed = false)
    ?(attrs = [])
    children
    ~size
    ~close_button_present
    ~is_first
    ~is_last
    =
    [%html
      {|
        <section
          %{Style.section ~is_first ~is_last ~close_button_present ~scrollable ~full_bleed size}
          %{Bonsai.Test_selector.attr_of_opt test_selector}
          *{attrs}
        >
          *{children}
        </section>
      |}]
  ;;

  let content ?test_selector ?scrollable ?full_bleed ?attrs children =
    Content.Section (content' ?test_selector ?scrollable ?full_bleed ?attrs children)
  ;;

  let title ?test_selector ?(scrollable = false) ?full_bleed ?(attrs = []) children =
    Content.Section
      (fun ~size ~close_button_present ~is_first ~is_last ->
        Skyline_text_v2.view
          ~size:(size :> Skyline_text_v2.Size.t)
          ~weight:`Bold
          ~layout:`Contents
          [ content'
              ~attrs:(Attr.role "sectionhead" :: attrs)
              ~scrollable
              ?full_bleed
              ?test_selector
              children
              ~size
              ~close_button_present
              ~is_first
              ~is_last
          ])
  ;;

  let text ?test_selector ?(scrollable = true) ?full_bleed ?attrs children =
    Content.Section
      (fun ~size ~close_button_present ~is_first ~is_last ->
        Skyline_text_v2.view
          ~size:(size :> Skyline_text_v2.Size.t)
          ~layout:`Contents
          [ content'
              ?attrs
              ~scrollable
              ?full_bleed
              ?test_selector
              children
              ~size
              ~close_button_present
              ~is_first
              ~is_last
          ])
  ;;

  let footer ?test_selector ?(scrollable = false) ?full_bleed ?(attrs = []) l =
    content
      ?test_selector
      ~scrollable
      ?full_bleed
      ~attrs:(Attr.role "navigation" :: attrs)
      l
  ;;

  let separator ?(attrs = []) () =
    Content.Separator (Node.hr ~attrs:(Style.separator :: attrs) ())
  ;;
end

module Close_button = struct
  let content ?test_selector ?(attrs = []) () ~close =
    Content.Close_button
      (fun ~size ->
        [%html
          {|
            <Skyline_button_v2.view
              ~type_attr:%{Submit}
              ?test_selector
              ~size
              ~variant:%{Ghost}
              ~intent:%{`Secondary}
              %{Style.close_button size}
              ~on_click:%{close}
              *{attrs}
            >
              <Skyline_button_v2.Icon.view ~icon:%{Lucide.x} />
            </>
          |}])
  ;;
end

module For_docs = struct
  let ml_filepath = [%here].pos_fname
end