aboutsummaryrefslogtreecommitdiff
path: root/frontend/ts/src/ui/modal.ts
blob: 75b3ad93f0649f8360a8dd6d15b637527e18de18 (plain)
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
import { h, Html } from 'lib/rx'
import * as rx from 'lib/rx'

interface Params {
    header: Html
    body: Html
    onClose: () => void
    className?: string
    onmount?: (element: Element) => void
    onunmount?: (element: Element) => void
}

export function view({ header, body, onClose, className, onmount, onunmount }: Params): Html {
    return rx.withState<boolean>(false, closingVar => {
        const onKeyDown = (event: KeyboardEvent) => {
            if (event.key === 'Escape') onClose()
        }

        return h('div',
            { className: closingVar.map(isClosing => `g-Modal ${className ?? ''} ${isClosing ? 'g-Modal--Fade' : ''}`),
              onmousedown: () => onClose(),
              onmount: (element: Element) => {
                  document.addEventListener('keydown', onKeyDown)
                  if (onmount) onmount(element)
              },
              onunmount: (element: Element) => {
                  document.removeEventListener('keydown', onKeyDown)
                  if (onunmount) onunmount(element)
              }
            },
            h('div',
                { className: 'g-Modal__Content',
                  onmousedown: (e: Event) => e.stopPropagation()
                },
                h('div',
                    { className: 'g-Modal__Header' },
                    header,
                    h('button',
                       { className: 'g-Modal__Close',
                         onclick: onClose
                       },
                      '✕'
                    )
              ),
              h('div',
                  { className: 'g-Modal__Body' },
                  body
              )
            )
        )
    })
}

interface ErrorParams {
    header: string
    message: string
    onClose: () => void
}

export function error({ header, message, onClose }: ErrorParams): Html {
    return view({
        header,
        body: h('div', { className: 'g-Modal__ContentError' }, message),
        onClose,
        className: 'g-Modal--Danger'
    })
}