Tooltip

Instant labels; neighbours show without a wait.

Installation

Terminal
npx shadcn@latest add https://fabrickit.org/r/tooltip.json

This copies the component into components/fabricui and its engine into lib/fabricui. It's your code from then on. First time? Add the theme once: see Installation.

Code

components/fabricui/tooltip.tsx
"use client";

import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { Tooltips } from "@/lib/fabricui/tooltip-engine";
import "./tooltip.css";

const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;

export type TooltipProviderProps = React.HTMLAttributes<HTMLDivElement> & {
  /** ms before the first tooltip shows (no flashing while the pointer passes over) */
  delay?: number;
  /** ms after one hides during which its neighbours still show at once */
  skip?: number;
  /** px between the tooltip and its trigger */
  gap?: number;
components/fabricui/tooltip.css
/*
 * Tooltip — one shared tooltip per page for everything marked [data-tip]
 * (tooltip.js). It's the library's restraint piece: no cloth, no goo.
 *  - the first one waits 500ms (no flashing while the pointer passes over)
 *  - once one is showing, its neighbours appear at once with no animation,
 *    and that stays true for 300ms after it hides
 *  - it grows out of its trigger: 150ms, from 97% and 3px toward the trigger
 *  - it leaves in 100ms, a plain fade; keyboard focus shows it at once
 */
.tip:where(.fui-tooltip, .fui-tooltip *) {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 30;
  height: 28px;
  padding: 0 9px;
lib/fabricui/tooltip-engine.js
/*
 * tooltip-engine.js — the shared Tooltip (styles in tooltip.css). From tooltip.js.
 *
 *   const tips = Tooltips.attach(scopeEl);   // adds the tooltip element, wires every [data-tip] inside
 *
 * Triggers: data-tip (text), data-keys ("⌘ B", shown dimmed), data-side
 * ("top" default / "bottom", flips if there's no room), data-copy (a copy
 * button: says "Copied" in place). Triggers keep their own aria-label.
 *
 * Changes from tooltip.js, for React: triggers added later are wired too (a
 * MutationObserver); data-copy copies its own value when it has one (the
 * page's address otherwise); the toolbar's aria-pressed toggle moved to the
 * demo; destroy() lets go of everything.
 */
const CHECK = `<svg class="icon ok" viewBox="0 0 24 24" aria-hidden="true"><path d="M5 14L8.5 17.5L19 6.5" /></svg>`;
const esc = s => String(s).replace(/[&<>"]/g, c => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[c]);

Usage

Usage
import { TooltipProvider } from "@/components/fabricui/tooltip";

// one provider around an area; anything inside with data-tip gets a tooltip
export function Editor({ children }: { children: React.ReactNode }) {
  return (
    <TooltipProvider>
      <div role="toolbar" aria-label="Formatting">
        <button aria-label="Bold" data-tip="Bold" data-keys="⌘ B">B</button>
        <button aria-label="Copy link" data-tip="Copy link" data-copy="https://acme.studio/f/q3">⧉</button>
      </div>
      {children}
    </TooltipProvider>
  );
}

Props

PropTypeDefault
data-tip

On a trigger: the tooltip's text. Give the trigger its own aria-label too; the tooltip is visual only.

string—
data-keys

On a trigger: a shortcut shown dimmed after the text, keys separated by spaces ("⌘ B").

string—
data-side

On a trigger: which side it shows on. It flips if there's no room.

"top" | "bottom""top"
data-copy

On a trigger: makes it a copy button. Clicking copies the value (or the page's address) and the tooltip says "Copied" in place.

string—
delay

TooltipProvider: ms before the first one shows.

number500
skip

TooltipProvider: ms after one hides during which neighbours still show at once.

number300
gap

TooltipProvider: px between the tooltip and its trigger.

number8
ref.hide

TooltipProvider: hide it from code; now skips the fade.

(now?: boolean) => void—