Installation

  1. Set up the project

    Once per project: the packages, Tailwind CSS, the theme tokens and the cn helper. Follow Installation.

  2. Copy the component into your project

    The kit owns behaviour, ARIA and data attributes. This file is the look, written with Tailwind classes, and it is yours to change.

    src/ui/tooltip.ts
    import type { Component } from "@effect/ui"
    import type { Html } from "@effect/ui/html"
    import type { Tooltip } from "@effect/ui-kit"
    import { cn } from "#lib/cn"
    
    /** What a `Tooltip`'s `view` closure receives. */
    export type TooltipParts = Component.Headless.Parts<typeof Tooltip>
    
    interface ClassOption {
      readonly class?: string | undefined
    }
    
    const variants = {
      default: "bg-primary px-2.5 py-1.5 text-xs font-medium text-primary-foreground",
      rich: "max-w-64 border bg-popover p-3 text-xs leading-relaxed text-foreground shadow-float"
    } as const
    
    export type TooltipVariant = keyof typeof variants
    
    /**
     * The box the kit places next to the trigger. The tooltip's `sideOffset` is its padding.
     * It stays displayed for the length of the popup's exit transition.
     */
    export const TooltipPositioner = <Msg>(h: Html.H<Msg>, tooltip: TooltipParts, options: ClassOption = {}) =>
      tooltip.positioner(h.div.class(cn("transition-[display,overlay] transition-discrete duration-150", options.class)))
    
    /** The bubble. `variant: "rich"` is a small card for more than a label. */
    export const TooltipPopup = <Msg>(
      h: Html.H<Msg>,
      tooltip: TooltipParts,
      options: ClassOption & { readonly variant?: TooltipVariant | undefined } = {}
    ) =>
      tooltip.popup(
        h.div.class(cn(
          "relative flex origin-[var(--transform-origin)] items-center gap-2 rounded-sm whitespace-nowrap",
          "transition-[opacity,scale] duration-150 ease-out",
          "data-[closed]:scale-95 data-[closed]:opacity-0 starting:scale-95 starting:opacity-0",
          "data-[instant]:duration-0",
          variants[options.variant ?? "default"],
          options.variant === "rich" && "flex-col items-stretch gap-1.5 rounded-lg whitespace-normal",
          options.class
        ))
      )
    
    /** A small diamond on the popup's edge, in the default variant's colour. The kit points it at the trigger. */
    export const TooltipArrow = <Msg>(h: Html.H<Msg>, tooltip: TooltipParts, options: ClassOption = {}) =>
      tooltip.arrow(
        h.div.class(cn(
          "size-2 rotate-45 rounded-[1px] bg-primary",
          "data-[side=bottom]:-top-1 data-[side=left]:-right-1 data-[side=right]:-left-1 data-[side=top]:-bottom-1",
          options.class
        ))
      )
    
    /** A keyboard shortcut inside a tooltip. */
    export const TooltipShortcut = <Msg>(h: Html.H<Msg>, options: ClassOption = {}) =>
      h.kbd.class(cn("rounded-xs bg-primary-foreground/15 px-1 py-px font-mono text-[11px]", options.class))
    
  3. Import it

    import { TooltipPositioner, TooltipPopup, TooltipArrow, TooltipShortcut } from "#ui/tooltip"
    

Usage

Basic.ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Tooltip } from "@effect/ui-kit"
import { Schema } from "effect"
import { buttonVariants } from "#ui/button"
import { TooltipArrow, TooltipPopup, TooltipPositioner } from "#ui/tooltip"

const TooltipBasicMessage = UiSchema.TaggedEnum({
  RequestedOpenTooltip: {},
  RequestedCloseTooltip: {}
})

export const TooltipBasic = Html.component({
  name: "TooltipBasic",
  model: Schema.Struct({ open: Schema.Boolean }),
  message: TooltipBasicMessage
}).make({
  init: () => ({ open: false }),
  update: {
    RequestedOpenTooltip: ($) => $.evolve({ open: true }),
    RequestedCloseTooltip: ($) => $.evolve({ open: false })
  },
  view: (h, model) =>
    Tooltip(h, {
      props: { open: model.open, sideOffset: 8 },
      onEmit: {
        RequestedOpen: () => TooltipBasicMessage.RequestedOpenTooltip(),
        RequestedClose: () => TooltipBasicMessage.RequestedCloseTooltip()
      },
      view: (tooltip) =>
        h.div(
          tooltip.trigger(h.button.class(buttonVariants({ variant: "outline" }))).children("Hover me"),
          TooltipPositioner(h, tooltip).children(
            TooltipPopup(h, tooltip).children(
              TooltipArrow(h, tooltip),
              "Add to library"
            )
          )
        )
    })
})

Examples

Sides

The side prop, with one Model field for which tooltip is open.

TooltipProvider

In a group, once one tooltip has shown, its neighbours open without their delay.

Delays

delay before it opens and closeDelay before it closes.

On a disabled button

A Button that stays focusable while disabled can still explain itself.

Published 0 times

Rich content

The pointer can move onto the popup, so it can hold more than a label.

Usage this month: 82% of quota

Anatomy

The parts the view closure receives. Apply each one to an element of your own.

PartDescription
triggerThe <button> or <a href> the tooltip describes.
positionerA <div>, a manual popover placed by the tooltip. sideOffset is its padding.
popupA <div> inside the positioner.
arrowA <div> inside the popup that points at the trigger. Optional.

API reference

Props

PropTypeDescription
openbooleanWhether the tooltip shows. Required: the consumer owns it.
disabledbooleanThe tooltip never shows and emits nothing. The trigger stays enabled.
delaynumberMilliseconds the pointer rests on the trigger before it opens, 600 by default.
closeDelaynumberMilliseconds after the pointer leaves before it closes, 0 by default.
closeOnClickbooleanWhether activating the trigger closes the tooltip, true by default.
disableHoverablePopupbooleanThe pointer on the popup doesn't keep the tooltip open.
describesTriggerbooleanMakes the popup the trigger's accessible description.
side"top" | "bottom" | "left" | "right" | "inline-start" | "inline-end"The side of the trigger the popup shows on.
align"start" | "center" | "end"How the popup lines up with the trigger along that side. "center" by default.
sideOffsetnumberThe gap between the trigger and the popup, in pixels.
alignOffsetnumberPixels the popup moves along the side.
collisionPaddingnumberPixels kept from the viewport's edges, 5 by default.
positionMethod"absolute" | "fixed""absolute" by default, which scrolls with the page.
timeout (TooltipProvider)numberMilliseconds after the last tooltip closes during which another opens at once, 400 by default.

Events

EventDescription
RequestedOpenAfter the hover delay, or when the trigger receives keyboard focus.
RequestedCloseThe pointer left, focus moved away, the trigger was activated, or Escape or a press outside.

Data attributes

AttributeDescription
data-popup-openOn the trigger while the tooltip shows.
data-open / data-closedOn the positioner, popup and arrow.
data-side / data-alignOn the positioner, popup and arrow: where the popup ended up.
data-instantOn the popup and arrow: focus, dismiss or delay, for a change that should skip its transition.
data-anchor-hiddenOn the positioner while the trigger is scrolled out of view.
--transform-originOn the positioner: the point on the trigger the popup grows from.

The components this page's file and examples import.

  • Button: A native button that can stay focusable while disabled.

effectcn is built with Effect UI and @effect/ui-kit. Not affiliated with Effectful Technologies.