Skip to content

Instantly share code, notes, and snippets.

@chevinbrown
Last active January 28, 2024 08:24
Show Gist options
  • Save chevinbrown/d1c5526eba16c7dd0355815279b43df2 to your computer and use it in GitHub Desktop.
Save chevinbrown/d1c5526eba16c7dd0355815279b43df2 to your computer and use it in GitHub Desktop.
Quick toast messages from flash with LiveView
<%= if assigns[:flash] && !assigns[:skip_flash] do %>
<%= render_flash(@flash) %>
<% end %>
...
defmodule MyWeb.LayoutView do
use MyWeb, :view
import Phoenix.LiveView, only: [send_update: 2]
@doc """
Sends a message to the toast component if the flash-type is supported
"""
def render_flash(flash) do
if ((flash |> Map.keys) -- ["error", "success", "info"]) |> Enum.any? do
nil
else
send_update(MyWeb.Components.Toast,
id: "toast",
show: true,
type: :success,
message: message
)
nil
end
end
end
defmodule MyWeb.Components.Toast do
use MyWeb, :live_component
def mount(socket) do
{:ok, socket |> assign(type: nil, message: nil)}
end
def render(assigns) do
~L"""
<div class="fixed inset-0 flex items-end justify-center px-4 py-6 pointer-events-none sm:p-6 sm:items-start sm:justify-end">
<%= if @show do %>
<div
x-data="{ open: false }"
x-init="() => {
setTimeout(() => open = true, 300);
// setTimeout(() => open = false, 10000);
$watch('open', open => $dispatch('toast-change', { open: open }))
}"
x-show="open" x-description="Notification panel, show/hide based on alert state." x-transition:enter="transform ease-out duration-300 transition" x-transition:enter-start="translate-y-2 opacity-0 sm:translate-y-0 sm:translate-x-2" x-transition:enter-end="translate-y-0 opacity-100 sm:translate-x-0" x-transition:leave="transition ease-in duration-100" x-transition:leave-start="opacity-100" x-transition:leave-end="opacity-0" class="max-w-sm w-full bg-white shadow-lg rounded-lg pointer-events-auto">
<div class="rounded-lg shadow-xs overflow-hidden">
<div class="p-4">
<div class="flex items-start">
<div class="flex-shrink-0">
<svg class="h-6 w-6 text-green-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
</svg>
</div>
<%= body(@type, @message) %>
<div class="ml-4 flex-shrink-0 flex">
<button @click="open = false" class="inline-flex text-gray-400 focus:outline-none focus:text-gray-500 transition ease-in-out duration-150">
<svg class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path>
</svg>
</button>
</div>
</div>
</div>
</div>
</div>
<% end %>
</div>
"""
end
defp body(_, message) when is_binary(message) do
~e"""
<div class="ml-3 w-0 flex-1 pt-0.5">
<p class="text-sm leading-5 font-medium text-gray-900 mb-0">
<%= message %>
</p>
</div>
"""
end
defp body(_, [message]) do
~e"""
<div class="ml-3 w-0 flex-1 pt-0.5">
<p class="text-sm leading-5 font-medium text-gray-900 mb-0">
<%= message %>
</p>
</div>
"""
end
defp body(_, [message, body]) do
~e"""
<div class="ml-3 w-0 flex-1 pt-0.5">
<p class="text-sm leading-5 font-medium text-gray-900 mb-0">
<%= message %>
</p>
<p class="mt-1 text-sm leading-5 text-gray-500 mb-0">
<%= body %>
</p>
</div>
"""
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment