4 ms·
Show HN: Hyphen – an elegant custom element base class with good ergonomics
- rco8786 3y agoI read the entire README and have absolutely zero idea what Hypen does (and I am a relatively experienced javascript/react programmer). Perhaps a 1-2 sentence explanation, plus some code examples, would help.
- dmix 3y agoI didn't understand it until I looked at the index.html source code example (code > words sometimes) and I think it's a thin wrapper around Web components https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements https://developer.mozilla.org/en-US/docs/Web/API/Web_compone... But don't quote me on that, I just use Vue for a living
- deanebarker 3y agoThis is correct. It's a wrapper around web components, kind of like Lit (which they credit in the acknowledgements). Web components are cool, but the spec is bare. This means, a lot of rote work gets custom implemented. There are a lot of frameworks popping up, trying to "standardize" this. Hyphen appears to one of these.
- dhimes 3y agoThank you. That would be a great leading paragraph on their web page if anyone from the project is reading this.
- andybak 3y agoMe neither. "An elegant custom element base class" could relate to any programming language. I skim the page and see angle brackets - so it's browser-related. Great - but for what? Is this client-side, server-side? Does it relate to a specific framework or approach? I don't blame the project. They know their audience. It's the HN policy where extra context in the post title is forbidden that hurts stuff like this.
- keepamovin 3y agoThanks, its good feedback! :)
- keepamovin 3y agoPoint taken, good idea on the summary and code to all who provided that feedback. Indeed code > words sometimes! That was silly of me! I’m sorry, I’ll correct that. Thank y’all for pointing that out! :)
- croes 3y ago>Hyphen simplifies the creation of custom Web Components, which are a standard for reusable user interface elements for web pages. Was that part added later?
- rco8786 3y agoYes
- deleted 3y ago[deleted]
- verisimilidude 3y agoIt's really clear to me. (And I did account for the subsequent README commits since your comment). But that's probably because I already know Web Components well. I'm in the market. In the following line... > Hyphen - A custom element base class for great developer ergonomics. ...I would recommend adding a link on "custom element" that points to a definition somewhere. This might make it easier for skimmers to parse the meaning of this opening line.
- keepamovin 3y agoThank you, sir! I'll consider adding that link. What do you think of this? I'd love to hear your thoughts on how it could be improved in the issues or discussions, or just here! :)
- garrettjoecox 3y agoI’d suggest not naming the your main interface of your library “$”, for various obvious reasons.
- regularfry 3y agoObvious if you've been around long enough. I wouldn't be surprised if the majority of people reading this have never actually worked with jquery, only heard the bedtime horror stories. (I mean, leaving aside the fact that it's utterly nondescriptive...)
- andybak 3y agoI still rather miss it. Sniff...
- dr_kiszonka 3y agoI used jQuery in a few personal projects and it was very useful. It was 10 - 15 years ago, so I am sure there are better tools now, but for a hobbyist it was great.
- andybak 3y agoThe syntax still seems much more pleasant for DOM manipulation than vanillaJS. It had a nice pseudo-functional interface and a ton of elegant helpers. Whenever I do DOM manipulation in vanilla I wonder to myself if the API couldn't be a bit more user-friendly and readable.
- eternityforest 3y agoI think I'd definitely use some kind of library if I still had to touch the DOM heavily. I liked JQuery, but I like reactive frameworks a lot more.
- troupo 3y agoFor low-level-ish work (like DOM manipulation) there are still no better tools than jQuery
- morbicer 3y agoFair warning about the use of eval in the readme. Anyone caring about CSP headers can't use this project and that anyone should be everyone even though the CSP header is PITA to setup.
- keepamovin 3y agoThat's a good point re CSP, while I don't want to restrict it, I also don't want to restrict developers. For example, unsafe-inline may even be required as we use inline event handler syntax. But complying with a requirement to remove unsafe-inline that some may have, would produce a worse developer experience, as you could no longer write inline handlers. I could implement turning those inlines into addEventListener calls, as I've done in good.html, but I just want to keep everything simple here, at least for now. Trade offs, ha! :) Will `new Function` also be covered by the same unsafe-eval CSP restriction? I know I could just search, but I suspect more discussion with you will be fruitful.
- morbicer 3y agoImho same problem as eval and on top of that it creates the function only in the global scope. Honestly it's a construct I've never seen out in the wild so I am not an expert.
- catapart 3y agoOdd that they chose to use the unsafe "eval", where there's a drop-in, safe(r) replacement with ``` const properties = Object.keys(injections); const values = Object.values(injections); htmlString = new Function(...properties, 'return `' + templateString + '`')(...values); ``` Other than that weirdness, this is a pretty sweet little library! I've already built my own version of it, so it's nice to see a lot of the same techniques I used re-applied here. Definitely feels like doing something right if other people are thinking along the same lines! I hope this more project sees more utility than mine! I backpedaled on the idea of 'wrapping' custom web components after I figured that learning my abstractions wasn't any different than learning native web component abstractions (which I couldn't justify). I definitely think there are good uses for this type of thing, but it was very hard to scale it to app level and not have to build data-oriented abstractions which started mildly coupling components (on the interface level, rather than as a requirement for rendering). So once I started breaking down the components abstract enough pieces that they were useful as standalone components, I wasn't using any of my custom functionality, so I just migrated it all back to native, not-extended-from-my-library components. I'm very eager to see mature projects using these kinds of wrappers, though, because I do think there are ways to push the native web-component experience further, and I think these are the kinds of projects that help with that.
- keepamovin 3y agoThank you for your kind words about Hyphen, catapart! And that is a very sweet little thing, the code. I like your bloody clever little code hack, that is pretty cool. I modified it a little to fit: return new Function( ...Object.keys(this.state), ` return (function ${ this.template.toString() .replace(/^\s*function\s+/,'') }()) `)(...Object.values(this.state)); I might consider including it. Would you like to make the PR? If you don't want to get involved more than that, I get it! No worries. But I mean this you do care about this, and this is your work. It's very clever! My concern is it will fail in some cases that with(state)/eval works in, and require annoying edge-case patching to support cases we already support. For dubious security gains (I don't see the risk of eval here--if anyone can point so specifics I'd value learning!) As to why I didn't choose it, I didn't see it (!!), but I also didn't think I should bother trying to get all properties and values because there are some that can slip through (say state contained some prototype chain and had properties up the chain, these will not be returned by keys). So I just went with with(state), because I know that's safe regarding that. with(state)/eval is simple, neat and elegant. Also I just like the aesthetics of the simpler shorter code. And I guess I like to live dangerously, hahahaha! So those reasons are why I chose it. But your way is very clever! And it looks very clean the way you present it above. I love to see these new, like, tight, idiomatic ways of using JavaScript to push the language. I love what you've done, it's really cool! :) Also, it's good to hear about your experience with that. I encourage you to keep working on your library. I mean, a diversity of approaches is valuable. Even if it's just we're researching and learning and progressing toward something, through our collective effort, even if none of it is actually the destination. Although with multiple demands on your time, I'm sure it makes sense to pull back if you don't see it as worth it. If you want to contribute more of your really clever ideas and experience to Hyphen that would be cool. Maybe not everything is a fit but we could certainly learn from you. Up to you! Thanks for your cool comment on this. High value! Have a good day :)
- spankalee 3y agoHow does something like this work: template() { return ` <span>${greeting}, ${host.name}!</span> <button onclick="changeGreeting">Change Greeting</button> `; } I don't see `greeting` or `host` defined anywhere in scope, so this should be an error. Did you forget to prefix them with `this.`?
- evan_ 3y agoHere’s the function that evaluates the template: with (this.state) { return eval(`(function ${this.template.toString().replace(/^\s*function\s+/,'')}())`); } So it’s effectively turning everything in `this.state` into a local var and evaluating the code in the template function in that context.
- spankalee 3y agoThat's so difficult for humans and tools to reason about. It would completely break intellisense and lots of lint rules. What's the reason for this?