Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make syntax less verbose #132

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bjartek
Copy link

@bjartek bjartek commented Mar 28, 2024

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

bjartek commented Mar 28, 2024

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

@bjartek
Copy link
Author

bjartek commented Mar 28, 2024

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

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