Skip to content

make syntax less verbose#132

Open
bjartek wants to merge 1 commit into
chasefleming:mainfrom
bjartek:use-props-as-node
Open

make syntax less verbose#132
bjartek wants to merge 1 commit into
chasefleming:mainfrom
bjartek:use-props-as-node

Conversation

@bjartek

@bjartek bjartek commented Mar 28, 2024

Copy link
Copy Markdown
  • 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

bjartek commented Mar 28, 2024

Copy link
Copy Markdown
Author

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

@bjartek

bjartek commented Mar 28, 2024

Copy link
Copy Markdown
Author

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.

@chrismalek

Copy link
Copy Markdown

@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.

@chasefleming

Copy link
Copy Markdown
Owner

@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.")),
)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants