-
Notifications
You must be signed in to change notification settings - Fork 0
/
tenode.go
45 lines (36 loc) · 1.06 KB
/
tenode.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
package tenode
import (
"context"
"io"
"github.com/a-h/templ"
"github.com/maragudk/gomponents"
)
// TemplToNode allows for templ components to be used as a gomponent.
func TemplToNode(ctx context.Context, required templ.Component, input ...templ.Component) gomponents.Node {
components := append([]templ.Component{required}, input...)
group := gomponents.Map(components, func(in templ.Component) gomponents.Node {
return gomponents.NodeFunc(func(w io.Writer) error {
return in.Render(ctx, w)
})
})
return gomponents.NodeFunc(func(w io.Writer) error {
for _, x := range group {
if err := x.Render(w); err != nil {
return err
}
}
return nil
})
}
// NodeToTempl allows for gomponents to be used in a templ component.
func NodeToTempl(required gomponents.Node, optional ...gomponents.Node) templ.Component {
list := append([]gomponents.Node{required}, optional...)
return templ.ComponentFunc(func(_ context.Context, w io.Writer) error {
for _, x := range list {
if err := x.Render(w); err != nil {
return err
}
}
return nil
})
}