Created
March 1, 2018 18:09
-
-
Save egonelbre/c71fdb90955c599e6f1b8007bf6c6af9 to your computer and use it in GitHub Desktop.
Size visual changing
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
package main | |
import ( | |
"encoding/hex" | |
"fmt" | |
"math/rand" | |
"github.com/gopherjs/vecty" | |
"github.com/gopherjs/vecty/elem" | |
"github.com/gopherjs/vecty/event" | |
) | |
func main() { | |
items := &Items{} | |
for i := 0; i < 10; i++ { | |
items.Add() | |
} | |
vecty.RenderBody(&App{Items: items}) | |
} | |
type Items struct { | |
lastID ItemID | |
List []*Item | |
} | |
func (items *Items) Add() { | |
items.lastID++ | |
var data [8]byte | |
rand.Read(data[:]) | |
name := hex.EncodeToString(data[:]) | |
items.List = append(items.List, &Item{items.lastID, name}) | |
} | |
func (items *Items) Remove(id ItemID) { | |
for i := 0; i < len(items.List); i++ { | |
if items.List[i].ID == id { | |
items.List = append(items.List[:i], items.List[i+1:]...) | |
return | |
} | |
} | |
} | |
func (items *Items) Delete(i int) { | |
items.List = append(items.List[:i], items.List[i+1:]...) | |
} | |
type ItemID int | |
type Item struct { | |
ID ItemID | |
Name string | |
} | |
type App struct { | |
vecty.Core | |
Items *Items | |
} | |
func (app *App) AddRandom(ev *vecty.Event) { | |
app.Items.Add() | |
vecty.Rerender(app) | |
} | |
func (app *App) DeleteRandom(ev *vecty.Event) { | |
if len(app.Items.List) == 0 { | |
return | |
} | |
app.Items.Delete(rand.Intn(len(app.Items.List))) | |
vecty.Rerender(app) | |
} | |
func (app *App) Remove(id ItemID) { | |
app.Items.Remove(id) | |
vecty.Rerender(app) | |
} | |
func (app *App) Render() vecty.ComponentOrHTML { | |
var items []vecty.MarkupOrChild | |
for i, item := range app.Items.List { | |
items = append(items, &ItemView{ | |
App: app, | |
Index: i, | |
Count: len(app.Items.List), | |
Item: item, | |
}) | |
} | |
return elem.Body( | |
elem.Style(vecty.Text(` | |
.items { | |
padding-top: 1rem; | |
} | |
.close { | |
cursor: pointer; | |
} | |
.close:hover { | |
background: #fff; | |
} | |
.item { | |
display: flex; | |
transition: background 1s ease-in-out; | |
} | |
.item .close { | |
width: 1rem; | |
} | |
.item .content { | |
flex: 1; | |
} | |
.item .progress { | |
position: relative; | |
flex: 1; | |
} | |
.item .progress .progress-bar { | |
position: absolute; | |
left: 0; | |
top: 0; | |
bottom: 0; | |
background: #000; | |
transition: width 1s ease-in-out; | |
} | |
`)), | |
elem.Button( | |
vecty.Markup(event.Click(app.AddRandom)), | |
vecty.Text("Add"), | |
), | |
elem.Button( | |
vecty.Markup(event.Click(app.DeleteRandom)), | |
vecty.Text("Delete"), | |
), | |
elem.Div( | |
vecty.Markup( | |
vecty.Class("items"), | |
), | |
elem.Div(items...), | |
), | |
) | |
} | |
type ItemView struct { | |
vecty.Core | |
App *App | |
Index int `vecty:"prop"` | |
Count int `vecty:"prop"` | |
Item *Item `vecty:"prop"` | |
} | |
func (view *ItemView) Key() interface{} { | |
//return nil | |
return view.Item.ID | |
} | |
func (view *ItemView) Mount() { | |
fmt.Println("Mount: ", view.Item.Name) | |
} | |
func (view *ItemView) Unmount() { | |
fmt.Println("Unmount: ", view.Item.Name) | |
} | |
func (view *ItemView) Render() vecty.ComponentOrHTML { | |
return elem.Div( | |
vecty.Markup( | |
vecty.Class("item"), | |
vecty.Style("background", fmt.Sprintf("hsla(%d,70%%,%d%%,1)", view.Item.ID*67%360, max(100-view.Index*10, 30))), | |
), | |
elem.Div( | |
vecty.Markup( | |
vecty.Class("close"), | |
event.Click(func(ev *vecty.Event) { | |
view.App.Remove(view.Item.ID) | |
}), | |
), | |
vecty.Text("X"), | |
), | |
elem.Div( | |
vecty.Markup( | |
vecty.Class("content"), | |
), | |
vecty.Text(fmt.Sprintf("#%d | %d: %s", view.Index, view.Item.ID, view.Item.Name)), | |
), | |
elem.Div( | |
vecty.Markup(vecty.Class("progress")), | |
elem.Div( | |
vecty.Markup( | |
vecty.Class("progress-bar"), | |
vecty.Style("width", fmt.Sprintf("%.2f%%", float64(view.Index+1)*100/float64(view.Count))), | |
), | |
), | |
), | |
) | |
} | |
func max(a, b int) int { | |
if a > b { | |
return a | |
} | |
return b | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment