Compare these two approaches, one using vanilla js with HTMX and one using hyperscript. Explain the syntax of Hyperscript carefully to a programmer that has no knowledge of hyeprscript
<plain-js-example>
<button id="contacts-btn" hx-get="/contacts" hx-target="body"> β
Get Contacts
</button>
<button onclick="document.getElementById('contacts-btn').dispatchEvent(new Event('htmx:abort'))"> β‘
Cancel
</button>
</plain-js-example>
<hyperscript-example>
<button id="contacts-btn" hx-get="/contacts" hx-target="body">
Get Contacts
</button>
<button _="on click send htmx:abort to #contacts-btn
on htmx:beforeRequest from #contacts-btn remove @disabled from me
on htmx:afterRequest from #contacts-btn add @disabled to me">
Cancel
</button>
</hyperscript-example>
Let's compare these two approaches and then delve into the syntax of Hyperscript.
Comparison:
-
Vanilla JS with HTMX:
- The first button (β ) uses HTMX attributes (
hx-get
andhx-target
) to make an AJAX request. - The second button (β‘) uses vanilla JavaScript to abort the HTMX request by dispatching a custom event.
- This approach requires understanding of both HTMX attributes and JavaScript DOM manipulation.
- The first button (β ) uses HTMX attributes (
-
Hyperscript:
- The first button remains the same, using HTMX attributes.
- The second button uses Hyperscript (prefixed with
_
) to handle the abort action and manage its disabled state. - This approach is more declarative and handles both the abort action and button state management in one place.
Now, let's explain the Hyperscript syntax in detail:
Hyperscript Syntax Explanation:
_="on click send htmx:abort to #contacts-btn
on htmx:beforeRequest from #contacts-btn remove @disabled from me
on htmx:afterRequest from #contacts-btn add @disabled to me"
-
The underscore
_
attribute is used to denote Hyperscript code. -
Each line in the Hyperscript code represents a separate event handler or action:
a.
on click send htmx:abort to #contacts-btn
on click
: This listens for a click event on the current element (the Cancel button).send htmx:abort to #contacts-btn
: When clicked, it sends thehtmx:abort
event to the element with idcontacts-btn
.
b.
on htmx:beforeRequest from #contacts-btn remove @disabled from me
on htmx:beforeRequest from #contacts-btn
: This listens for thehtmx:beforeRequest
event from thecontacts-btn
element.remove @disabled from me
: When the event occurs, it removes thedisabled
attribute from the current element (me
refers to the Cancel button).
c.
on htmx:afterRequest from #contacts-btn add @disabled to me
on htmx:afterRequest from #contacts-btn
: This listens for thehtmx:afterRequest
event from thecontacts-btn
element.add @disabled to me
: When the event occurs, it adds thedisabled
attribute to the current element.
Key Hyperscript concepts:
- Events are specified using
on [event name]
. send [event] to [target]
is used to trigger custom events.- Element selectors use CSS syntax (e.g.,
#contacts-btn
for id). me
refers to the current element.- Attributes are prefixed with
@
(e.g.,@disabled
). - Actions like
add
,remove
,toggle
are used to manipulate attributes or classes.
This Hyperscript approach provides a more concise and readable way to handle complex interactions, combining event listening, custom event dispatching, and attribute manipulation in a single, declarative syntax.
Can you explain the @disabled
attribute more? How does it work? Also, what is going on with the _=
, specifically the equals sign?
Certainly! Let's dive deeper into the @disabled
attribute and the _=
syntax in Hyperscript.
The @disabled
Attribute:
In Hyperscript, the @
symbol is used to refer to HTML attributes. So @disabled
specifically refers to the HTML disabled
attribute.
-
Purpose: The
disabled
attribute is a boolean attribute that, when present on an element (usually form controls like buttons or inputs), makes that element non-interactive and visually indicates this state. -
Functionality:
- When you
add @disabled to me
, it's equivalent to settingelement.disabled = true
in JavaScript. - When you
remove @disabled from me
, it's equivalent to settingelement.disabled = false
or removing the attribute entirely.
- When you
-
Effect:
- A disabled button can't be clicked and often appears grayed out (exact styling depends on the browser and any CSS you've applied).
- In this context, the Cancel button becomes disabled after sending the abort signal, preventing multiple abort attempts.
The _=
Syntax:
The _=
is part of how Hyperscript is integrated into HTML elements.
-
The underscore
_
:- This is the default attribute name for Hyperscript code.
- It's chosen because it's short and unlikely to conflict with other attributes.
-
The equals sign
=
:- This is standard HTML syntax for assigning a value to an attribute.
- In this case, it's assigning the Hyperscript code to the
_
attribute.
-
Together
_=
:- This tells the Hyperscript parser "everything following this is Hyperscript code".
- It's similar to how you might use
onclick=
for inline JavaScript, but_=
is for Hyperscript.
-
Usage:
<button _="[Hyperscript code goes here]">Cancel</button>
-
Alternative: If you prefer, you can use a different attribute name by configuring Hyperscript. For example, some people use
script=
instead of_=
for clarity:<button script="[Hyperscript code goes here]">Cancel</button>
In the context of your example:
<button _="on click send htmx:abort to #contacts-btn
on htmx:beforeRequest from #contacts-btn remove @disabled from me
on htmx:afterRequest from #contacts-btn add @disabled to me">
Cancel
</button>
This syntax tells the browser:
- This button has associated Hyperscript code.
- The code defines behavior for click events and responses to HTMX events.
- Part of that behavior involves manipulating the
disabled
state of the button itself.
The use of _=
allows for this rich, declarative behavior to be defined directly in the HTML, combining event handling and attribute manipulation in a concise, readable manner.