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

Syntax goals #259

Closed
jfbrennan opened this issue Apr 25, 2024 · 5 comments
Closed

Syntax goals #259

jfbrennan opened this issue Apr 25, 2024 · 5 comments

Comments

@jfbrennan
Copy link

jfbrennan commented Apr 25, 2024

I'm new to htm. I like it. I chose it because it can be used with Preact in a no-build environment, i.e. our micro-frontend app will use https://esm.sh/htm/preact/standalone at runtime.

However, I'm a bit confused as to whether htm wants to be more like HTML or JSX. The docs say JSX-like, but there's differences...and those differences seem to more align with vanilla HTML and JavaScript. Well, sort of.

I see support for class instead of className, which is great and I understand that's more of a Preact thing, but then I see onClick used here in the docs instead of onclick. Both work, and I prefer the HTML style, but it feels like htm doesn't know what it wants to be. This is made more confusing because void elements like <img> don't work. You apparently have to self-close them as if this were XML. But then non-void elements like <div></div> can be self-closed, which is not valid HTML.

Are there docs of some kind that go into this in more detail? I feel like I'm gonna keep finding more gotchas like when you first start using JSX. Can't a dev just write some HTML anymore😭

@dy
Copy link

dy commented Apr 25, 2024

HTM is for JSX and is highly optimized for that. It's recommended you avoid HTML quirks and stick to minimal JSX syntax. If you still need HTML you can have a look at xhtm.

@rschristian
Copy link

rschristian commented Apr 25, 2024

It's very much an attempt to provide a similar experience to authoring JSX in plain JS. A few HTML attributes are encouraged, but in general, the idea is to use JS properties.

Can't a dev just write some HTML anymore

You absolutely can. I'm not sure HTM is really the best at facilitating that, but you can get a pretty nice experience out of template literals & .innerHTML these days. Certainly are security risks, but, hard to beat stringified HTML if that's what you're after.

@jfbrennan
Copy link
Author

I'm looking for templates that work with Preact and support valid HTML, e.g. class, onclick, <img>. Very much what the original goal of HTM apparently is/was: something that "felt natural for use untranspiled in the browser."

If it is capable of other proprietary stuff like <//> that's cool too, but I want to write valid HTML without having to think twice about it. HTM seems close enough, XHTM shared by @dy looks like it is maybe even closer...docs just don't say enough though.

Anyway, thanks for the replies.

@rschristian
Copy link

If you need actual HTML, __dangerouslySetInnerHTML might be the solution.

The 'X' in JSX is XML. HTM is certainly closer to HTML than standard JSX, but if you want actual HTML, you're left with strings & reassembling yourself or using __dangerouslySetInnerHTML to pass these strings to Preact.

@jfbrennan
Copy link
Author

No I don't need that. I'm all good, I was just under the impression that HTM supported 100% valid HTML syntax. It's closer to HTML than JSX, like you said, and that's really nice, I just wish it went all the way.

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

No branches or pull requests

3 participants