Skip to content

Commit 6eade74

Browse files
committed
feat: use composition API in docs plugin
1 parent efcac22 commit 6eade74

File tree

3 files changed

+112
-35
lines changed

3 files changed

+112
-35
lines changed

docs/components/docs_component.go

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
"time"
1010

11+
"github.com/rfwlab/rfw/v1/composition"
1112
"github.com/rfwlab/rfw/v1/core"
1213
dom "github.com/rfwlab/rfw/v1/dom"
1314
events "github.com/rfwlab/rfw/v1/events"
@@ -106,11 +107,11 @@ func (c *DocsComponent) mount(hc *core.HTMLComponent) {
106107
if !strings.Contains(strings.ToLower(title), q) {
107108
continue
108109
}
109-
a := doc.CreateElement("a")
110-
a.Set("href", "/docs/"+link)
111-
a.Set("textContent", title)
112-
a.Set("className", "block px-2 py-1 text-gray-700 dark:text-zinc-200 hover:bg-gray-100 dark:hover:bg-zinc-700")
113-
ch := events.Listen("mousedown", a.Value)
110+
a := composition.A().
111+
Href("/docs/"+link).
112+
Text(title).
113+
Classes("block", "px-2", "py-1", "text-gray-700", "dark:text-zinc-200", "hover:bg-gray-100", "dark:hover:bg-zinc-700")
114+
ch := events.Listen("mousedown", a.Element().Value)
114115
go func(l string) {
115116
for e := range ch {
116117
if !c.mounted {
@@ -123,7 +124,7 @@ func (c *DocsComponent) mount(hc *core.HTMLComponent) {
123124
router.Navigate("/docs/" + l)
124125
}
125126
}(link)
126-
results.Call("appendChild", a.Value)
127+
results.AppendChild(a.Element())
127128
count++
128129
if count >= 5 {
129130
break
@@ -199,11 +200,11 @@ func (c *DocsComponent) mount(hc *core.HTMLComponent) {
199200
id := h.Get("id").String()
200201
text := h.Get("text").String()
201202
depth := h.Get("depth").Int()
202-
a := doc.CreateElement("a")
203-
a.Set("href", "#"+id)
204-
a.Set("textContent", text)
205-
a.Set("className", "block py-1 pl-"+strconv.Itoa((depth-1)*4)+" text-gray-700 dark:text-zinc-200 dark:hover:text-white hover:text-black")
206-
ch := events.Listen("click", a.Value)
203+
a := composition.A().
204+
Href("#"+id).
205+
Text(text).
206+
Classes("block", "py-1", "pl-"+strconv.Itoa((depth-1)*4), "text-gray-700", "dark:text-zinc-200", "dark:hover:text-white", "hover:text-black")
207+
ch := events.Listen("click", a.Element().Value)
207208
go func(i string) {
208209
for e := range ch {
209210
e.Call("preventDefault")
@@ -212,7 +213,7 @@ func (c *DocsComponent) mount(hc *core.HTMLComponent) {
212213
}
213214
}
214215
}(id)
215-
toc.Call("appendChild", a.Value)
216+
toc.AppendChild(a.Element())
216217
}
217218
}
218219
}
@@ -236,33 +237,33 @@ func (c *DocsComponent) mount(hc *core.HTMLComponent) {
236237
nav.SetHTML("")
237238
if idx > 0 {
238239
prev := c.order[idx-1]
239-
a := doc.CreateElement("a")
240-
a.Set("className", "text-white")
241-
a.Set("href", "/docs/"+prev)
242-
a.Set("textContent", "\u2190 "+c.titleFor(prev))
243-
ch := events.Listen("click", a.Value)
240+
a := composition.A().
241+
Classes("text-white").
242+
Href("/docs/" + prev).
243+
Text("\u2190 " + c.titleFor(prev))
244+
ch := events.Listen("click", a.Element().Value)
244245
go func(p string) {
245246
for e := range ch {
246247
e.Call("preventDefault")
247248
router.Navigate("/docs/" + p)
248249
}
249250
}(prev)
250-
nav.Call("appendChild", a.Value)
251+
nav.AppendChild(a.Element())
251252
}
252253
if idx >= 0 && idx < len(c.order)-1 {
253254
next := c.order[idx+1]
254-
a := doc.CreateElement("a")
255-
a.Set("className", "ml-auto text-white")
256-
a.Set("href", "/docs/"+next)
257-
a.Set("textContent", c.titleFor(next)+" \u2192")
258-
ch := events.Listen("click", a.Value)
255+
a := composition.A().
256+
Classes("ml-auto", "text-white").
257+
Href("/docs/" + next).
258+
Text(c.titleFor(next) + " \u2192")
259+
ch := events.Listen("click", a.Element().Value)
259260
go func(n string) {
260261
for e := range ch {
261262
e.Call("preventDefault")
262263
router.Navigate("/docs/" + n)
263264
}
264265
}(next)
265-
nav.Call("appendChild", a.Value)
266+
nav.AppendChild(a.Element())
266267
}
267268
}
268269
}()
@@ -294,7 +295,6 @@ func (c *DocsComponent) unmount(hc *core.HTMLComponent) {
294295
}
295296

296297
func (c *DocsComponent) renderSidebar(items js.Value, parent dom.Element, level int) {
297-
doc := dom.Doc()
298298
length := items.Length()
299299
for i := 0; i < length; i++ {
300300
item := items.Index(i)
@@ -307,11 +307,11 @@ func (c *DocsComponent) renderSidebar(items js.Value, parent dom.Element, level
307307
link := strings.TrimSuffix(path.String(), ".md")
308308
c.meta[link] = struct{ Title, Description string }{Title: title, Description: desc}
309309
c.order = append(c.order, link)
310-
a := doc.CreateElement("a")
311-
a.Set("href", "/docs/"+link)
312-
a.Set("textContent", title)
313-
a.Set("className", "block py-1 pl-"+strconv.Itoa(4*level)+" text-gray-700 dark:text-zinc-200 dark:hover:text-white hover:text-black")
314-
ch := events.Listen("click", a.Value)
310+
a := composition.A().
311+
Href("/docs/"+link).
312+
Text(title).
313+
Classes("block", "py-1", "pl-"+strconv.Itoa(4*level), "text-gray-700", "dark:text-zinc-200", "dark:hover:text-white", "hover:text-black")
314+
ch := events.Listen("click", a.Element().Value)
315315
go func(l string) {
316316
for evt := range ch {
317317
if !c.mounted {
@@ -321,14 +321,14 @@ func (c *DocsComponent) renderSidebar(items js.Value, parent dom.Element, level
321321
router.Navigate("/docs/" + l)
322322
}
323323
}(link)
324-
parent.Call("appendChild", a.Value)
324+
parent.AppendChild(a.Element())
325325
}
326326
if children := item.Get("children"); children.Truthy() {
327327
if !item.Get("path").Truthy() && title != "" {
328-
h := doc.CreateElement("div")
329-
h.Set("textContent", title)
330-
h.Set("className", "mt-4 mb-1 font-semibold text-gray-900 dark:text-white pl-"+strconv.Itoa(4*level))
331-
parent.Call("appendChild", h.Value)
328+
h := composition.Div().
329+
Text(title).
330+
Classes("mt-4", "mb-1", "font-semibold", "text-gray-900", "dark:text-white", "pl-"+strconv.Itoa(4*level))
331+
parent.AppendChild(h.Element())
332332
}
333333
c.renderSidebar(children, parent, level+1)
334334
}

v1/composition/node.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,66 @@ func (d *divNode) Group(g *Elements) *divNode {
219219
}
220220
return d
221221
}
222+
223+
// anchorNode builds an <a> element.
224+
type anchorNode struct{ el dom.Element }
225+
226+
// A creates a new <a> node builder.
227+
func A() *anchorNode { return &anchorNode{el: dom.Doc().CreateElement("a")} }
228+
229+
// Element returns the underlying DOM element.
230+
func (a *anchorNode) Element() dom.Element { return a.el }
231+
232+
// Class adds a class to the element.
233+
func (a *anchorNode) Class(name string) *anchorNode {
234+
a.el.AddClass(name)
235+
return a
236+
}
237+
238+
// Classes adds multiple classes to the element.
239+
func (a *anchorNode) Classes(names ...string) *anchorNode {
240+
for _, name := range names {
241+
a.el.AddClass(name)
242+
}
243+
return a
244+
}
245+
246+
// Style sets an inline style property on the element.
247+
func (a *anchorNode) Style(prop, value string) *anchorNode {
248+
a.el.SetStyle(prop, value)
249+
return a
250+
}
251+
252+
// Styles adds multiple inline style properties to the element.
253+
func (a *anchorNode) Styles(props ...string) *anchorNode {
254+
for i := 0; i < len(props); i += 2 {
255+
a.el.SetStyle(props[i], props[i+1])
256+
}
257+
return a
258+
}
259+
260+
// Attr sets an attribute on the element.
261+
func (a *anchorNode) Attr(name, value string) *anchorNode {
262+
a.el.SetAttr(name, value)
263+
return a
264+
}
265+
266+
// Href sets the href attribute on the element.
267+
func (a *anchorNode) Href(h string) *anchorNode {
268+
a.el.SetAttr("href", h)
269+
return a
270+
}
271+
272+
// Text sets the text content of the element.
273+
func (a *anchorNode) Text(t string) *anchorNode {
274+
a.el.SetText(t)
275+
return a
276+
}
277+
278+
// Group adds the node to the provided group.
279+
func (a *anchorNode) Group(g *Elements) *anchorNode {
280+
if g != nil {
281+
g.add(a)
282+
}
283+
return a
284+
}

v1/composition/node_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,17 @@ func TestGroupMutators(t *testing.T) {
141141
}
142142
})
143143
}
144+
145+
func TestAnchorBuilder(t *testing.T) {
146+
a := A().Class("c").Href("/docs").Text("docs")
147+
el := a.Element()
148+
if v := el.Attr("href"); v != "/docs" {
149+
t.Fatalf("expected href /docs, got %q", v)
150+
}
151+
if el.Text() != "docs" {
152+
t.Fatalf("expected text docs")
153+
}
154+
if !el.HasClass("c") {
155+
t.Fatalf("expected class c")
156+
}
157+
}

0 commit comments

Comments
 (0)