L&D experiment on replacing JS with Hyperscript #89
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hyperscript is a JS library developed by the same team as HTMX along the same principles, with "Locality of Behaviour" being key. Rather than having JS in a separate file affecting HTML elsewhere, the intention is to keep the code that affects change in the same location as the elements that need changing.
It does this with a natural language approach. In a lot of ways, it is simpler than much of the JS it is replacing and it is easy to see at a glance what it is doing. Writing it however is harder, as it is effectively a whole new language, which is a barrier to adoption. I was able to replace most of the JS, including for opening and closing the additional details in the Invoices table, but found it hard to work out the syntax to replace the error validation handling.
Another benefit of Hyperscript and Locality of Behaviour is the code is included in the HTML returned from the server. This gets round the issue of needing to place all scripts within the
htmx.onLoad
function to reestablish event listeners.In summary, it is an odd language but has some really cool features and potential benefits. While I would be keen to adopt it (because I like odd things - I'd also be keen to write everything in Lisp for example), I'm not necessarily saying we definitely should. Much like HTMX though, it is opt-in, in that it doesn't stop you from using vanilla JS in any way, so it can be used as a progressive enhancement rather than a replacement.