Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagomajesk committed Mar 21, 2023
1 parent 24795cb commit 9d43c57
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
24 changes: 15 additions & 9 deletions lib/swish/toast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ defmodule Swish.Toast do
"""
@type t :: %Swish.Toast{}

@enforce_keys [:id, :kind]
defstruct [:id, :kind, :message, :js_show, :js_hide]
@enforce_keys [:id, :kind, :message]
defstruct [:id, :kind, :flash, :message, :group, :js_show, :js_hide]

use Phoenix.Component

Expand All @@ -40,19 +40,24 @@ defmodule Swish.Toast do
alias Phoenix.LiveView.JS

@doc false
def new() do
def new(attrs \\ %{}) do
js_module = Swish.JS.dynamic!()
toast_id = System.unique_integer([:positive, :monotonic])

kind = attrs[:kind] || :info
flash = attrs[:flash] || %{}

%Toast{
id: "toast-#{toast_id}",
kind: :info,
id: attrs[:id] || "toast-#{toast_id}",
kind: attrs[:kind] || :info,
group: attrs[:group],
message: attrs[:message] || Phoenix.Flash.get(flash, kind),
js_show: Function.capture(js_module, :show_toast, 2),
js_hide: Function.capture(js_module, :hide_toast, 2)
}
end

attr(:id, :string, default: "flash")
attr(:id, :string)
attr(:toast, Toast, required: false)
attr(:group, Group, required: false)
attr(:flash, :map, default: %{})
Expand All @@ -64,10 +69,11 @@ defmodule Swish.Toast do
def root(assigns) do
assigns =
assign_new(assigns, :toast, fn ->
Map.merge(Toast.new(), %{
Toast.new(%{
id: assigns.id,
flash: assigns.flash,
kind: assigns.kind,
message: Phoenix.Flash.get(assigns.flash, assigns.kind)
group: assigns.group
})
end)

Expand All @@ -76,7 +82,7 @@ defmodule Swish.Toast do
:if={@toast.message}
id={@id}
role="alert"
name={(@group && "li") || @as}
name={(@toast.group && "li") || @as}
phx-mounted={show(@toast)}
phx-click={hide(@toast)}
tabindex="0"
Expand Down
3 changes: 2 additions & 1 deletion lib/swish/toast/group.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Swish.Toast.Group do
use Phoenix.Component

alias __MODULE__
alias Phoenix.LiveView.JS

@doc false
def new() do
Expand All @@ -27,7 +28,7 @@ defmodule Swish.Toast.Group do
assigns = assign_new(assigns, :group, fn -> Group.new() end)

~H"""
<Swish.Tag.portal id={@group.portal_id}>
<Swish.Tag.portal id={@group.portal_id} phx-mounted={JS.dispatch("portal:open")}>
<ol tabindex="-1" {@rest}>
<%= render_slot(@inner_block, @group) %>
</ol>
Expand Down

0 comments on commit 9d43c57

Please sign in to comment.