Toolbar tooltips

Putcode·12 hours ago
body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: #f3f2ee;
  color: #1b1c20;
  font: 15px/1.5 system-ui, sans-serif;
}

.editor {
  width: min(440px, 100% - 32px);
  padding: 72px 18px 20px;
  border: 1px solid #e2dfd8;
  border-radius: 16px;
  background: #fff;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 2px;
  width: fit-content;
  padding: 4px;
  border: 1px solid #e6e3dc;
  border-radius: 10px;
  background: #faf9f7;
}

.toolbar__sep {
  width: 1px;
  height: 18px;
  margin: 0 4px;
  background: #e1ded7;
}

.toolbar button {
  position: relative;
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: 0;
  border-radius: 7px;
  background: none;
  color: #3d4048;
  font: 600 15px system-ui, sans-serif;
  cursor: pointer;
}

.toolbar button:hover,
.toolbar button:focus-visible {
  background: #edebe6;
  outline: none;
}

.toolbar button::after,
.toolbar button::before {
  position: absolute;
  left: 50%;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s, transform 0.15s;
}

.toolbar button::after {
  content: attr(data-tip) "  " attr(data-key);
  bottom: calc(100% + 10px);
  padding: 5px 9px;
  border-radius: 7px;
  background: #1b1c20;
  color: #fff;
  font: 500 12px/1.3 system-ui, sans-serif;
  white-space: pre;
  transform: translate(-50%, 4px);
}

.toolbar button::before {
  content: "";
  bottom: calc(100% + 5px);
  border: 5px solid transparent;
  border-top-color: #1b1c20;
  border-bottom: 0;
  transform: translate(-50%, 4px);
}

.toolbar button:hover::after,
.toolbar button:hover::before {
  opacity: 1;
  transform: translate(-50%, 0);
  transition-delay: 0.35s;
}

.toolbar button:focus-visible::after,
.toolbar button:focus-visible::before {
  opacity: 1;
  transform: translate(-50%, 0);
}

.editor__body {
  margin: 16px 0 0;
  color: #5d6068;
}

kbd {
  padding: 1px 5px;
  border: 1px solid #dedbd4;
  border-radius: 5px;
  font: 12px ui-monospace, monospace;
}

@media (prefers-reduced-motion: reduce) {
  .toolbar button::after,
  .toolbar button::before {
    transition: none;
  }
}

How to use it

Put the tooltip text in data-tip and the shortcut in data-key. Tooltips appear after a short delay on hover and immediately on :focus-visible. Each button keeps an aria-label, so screen readers don't rely on the tooltip.