Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created an HTMX extension for astro 3.0 view transition #1761

Open
AceCodePt opened this issue Sep 4, 2023 · 0 comments
Open

Created an HTMX extension for astro 3.0 view transition #1761

AceCodePt opened this issue Sep 4, 2023 · 0 comments

Comments

@AceCodePt
Copy link

AceCodePt commented Sep 4, 2023

My Take: Why?

This is my way to thanks HTMX for making my life much easier.
I would like to make HTMX more relevant in this modern framework based era.
I also think that having some sense of animations to HTMX looks pretty. (I couldn't find one that did the same using HTMX)

How?

This is currently how the extension is defined.
You can copy and paste or use a gist.

<script>
      htmx.defineExtension("hx-astro-view-transition", {
        onEvent: function (name, evt: any) {
          if (name === "htmx:afterRequest") {
            const attributes = evt.target.attributes;
            const viewTransitionTarget =
              attributes["hx-view-transition"]?.value;
            if (!viewTransitionTarget) {
              return;
            }
            const a = document.createElement("a");
            a.href = viewTransitionTarget;
            document.body.appendChild(a);
            a.dispatchEvent(
              new PointerEvent("mouseenter", {
                bubbles: true,
                cancelable: true,
              }),
            );
            a.dispatchEvent(
              new PointerEvent("click", {
                bubbles: true,
                cancelable: true,
              }),
            );
          }
        },
      });
      // Runs on view transitions navigation
      document.addEventListener("astro:after-swap", () =>
        htmx.process(document),
      );
    </script>

Add it to the body

<body hx-ext="hx-astro-view-transition">

An example

<form
        hx-post="/api/signIn"
        hx-view-transition="/"
        hx-swap="none"
      >

Note: if you are using SSR

You might want to add the following in the layout.astro:

Astro.response.headers.set(
  "Cache-Control",
  "no-cache, no-store, must-revalidate",
);
Astro.response.headers.set("Pragma", "no-cache");
Astro.response.headers.set("Expires", "0");
@AceCodePt AceCodePt changed the title I created an extension for astro 3.0 view transition I created an HTMX extension for astro 3.0 view transition Sep 4, 2023
@AceCodePt AceCodePt changed the title I created an HTMX extension for astro 3.0 view transition Created an HTMX extension for astro 3.0 view transition Sep 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant