Hypa.Conn (Hypa v0.3.0)

View Source

Utilities to work with Plug.Conn structs that make it easier to use htmx.

Response headers

This module provides functions to add all htmx response headers.

HeaderFunction
HX-Locationput_hx_location/2
HX-Push-Urlput_hx_push_url/2
HX-Redirectput_hx_redirect/2
HX-Refreshput_hx_refresh/2
HX-Replace-Urlput_hx_replace_url/2
HX-Reswapput_hx_reswap/2
HX-Retargetput_hx_retarget/2
HX-Reselectput_hx_reselect/2
HX-Triggerput_hx_trigger/2
HX-Trigger-After-Settleput_hx_trigger_after_settle/2
HX-Trigger-After-Swapput_hx_trigger_after_swap/2

Summary

Functions

Adds the HX-Location response header which allows you to do a client-side redirect that does not do a full page reload.

Adds the HX-Push-Url response header which pushes a new URL into the history stack.

Adds the HX-Redirect response header which can be used to do a client-side redirect to a new location.

Adds the HX-Refresh response header which, when set to "true", will do a client-side full refresh of the page.

Adds the HX-Replace-Url response header which will replace the current URL in the location bar.

Adds the HX-Reselect response header which is a CSS selector that allows you to choose which part of the response is used to be swapped in. It overrides an existing hx-select on the triggering element.

Adds the HX-Reswap response header which allows you to specify how the response will be swapped.

Adds the HX-Retarget response header which is a CSS selector that updates the target of the content update to a different element on the page.

Adds the HX-Trigger response header which allows you to trigger client-side events.

Adds the HX-Trigger-After-Settle response header which allows you to trigger client-side events after the settle step.

Adds the HX-Trigger-After-Swap response header which allows you to trigger client-side events after the swap step.

Functions

put_hx_location(conn, location)

Adds the HX-Location response header which allows you to do a client-side redirect that does not do a full page reload.

See the htmx docs for more details.

Examples

iex> conn(:get, "/foo")
...> |> Hypa.Conn.put_hx_location("/bar")
...> |> get_resp_header("hx-location")
["/bar"]

put_hx_push_url(conn, url)

Adds the HX-Push-Url response header which pushes a new URL into the history stack.

See the htmx docs for more details.

Examples

iex> conn(:get, "/foo")
...> |> Hypa.Conn.put_hx_push_url("/bar")
...> |> get_resp_header("hx-push-url")
["/bar"]

put_hx_redirect(conn, url)

Adds the HX-Redirect response header which can be used to do a client-side redirect to a new location.

See the htmx docs for more details.

Examples

iex> conn(:get, "/foo")
...> |> Hypa.Conn.put_hx_redirect("/bar")
...> |> get_resp_header("hx-redirect")
["/bar"]

put_hx_refresh(conn, should_refresh)

Adds the HX-Refresh response header which, when set to "true", will do a client-side full refresh of the page.

Examples

iex> conn(:get, "/foo")
...> |> Hypa.Conn.put_hx_refresh(true)
...> |> get_resp_header("hx-refresh")
["true"]

put_hx_replace_url(conn, url)

Adds the HX-Replace-Url response header which will replace the current URL in the location bar.

See the htmx docs for more details.

Examples

iex> conn(:get, "/foo")
...> |> Hypa.Conn.put_hx_replace_url("/bar")
...> |> get_resp_header("hx-replace-url")
["/bar"]

put_hx_reselect(conn, selector)

Adds the HX-Reselect response header which is a CSS selector that allows you to choose which part of the response is used to be swapped in. It overrides an existing hx-select on the triggering element.

Examples

iex> conn(:get, "/foo")
...> |> Hypa.Conn.put_hx_reselect("#myelement")
...> |> get_resp_header("hx-reselect")
["#myelement"]

put_hx_reswap(conn, reswap)

Adds the HX-Reswap response header which allows you to specify how the response will be swapped.

See the htmx docs for more details.

Examples

iex> conn(:get, "/foo")
...> |> Hypa.Conn.put_hx_reswap("outerHTML")
...> |> get_resp_header("hx-reswap")
["outerHTML"]

put_hx_retarget(conn, target)

Adds the HX-Retarget response header which is a CSS selector that updates the target of the content update to a different element on the page.

Examples

iex> conn(:get, "/foo")
...> |> Hypa.Conn.put_hx_retarget("#myelement")
...> |> get_resp_header("hx-retarget")
["#myelement"]

put_hx_trigger(conn, trigger)

Adds the HX-Trigger response header which allows you to trigger client-side events.

See the htmx docs for more details.

Examples

iex> conn(:get, "/foo")
...> |> Hypa.Conn.put_hx_trigger("firstevent, secondevent")
...> |> get_resp_header("hx-trigger")
["firstevent, secondevent"]

put_hx_trigger_after_settle(conn, trigger)

Adds the HX-Trigger-After-Settle response header which allows you to trigger client-side events after the settle step.

See the htmx docs for more details.

Examples

iex> conn(:get, "/foo")
...> |> Hypa.Conn.put_hx_trigger_after_settle("firstevent, secondevent")
...> |> get_resp_header("hx-trigger-after-settle")
["firstevent, secondevent"]

put_hx_trigger_after_swap(conn, trigger)

Adds the HX-Trigger-After-Swap response header which allows you to trigger client-side events after the swap step.

See the htmx docs for more details.

Examples

iex> conn(:get, "/foo")
...> |> Hypa.Conn.put_hx_trigger_after_swap("firstevent, secondevent")
...> |> get_resp_header("hx-trigger-after-swap")
["firstevent, secondevent"]