ExLiveUrl (ExLiveUrl v0.3.2)

ExLiveUrl is just some simple Phoenix LiveView lifecycle hooks and helper functions. It helps you store the live view's current query params and path in your assigns. Additionally, it exposes ways to work with those values both synchronously (only from the root live view) and asynchronously (from anywhere).

Installation

You can install ExLiveUrl like so:

defp deps do
  [
    {:ex_live_url, "~> 0.2.0"}
  ]
end

To use ExLiveUrl you will need to call Phoenix.LiveView.on_mount/1 with the module, ExLiveUrl in your live view. For example:

defmodule YourLiveView do
  use Phoenix.LiveView

  on_mount ExLiveUrl

  # your live view implementation
end

Link to this section Summary

Functions

Asynchronously annotates the socket for navigation to another LiveView. Whenever this operation is eventually executed it calls Phoenix.LiveView.push_navigate/2 internally.

Asynchronously annotates the socket for navigation within the current LiveView. Whenever this operation is eventually executed it calls Phoenix.LiveView.push_navigate/2 internally.

Asynchronously annotates the socket for redirect to a destination path. Whenever this operation is eventually executed it calls Phoenix.LiveView.redirect/2 internally.

Link to this section Functions

Link to this function

push_navigate(pid \\ self(), opts)

(since 0.3.0)

Asynchronously annotates the socket for navigation to another LiveView. Whenever this operation is eventually executed it calls Phoenix.LiveView.push_navigate/2 internally.

options

Options

  • :to - a function which takes the current ExLiveUrl.Url and must return either a new ExLiveUrl.Url or a relative url string.
  • :replace - the flag to replace the current history or push a new state. Defaults false.

examples

Examples

ExLiveUrl.push_navigate(to: fn url -> %ExLiveUrl.Url{url | path: ExLiveUrl.Path.new("/")} end)
ExLiveUrl.push_navigate(to: fn _url -> "/"} end)
ExLiveUrl.push_navigate(
  to: fn url -> %ExLiveUrl.Url{url | path: ExLiveUrl.Path.new("/")} end,
  replace: true
)
Link to this function

push_patch(pid \\ self(), opts)

(since 0.3.0)

Asynchronously annotates the socket for navigation within the current LiveView. Whenever this operation is eventually executed it calls Phoenix.LiveView.push_navigate/2 internally.

options

Options

  • :to - a function which takes the current ExLiveUrl.Url and must return either a new ExLiveUrl.Url or a relative url string.
  • :replace - the flag to replace the current history or push a new state. Defaults false.

examples

Examples

ExLiveUrl.push_patch(to: fn url -> %ExLiveUrl.Url{url | path: ExLiveUrl.Path.new("/")} end)
ExLiveUrl.push_patch(to: fn _url -> "/"} end)
ExLiveUrl.push_patch(
  to: fn url -> %ExLiveUrl.Url{url | path: ExLiveUrl.Path.new("/")} end,
  replace: true
)
Link to this function

redirect(pid \\ self(), opts)

(since 0.3.0)

Asynchronously annotates the socket for redirect to a destination path. Whenever this operation is eventually executed it calls Phoenix.LiveView.redirect/2 internally.

Note: LiveView redirects rely on instructing client to perform a window.location update on the provided redirect location. The whole page will be reloaded and all state will be discarded.

options

Options

  • :to - a function which takes the current ExLiveUrl.Url and must return either a new ExLiveUrl.Url or a relative url string. It must always be a local path.
  • :external - a function which takes the current ExLiveUrl.Url and must return either a new ExLiveUrl.Url or a fully qualified url string.

examples

Examples

ExLiveUrl.redirect(to: fn url -> %ExLiveUrl.Url{url | path: ExLiveUrl.Path.new("/")} end)
ExLiveUrl.redirect(to: fn _url -> "/"} end)
ExLiveUrl.redirect(external: fn _url -> "https://google.com"} end)