-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix modal component deprecated warning
- Loading branch information
1 parent
35fed9e
commit a1a2784
Showing
3 changed files
with
53 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,65 @@ | ||
defmodule QuadblockquizWeb.LiveHelpers do | ||
import Phoenix.LiveView.Helpers | ||
@moduledoc false | ||
import Phoenix.Component | ||
|
||
alias Phoenix.LiveView.JS | ||
|
||
@doc """ | ||
Renders a component inside the `QuadblockquizWeb.ModalComponent` component. | ||
Renders a live component inside a modal. | ||
The rendered modal receives a `:return_to` option to properly update | ||
the URL when the modal is closed. | ||
## Examples | ||
<%= live_modal @socket, QuadblockquizWeb.UserLive.FormComponent, | ||
id: @user.id || :new, | ||
action: @live_action, | ||
user: @user, | ||
return_to: Routes.user_index_path(@socket, :index) %> | ||
<.modal return_to={Routes.user_index_path(@socket, :index)}> | ||
<.live_component | ||
module={QuadblockquizWeb.UserLive.FormComponent} | ||
id={@user.id || :new} | ||
title={@page_title} | ||
action={@live_action} | ||
return_to={Routes.user_index_path(@socket, :index)} | ||
user: @user | ||
/> | ||
</.modal> | ||
""" | ||
def modal(assigns) do | ||
assigns = assign_new(assigns, :return_to, fn -> nil end) | ||
|
||
~H""" | ||
<div id="modal" class="phx-modal fade-in" phx-remove={hide_modal()}> | ||
<div | ||
id="modal-content" | ||
class="phx-modal-content fade-in-scale" | ||
phx-click-away={JS.dispatch("click", to: "#close")} | ||
phx-window-keydown={JS.dispatch("click", to: "#close")} | ||
phx-key="escape" | ||
> | ||
<%= if @return_to do %> | ||
<.link | ||
patch={@return_to} | ||
id="close" | ||
class="phx-modal-close fs-3 text-dark" | ||
phx-click={hide_modal()} | ||
> | ||
<span aria-hidden="true">×</span> | ||
</.link> | ||
<% else %> | ||
<a id="close" href="#" class="phx-modal-close" phx-click={hide_modal()}>×</a> | ||
<% end %> | ||
def live_modal(_socket, component, opts) do | ||
path = Keyword.fetch!(opts, :return_to) | ||
modal_opts = [id: :modal, return_to: path, component: component, opts: opts] | ||
live_component(QuadblockquizWeb.ModalComponent, modal_opts) | ||
<%= render_slot(@inner_block) %> | ||
</div> | ||
</div> | ||
""" | ||
end | ||
|
||
defp hide_modal(js \\ %JS{}) do | ||
js | ||
|> JS.hide(to: "#modal", transition: "fade-out") | ||
|> JS.hide(to: "#modal-content", transition: "fade-out-scale") | ||
end | ||
end | ||
|
||
# https://elixirforum.com/t/how-to-create-a-modal/54286 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters