Stacked toasts

Putcode·11 hours ago
Open in editor
body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: #eceae5;
  font: 14px/1.4 system-ui, sans-serif;
}

.toasts {
  position: relative;
  width: min(340px, 100% - 32px);
  height: 230px;
}

.toast {
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: calc(10 - var(--i));
  display: flex;
  align-items: flex-start;
  gap: 12px;
  height: 64px;
  padding: 14px;
  border: 1px solid #e4e1da;
  border-radius: 14px;
  background: #fff;
  box-shadow: 0 10px 30px -12px rgb(0 0 0 / 0.25);
  transform: translateY(calc(var(--i) * -12px)) scale(calc(1 - var(--i) * 0.05));
  transform-origin: bottom center;
  transition: transform 0.4s cubic-bezier(0.2, 0.9, 0.3, 1.1), opacity 0.3s;
}

.toast + .toast > * {
  opacity: 0;
  transition: opacity 0.2s;
}

.toasts:is(:hover, :focus-within) .toast {
  transform: translateY(calc(var(--i) * -76px));
}

.toasts:is(:hover, :focus-within) .toast > * {
  opacity: 1;
}

.toast:focus-visible {
  outline: 2px solid #18191d;
}

.toast__icon {
  display: grid;
  flex: none;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: #f0eee9;
  font-size: 14px;
}

.toast__icon--ok { background: #dcf5e4; color: #1f7a45; font-weight: 700; }
.toast__icon--pay { background: #e8e4ff; color: #4f3fd1; font-weight: 700; }

.toast div {
  flex: 1;
  min-width: 0;
}

.toast__title {
  margin: 0;
  color: #18191d;
  font-weight: 600;
}

.toast__text {
  margin: 1px 0 0;
  overflow: hidden;
  color: #6b6e77;
  font-size: 0.85rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toast time {
  color: #9a9ca4;
  font-size: 0.75rem;
}

@media (prefers-reduced-motion: reduce) {
  .toast {
    transition: none;
  }
}

How to use it

Each toast sets --i, its position from the front. Collapsed, toasts behind the first are scaled down and tucked under it. On :hover or :focus-within they spread out to full height. Newest goes first in the markup.