cannery/lib/lokal_web/live/live_helpers.ex

82 lines
2.4 KiB
Elixir
Raw Normal View History

2021-09-02 23:32:53 -04:00
defmodule LokalWeb.LiveHelpers do
2022-01-22 20:44:38 -05:00
@moduledoc """
Contains resuable methods for all liveviews
"""
2023-01-26 00:28:34 -05:00
import Phoenix.Component
2022-02-25 21:55:27 -05:00
alias Phoenix.LiveView.JS
2021-09-02 23:32:53 -04:00
@doc """
2022-02-25 21:55:27 -05:00
Renders a live component inside a modal.
2021-09-02 23:32:53 -04:00
The rendered modal receives a `:return_to` option to properly update
the URL when the modal is closed.
## Examples
2022-02-25 21:55:27 -05:00
<.modal return_to={Routes.<%= schema.singular %>_index_path(Endpoint, :index)}>
<.live_component
module={<%= inspect context.web_module %>.<%= inspect Module.concat(schema.web_namespace, schema.alias) %>Live.FormComponent}
id={@<%= schema.singular %>.id || :new}
title={@page_title}
action={@live_action}
return_to={Routes.<%= schema.singular %>_index_path(Endpoint, :index)}
<%= schema.singular %>: @<%= schema.singular %>
/>
</.modal>
2021-09-02 23:32:53 -04:00
"""
2022-02-25 21:55:27 -05:00
def modal(assigns) do
~H"""
2023-01-26 00:28:34 -05:00
<.link
id="modal-bg"
patch={@return_to}
class="fade-in fixed z-10 left-0 top-0
2023-02-04 09:16:51 -05:00
w-full h-full overflow-hidden
p-8 flex flex-col justify-center items-center cursor-auto"
2023-01-26 00:28:34 -05:00
style="background-color: rgba(0,0,0,0.4);"
2023-02-04 09:16:51 -05:00
phx-remove={hide_modal()}
2023-01-26 00:28:34 -05:00
>
2022-02-25 21:55:27 -05:00
<span class="hidden"></span>
2023-01-26 00:28:34 -05:00
</.link>
2021-09-02 23:32:53 -04:00
2022-02-25 21:55:27 -05:00
<div
id="modal"
class="fixed z-10 left-0 top-0 pointer-events-none
w-full h-full overflow-hidden
p-4 sm:p-8 flex flex-col justify-center items-center"
>
<div
id="modal-content"
class="fade-in-scale w-full max-w-3xl relative
2023-02-04 09:16:51 -05:00
pointer-events-auto overflow-hidden
2023-02-04 17:28:53 -05:00
px-8 py-4 sm:py-8
2023-02-04 09:16:51 -05:00
flex flex-col justify-start items-center
bg-white border-2 rounded-lg"
2022-02-25 21:55:27 -05:00
>
2023-01-26 00:28:34 -05:00
<.link
id="close"
href={@return_to}
class="absolute top-8 right-10
2023-02-04 09:16:51 -05:00
text-gray-500 hover:text-gray-800
transition-all duration-500 ease-in-out"
phx-remove={hide_modal()}
2023-01-26 00:28:34 -05:00
>
2022-02-25 21:55:27 -05:00
<i class="fa-fw fa-lg fas fa-times"></i>
2023-01-26 00:28:34 -05:00
</.link>
2022-02-25 21:55:27 -05:00
2022-05-05 21:07:02 -04:00
<div class="overflow-x-hidden overflow-y-auto w-full p-8 flex flex-col space-y-4 justify-start items-center">
2022-02-25 21:55:27 -05:00
<%= render_slot(@inner_block) %>
</div>
</div>
</div>
"""
2021-09-02 23:32:53 -04:00
end
2022-01-22 13:01:36 -05:00
2022-02-25 21:55:27 -05:00
def hide_modal(js \\ %JS{}) do
js
|> JS.hide(to: "#modal", transition: "fade-out")
|> JS.hide(to: "#modal-bg", transition: "fade-out")
|> JS.hide(to: "#modal-content", transition: "fade-out-scale")
2021-09-02 23:32:53 -04:00
end
end