Last active
June 9, 2022 13:45
-
-
Save atdiar/725844d85d4b9835c58f7f834570ab69 to your computer and use it in GitHub Desktop.
Flavors of declarative UIs
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
ui.New(doc.NewDocument("Todo-App"), | |
Children( | |
E(doc.NewSection("todoapp", "todoapp"), | |
Ref(&AppSection), | |
CSS("todoapp"), | |
Children( | |
E(doc.NewHeader("header", "header"), | |
CSS("header"), | |
Children( | |
E(doc.NewH1("todo", "apptitle").SetText("Todo")), | |
E(NewTodoInput("todo", "new-todo"), | |
Ref(&todosinput), | |
CSS("new-todo"), | |
), | |
), | |
), | |
E(doc.NewSection("main", "main"), | |
Ref(&MainSection), | |
CSS("main"), | |
Children( | |
E(doc.NewInput("checkbox", "toggle-all", "toggle-all"), | |
Ref(&ToggleAllInput), | |
CSS("toggle-all"), | |
Listen("click",toggleallhandler,doc.NativeEventBridge), | |
), | |
E(doc.NewLabel("toggle-all-Label", "toggle-all-label").For(ToggleAllInput.AsElement())), | |
E(NewTodosListElement("todo-list", "todo-list", doc.EnableLocalPersistence()), | |
Ref(&TodosList), | |
InitRouter(Hijack("/","all"),doc.RouterConfig), | |
), | |
), | |
), | |
E(doc.NewFooter("footer", "footer"), | |
Ref(&MainFooter), | |
CSS("footer"), | |
Children( | |
E(NewTodoCount("todo-count", "todo-count"), Ref(&TodoCount)), | |
E(NewFilterList("filters", "filters"), Ref(&FilterList)), | |
E(ClearCompleteBtn("clear-complete", "clear-complete"), | |
Ref(&ClearCompleteButton), | |
Listen("click",ClearCompleteHandler,doc.NativeEventBridge), | |
), | |
), | |
), | |
), | |
), | |
E(doc.NewFooter("infofooter", "infofooter"), | |
CSS("info"), | |
Children( | |
E(doc.NewParagraph("editinfo", "editinfo").SetText("Double-click to edit a todo")), | |
E(doc.NewParagraph("createdWith", "createdWith").SetText("Created with: "), | |
Children( | |
E(doc.NewAnchor("particleui", "particleui").SetHREF("http://github.com/atdiar/particleui").SetText("ParticleUI")), | |
), | |
), | |
), | |
), | |
), | |
) |
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
ui.New( | |
doc.NewDocument("Todo-App"), | |
Modifier(), | |
E(doc.NewSection("Appsection","todoapp"), | |
Modifier(Ref(&AppSection), CSS("todoapp"),), | |
E(doc.NewHeader("mainheader","header"), | |
Modifier(CSS("header")), | |
E(doc.NewH1("todo", "apptitle").SetText("Todo"), Modifier()), | |
E(NewTodoInput("todo", "new-todo"), Modifier(Ref(&todosinput),CSS("new-todo"))), | |
), | |
E(doc.NewSection("mainsection","main"), | |
Modifier(Ref(&MainSection), CSS("main")), | |
E(doc.NewInput("checkbox", "toggle-all", "toggle-all"), | |
Modifier( | |
Ref(&ToggleAllInput), | |
CSS("toggle-all"), | |
Listen("click",toggleallhandler,doc.NativeEventBridge), | |
), | |
), | |
E(doc.NewLabel("toggle-all-Label", "toggle-all-label").For(ToggleAllInput.AsElement()),Modifier()), | |
E(NewTodosListElement("todo-list", "todo-list", doc.EnableLocalPersistence()), | |
Modifier(Ref(&TodosList), InitRouter(Hijack("/","all"),doc.RouterConfig)), | |
), | |
E(doc.NewFooter("mainfooter","footer"), | |
Modifier(Ref(&MainFooter), CSS("footer"),), | |
E(NewTodoCount("todo-count", "todo-count"),Modifier()), | |
E(Filterlist,Modifier()), | |
E(ClearCompleteBtn("clear-complete", "clear-complete"), | |
Modifier( | |
Ref(&ClearCompleteButton), | |
Listen("click",ClearCompleteHandler,doc.NativeEventBridge), | |
), | |
), | |
), | |
), | |
E(doc.NewFooter("Appfooter","infofooter"), | |
Modifier(CSS("info"),), | |
E(doc.NewParagraph("editinfo","editinfo").SetText("Double-click to edit a todo"), Modifier()), | |
E(doc.NewParagraph("createdWith,createdWith").SetText("Created with: "), | |
Modifier(), | |
E(doc.NewAnchor("particleui", "particleui").SetHREF("http://github.com/atdiar/particleui").SetText("ParticleUI"), NoModifier), | |
), | |
), | |
), | |
), Modifier(),) |
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
// 1. Create a new document | |
Document := doc.NewDocument("Todo-App") | |
AppSection := doc.NewSection("todoapp", "todoapp") | |
AppFooter := doc.NewFooter("infofooter", "infofooter") | |
Document.SetChildren(AppSection, AppFooter) | |
// 2. Build AppSection | |
MainHeader := doc.NewHeader("header", "header") | |
MainSection := doc.NewSection("main", "main") | |
MainFooter := doc.NewFooter("footer", "footer") | |
AppSection.SetChildren(MainHeader, MainSection, MainFooter) | |
// 3. Build MainHeader | |
MainHeading := doc.NewH1("todo", "apptitle").SetText("Todo") | |
todosinput := NewTodoInput("todo", "new-todo") | |
MainHeader.SetChildren(MainHeading, todosinput) | |
// 4. Build MainSection | |
ToggleAllInput := doc.NewInput("checkbox", "toggle-all", "toggle-all") | |
ToggleAllInput.AsElement().AddEventListener("click", ui.NewEventHandler(func(evt ui.Event) bool { | |
togglestate, ok := ToggleAllInput.AsElement().GetData("checked") | |
if !ok { | |
ToggleAllInput.AsElement().Set("event", "toggled", ui.Bool(true)) | |
return false | |
} | |
ts := togglestate.(ui.Bool) | |
ToggleAllInput.AsElement().Set("event", "toggled", !ts) | |
return false | |
}), doc.NativeEventBridge) | |
ToggleLabel := doc.NewLabel("toggle-all-Label", "toggle-all-label").For(ToggleAllInput.AsElement()) | |
TodosList := NewTodosListElement("todo-list", "todo-list", doc.EnableLocalPersistence()) | |
todolistview, ok := TodosList.AsViewElement() | |
if !ok { | |
panic("Expected TodosList to be constructed as a ViewElement") | |
} | |
MainSection.SetChildren(ToggleAllInput, ToggleLabel, TodosList) | |
// 5. Build MainFooter | |
TodoCount := NewTodoCount("todo-count", "todo-count") | |
FilterList := NewFilterList("filters", "filters") | |
// links | |
router := ui.NewRouter("/", todolistview,doc.RouterConfig) | |
router.Hijack("/", "/all") | |
linkall := router.NewLink(todolistview, "all") | |
linkactive := router.NewLink(todolistview, "active") | |
linkcompleted := router.NewLink(todolistview, "completed") | |
allFilter := NewFilter("All", "all-filter", linkall) | |
activeFilter := NewFilter("Active", "active-filter", linkactive) | |
completedFilter := NewFilter("Completed", "completed-filter", linkcompleted) | |
FilterList.SetFilterList(allFilter, activeFilter, completedFilter) | |
ClearCompleteButton := ClearCompleteBtn("clear-complete", "clear-complete") | |
ClearCompleteButton.AsElement().AddEventListener("click", ui.NewEventHandler(func(evt ui.Event) bool { | |
ClearCompleteButton.AsElement().Set("event", "clear", ui.Bool(true)) | |
return false | |
}), doc.NativeEventBridge) | |
MainFooter.SetChildren(TodoCount, FilterList, ClearCompleteButton) | |
// 6.Build AppFooter | |
editinfo := doc.NewParagraph("editinfo", "editinfo").SetText("Double-click to edit a todo") | |
createdWith := doc.NewParagraph("createdWith", "createdWith").SetText("Created with: ").SetChildren(doc.NewAnchor("particleui", "particleui").SetHREF("http://github.com/atdiar/particleui").SetText("ParticleUI")) | |
AppFooter.SetChildren(editinfo, createdWith) | |
//css | |
doc.AddClass(AppSection.AsElement(), "todoapp") | |
doc.AddClass(AppFooter.AsElement(), "info") | |
doc.AddClass(MainHeader.AsElement(), "header") | |
doc.AddClass(MainSection.AsElement(), "main") | |
doc.AddClass(MainFooter.AsElement(), "footer") | |
doc.AddClass(todosinput.AsElement(), "new-todo") | |
doc.AddClass(ToggleAllInput.AsElement(), "toggle-all") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The top example is simply function composition.
E
is defined as:func(e *Element, modifiers ...func(*Element)*Element) *Element
So
Children
is just a mere Element modifier function like CSS and Listen and whatnot.The middle example does not directly use function composition. Children elements are passed as argument (variadically).
The signature of
E
has becomefunc(*Element, modifiers []func(e *Element)*Element, children ...*Element) *Element
the bottom example is just to show how it would look if the app was designed imperatively only.