defmodule SigmaKit.Components.Modal do use Phoenix.LiveComponent alias Phoenix.LiveView.JS import SigmaKit.Components.Js, only: [show: 2, hide: 2] import SigmaKit.Components.Buttons, only: [button: 1] @doc """ Renders a modal. ## Examples <.modal id="confirm-modal"> This is a modal. JS commands may be passed to the `:on_cancel` to configure the closing/cancel event, for example: <.modal id="confirm" on_cancel={JS.navigate(~p"/posts")}> This is another modal. """ attr :id, :string, required: true, doc: "the unique id of the modal" attr :show, :boolean, default: false, doc: "whether the modal is visible" attr :title, :string, default: nil, doc: "the title of the modal" attr :on_cancel, JS, default: %JS{}, doc: "the JS command to run when the modal is closed" attr :form, :any, default: nil, doc: "If a form object is passed the modal will render form buttons and process submit/change events" attr :as, :any, default: nil, doc: "the server side parameter to collect all input under" attr :noclose, :boolean, default: false, doc: "whether the modal can be closed" attr :submit, :any, default: "submit", doc: "the phx-submit event for the form" attr :change, :any, default: "validate", doc: "the phx-change event for the form" attr :submit_text, :string, default: "Submit", doc: "the text for the submit button" attr :target, :any, default: nil, doc: "the target for the form events" attr :thin, :boolean, default: false, doc: "whether the modal is wide" attr :wide, :boolean, default: false, doc: "whether the modal is wide" attr :error, :string, default: nil, doc: "an error message to display" slot :inner_block, required: true, doc: "the slot for the modal content" slot :description, doc: "the slot for the modal description/subheading" slot :actions, doc: "the slot for modal actions, such as buttons" def modal(assigns) do ~H"""
""" end def push_modal_close(socket) do Phoenix.LiveView.push_event(socket, "close-modal", %{}) end def show_modal(js \\ %JS{}, id) when is_binary(id) do js |> JS.show(to: "##{id}") |> JS.show( to: "##{id}-bg", time: 300, transition: {"transition-all transform ease-out duration-300", "opacity-0 scale-75", "opacity-100 scale-100"} ) |> show("##{id}-container") |> JS.add_class("overflow-hidden", to: "body") |> JS.focus_first(to: "##{id}-content") end def hide_modal(js \\ %JS{}, id) do js |> JS.hide( to: "##{id}-bg", transition: {"transition-all transform ease-in duration-200", "opacity-100", "opacity-0"} ) |> hide("##{id}-container") |> JS.hide(to: "##{id}", transition: {"block", "block", "hidden"}) |> JS.remove_class("overflow-hidden", to: "body") |> JS.pop_focus() end end