Created
November 20, 2020 20:19
-
-
Save leastbad/48466c8f16cbd3bc6a192666629a6bf5 to your computer and use it in GitHub Desktop.
Nate's flash setup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_flash_types :primary, :secondary, :success, :danger, :warning, :info, :light, :dark |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="flash" class="container"> | |
<% flash.each do |type, message| %> | |
<%= tag.div message, class: "alert alert-#{type} shadow-lg", | |
data: { controller: "flash", action: "animationend->flash#next click->flash#hide" } %> | |
<% end %> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ApplicationController from './application_controller' | |
document.addEventListener('turbolinks:before-cache', () => | |
document.getElementById('flash').remove() | |
) | |
export default class extends ApplicationController { | |
connect () { | |
this.duration = 3500 | |
this.showClassName = 'animate__headShake' | |
this.hideClassName = 'animate__zoomOut' | |
this.show() | |
} | |
next () { | |
if (this.hidden) return this.element.remove() | |
this.hideSlow() | |
} | |
show () { | |
this.element.classList.add( | |
'animate__animated', | |
'animate__faster', | |
this.showClassName | |
) | |
} | |
hideSlow () { | |
this.element.classList.remove(this.showClassName) | |
setTimeout(this.hide.bind(this), this.duration) | |
} | |
hide () { | |
this.element.classList.remove(this.showClassName) | |
this.element.classList.add(this.hideClassName) | |
} | |
get hidden () { | |
return this.element.classList.contains(this.hideClassName) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment