-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilders.go
More file actions
74 lines (63 loc) · 1.75 KB
/
Copy pathbuilders.go
File metadata and controls
74 lines (63 loc) · 1.75 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
package image
import (
"github.com/tinywasm/dom"
"github.com/tinywasm/fmt"
)
// ImgElement wraps *dom.Element to provide a fluent image-specific API.
type ImgElement struct {
el *dom.Element
}
// Img builds an <img> element with src and alt.
func Img(src, alt string) *ImgElement {
el := dom.NewElement("img").
Attr("src", src).
Attr("alt", alt).
NoCloseTag()
return &ImgElement{el: el}
}
// Lazy sets loading="lazy".
func (i *ImgElement) Lazy() *ImgElement {
i.el.Attr("loading", "lazy")
return i
}
// Size sets width and height (reduces CLS).
func (i *ImgElement) Size(w, h int) *ImgElement {
i.el.Attr("width", fmt.Sprint(w))
i.el.Attr("height", fmt.Sprint(h))
return i
}
// Class adds CSS classes.
func (i *ImgElement) Class(classes ...string) *ImgElement {
i.el.Class(classes...)
return i
}
// Attr sets an arbitrary attribute.
func (i *ImgElement) Attr(key, val string) *ImgElement {
i.el.Attr(key, val)
return i
}
// AsElement returns the underlying *dom.Element for embedding in Render() trees.
func (i *ImgElement) AsElement() *dom.Element {
return i.el
}
// String serializes the image element (satisfies dom.Component).
func (i *ImgElement) String() string {
return i.el.String()
}
// Picture builds a <picture> element for responsive images.
func Picture(children ...any) *dom.Element {
return dom.NewElement("picture").Add(children...)
}
// Source builds a <source> for use inside <picture>.
// mediaOrType: media query "(max-width: 600px)" or MIME type "image/webp".
func Source(srcset, mediaOrType string) *dom.Element {
el := dom.NewElement("source").Attr("srcset", srcset).NoCloseTag()
if len(mediaOrType) > 0 {
if mediaOrType[0] == '(' {
el.Attr("media", mediaOrType)
} else {
el.Attr("type", mediaOrType)
}
}
return el
}