-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add 2e-dynamic feature and refactor to use documentFragments
- Loading branch information
Showing
14 changed files
with
250 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include $(dir $(shell go env GOWORK))/examples/_common/Makefile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
The <Dynamic> tag is useful when you render from data. It lets you pass either a string for a native element or a component function and it will render that with the rest of the provided props. | ||
This is often more compact than writing a number of <Show> or <Switch> components. | ||
*/ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"syscall/js" | ||
|
||
"github.com/VJftw/vigor" | ||
"github.com/VJftw/vigor/html" | ||
"github.com/VJftw/vigor/web" | ||
) | ||
|
||
func App() html.Node { | ||
selected, setSelected := vigor.CreateSignal("red") | ||
|
||
options := map[string]html.Node{ | ||
"red": html.El("strong", html.Style("color", "red"), "Red Thing"), | ||
"green": html.El("strong", html.Style("color", "green"), "Green Thing"), | ||
"blue": html.El("strong", html.Style("color", "blue"), "Blue Thing"), | ||
} | ||
optionKeys := []any{"red", "green", "blue"} // static order | ||
|
||
return html.El("div", | ||
html.El("select", | ||
html.Property("value", selected), | ||
html.On("input", func(this js.Value, args []js.Value) { | ||
setSelected(args[0].Get("currentTarget").Get("value").String()) | ||
}), | ||
html.For(optionKeys, func(i int, v any) html.Node { | ||
return html.El("option", html.Attr("value", v), html.Text(v)) | ||
}), | ||
), | ||
html.Dynamic(func(s vigor.Subscriber) html.Node { | ||
return options[selected(s).(string)] | ||
}), | ||
) | ||
} | ||
|
||
func main() { | ||
if err := web.RenderToElementID( | ||
context.Background(), | ||
App(), "app", | ||
); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-rod/rod" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestApp(t *testing.T) { | ||
fs := http.FileServer(http.Dir("../build")) | ||
s := httptest.NewServer(fs) | ||
defer s.Close() | ||
|
||
page := rod.New().MustConnect().Timeout(10 * time.Second).Logger(rod.DefaultLogger).MustPage(s.URL) | ||
defer page.MustClose() | ||
page.Race().Element("#vigor-info").MustDo() | ||
|
||
appEl := page.MustElement("#app") | ||
|
||
assert.Equal(t, | ||
`<div id="app"><div><select><option value="red">red</option><option value="green">green</option><option value="blue">blue</option></select><strong style="color: red;">Red Thing</strong></div></div>`, | ||
appEl.MustHTML(), | ||
) | ||
|
||
page.MustElement("select").MustSelect("green") | ||
assert.Equal(t, | ||
`<div id="app"><div><select><option value="red">red</option><option value="green">green</option><option value="blue">blue</option></select><strong style="color: green;">Green Thing</strong></div></div>`, | ||
appEl.MustHTML(), | ||
) | ||
|
||
page.MustElement("select").MustSelect("blue") | ||
assert.Equal(t, | ||
`<div id="app"><div><select><option value="red">red</option><option value="green">green</option><option value="blue">blue</option></select><strong style="color: blue;">Blue Thing</strong></div></div>`, | ||
appEl.MustHTML(), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package html | ||
|
||
import ( | ||
"syscall/js" | ||
|
||
"github.com/VJftw/vigor" | ||
) | ||
|
||
type nodeDynamic struct { | ||
fnThatReturnsNode func(vigor.Subscriber) Node | ||
} | ||
|
||
func Dynamic(fn func(vigor.Subscriber) Node) Node { | ||
return &nodeDynamic{ | ||
fnThatReturnsNode: fn, | ||
} | ||
} | ||
|
||
func (n *nodeDynamic) DOMObject(doc js.Value) js.Value { | ||
obj := doc.Call("createDocumentFragment") | ||
fragmentRendered := false | ||
|
||
subscriber := vigor.NewFnSubscriber() | ||
subscriber.SetFn(func() { | ||
newObj := n.fnThatReturnsNode(subscriber).DOMObject(doc) | ||
|
||
if !fragmentRendered { | ||
obj.Call("replaceChildren", newObj) | ||
fragmentRendered = true | ||
} else { | ||
obj.Call("replaceWith", newObj) | ||
} | ||
|
||
obj = newObj | ||
}).Run() | ||
|
||
return obj | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package html | ||
|
||
import ( | ||
"syscall/js" | ||
|
||
"github.com/VJftw/vigor" | ||
) | ||
|
||
type elementProperty struct { | ||
k string | ||
v any | ||
} | ||
|
||
type PropertyElementPlugin struct { | ||
props map[string]any | ||
} | ||
|
||
func NewPropertyElementPlugin() ElementPlugin { | ||
return &PropertyElementPlugin{ | ||
props: map[string]any{}, | ||
} | ||
} | ||
|
||
func (p *PropertyElementPlugin) HandleChild(child any) (bool, error) { | ||
if x, ok := child.(*elementProperty); ok { | ||
p.props[x.k] = x.v | ||
|
||
return true, nil | ||
} | ||
|
||
return false, nil | ||
} | ||
|
||
func (p *PropertyElementPlugin) Render(doc, obj js.Value) error { | ||
subscriber := vigor.NewFnSubscriber() | ||
subscriber.SetFn(func() { | ||
for k, v := range p.props { | ||
if x, ok := v.(vigor.GetterFn); ok { | ||
v = x(subscriber) | ||
} | ||
obj.Set(k, v) | ||
} | ||
}).Run() | ||
|
||
return nil | ||
} | ||
|
||
func Property(k string, v any) *elementProperty { | ||
return &elementProperty{ | ||
k: k, | ||
v: v, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters