Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="notifications"></div>9
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

:root {
  font-family: "Inter";
  font-size: 16px;
  --gradient: linear-gradient(to bottom, #2eadff, #3d83ff, #7e61ff);
  --color: #32a6ff
}

*, *:before, *:after {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

body {
  margin: 0;
  height: 100vh;
  width: 100%;
  background: #0c0c0b;
  overflow: hidden;
  display: flex;
  justify-content: center;
}

.notifications {
  width: 18rem;
  display: flex;
  flex-direction: column;
  justify-content: end;
  margin: 0.5rem;
}

.noti {
  position: relative;
  height: 0;
  transition: height 300ms ease;
  flex-shrink: 0;
  opacity: 1;
}
.noti.out {animation: notiOut 500ms ease forwards}
@keyframes notiOut {
  to {height: 0;}
}

.noticard {
  position: absolute;
  bottom: 0;
  display: flex;
  flex-direction: column;
  isolation: isolate;
  width: 18rem;
  d-height: 8rem;
  background: #29292c;
  border-radius: 1rem;
  overflow: hidden;
  animation: notiCardIn 500ms ease;
}
@keyframes notiCardIn {
  from {
    transform: translateX(50%);
    opacity: 0;
  }
}
.noti.out .noticard {animation: notiCardOut 500ms ease forwards}
@keyframes notiCardOut {
  to {
    opacity: 0;
    transform: scale(0.5)
  }
}
.noticard:before {
  position: absolute;
  content: "";
  inset: 0.0625rem;
  border-radius: 0.9375rem;
  background: #18181b;
  z-index: 2
}
.noticard:after {
  position: absolute;
  content: "";
  width: 0.25rem;
  inset: 0.65rem auto 0.65rem 0.5rem;
  border-radius: 0.125rem;
  background: var(--gradient);
  transition: transform 300ms ease;
  z-index: 4;
}

.noticard:hover:after {
  transform: translateX(0.15rem)
}

.notititle {
  color: var(--color);
  padding: 0.65rem 0.5rem 0.4rem 1.25rem;
  font-weight: 500;
  font-size: 1.1rem;
  transition: transform 300ms ease;
  z-index: 5;
}
.noti:hover .notititle {
  transform: translateX(0.15rem)
}
.notidesc {
  color: #99999d;
  padding: 0 0.5rem 0.85rem 1.25rem;
  transition: transform 300ms ease;
  z-index: 5;
}
.noti:hover .notidesc {
  transform: translateX(0.25rem)
}

.notiglow, .notiborderglow {
  position: absolute;
  width: 20rem;
  height: 20rem;
  transform: translate(-50%, -50%);
  background: radial-gradient(circle closest-side at center, white, transparent);
  opacity: 0;
  transition: opacity 300ms ease;
}
.notiglow { z-index: 3; }
.notiborderglow { z-index: 1; }

.noti:hover .notiglow {opacity: 0.1}
.noti:hover .notiborderglow {opacity: 0.1}
              
            
!

JS

              
                // if the pen is in thumbnail view, scale it up
if (location.pathname.match(/fullcpgrid/i) ? true : false) {
  document.documentElement.style.fontSize = "32px"
  document.querySelector(".notifications").style.transform = "translate(0.5rem, calc(-50% + 3rem))"
}


class Notifications {
  constructor(el) {
    this.el = el
  }
  
  // function to create new elements with a class (cleans up code)
  createDiv(className = "") {
    const el = document.createElement("div")
    el.classList.add(className)
    return el
  }
  // function to add text nodes to elements
  addText(el, text) {
    el.appendChild(document.createTextNode(text))
  }
  
  create(
    title = "Untitled notification",
    description = "",
    duration = 2,
    destroyOnClick = false,
    clickFunction = undefined
  ) {
    
    // functions
    function destroy(animate) {
      if (animate) {
        notiEl.classList.add("out")
        notiEl.addEventListener("animationend", () => {notiEl.remove()})
      } else {
        notiEl.remove()
      }
    }
    
    // create the elements and add their content
    const notiEl = this.createDiv("noti")
    const notiCardEl = this.createDiv("noticard")
    const glowEl = this.createDiv("notiglow")
    const borderEl = this.createDiv("notiborderglow")
    
    const titleEl = this.createDiv("notititle")
    this.addText(titleEl, title)
    
    const descriptionEl = this.createDiv("notidesc")
    this.addText(descriptionEl, description)
    
    // append the elements to each other
    notiEl.appendChild(notiCardEl)
    notiCardEl.appendChild(glowEl)
    notiCardEl.appendChild(borderEl)
    notiCardEl.appendChild(titleEl)
    notiCardEl.appendChild(descriptionEl)
    
    this.el.appendChild(notiEl)
    
    // transition the height of the container to the height of the visible card
    console.log("height", notiCardEl.scrollHeight)
    requestAnimationFrame(function() {
      notiEl.style.height = "calc(0.25rem + " + notiCardEl.getBoundingClientRect().height + "px)";
    });

    // hover animation
    notiEl.addEventListener("mousemove", (event) => {
      const rect = notiCardEl.getBoundingClientRect()
      const localX = (event.clientX - rect.left) / rect.width
      const localY = (event.clientY - rect.top) / rect.height

      console.log(localX, localY)
      glowEl.style.left = localX * 100 + "%"
      glowEl.style.top = localY * 100 + "%"

      borderEl.style.left = localX * 100 + "%"
      borderEl.style.top = localY * 100 + "%"
    });
    
    // onclick function if one is set
    if (clickFunction != undefined) {
      notiEl.addEventListener("click", clickFunction)
    }
    
    // destroy the notification on click if it is set to do so
    if (destroyOnClick) {
      notiEl.addEventListener("click", () => destroy(true))
    }
    
    
    // remove the notification after the set time if there is one
    if (duration != 0) {
      setTimeout(() => {
        notiEl.classList.add("out")
        notiEl.addEventListener("animationend", () => {notiEl.remove()})
      }, duration * 1000)
    }
    return notiEl
  }
}

// demo
notis = new Notifications(document.querySelector(".notifications"))

const demonotis = [
  () => {notis.create("Neon notifications", "wow, these notifications really do look beautiful", 5)},
  () => {notis.create("Hover effects", "and the hover effects make it even better!", 5)},
  () => {notis.create("Ease of use", "on top of that, you can easily add this to any project", 10)},
  () => {notis.create("Customisation", "you can even customise the duration and add actions on click", 10)},
  () => {notis.create("Click me", "try clicking this for a surprise!", 5, true, () => notis.create("Surprise", "Wow, you clicked the previous notification,", 4))},
  () => {notis.create("Animations", "all the animations stay smooth even when notifications disappear out of order or when multiple appear at once", 15)},
]
let i = 1;
demonotis[0]()
setInterval(()=>{
  if (i == demonotis.length) {
    notis.create("Demo done", "click on this notification to restart the demo or go look at the code if you're interested", 0, true, () => {i = 0})
  } else if (i < demonotis.length) {
    demonotis[i]()
  }
  i++
}, 4000)

/*
How to use:

create the notification object using new Notifications and pass it the notification wrapper element
ex: const notis = new Notifications(document.querySelector(".notifications"))

make notifications by using notis.create() (or *.create() if you names it something else)

notis.create() parameters:
  Title: string,
  Description: string,
  Duration: seconds (default 2s, 0 makes it stay forever),
  Destroy on click: boolean
    (determines if the notification should disappear when clicked, default: false)
  Click function: function
    (gets called when the notification is clicked if it isnt undefined, default: undefined)
*/
              
            
!
999px

Console