Start a project

// Loaders & feedback

024 / 112

A tooltip that springs out of its trigger and flips side when it would leave the viewport.

Loading…

The code

All of it, in one file: the component above, restyle included. Nothing imported but React.

"use client";
import { useRef, useState } from "react";
/** SpringTooltip — springs out from its trigger and flips to the other side when it would leave the viewport. React only. */
export default function SpringTooltip({ tip = "Springs out of the trigger" }: { tip?: string }) {
  const [open, setOpen] = useState(false);
  const [below, setBelow] = useState(false);
  const btn = useRef<HTMLButtonElement>(null);
  const show = () => { setBelow(btn.current!.getBoundingClientRect().top < 90); setOpen(true); };
  return (
    <span style={{ position: "relative", display: "inline-block" }}>
      <button ref={btn} className="chip" aria-describedby="tt" onPointerEnter={show} onPointerLeave={() => setOpen(false)} onFocus={show} onBlur={() => setOpen(false)} style={{ height: 40, padding: "0 18px" }}>Hover or focus</button>
      <span id="tt" role="tooltip" className="tt" data-open={open} data-below={below}>{tip}</span>
      <style dangerouslySetInnerHTML={{ __html: `.tt{position:absolute;left:50%;bottom:calc(100% + 10px);padding:8px 12px;border-radius:10px;background:#fff;color:#000;font-size:13px;white-space:nowrap;pointer-events:none;opacity:0;transform:translateX(-50%) translateY(8px) scale(.6);transform-origin:50% 100%;transition:transform .45s cubic-bezier(.34,1.56,.64,1),opacity .2s}.tt[data-below="true"]{bottom:auto;top:calc(100% + 10px);transform-origin:50% 0;transform:translateX(-50%) translateY(-8px) scale(.6)}.tt[data-open="true"]{opacity:1;transform:translateX(-50%) scale(1)}` }} />
    </span>
  );
}

17 lines · 1.5K · SpringTooltip.tsx

Props

PropDefault
tip"Springs out of the trigger"

Or install it

Published to a shadcn-style registry, so one command fetches the file straight into your project.

npx shadcn@latest add https://your-domain.com/r/spring-tooltip.json

Placeholder domain — set NEXT_PUBLIC_SITE_URL to your deployed origin and the command updates.

// Next up