Tooltip
Instant labels; neighbours show without a wait.
Installation
npx shadcn@latest add https://fabrickit.org/r/tooltip.jsonpnpm dlx shadcn@latest add https://fabrickit.org/r/tooltip.jsonyarn shadcn@latest add https://fabrickit.org/r/tooltip.jsonbunx --bun shadcn@latest add https://fabrickit.org/r/tooltip.jsonThis 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.
Step 1: Add the theme (once)
Every colour, radius and curve the components use, as --fabric-* variables. Save it as fabric.css next to your global CSS and import it there. If you've added another Fabric Kit component before, skip this.globals.css@import "./fabric.css";fabric.css/* * fabric.css — Fabric Kit's theme: every colour, radius, ring and curve the * components use, as --fabric-* variables (prefixed, so they never clash * with your own shadcn theme). Installed by `shadcn add @fabrickit/theme` * (it lands in your global CSS) — or import this file once, globally. * Dark by default. For light, put data-theme="light" on <html> (or on any * element: the tokens follow it down the tree; data-theme="dark" turns back). */ :root, [data-theme="dark"] { --fabric-page: #111111; --fabric-surface: #191919; /* triggers, recessed tiles */ --fabric-panel: #202020; /* open panels */ --fabric-raised: #2a2a2a; /* hover, range band */ --fabric-raised-hi: #343434; /* hover inside a band */ --fabric-preview: #252525; /* soft preview fills */ --fabric-text: #e0e0e0;Step 2: Copy the shared engines
Plain JavaScript that does the motion, shared between components. Create each file at the same path in your project; if a file is already there from another component, keep it.lib/fabricui/mount.js/* * mount.js — runs a Fabric Kit engine for a React component and cleans up * after it. The engines are plain JavaScript that wire themselves to the page * (listeners on the document, resize observers, a few elements of their own); * mount() records everything an engine attaches and changes while it's being * set up, and dispose() takes it all back, so a component can come and go (or * be set up twice in a row, as React's Strict Mode does) without leaving * anything behind. * * const m = mount(() => new Engine(root, options)); * m.instance; // the engine * m.dispose(); // on unmount: listeners and observers off, the DOM as it was, then instance.destroy() * // (and destroy() on anything it owns that registered itself, like each ClothDrop) */ export function mount(create) { const ac = new AbortController();Step 3: Copy the Tooltip files
The component itself. Create each one at the same path in your project. It's your code from here on.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 => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]);Step 4: Check the import paths
The files import each other through the @/ alias (the shadcn default, there in a new Next.js app). If your project doesn't have it, add it to tsconfig.json, or change the imports to relative paths.tsconfig.json{ "compilerOptions": { "baseUrl": ".", "paths": { "@/*": ["./*"] } } }Step 5: Use it
Import it from @/components/fabricui/tooltip. The Usage example below shows it in a page.
Code
"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;/*
* 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;/*
* 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 => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]);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
| Prop | Type | Default |
|---|---|---|
| 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. | number | 500 |
| skip TooltipProvider: ms after one hides during which neighbours still show at once. | number | 300 |
| gap TooltipProvider: px between the tooltip and its trigger. | number | 8 |
| ref.hide TooltipProvider: hide it from code; now skips the fade. | (now?: boolean) => void | — |