-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.templ
More file actions
249 lines (239 loc) · 5.8 KB
/
app.templ
File metadata and controls
249 lines (239 loc) · 5.8 KB
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
package popui
import (
"github.com/invopop/popui.go/classes"
"github.com/invopop/popui.go/htmx"
"github.com/invopop/popui.go/props"
"github.com/invopop/popui.go/tailwind"
)
const (
// AppID is the ID used for the main app container.
AppID = "app-container"
// AppTarget is the CSS selector for targeting the main app container.
AppTarget = "#" + AppID
)
// App provides the root application container for a full-window app
// designed to be a standalone application or embedded inside the Invopop
// Console in an iframe.
//
// It sets up the basic layout and styling for the app, including the
// base HTML header and body structure ready to just add content.
//
// Applications will always include Alpine.js for interactivity.
//
// If the HTMX option is enabled and the request is detected as an HTMX
// request, only the body's content will be rendered. This assumes that
// the request was made with the `hx-swap` property set to `innerHTML`,
// the default behavior.
//
// Ensure that the `hx-target` property is set to use the AppTarget selector
// so that HTMX knows where to swap the content.
templ App(opts ...props.App) {
{{ p := props.First(opts) }}
if p.HTMX && htmx.IsRequest(ctx) {
// HTMX request, only render the inner contents.
@appBody(p) {
{ children... }
}
} else {
@HTML() {
@Head(props.Head{
Title: p.Title,
Description: p.Description,
AlpineJS: true,
HTMX: p.HTMX,
Axios: p.Axios,
Auth: p.Auth,
Stylesheets: p.Stylesheets,
Scripts: p.Scripts,
}) {
if p.Head != nil {
@p.Head
}
}
@Body(props.Body{ID: AppID}) {
@appBody(p) {
{ children... }
}
}
}
}
}
templ appBody(p props.App) {
// Main app container uses universal ID for HTMX targeting.
<div
if p.Data != "" {
x-data={ p.Data }
}
if p.AccentColor != "" {
data-accent-color={ p.AccentColor }
}
class="grid grid-cols-[auto_1fr] grid-rows-[auto_1fr_auto] w-full h-full [&_nav]:col-start-1 [&_nav]:row-span-full bg-background text-foreground"
>
{ children... }
</div>
}
// Main provides a wrapper for the main content area of apps.
// This should be used outside the Header, or Footer components.
// For the contents, you may want to use the Article component that provides
// padding.
templ Main(opts ...props.Main) {
{{ p := props.First(opts) }}
<main
if p.ID != "" {
id={ p.ID }
}
if p.Cloak {
x-cloak
}
if p.Data != "" {
x-data={ p.Data }
}
class={
tailwind.Merge(
"overflow-auto col-start-2 row-start-2 flex flex-col gap-4",
classes.If(!p.Center, "justify-between"),
classes.If(p.Center, "items-center"),
p.Class,
),
}
if p.Data != "" {
x-data={ p.Data }
}
{ p.Attributes... }
>
{ children... }
</main>
}
// Article is used to wrap content sections inside a main area providing
// padding and fixed width that makes the contents more readable. If you
// want a full-width article, set FullWidth to true.
templ Article(opts ...props.Article) {
{{ p := props.First(opts) }}
<article
if p.ID != "" {
id={ p.ID }
}
class={
tailwind.Merge(
"flex flex-col p-4 flex-1 w-full gap-4",
classes.If(!p.FullWidth, "lg:w-200"),
p.Class,
),
}
{ p.Attributes... }
>
{ children... }
</article>
}
// Block provides a simple block container that stacks its children
// vertically with a gap in the same way as the Article and Main components.
// This can be used inside Articles or Mains to group related content.
templ Block(opts ...props.Block) {
{{ p := props.First(opts) }}
<div
if p.ID != "" {
id={ p.ID }
}
class={
tailwind.Merge(
"flex flex-col flex-1 w-full gap-4",
p.Class,
),
}
{ p.Attributes... }
>
{ children... }
</div>
}
// Header provides a header area for apps with a set of breadcrumbs
// if provided for navigation. This should be used directly inside
// the App component, and not inside Main.
templ Header(opts ...props.Header) {
{{ p := props.First(opts) }}
<header
if p.ID != "" {
id={ p.ID }
}
class={
tailwind.Merge(
"bg-background z-[2] h-12 sticky inset-0 flex px-4 justify-between items-center border-b border-border transition-[left] duration-300 ease-in-out col-start-2 col-span-2 row-start-1",
p.Class,
),
}
if p.Data != "" {
x-data={ p.Data }
}
{ p.Attributes... }
>
/* TODO: add menu button
<button class="block p-[5px] mr-2 md:hidden">
@icons.Menu()
</button>
*/
<div class="flex items-center gap-1">
if p.Title != nil {
@p.Title
}
if p.Breadcrumbs != nil {
if p.Breadcrumbs != nil {
@Breadcrumbs() {
for _, b := range p.Breadcrumbs {
@Breadcrumb(b)
}
}
}
}
</div>
<div class="flex items-center gap-2">
{ children... }
</div>
</header>
}
// Aside provides a fixed-width sidebar on the right side of the app
// next to the Main content area, between the Header and Footer.
// It has a fixed width of 400px.
templ Aside(opts ...props.Aside) {
{{ p := props.First(opts) }}
<aside
if p.ID != "" {
id={ p.ID }
}
if p.Data != "" {
x-data={ p.Data }
}
class={
tailwind.Merge(
"col-start-3 row-start-2 w-[400px] overflow-auto border-l border-border bg-background",
p.Class,
),
}
{ p.Attributes... }
>
{ children... }
</aside>
}
// Footer provides a footer to be used inside the App with contents
// justified to the end. This should be used directly inside the App
// component.
templ Footer(opts ...props.Footer) {
{{ p := props.First(opts) }}
<footer
if p.ID != "" {
id={ p.ID }
}
if p.Data != "" {
x-data={ p.Data }
}
class={
tailwind.Merge(
"flex flex-col justify-end w-full col-start-2 col-span-2 row-start-3",
p.Class,
),
}
{ p.Attributes... }
>
<div class="flex justify-end border-t border-border gap-3 p-4">
{ children... }
</div>
</footer>
}