deco-cx / community

Deco community material
MIT License
1 stars 0 forks source link

Introducing `useScript`: Simplifying Event Handling with HTMX #12

Closed tlgimenes closed 4 months ago

tlgimenes commented 5 months ago

Introducing useScript: Simplifying Event Handling with HTMX

At deco.cx, we are always striving to make web development more efficient and effective for developers. Today, we are excited to introduce a new utility function: useScript. This function is designed to streamline the process of handling events on HTML elements by leveraging the powerful hx-on: handlers from HTMX.

What is useScript?

useScript is a utility function that takes a JavaScript function and its arguments, and returns a stringified, minified version of the function. This allows developers to inline scripts directly into their HTML with minimal payload, which is especially useful for event handling.

Why useScript is Incredibly Useful

With HTMX, developers can create dynamic, server-rendered HTML pages with ease. However, when it comes to handling client-side events, there’s often a need to include small pieces of JavaScript. This is where useScript shines. By using useScript, you can add JavaScript only where necessary, avoiding the overhead of a full client-side framework like React.

Example: Inline Script with hx-on:click

Let's look at a simple example where useScript is used to handle a click event:

import { useScript } from "apps/utils/useScript.ts";

const onClick = () => {
  event!.currentTarget.innerText = "Clicked!";
};

function ExampleButton() {
  return (
    <button hx-on:click={useScript(onClick)}>
      Click me
    </button>
  );
}

export default ExampleButton;

In this example, useScript takes the onClick function and inlines it into the hx-on:click attribute, making the button interactive without loading a large JavaScript framework.

Bridging the Gap Between Server and Client

useScript offers a unique balance between server-rendered and client-side interactions. By combining the strengths of HTMX for processing large HTML chunks on the server with the ability to add small, targeted JavaScript interactions, useScript delivers the best of both worlds. This approach allows developers to build performant, interactive web applications without the need for complex toolsets like React.

Notes and Limitations

While useScript is a powerful tool, there are a few things to keep in mind:

  1. No Build Tool: Since we don’t use a build tool, developers must ensure that their JavaScript functions are compatible with their target browsers. This means keeping your code in sync with your browserslist target.

  2. Scope and Dependencies: The function you pass to useScript should not rely on external variables or closures that won’t be available when the script is executed inline. Make sure the function is self-contained and does not depend on external state.

  3. Attribute Length: When using hx-on: handlers, ensure the minified function does not exceed any attribute length limits imposed by browsers or HTML specifications.

Conclusion

useScript is a valuable addition to our toolkit, enabling developers to add small, targeted JavaScript interactions to their server-rendered HTML. By leveraging the power of HTMX for large chunks of HTML and using useScript for small event handlers, you can create efficient, interactive web applications without the overhead of a full client-side framework. Try out useScript today and experience the best of both worlds in your web development projects.

For more details and documentation, visit our documentation page or reach out to our support team at support@deco.cx.

Happy coding!

cecilia-marques commented 5 months ago

Introducing useScript: simplifying event handling with HTMX

At deco.cx, we are always making web development more efficient and effective for developers. Today, we are excited to introduce a new utility function: useScript. This function is designed to streamline the process of handling events on HTML elements by leveraging the powerful hx-on: handlers from HTMX.

What is useScript?

useScript is a utility function that takes a JavaScript function and its arguments, and returns a stringified, minified version of the function. This allows developers to inline scripts directly into their HTML with minimal payload, which is especially useful for event handling.

Why is useScript incredibly useful?

With HTMX, developers can create dynamic, server-rendered HTML pages with ease. However, when it comes to handling client-side events, there’s often a need to include small pieces of JavaScript. This is where useScript shines. By using useScript, you can add JavaScript only where necessary, avoiding the overhead of a full client-side framework like React.

Example: Inline script with hx-on:click

Let's look at a simple example where useScript is used to handle a click event:

import { useScript } from "deco/hooks/useScript.ts";

const onClick = () => {
  event!.currentTarget.innerText = "Clicked!";
};

function ExampleButton() {
  return (
    <button hx-on:click={useScript(onClick)}>
      Click me
    </button>
  );
}

export default ExampleButton;

In this example, useScript takes the onClick function and inlines it into the hx-on:click attribute, making the button interactive without loading a large JavaScript framework.

Bridging the gap between server and client

useScript offers a unique balance between server-rendered and client-side interactions. By combining the strengths of HTMX for processing large HTML chunks on the server with the ability to add small, targeted JavaScript interactions, useScript delivers the best of both worlds. This approach allows developers to build performant, interactive web applications without the need for complex toolsets like React.

Notes and Limitations

While useScript is a powerful tool, there are a few things to keep in mind:

  1. No Build Tool: Since we don’t use a build tool, developers must ensure that their JavaScript functions are compatible with their target browsers. This means keeping your code in sync with your browserslist target.
  2. Scope and Dependencies: The function you pass to useScript should not rely on external variables or closures that won’t be available when the script is executed inline. Make sure the function is self-contained and does not depend on external state.
  3. Attribute Length: When using hx-on: handlers, ensure the minified function does not exceed any attribute length limits imposed by browsers or HTML specifications.

Conclusion

useScript is a valuable addition to our toolkit, enabling developers to add small, targeted JavaScript interactions to their server-rendered HTML. By leveraging the power of HTMX for large chunks of HTML and using useScript for small event handlers, you can create efficient, interactive web applications without the overhead of a full client-side framework. Try out useScript today and experience the best of both worlds in your web development projects.

For more details visit the useScript API reference.

Happy coding!