Skip to content

Instantly share code, notes, and snippets.

@cschulte22
Created July 29, 2020 22:16
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cschulte22/285d15d2e72e530a423204b3cf175b9e to your computer and use it in GitHub Desktop.
Save cschulte22/285d15d2e72e530a423204b3cf175b9e to your computer and use it in GitHub Desktop.
Rails flash with tailwind / alpinejs
<% if flash.any? %>
<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 z-50" data-flash-notifications='true'>
<% flash.each do |msg_type, msg| %>
<div x-data="{flashVisible: false, flashType: '<%= msg_type %>'}" x-show='flashVisible' x-init="() => {flashVisible=true; setTimeout(() => {flashVisible=false}, 5000)}" class="max-w-sm w-full bg-white shadow-lg rounded-lg pointer-events-auto"
x-transition:enter="transition ease-out duration-300"
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-out duration-100"
x-transition:leave-start="transform opacity-100"
x-transition:leave-end="transform opacity-0">
<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-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor"
x-bind:class="{'text-green-400': flashType == 'success', 'text-red-500': flashType == 'error', 'text-yellow-300': flashType == 'alert'}">
<path x-show="flashType == 'success'" 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 x-show="flashType == 'alert'" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
<path x-show="flashType == 'error'" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"/>
<path x-show="flashType == 'notice'" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
</svg>
</div>
<div class="ml-3 w-0 flex-1 pt-0.5">
<p class="text-sm leading-5 font-medium text-gray-900">
<%= msg %>
</p>
</div>
<div class="ml-4 flex-shrink-0 flex">
<button x-on:click="flashVisible = 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" />
</svg>
</button>
</div>
</div>
</div>
</div>
</div>
<% end %>
</div>
<% end %>
@toekneema
Copy link

goat!

@Pefington
Copy link

Neat, thanks!

@nwallis
Copy link

nwallis commented Sep 29, 2022

Legendary - thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment