Skip to content

Instantly share code, notes, and snippets.

@jsmithdev
Created April 4, 2019 14:27
Show Gist options
  • Save jsmithdev/262dca27457d8bf8fc7586d70f5abae8 to your computer and use it in GitHub Desktop.
Save jsmithdev/262dca27457d8bf8fc7586d70f5abae8 to your computer and use it in GitHub Desktop.
Toast / send messages to users with no dependencies.
.toaster {
font-family: inherit;
display: none;
background: lightblue;
padding: 1rem;
border-radius: 5px;
max-width: 75%;
margin: 0 auto;
cursor: pointer;
}
.toaster > .t_type {
font-size: 1.3rem;
line-height: 1.7rem;
padding-right: .25rem;
}
.toaster > .t_msg {
font-size: 1rem;
line-height: 2rem;
padding-left: .35rem;
}
<div class="toaster">
<span class="t_type"></span>
<span class="t_msg"></span>
</div>
// jshint asi= true, esversion= 6, laxcomma= true
'use strict()'
function Toast() {
const el = document.querySelector('.toaster')
, type = el.querySelector('.t_type')
, msg = el.querySelector('.t_msg')
, show = el => el.style.display = 'flex'
, hide = el => el.style.display = 'none'
, closeOnClick = bool => bool
? el.onclick = () => hide(el)
: null
, closeAuto = bool => bool
? setTimeout(() => hide(el), 3500)
: null
, pop = (t, m) => {
msg.textContent = m
type.textContent = t
show(el)
};
return {
is: 'Native Toaster',
pop,
hide,
closeAuto,
closeOnClick
}
}
const toast = new Toast()
toast.closeAuto(false)
toast.closeOnClick(true)
toast.pop('Success', 'Click to close the message')
console.log(toast.is)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment