From b35cdf1fd7589ccefc9f3370ad1fe384ecead3c8 Mon Sep 17 00:00:00 2001 From: Mayel de Borniol Date: Tue, 8 Oct 2024 16:55:26 +0100 Subject: [PATCH] WIP https://github.com/bonfire-networks/bonfire-app/issues/1043 --- assets/static/assets/_.swiftui.styles | 0 lib/components/core_components.swiftui.ex | 81 ++++++++++-- .../nav/header/guest_header_live.ex | 2 +- .../nav/user_menu/user_menu_links_live.sface | 4 +- lib/endpoint_live_reload.ex | 2 +- lib/layout/app.swiftui.heex | 1 - lib/layout/app.swiftui.neex | 2 + lib/layout/root.swiftui.heex | 1 - lib/layout/root.swiftui.neex | 5 + lib/static_generator/static_generator_plug.ex | 20 ++- lib/themes/default/app.swiftui.ex | 70 +++++++++++ lib/ui_common.ex | 28 +++++ lib/web.ex | 118 +++++++++++------- lib/web_native.ex | 15 +-- mix.exs | 12 +- 15 files changed, 285 insertions(+), 76 deletions(-) create mode 100644 assets/static/assets/_.swiftui.styles delete mode 100644 lib/layout/app.swiftui.heex create mode 100644 lib/layout/app.swiftui.neex delete mode 100644 lib/layout/root.swiftui.heex create mode 100644 lib/layout/root.swiftui.neex create mode 100644 lib/themes/default/app.swiftui.ex diff --git a/assets/static/assets/_.swiftui.styles b/assets/static/assets/_.swiftui.styles new file mode 100644 index 00000000..e69de29b diff --git a/lib/components/core_components.swiftui.ex b/lib/components/core_components.swiftui.ex index 2bd2cfc1..da16bc16 100644 --- a/lib/components/core_components.swiftui.ex +++ b/lib/components/core_components.swiftui.ex @@ -1,4 +1,5 @@ if Code.ensure_loaded?(LiveViewNative.Component) do + defmodule Bonfire.UI.Common.CoreComponents.SwiftUI do @moduledoc """ Provides core UI components built for SwiftUI. @@ -18,9 +19,62 @@ if Code.ensure_loaded?(LiveViewNative.Component) do """ use LiveViewNative.Component + use Bonfire.UI.Common import LiveViewNative.LiveForm.Component + + @doc """ + A special component that allows users to inject dynamic live components + """ + attr :module, :atom, required: true + attr :id, :string, required: true + attr :function, :atom, default: :render + slot :default + def stateful_component(assigns) do + # TEMP: until LVN supports live components + stateless_component(with true <- module_enabled?(assigns[:module]), + {:ok, assigns} <- assigns + |> assign(module_default_assigns(assigns[:module])) + |> assign(assigns) # again so we override defaults + |> assign_new(:myself, fn -> nil end) + |> assign_new(:streams, fn -> nil end) + |> apply(assigns[:module], :mount, [...]), + {:ok, assigns} <- apply(assigns[:module], :update, [assigns, assigns]) do + assign_new(assigns, :function, fn -> :render end) + |> debug("assi") + else e -> + error(e) + assigns + |> debug("erssi") + |> assign(:module, __MODULE__) + |> assign(:function, :error_msg) + |> assign(:text, "Could not render component") + end + ) + # TODO: when LVN supports live components + # ~LVN""" + # <%= render_slot(@default) %> + # """ + end + + @doc """ + A special component that allows users to inject dynamic function components + """ + attr :module, :atom, default: nil + attr :function, :atom, default: :render + attr :id, :string, default: nil + slot :default + def stateless_component(assigns) do + ~LVN""" + <%= Phoenix.LiveView.TagEngine.component( + &apply(@module || __MODULE__, @function || :render, [&1]), + assigns, + {__ENV__.module, __ENV__.function, __ENV__.file, __ENV__.line} + ) %> + """ + end + @doc """ Renders an input with label and error messages. @@ -124,7 +178,7 @@ if Code.ensure_loaded?(LiveViewNative.Component) do <%= @label %> - <.error :for={msg <- @errors}><%= msg %> + <.error_msg :for={msg <- @errors}><%= msg %> """ end @@ -135,7 +189,7 @@ if Code.ensure_loaded?(LiveViewNative.Component) do <%= @label %> - <.error :for={msg <- @errors}><%= msg %> + <.error_msg :for={msg <- @errors}><%= msg %> """ end @@ -147,7 +201,7 @@ if Code.ensure_loaded?(LiveViewNative.Component) do <%= @label %> <%= @label %> - <.error :for={msg <- @errors}><%= msg %> + <.error_msg :for={msg <- @errors}><%= msg %> """ end @@ -164,7 +218,7 @@ if Code.ensure_loaded?(LiveViewNative.Component) do <%= name %> - <.error :for={msg <- @errors}><%= msg %> + <.error_msg :for={msg <- @errors}><%= msg %> """ end @@ -176,7 +230,7 @@ if Code.ensure_loaded?(LiveViewNative.Component) do <%= @label %> <%= @label %> - <.error :for={msg <- @errors}><%= msg %> + <.error_msg :for={msg <- @errors}><%= msg %> """ end @@ -188,7 +242,7 @@ if Code.ensure_loaded?(LiveViewNative.Component) do <%= @label %> - <.error :for={msg <- @errors}><%= msg %> + <.error_msg :for={msg <- @errors}><%= msg %> """ end @@ -200,7 +254,7 @@ if Code.ensure_loaded?(LiveViewNative.Component) do <%= @label %> - <.error :for={msg <- @errors}><%= msg %> + <.error_msg :for={msg <- @errors}><%= msg %> """ end @@ -209,7 +263,7 @@ if Code.ensure_loaded?(LiveViewNative.Component) do ~LVN""" <%= @placeholder || @label %> - <.error :for={msg <- @errors}><%= msg %> + <.error_msg :for={msg <- @errors}><%= msg %> """ end @@ -218,7 +272,7 @@ if Code.ensure_loaded?(LiveViewNative.Component) do ~LVN""" <%= @placeholder || @label %> - <.error :for={msg <- @errors}><%= msg %> + <.error_msg :for={msg <- @errors}><%= msg %> """ end @@ -230,7 +284,7 @@ if Code.ensure_loaded?(LiveViewNative.Component) do <%= @label %> - <.error :for={msg <- @errors}><%= msg %> + <.error_msg :for={msg <- @errors}><%= msg %> """ end @@ -239,12 +293,13 @@ if Code.ensure_loaded?(LiveViewNative.Component) do Generates a generic error message. """ @doc type: :component - slot :inner_block, required: true + attr :text, :string, required: false, default: nil + slot :inner_block, required: false - def error(assigns) do + def error_msg(assigns) do ~LVN""" - <%= render_slot(@inner_block) %> + <%= @text %> <%= render_slot(@inner_block) %> """ end diff --git a/lib/components/nav/header/guest_header_live.ex b/lib/components/nav/header/guest_header_live.ex index 9fa359ca..585191fa 100644 --- a/lib/components/nav/header/guest_header_live.ex +++ b/lib/components/nav/header/guest_header_live.ex @@ -4,5 +4,5 @@ defmodule Bonfire.UI.Common.GuestHeaderLive do prop page, :string, default: "home" # @decorate time() - render_sface_or_native() + # render_sface_or_native() end diff --git a/lib/components/nav/user_menu/user_menu_links_live.sface b/lib/components/nav/user_menu/user_menu_links_live.sface index 1a064f13..75ce189f 100644 --- a/lib/components/nav/user_menu/user_menu_links_live.sface +++ b/lib/components/nav/user_menu/user_menu_links_live.sface @@ -48,14 +48,14 @@ --> - + --}
  • diff --git a/lib/endpoint_live_reload.ex b/lib/endpoint_live_reload.ex index 0657b871..5bf1e5bf 100644 --- a/lib/endpoint_live_reload.ex +++ b/lib/endpoint_live_reload.ex @@ -15,7 +15,7 @@ defmodule Bonfire.UI.Common.Endpoint.LiveReload do plug(Phoenix.LiveReloader) plug(Phoenix.CodeReloader) - if unquote(System.get_env("NATIVE_ENABLED") in ["1", "true"]) do + if unquote(System.get_env("WITH_LV_NATIVE") in ["1", "true"]) do plug LiveViewNative.LiveReloader end diff --git a/lib/layout/app.swiftui.heex b/lib/layout/app.swiftui.heex deleted file mode 100644 index 05433985..00000000 --- a/lib/layout/app.swiftui.heex +++ /dev/null @@ -1 +0,0 @@ -<%= @inner_content %> diff --git a/lib/layout/app.swiftui.neex b/lib/layout/app.swiftui.neex new file mode 100644 index 00000000..3d8af175 --- /dev/null +++ b/lib/layout/app.swiftui.neex @@ -0,0 +1,2 @@ +<.flash_group flash={@flash} /> +<%= @inner_content %> diff --git a/lib/layout/root.swiftui.heex b/lib/layout/root.swiftui.heex deleted file mode 100644 index 05433985..00000000 --- a/lib/layout/root.swiftui.heex +++ /dev/null @@ -1 +0,0 @@ -<%= @inner_content %> diff --git a/lib/layout/root.swiftui.neex b/lib/layout/root.swiftui.neex new file mode 100644 index 00000000..75440c9c --- /dev/null +++ b/lib/layout/root.swiftui.neex @@ -0,0 +1,5 @@ +<.csrf_token /> +