-
Notifications
You must be signed in to change notification settings - Fork 1
/
components_tabs.go
120 lines (104 loc) · 2.2 KB
/
components_tabs.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
package bulma
import (
"io"
e "github.com/willoma/gomplements"
"maragu.dev/gomponents/html"
)
// Tabs create a tabs container.
//
// https://willoma.github.io/bulma-gomponents/tabs.html
func Tabs(children ...any) e.Element {
list := e.Ul()
t := &tabs{
tabs: e.Div(e.Class("tabs")),
list: list,
}
t.With(children...)
return t
}
type tabs struct {
tabs e.Element
list e.Element
intermediateContainer *container
}
func (t *tabs) With(children ...any) e.Element {
for _, c := range children {
switch c := c.(type) {
case onTabs:
t.tabs.With(c...)
case onUl:
t.list.With(c...)
case e.Class, e.Classer, e.Classeser, e.Styles:
t.tabs.With(c)
case *container:
t.intermediateContainer = c
case []any:
t.With(c...)
default:
t.list.With(c)
}
}
return t
}
func (t *tabs) Render(w io.Writer) error {
tabs := t.tabs.Clone()
if t.intermediateContainer != nil {
tabs.With(t.intermediateContainer.Clone().With(t.list))
} else {
tabs.With(t.list)
}
return tabs.Render(w)
}
func (t *tabs) Clone() e.Element {
return &tabs{
tabs: t.tabs.Clone(),
list: t.list.Clone(),
intermediateContainer: t.intermediateContainer,
}
}
// TabLink creates a tab entry which is a link. Use html.Href as an argument
// to define a link target if needed.
//
// https://willoma.github.io/bulma-gomponents/tabs.html
func TabLink(children ...any) e.Element {
a := &spanAroundNonIconsIfHasIcons{elemFn: html.A}
t := &tabLink{
Element: e.Li(a),
a: a,
}
t.With(children...)
return t
}
type tabLink struct {
e.Element
a e.Element
}
func (t *tabLink) With(children ...any) e.Element {
for _, c := range children {
switch c := c.(type) {
case e.Class:
if c == Active {
t.Element.With(c)
} else {
t.a.With(c)
}
case []any:
t.With(c...)
default:
t.a.With(c)
}
}
return t
}
func (t *tabLink) Clone() e.Element {
return &tabLink{
Element: t.Element.Clone(),
a: t.a.Clone(),
}
}
// TabAHref creates a tab link with an a element.
//
// https://willoma.github.io/bulma-gomponents/tabs.html
func TabAHref(href string, children ...any) e.Element {
return TabLink(html.Href(href), children)
}