Skip to content

Instantly share code, notes, and snippets.

@atdiar
Last active June 9, 2022 13:45
Show Gist options
  • Save atdiar/725844d85d4b9835c58f7f834570ab69 to your computer and use it in GitHub Desktop.
Save atdiar/725844d85d4b9835c58f7f834570ab69 to your computer and use it in GitHub Desktop.
Flavors of declarative UIs
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")),
),
),
),
),
),
)
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(),)
// 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")
@atdiar
Copy link
Author

atdiar commented Jun 9, 2022

Ref is a way to store an element defined in the tree out of the tree.
The variables should be defined before the tree is constructed.

        var AppSection *ui.Element
	var MainSection *ui.Element
	var MainFooter *ui.Element
	var todosinput *ui.Element
	var ToggleAllInput *ui.Element
	var TodosList *ui.Element
	var TodoCount *ui.Element
	var FilterList *ui.Element
	var ClearCompleteButton *ui.Element

goes on top.
I'm pretty happy about that part because I needed to be able to reference elements so that I can make them subscribe to each other data/state mutations for reactivity.

The UI event handlers can go anywhere if defined as top-level functions:

        toggleallhandler:= ui.NewEventHandler(func(evt ui.Event) bool {
		togglestate, ok := evt.Target().GetData("checked")
		if !ok {
			evt.Target().Set("event", "toggled", ui.Bool(true))
			return false
		}
		ts := togglestate.(ui.Bool)
		evt.Target().Set("event", "toggled", !ts)
		return false
	})

	ClearCompleteHandler := ui.NewEventHandler(func(evt ui.Event) bool {
		ClearCompleteButton:= evt.Target()
		ClearCompleteButton.Set("event", "clear", ui.Bool(true))
		return false
	})

@atdiar
Copy link
Author

atdiar commented Jun 9, 2022

Note: these are fully expanded tree.
In a typical application, intermediate subtrees/ components can be defined.
This should mitigate the expansion of the tree to the right due to indentation. (a few people have been critical of this on reddit)

The second example requires a slice of *Element modifying functions (Modifiers) as second argument. Hence, we use Modifier() to denote the absence of modifiers instead of nil.

@atdiar
Copy link
Author

atdiar commented Jun 9, 2022

  • 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 become func(*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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment