Skip to content

Instantly share code, notes, and snippets.

@hamelsmu
Last active September 21, 2024 17:31
Show Gist options
  • Save hamelsmu/041843745232827440e4e5421f93c12c to your computer and use it in GitHub Desktop.
Save hamelsmu/041843745232827440e4e5421f93c12c to your computer and use it in GitHub Desktop.

Comparing Vanilla JS with HTMX and Hyperscript

πŸ§‘ human (Sep 21, 2024, 10:10 AM)

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>

πŸ€– assistant (Sep 21, 2024, 10:10 AM)

Let's compare these two approaches and then delve into the syntax of Hyperscript.

Comparison:

  1. Vanilla JS with HTMX:

    • The first button (β‘ ) uses HTMX attributes (hx-get and hx-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.
  2. 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"
  1. The underscore _ attribute is used to denote Hyperscript code.

  2. 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 the htmx:abort event to the element with id contacts-btn.

    b. on htmx:beforeRequest from #contacts-btn remove @disabled from me

    • on htmx:beforeRequest from #contacts-btn: This listens for the htmx:beforeRequest event from the contacts-btn element.
    • remove @disabled from me: When the event occurs, it removes the disabled 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 the htmx:afterRequest event from the contacts-btn element.
    • add @disabled to me: When the event occurs, it adds the disabled 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.

πŸ§‘ human (Sep 21, 2024, 10:13 AM)

Can you explain the @disabled attribute more? How does it work? Also, what is going on with the _=, specifically the equals sign?

πŸ€– assistant (Sep 21, 2024, 10:13 AM)

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.

  1. 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.

  2. Functionality:

    • When you add @disabled to me, it's equivalent to setting element.disabled = true in JavaScript.
    • When you remove @disabled from me, it's equivalent to setting element.disabled = false or removing the attribute entirely.
  3. 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.

  1. 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.
  2. 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.
  3. 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.
  4. Usage:

    <button _="[Hyperscript code goes here]">Cancel</button>
  5. 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:

  1. This button has associated Hyperscript code.
  2. The code defines behavior for click events and responses to HTMX events.
  3. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment