Segmented control with chart

CSS Observer·10 hours ago
body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: #0f1013;
  color: #eceef2;
  font: 14px/1.4 system-ui, sans-serif;
}

.stats {
  width: min(440px, 100% - 32px);
  padding: 22px;
  border: 1px solid #24262d;
  border-radius: 18px;
  background: #16171c;
}

.stats__head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}

.stats__label {
  margin: 0;
  color: #8f929d;
}

.stats__total {
  margin: 2px 0 0;
  font-size: 1.8rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.03em;
}

.stats__total span:not(.t-d) { display: none; }
.stats:has(#week:checked) .stats__total .t-d,
.stats:has(#month:checked) .stats__total .t-d { display: none; }
.stats:has(#week:checked) .t-w,
.stats:has(#month:checked) .t-m { display: inline; }

.segmented {
  position: relative;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  margin: 0;
  padding: 3px;
  border: 0;
  border-radius: 10px;
  background: #23252c;
}

.segmented input {
  position: absolute;
  opacity: 0;
}

.segmented label {
  position: relative;
  z-index: 1;
  padding: 0.4em 0.85em;
  color: #9a9da8;
  font-weight: 500;
  text-align: center;
  cursor: pointer;
  transition: color 0.2s;
}

.segmented input:checked + label {
  color: #111;
}

.segmented input:focus-visible + label {
  outline: 2px solid #fff;
  border-radius: 8px;
}

.segmented__pill {
  position: absolute;
  top: 3px;
  bottom: 3px;
  left: 3px;
  width: calc((100% - 6px) / 3);
  border-radius: 8px;
  background: #fff;
  transition: transform 0.3s cubic-bezier(0.3, 1.2, 0.5, 1);
}

.segmented:has(#week:checked) .segmented__pill { transform: translateX(100%); }
.segmented:has(#month:checked) .segmented__pill { transform: translateX(200%); }


.chart {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  height: 150px;
  margin-top: 22px;
}

/* Custom properties resolve where they're declared, so each bar picks its own height. */
.chart span {
  --h: var(--d);

  flex: 1;
  height: calc(var(--h) * 1%);
  border-radius: 6px 6px 2px 2px;
  background: linear-gradient(#a78bfa, #6d5cff);
  transition: height 0.45s cubic-bezier(0.3, 1.2, 0.5, 1);
}

.stats:has(#week:checked) .chart span { --h: var(--w); }
.stats:has(#month:checked) .chart span { --h: var(--m); }

.chart span:nth-child(odd) {
  background: linear-gradient(#c4b5fd, #8b7cff);
}

@media (prefers-reduced-motion: reduce) {
  .chart span,
  .segmented__pill {
    transition: none;
  }
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

How to use it

The control is three radio buttons. The indicator and every bar read the checked option through :has(). Set the bar heights per period with --d, --w and --m on each bar.