elem-go icon indicating copy to clipboard operation
elem-go copied to clipboard

make syntax less verbose

Open bjartek opened this issue 1 year ago • 4 comments

  • make attrs.Props a Node
  • when parsing new element check if first is props and if it is use it if else empty props.
  • some builders are now not 100% correct like TextArea can take any Node, this can be fixed though with some simple guards inside that one to allow only 1 textNode and props.
  • fixed tests to remove nil

bjartek avatar Mar 28 '24 00:03 bjartek

if you want to send in more Props here you could even allow merging them all in newElement.

bjartek avatar Mar 28 '24 01:03 bjartek

or you could even create a method to generate a single Prop and write code like

Div(Name("foo"), Width("100px"), TextNode("foo")) not sure if that is a good idea though.

bjartek avatar Mar 28 '24 01:03 bjartek

@bjartek If you are looking for that Div(... syntax, you might want to check out https://www.gomponents.com, which is an alternative to this library. I have competing prototypes in development using this library and https://www.gomponents.com with a few of the + packages (see header on website).

This is a great library, as is the other. There are pros and cons to each.

chrismalek avatar Mar 28 '24 02:03 chrismalek

@bjartek, I'm intrigued by removing the nil concept for the attrs argument. One potential hiccup might be if someone places attrs in the middle of element children like:

content := elem.Div(
    elem.H1(nil, elem.Text("Hello, Elem!")),
    attrs.Props{
        attrs.ID:    "container",
        attrs.Class: "my-class",
    },
    elem.H2(nil, elem.Text("Subheading")),
    elem.P(nil, elem.Text("This is a paragraph.")),
)

I guess technically, this syntax still works, but I'm curious if it feels strange. Also, I'm wondering if this constitutes a breaking change. Moving some functions or attributes to an options subpackage might be.

Regarding the Div syntax that @chrismalek is discussing, one can eliminate the elem. package prefix by using the following dot import syntax, if they prefer a cleaner look:

import (
    . "github.com/chasefleming/elem-go"
    "github.com/chasefleming/elem-go/attrs"
    "github.com/chasefleming/elem-go/styles"
)

content := Div(attrs.Props{
    attrs.ID:    "container",
    attrs.Class: "my-class",
},
    H1(nil, Text("Hello, Elem!")),
    H2(nil, Text("Subheading")),
    P(nil, Text("This is a paragraph.")),
)

chasefleming avatar Mar 28 '24 18:03 chasefleming