diff --git a/lib/cookbook_web/live/recipes/modals_live.ex b/lib/cookbook_web/live/recipes/modals_live.ex new file mode 100644 index 0000000..fad25f1 --- /dev/null +++ b/lib/cookbook_web/live/recipes/modals_live.ex @@ -0,0 +1,43 @@ +defmodule CookbookWeb.ModalsLive do + use CookbookWeb, :live_view + use CookbookNative, :live_view + + def mount(_params, _session, socket) do + {:ok, socket + |> assign(:modal_type, nil) + |> assign(:form, to_form(%{ + "type" => "sheet", + "detents" => false, + "drag_indicator" => false, + "background" => "", + "corner_radius" => "", + }))} + end + + def render(assigns) do + ~H"" + end + + def handle_event("form-changed", params, socket) do + {:noreply, socket + |> assign(:form, cast_form(params))} + end + + def handle_event("open", %{ "type" => type } = params, socket) do + {:noreply, socket + |> assign(:modal_type, type) + |> assign(:form, cast_form(params))} + end + + def handle_event("presentation-changed", _params, socket) do + {:noreply, assign(socket, :modal_type, nil)} + end + + defp cast_form(%{ "detents" => detents, "drag_indicator" => drag_indicator } = params) do + to_form( + params + |> Map.put("detents", detents == "true") + |> Map.put("drag_indicator", drag_indicator == "true") + ) + end +end diff --git a/lib/cookbook_web/live/recipes/modals_live.swiftui.ex b/lib/cookbook_web/live/recipes/modals_live.swiftui.ex new file mode 100644 index 0000000..2b07c8e --- /dev/null +++ b/lib/cookbook_web/live/recipes/modals_live.swiftui.ex @@ -0,0 +1,138 @@ +defmodule CookbookWeb.ModalsLive.SwiftUI do + use CookbookNative, [:render_component, format: :swiftui] + + def render(assigns) do + ~LVN""" + + <.simple_form for={@form} id="type" phx-submit="open" phx-change="form-changed"> + <.input + type="Picker" + field={@form[:type]} + options={[ + {"Sheet", "sheet"}, + {"Full Screen Cover", "fullScreenCover"}, + {"Popover", "popover"}, + {"Alert", "alert"}, + {"Confirmation Dialog", "confirmationDialog"}, + {"Inspector", "inspector"}, + ]} + label="Modal Type" + /> +
+ Sheet Customization + <.input + type="Toggle" + field={@form[:detents]} + label="Detents" + /> + <.input + type="Toggle" + field={@form[:drag_indicator]} + label="Drag Indicator" + /> + <.input + type="Picker" + field={@form[:background]} + options={[ + {"Unspecified", ""}, + {"Red", "system-red"}, + {"Green", "system-green"}, + {"Blue", "system-blue"} + ]} + label="Background" + /> + <.input + type="Picker" + field={@form[:corner_radius]} + options={[ + {"Unspecified", ""}, + {"None", "0"}, + {"Medium", "50"}, + {"Large", "100"} + ]} + label="Corner Radius" + /> +
+ <%!-- the popover is attached to the "Open" button --%> + + <.button type="submit">Open + + <%!-- `presentationCompactAdaptation` forces a popover to be used on smaller devices --%> + + Popover content + + + + + + + + Sheet content + + + + + + + + + + Full screen cover content + <.button phx-click="presentation-changed">Dismiss + + + + + Alert message content + + + + + + + + + + + + Inspector content + + +
+ """ + end +end diff --git a/lib/cookbook_web/router.ex b/lib/cookbook_web/router.ex index 6e75907..938ec12 100644 --- a/lib/cookbook_web/router.ex +++ b/lib/cookbook_web/router.ex @@ -73,6 +73,12 @@ defmodule CookbookWeb.Router do description: "A list of message bubbles that starts at the bottom", category: "UI" } + live "/modals", ModalsLive, metadata: %{ + title: "Modals", + icon: "macwindow.on.rectangle", + description: "Various modal options in SwiftUI", + category: "Navigation" + } live "/onboarding", OnboardingLive, metadata: %{ title: "Onboarding", icon: "list.star",