-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodal.go
163 lines (141 loc) · 4.02 KB
/
modal.go
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
package components
import (
"fmt"
"github.com/adrianolmedo/components/alpine"
"github.com/adrianolmedo/components/gomputils"
"github.com/adrianolmedo/components/randutil"
lucide "github.com/eduardolat/gomponents-lucide"
"github.com/maragudk/gomponents"
"github.com/maragudk/gomponents/components"
"github.com/maragudk/gomponents/html"
)
// ModalProps are the props for the Modal component.
type ModalProps struct {
// ID is the ID of the modal dialog. If empty, a random ID will be generated.
ID string
// Content is the content of the modal dialog.
Content []gomponents.Node
// Size is the size of the modal dialog.
// Can be "sm", "md", and "lg".
// The default is "md".
Size Size
// Title is the title of the modal dialog.
// If you need more than a string, use TitleNode instead.
Title string
// TitleNode is the title of the modal dialog.
// If you need only a string, use Title instead.
TitleNode gomponents.Node
// HTMXIndicator is an optional ID of an HTMX indicator that
// should be inserted in the modal header.
HTMXIndicator string
}
// ModalResult is the result of creating a modal dialog.
type ModalResult struct {
// HTML is the modal dialog HTML.
HTML gomponents.Node
// OpenerAttr is the attribute to add to the element that opens the modal dialog.
OpenerAttr gomponents.Node
}
// Modal renders a modal dialog.
func Modal(props ModalProps) ModalResult {
id := props.ID
if id == "" {
id = "mo" + randutil.UUIDStrWithoutDashes()
}
openEventName := fmt.Sprintf("%s_open", id)
closeEventName := fmt.Sprintf("%s_close", id)
openerAttr := gomponents.Attr(
"onClick",
"event.preventDefault(); window.dispatchEvent(new Event('"+openEventName+"'));",
)
closerAttr := gomponents.Attr(
"onClick",
"event.preventDefault(); window.dispatchEvent(new Event('"+closeEventName+"'));",
)
openCode := `document.getElementById("` + id + `").classList.remove("hidden");`
closeCode := `document.getElementById("` + id + `").classList.add("hidden");`
size := SizeMd
if props.Size != "" {
size = props.Size
}
hasHTMXIndicator := props.HTMXIndicator != ""
content := html.Div(
alpine.XData(`{}`),
alpine.XOn(fmt.Sprintf("%s.window", openEventName), openCode),
alpine.XOn(fmt.Sprintf("%s.window", closeEventName), closeCode),
alpine.XOn("keyup.escape.window", closeCode),
html.ID(id),
Classes{
"hidden": true,
"!p-0 !m-0 w-[100dvw] h-[100dvh]": true,
"fixed left-0 top-0 z-[1000]": true,
},
// Backdrop
html.Div(
closerAttr,
Classes{
"bg-black opacity-25": true,
"!w-full !h-full": true,
"z-[1001]": true,
},
),
// Dialog
html.Div(
components.Classes{
"absolute z-[1002] top-[50%] left-[50%]": true,
"translate-y-[-50%] translate-x-[-50%]": true,
"max-w-[calc(100dvw-30px)] max-h-[85dvh]": true,
"bg-white rounded overflow-y-auto": true,
"overflow-x-hidden whitespace-normal": true,
"bg-base-100 p-0": true,
"w-[400px]": size == "sm",
"w-[600px]": size == "md",
"w-[800px]": size == "lg",
"w-[1200px]": size == "xl",
},
html.Div(
gomputils.ManyClasses(
"w-full sticky top-0 right-0 bg-base-100",
"flex items-center justify-between",
"border-b border-base-300 px-4 py-3",
),
html.Div(
gomponents.If(
props.TitleNode != nil,
props.TitleNode,
),
gomponents.If(
props.Title != "",
html.Span(
html.Class("h2"),
gomponents.Text(props.Title),
),
),
gomponents.If(
hasHTMXIndicator,
html.Div(
html.Class("inline-flex h-full items-center pl-2"),
HxLoadingSm(false, props.HTMXIndicator),
),
),
),
Button(ButtonProps{
Ghost: true,
Circle: true,
Children: []gomponents.Node{
lucide.X(html.Class("w-6 h-6")),
closerAttr,
},
}),
),
html.Div(
html.Class("p-4"),
gomponents.Group(props.Content),
),
),
)
return ModalResult{
OpenerAttr: openerAttr,
HTML: content,
}
}