Bouncing Dots

mobinkakei

"use client";

import type { CSSProperties } from "react";

/**
 * Bouncing Dots: three balls that squash on the floor and bounce, each with
 * a shadow that shrinks as it rises. Pure CSS. mobinkakei's loader on
 * uiverse.io (grumpy-turtle-41).
 *
 *   <BouncingDots />
 *   <BouncingDots color="#ff5c39" size={1.5} speed={2} />
 */
export type BouncingDotsProps = {
  color?: string;
  /** 1 is 200×60px. */
  size?: number;
  /** 1 is normal; 2 is twice as fast. */
  speed?: number;
  label?: string;
  className?: string;
  style?: CSSProperties;
};

const CSS = `
.zs-bd{width:200px;height:66px;position:relative;z-index:1;zoom:var(--z)}
.zs-bd i,.zs-bd b{position:absolute;left:15%;transform-origin:50%}
.zs-bd i{width:20px;height:20px;border-radius:50%;background:var(--c);animation:zs-bd-ball calc(.5s / var(--k)) alternate infinite ease}
.zs-bd b{width:20px;height:4px;border-radius:50%;background:rgba(0,0,0,.9);top:62px;z-index:-1;filter:blur(1px);
  animation:zs-bd-shadow calc(.5s / var(--k)) alternate infinite ease}
.zs-bd :nth-child(2),.zs-bd :nth-child(5){left:45%;animation-delay:calc(.2s / var(--k))}
.zs-bd :nth-child(3),.zs-bd :nth-child(6){left:auto;right:15%;animation-delay:calc(.3s / var(--k))}
@keyframes zs-bd-ball{0%{top:60px;height:5px;border-radius:50px 50px 25px 25px;transform:scaleX(1.7)}40%{height:20px;border-radius:50%;transform:scaleX(1)}100%{top:0}}
@keyframes zs-bd-shadow{0%{transform:scaleX(1.5)}40%{transform:scaleX(1);opacity:.7}100%{transform:scaleX(.2);opacity:.4}}
@media (prefers-reduced-motion:reduce){.zs-bd i,.zs-bd b{animation:none}.zs-bd i{top:40px}}
`;

export function BouncingDots({ color = "#fff", size = 1, speed = 1, label = "Loading", className, style }: BouncingDotsProps) {
  const vars = { "--c": color, "--z": size, "--k": speed } as CSSProperties;
  return (
    <>
      <style>{CSS}</style>
      <div className={`zs-bd${className ? ` ${className}` : ""}`} role="status" aria-label={label} style={{ ...vars, ...style }}>
      <i />
      <i />
      <i />
      <b />
      <b />
      <b />
      </div>
    </>
  );
}