Skip to content

Tooltip ​

A popup that shows a hint for sighted users when the mouse rests on an element or the element gets the focus.

vue
<script setup lang="ts">
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';
import { Tooltip } from '@vase-ui/vue/tooltip';
import styles from './index.module.css';

const ICON_PROPS = {
  width: '16',
  height: '16',
  viewBox: '0 0 16 16',
  fill: 'currentColor',
  style: { display: 'block' },
};

const BoldIcon: FunctionalComponent = (_, { attrs }) =>
  h('svg', { ...ICON_PROPS, ...attrs }, [
    h('path', {
      d: 'M3.73353 2.13333C3.4386 2.13333 3.2002 2.37226 3.2002 2.66666C3.2002 2.96106 3.4386 3.2 3.73353 3.2H4.26686V12.8H3.73353C3.4386 12.8 3.2002 13.0389 3.2002 13.3333C3.2002 13.6277 3.4386 13.8667 3.73353 13.8667H9.86686C11.7783 13.8667 13.3335 12.3115 13.3335 10.4C13.3335 8.9968 12.4945 7.78881 11.2929 7.24375C11.8897 6.70615 12.2669 5.93066 12.2669 5.06666C12.2669 3.44906 10.9506 2.13333 9.33353 2.13333H3.73353ZM6.93353 3.2H8.26686C9.29619 3.2 10.1335 4.03733 10.1335 5.06666C10.1335 6.096 9.29619 6.93333 8.26686 6.93333H6.93353V3.2ZM6.93353 8H7.73353H8.26686C9.59006 8 10.6669 9.0768 10.6669 10.4C10.6669 11.7232 9.59006 12.8 8.26686 12.8H6.93353V8Z',
    }),
  ]);

const ItalicIcon: FunctionalComponent = (_, { attrs }) =>
  h('svg', { ...ICON_PROPS, ...attrs }, [
    h('path', {
      d: 'M8.52599 2.12186C8.48583 2.12267 8.44578 2.1265 8.4062 2.13332H6.93328C6.86261 2.13232 6.79244 2.14538 6.72686 2.17173C6.66127 2.19808 6.60158 2.23721 6.55125 2.28683C6.50092 2.33646 6.46096 2.39559 6.43368 2.46079C6.4064 2.526 6.39235 2.59597 6.39235 2.66665C6.39235 2.73733 6.4064 2.80731 6.43368 2.87251C6.46096 2.93772 6.50092 2.99685 6.55125 3.04647C6.60158 3.0961 6.66127 3.13522 6.72686 3.16157C6.79244 3.18793 6.86261 3.20099 6.93328 3.19999H7.70099L6.69057 12.8H5.86661C5.79594 12.799 5.72577 12.812 5.66019 12.8384C5.59461 12.8648 5.53492 12.9039 5.48459 12.9535C5.43425 13.0031 5.39429 13.0623 5.36701 13.1275C5.33973 13.1927 5.32568 13.2626 5.32568 13.3333C5.32568 13.404 5.33973 13.474 5.36701 13.5392C5.39429 13.6044 5.43425 13.6635 5.48459 13.7131C5.53492 13.7628 5.59461 13.8019 5.66019 13.8282C5.72577 13.8546 5.79594 13.8677 5.86661 13.8667H9.06661C9.13729 13.8677 9.20745 13.8546 9.27304 13.8282C9.33862 13.8019 9.39831 13.7628 9.44864 13.7131C9.49897 13.6635 9.53894 13.6044 9.56622 13.5392C9.5935 13.474 9.60754 13.404 9.60754 13.3333C9.60754 13.2626 9.5935 13.1927 9.56622 13.1275C9.53894 13.0623 9.49897 13.0031 9.44864 12.9535C9.39831 12.9039 9.33862 12.8648 9.27304 12.8384C9.20745 12.812 9.13729 12.799 9.06661 12.8H8.2989L9.30932 3.19999H10.1333C10.204 3.20099 10.2741 3.18793 10.3397 3.16157C10.4053 3.13522 10.465 3.0961 10.5153 3.04647C10.5656 2.99685 10.6056 2.93772 10.6329 2.87251C10.6602 2.80731 10.6742 2.73733 10.6742 2.66665C10.6742 2.59597 10.6602 2.526 10.6329 2.46079C10.6056 2.39559 10.5656 2.33646 10.5153 2.28683C10.465 2.23721 10.4053 2.19808 10.3397 2.17173C10.2741 2.14538 10.204 2.13232 10.1333 2.13332H8.66349C8.61807 2.12555 8.57207 2.12171 8.52599 2.12186Z',
    }),
  ]);

const UnderlineIcon: FunctionalComponent = (_, { attrs }) =>
  h('svg', { ...ICON_PROPS, ...attrs }, [
    h('path', {
      d: 'M3.73331 2.13332C3.66264 2.13232 3.59247 2.14538 3.52689 2.17173C3.46131 2.19809 3.40161 2.23721 3.35128 2.28684C3.30095 2.33646 3.26099 2.39559 3.23371 2.4608C3.20643 2.526 3.19238 2.59598 3.19238 2.66666C3.19238 2.73734 3.20643 2.80731 3.23371 2.87252C3.26099 2.93772 3.30095 2.99685 3.35128 3.04648C3.40161 3.0961 3.46131 3.13523 3.52689 3.16158C3.59247 3.18793 3.66264 3.20099 3.73331 3.19999V7.99999C3.73331 10.224 5.55144 12.2667 7.99998 12.2667C10.4485 12.2667 12.2666 10.224 12.2666 7.99999V3.19999C12.3373 3.20099 12.4075 3.18793 12.4731 3.16158C12.5386 3.13523 12.5983 3.0961 12.6487 3.04648C12.699 2.99685 12.739 2.93772 12.7662 2.87252C12.7935 2.80731 12.8076 2.73734 12.8076 2.66666C12.8076 2.59598 12.7935 2.526 12.7662 2.4608C12.739 2.39559 12.699 2.33646 12.6487 2.28684C12.5983 2.23721 12.5386 2.19809 12.4731 2.17173C12.4075 2.14538 12.3373 2.13232 12.2666 2.13332H10.1333C10.0626 2.13232 9.99247 2.14538 9.92689 2.17173C9.8613 2.19809 9.80161 2.23721 9.75128 2.28684C9.70095 2.33646 9.66099 2.39559 9.63371 2.4608C9.60643 2.526 9.59238 2.59598 9.59238 2.66666C9.59238 2.73734 9.60643 2.80731 9.63371 2.87252C9.66099 2.93772 9.70095 2.99685 9.75128 3.04648C9.80161 3.0961 9.8613 3.13523 9.92689 3.16158C9.99247 3.18793 10.0626 3.20099 10.1333 3.19999V8.97187C10.1333 10.0855 9.32179 11.0818 8.21352 11.1896C6.94152 11.3138 5.86665 10.3136 5.86665 9.06666V3.19999C5.93732 3.20099 6.00748 3.18793 6.07307 3.16158C6.13865 3.13523 6.19834 3.0961 6.24867 3.04648C6.299 2.99685 6.33897 2.93772 6.36625 2.87252C6.39353 2.80731 6.40757 2.73734 6.40757 2.66666C6.40757 2.59598 6.39353 2.526 6.36625 2.4608C6.33897 2.39559 6.299 2.33646 6.24867 2.28684C6.19834 2.23721 6.13865 2.19809 6.07307 2.17173C6.00748 2.14538 5.93732 2.13232 5.86665 2.13332H3.73331ZM3.73331 13.3333C3.66264 13.3323 3.59247 13.3454 3.52689 13.3717C3.46131 13.3981 3.40161 13.4372 3.35128 13.4868C3.30095 13.5365 3.26099 13.5956 3.23371 13.6608C3.20643 13.726 3.19238 13.796 3.19238 13.8667C3.19238 13.9373 3.20643 14.0073 3.23371 14.0725C3.26099 14.1377 3.30095 14.1969 3.35128 14.2465C3.40161 14.2961 3.46131 14.3352 3.52689 14.3616C3.59247 14.3879 3.66264 14.401 3.73331 14.4H12.2666C12.3373 14.401 12.4075 14.3879 12.4731 14.3616C12.5386 14.3352 12.5983 14.2961 12.6487 14.2465C12.699 14.1969 12.739 14.1377 12.7662 14.0725C12.7935 14.0073 12.8076 13.9373 12.8076 13.8667C12.8076 13.796 12.7935 13.726 12.7662 13.6608C12.739 13.5956 12.699 13.5365 12.6487 13.4868C12.5983 13.4372 12.5386 13.3981 12.4731 13.3717C12.4075 13.3454 12.3373 13.3323 12.2666 13.3333H3.73331Z',
    }),
  ]);

const popupHiddenClass = styles.PopupHidden;
</script>

<template>
  <Tooltip.Provider>
    <div :class="styles.Panel">
      <Tooltip.Root>
        <Tooltip.Trigger :class="styles.Button" aria-label="Bold">
          <BoldIcon aria-hidden="true" />
        </Tooltip.Trigger>
        <Tooltip.Portal>
          <Tooltip.Positioner :side-offset="11">
            <Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
              <Tooltip.Popup :class="styles.Popup">
                <Tooltip.Arrow :class="styles.Arrow" />
                Bold
              </Tooltip.Popup>
            </Transition>
          </Tooltip.Positioner>
        </Tooltip.Portal>
      </Tooltip.Root>

      <Tooltip.Root>
        <Tooltip.Trigger :class="styles.Button" aria-label="Italic">
          <ItalicIcon aria-hidden="true" />
        </Tooltip.Trigger>
        <Tooltip.Portal>
          <Tooltip.Positioner :side-offset="11">
            <Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
              <Tooltip.Popup :class="styles.Popup">
                <Tooltip.Arrow :class="styles.Arrow" />
                Italic
              </Tooltip.Popup>
            </Transition>
          </Tooltip.Positioner>
        </Tooltip.Portal>
      </Tooltip.Root>

      <Tooltip.Root>
        <Tooltip.Trigger :class="styles.Button" aria-label="Underline">
          <UnderlineIcon aria-hidden="true" />
        </Tooltip.Trigger>
        <Tooltip.Portal>
          <Tooltip.Positioner :side-offset="11">
            <Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
              <Tooltip.Popup :class="styles.Popup">
                <Tooltip.Arrow :class="styles.Arrow" />
                Underline
              </Tooltip.Popup>
            </Transition>
          </Tooltip.Positioner>
        </Tooltip.Portal>
      </Tooltip.Root>
    </div>
  </Tooltip.Provider>
</template>
css
.Panel {
  display: flex;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
  }
}

.Button {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  padding: 0;
  margin: 0;
  border: 0;
  background-color: transparent;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    color: white;
  }

  &[data-popup-open] {
    background-color: oklch(97% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(26.9% 0 0deg);
    }
  }

  @media (hover: hover) {
    &:hover:not(:disabled) {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active:not(:disabled) {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &:focus-visible {
    background-color: transparent;
    outline: 2px solid oklch(14.5% 0 0deg);
    position: relative;
    z-index: 1;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Popup {
  box-sizing: border-box;
  font-size: 0.875rem;
  line-height: 1.25rem;
  display: flex;
  flex-direction: column;
  padding: 0.25rem 0.5rem;
  position: relative;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  color: oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
  transform-origin: var(--transform-origin);
  transition:
    scale 100ms ease-out,
    opacity 100ms ease-out;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
    box-shadow: none;
  }

  &[data-instant] {
    transition: none;
  }
}

/* The enter-from and leave-to class of the `<Transition>` around the popup. */
.PopupHidden {
  opacity: 0;
  scale: 0.98;
}

.Arrow {
  display: block;
  position: relative;
  width: 12px;
  height: 6px;
  overflow: clip;

  &[data-side='top'] {
    bottom: -6px;
    rotate: 180deg;
  }

  &[data-side='bottom'] {
    top: -6px;
    rotate: 0deg;
  }

  &[data-side='left'] {
    right: -9px;
    rotate: 90deg;
  }

  &[data-side='right'] {
    left: -9px;
    rotate: -90deg;
  }

  &::before {
    content: '';
    display: block;
    position: absolute;
    bottom: 0;
    left: 50%;
    box-sizing: border-box;
    width: calc(6px * sqrt(2));
    height: calc(6px * sqrt(2));
    background-color: white;
    border: 1px solid oklch(14.5% 0 0deg);
    transform: translate(-50%, 50%) rotate(45deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(14.5% 0 0deg);
      border: 1px solid white;
    }
  }
}
vue
<script setup lang="ts">
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';
import { Tooltip } from '@vase-ui/vue/tooltip';

const ICON_PROPS = {
  width: '16',
  height: '16',
  viewBox: '0 0 16 16',
  fill: 'currentColor',
  style: { display: 'block' },
};

const BoldIcon: FunctionalComponent = (_, { attrs }) =>
  h('svg', { ...ICON_PROPS, ...attrs }, [
    h('path', {
      d: 'M3.73353 2.13333C3.4386 2.13333 3.2002 2.37226 3.2002 2.66666C3.2002 2.96106 3.4386 3.2 3.73353 3.2H4.26686V12.8H3.73353C3.4386 12.8 3.2002 13.0389 3.2002 13.3333C3.2002 13.6277 3.4386 13.8667 3.73353 13.8667H9.86686C11.7783 13.8667 13.3335 12.3115 13.3335 10.4C13.3335 8.9968 12.4945 7.78881 11.2929 7.24375C11.8897 6.70615 12.2669 5.93066 12.2669 5.06666C12.2669 3.44906 10.9506 2.13333 9.33353 2.13333H3.73353ZM6.93353 3.2H8.26686C9.29619 3.2 10.1335 4.03733 10.1335 5.06666C10.1335 6.096 9.29619 6.93333 8.26686 6.93333H6.93353V3.2ZM6.93353 8H7.73353H8.26686C9.59006 8 10.6669 9.0768 10.6669 10.4C10.6669 11.7232 9.59006 12.8 8.26686 12.8H6.93353V8Z',
    }),
  ]);

const ItalicIcon: FunctionalComponent = (_, { attrs }) =>
  h('svg', { ...ICON_PROPS, ...attrs }, [
    h('path', {
      d: 'M8.52599 2.12186C8.48583 2.12267 8.44578 2.1265 8.4062 2.13332H6.93328C6.86261 2.13232 6.79244 2.14538 6.72686 2.17173C6.66127 2.19808 6.60158 2.23721 6.55125 2.28683C6.50092 2.33646 6.46096 2.39559 6.43368 2.46079C6.4064 2.526 6.39235 2.59597 6.39235 2.66665C6.39235 2.73733 6.4064 2.80731 6.43368 2.87251C6.46096 2.93772 6.50092 2.99685 6.55125 3.04647C6.60158 3.0961 6.66127 3.13522 6.72686 3.16157C6.79244 3.18793 6.86261 3.20099 6.93328 3.19999H7.70099L6.69057 12.8H5.86661C5.79594 12.799 5.72577 12.812 5.66019 12.8384C5.59461 12.8648 5.53492 12.9039 5.48459 12.9535C5.43425 13.0031 5.39429 13.0623 5.36701 13.1275C5.33973 13.1927 5.32568 13.2626 5.32568 13.3333C5.32568 13.404 5.33973 13.474 5.36701 13.5392C5.39429 13.6044 5.43425 13.6635 5.48459 13.7131C5.53492 13.7628 5.59461 13.8019 5.66019 13.8282C5.72577 13.8546 5.79594 13.8677 5.86661 13.8667H9.06661C9.13729 13.8677 9.20745 13.8546 9.27304 13.8282C9.33862 13.8019 9.39831 13.7628 9.44864 13.7131C9.49897 13.6635 9.53894 13.6044 9.56622 13.5392C9.5935 13.474 9.60754 13.404 9.60754 13.3333C9.60754 13.2626 9.5935 13.1927 9.56622 13.1275C9.53894 13.0623 9.49897 13.0031 9.44864 12.9535C9.39831 12.9039 9.33862 12.8648 9.27304 12.8384C9.20745 12.812 9.13729 12.799 9.06661 12.8H8.2989L9.30932 3.19999H10.1333C10.204 3.20099 10.2741 3.18793 10.3397 3.16157C10.4053 3.13522 10.465 3.0961 10.5153 3.04647C10.5656 2.99685 10.6056 2.93772 10.6329 2.87251C10.6602 2.80731 10.6742 2.73733 10.6742 2.66665C10.6742 2.59597 10.6602 2.526 10.6329 2.46079C10.6056 2.39559 10.5656 2.33646 10.5153 2.28683C10.465 2.23721 10.4053 2.19808 10.3397 2.17173C10.2741 2.14538 10.204 2.13232 10.1333 2.13332H8.66349C8.61807 2.12555 8.57207 2.12171 8.52599 2.12186Z',
    }),
  ]);

const UnderlineIcon: FunctionalComponent = (_, { attrs }) =>
  h('svg', { ...ICON_PROPS, ...attrs }, [
    h('path', {
      d: 'M3.73331 2.13332C3.66264 2.13232 3.59247 2.14538 3.52689 2.17173C3.46131 2.19809 3.40161 2.23721 3.35128 2.28684C3.30095 2.33646 3.26099 2.39559 3.23371 2.4608C3.20643 2.526 3.19238 2.59598 3.19238 2.66666C3.19238 2.73734 3.20643 2.80731 3.23371 2.87252C3.26099 2.93772 3.30095 2.99685 3.35128 3.04648C3.40161 3.0961 3.46131 3.13523 3.52689 3.16158C3.59247 3.18793 3.66264 3.20099 3.73331 3.19999V7.99999C3.73331 10.224 5.55144 12.2667 7.99998 12.2667C10.4485 12.2667 12.2666 10.224 12.2666 7.99999V3.19999C12.3373 3.20099 12.4075 3.18793 12.4731 3.16158C12.5386 3.13523 12.5983 3.0961 12.6487 3.04648C12.699 2.99685 12.739 2.93772 12.7662 2.87252C12.7935 2.80731 12.8076 2.73734 12.8076 2.66666C12.8076 2.59598 12.7935 2.526 12.7662 2.4608C12.739 2.39559 12.699 2.33646 12.6487 2.28684C12.5983 2.23721 12.5386 2.19809 12.4731 2.17173C12.4075 2.14538 12.3373 2.13232 12.2666 2.13332H10.1333C10.0626 2.13232 9.99247 2.14538 9.92689 2.17173C9.8613 2.19809 9.80161 2.23721 9.75128 2.28684C9.70095 2.33646 9.66099 2.39559 9.63371 2.4608C9.60643 2.526 9.59238 2.59598 9.59238 2.66666C9.59238 2.73734 9.60643 2.80731 9.63371 2.87252C9.66099 2.93772 9.70095 2.99685 9.75128 3.04648C9.80161 3.0961 9.8613 3.13523 9.92689 3.16158C9.99247 3.18793 10.0626 3.20099 10.1333 3.19999V8.97187C10.1333 10.0855 9.32179 11.0818 8.21352 11.1896C6.94152 11.3138 5.86665 10.3136 5.86665 9.06666V3.19999C5.93732 3.20099 6.00748 3.18793 6.07307 3.16158C6.13865 3.13523 6.19834 3.0961 6.24867 3.04648C6.299 2.99685 6.33897 2.93772 6.36625 2.87252C6.39353 2.80731 6.40757 2.73734 6.40757 2.66666C6.40757 2.59598 6.39353 2.526 6.36625 2.4608C6.33897 2.39559 6.299 2.33646 6.24867 2.28684C6.19834 2.23721 6.13865 2.19809 6.07307 2.17173C6.00748 2.14538 5.93732 2.13232 5.86665 2.13332H3.73331ZM3.73331 13.3333C3.66264 13.3323 3.59247 13.3454 3.52689 13.3717C3.46131 13.3981 3.40161 13.4372 3.35128 13.4868C3.30095 13.5365 3.26099 13.5956 3.23371 13.6608C3.20643 13.726 3.19238 13.796 3.19238 13.8667C3.19238 13.9373 3.20643 14.0073 3.23371 14.0725C3.26099 14.1377 3.30095 14.1969 3.35128 14.2465C3.40161 14.2961 3.46131 14.3352 3.52689 14.3616C3.59247 14.3879 3.66264 14.401 3.73331 14.4H12.2666C12.3373 14.401 12.4075 14.3879 12.4731 14.3616C12.5386 14.3352 12.5983 14.2961 12.6487 14.2465C12.699 14.1969 12.739 14.1377 12.7662 14.0725C12.7935 14.0073 12.8076 13.9373 12.8076 13.8667C12.8076 13.796 12.7935 13.726 12.7662 13.6608C12.739 13.5956 12.699 13.5365 12.6487 13.4868C12.5983 13.4372 12.5386 13.3981 12.4731 13.3717C12.4075 13.3454 12.3373 13.3323 12.2666 13.3333H3.73331Z',
    }),
  ]);

const triggerClass =
  'flex size-8 items-center justify-center border-0 bg-transparent text-neutral-950 select-none data-popup-open:bg-neutral-100 focus-visible:relative focus-visible:z-1 focus-visible:bg-transparent focus-visible:outline-2 focus-visible:outline-neutral-950 dark:focus-visible:outline-white hover:bg-neutral-100 active:bg-neutral-200 dark:text-white dark:data-popup-open:bg-neutral-800 dark:hover:bg-neutral-800 dark:active:bg-neutral-700';
const popupClass =
  'relative flex flex-col border border-neutral-950 bg-white px-2 py-1 text-sm text-neutral-950 origin-[var(--transform-origin)] shadow-[0.25rem_0.25rem_0] shadow-black/12 transition-[transform,opacity] duration-100 ease-out data-instant:transition-none dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none';
const popupHiddenClass = 'opacity-0 [transform:scale(0.98)]';
const arrowClass =
  "relative block w-3 h-1.5 overflow-clip data-[side=bottom]:top-[-6px] data-[side=left]:right-[-9px] data-[side=left]:rotate-90 data-[side=right]:left-[-9px] data-[side=right]:-rotate-90 data-[side=top]:bottom-[-6px] data-[side=top]:rotate-180 before:content-[''] before:absolute before:bottom-0 before:left-1/2 before:w-[calc(6px*sqrt(2))] before:h-[calc(6px*sqrt(2))] before:bg-white dark:before:bg-neutral-950 before:border before:border-neutral-950 dark:before:border-white before:[transform:translate(-50%,50%)_rotate(45deg)]";
</script>

<template>
  <Tooltip.Provider>
    <div class="flex border border-neutral-950 bg-white dark:border-white dark:bg-neutral-950">
      <Tooltip.Root>
        <Tooltip.Trigger :class="triggerClass" aria-label="Bold">
          <BoldIcon aria-hidden="true" />
        </Tooltip.Trigger>
        <Tooltip.Portal>
          <Tooltip.Positioner :side-offset="11">
            <Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
              <Tooltip.Popup :class="popupClass">
                <Tooltip.Arrow :class="arrowClass" />
                Bold
              </Tooltip.Popup>
            </Transition>
          </Tooltip.Positioner>
        </Tooltip.Portal>
      </Tooltip.Root>

      <Tooltip.Root>
        <Tooltip.Trigger :class="triggerClass" aria-label="Italic">
          <ItalicIcon aria-hidden="true" />
        </Tooltip.Trigger>
        <Tooltip.Portal>
          <Tooltip.Positioner :side-offset="11">
            <Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
              <Tooltip.Popup :class="popupClass">
                <Tooltip.Arrow :class="arrowClass" />
                Italic
              </Tooltip.Popup>
            </Transition>
          </Tooltip.Positioner>
        </Tooltip.Portal>
      </Tooltip.Root>

      <Tooltip.Root>
        <Tooltip.Trigger :class="triggerClass" aria-label="Underline">
          <UnderlineIcon aria-hidden="true" />
        </Tooltip.Trigger>
        <Tooltip.Portal>
          <Tooltip.Positioner :side-offset="11">
            <Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
              <Tooltip.Popup :class="popupClass">
                <Tooltip.Arrow :class="arrowClass" />
                Underline
              </Tooltip.Popup>
            </Transition>
          </Tooltip.Positioner>
        </Tooltip.Portal>
      </Tooltip.Root>
    </div>
  </Tooltip.Provider>
</template>

Usage guidelines ​

  • Use a tooltip only as a visual label: A tooltip is a supplementary visual label for sighted users with a mouse or a keyboard. Touch users and screen reader users do not get it. See Alternatives to tooltips.
  • Give the trigger an accessible name: A tooltip does not label its trigger. Give the trigger an aria-label that matches the text of the tooltip, so that screen reader users get the same information.

Anatomy ​

Import the component and assemble its parts:

vue
<script setup lang="ts">
import { Tooltip } from '@vase-ui/vue/tooltip';
</script>

<template>
  <Tooltip.Provider>
    <Tooltip.Root>
      <Tooltip.Trigger />
      <Tooltip.Portal>
        <Tooltip.Positioner>
          <Tooltip.Popup>
            <Tooltip.Arrow />
            <Tooltip.Viewport />
          </Tooltip.Popup>
        </Tooltip.Positioner>
      </Tooltip.Portal>
    </Tooltip.Root>
  </Tooltip.Provider>
</template>

Alternatives to tooltips ​

Use a tooltip only to give extra clarity in a dense interface. A user who never sees a tooltip must not miss important information.

A tooltip does not work well with touch input. A touch screen has no pointer that hovers, so a touch user cannot find a tooltip before a tap on its trigger. iOS has no standard tooltip gesture for touch. Android can show a tooltip on a long press, but on the web a long press usually opens the context menu of the browser. For this reason, a touch opens no tooltip.

Infotips ​

A popup that opens when the mouse rests on an info icon is a popover, not a tooltip. Use Popover with openOnHover on its trigger, so that touch users and screen reader users can get the content.

Look at the purpose of the trigger. If the trigger only opens the popup, use a popover. If the trigger has a different purpose, use a tooltip.

Description text ​

A tooltip is for sighted users. It does not give important information to touch users or to assistive technology. If the user must have the description to understand the element, show it as text on the page. If the space is small, use Popover.

An icon trigger with no text label must show its purpose with its icon, because a mobile screen can hide the text label. A tooltip can give extra clarity to sighted users with a mouse or a keyboard when the description is not important.

Contextual feedback messages ​

To show a feedback message next to an element, use an anchored toast. A screen reader then announces the message, and the message can hold complex content.

Examples ​

Detached triggers ​

A trigger can be inside or outside Tooltip.Root. For a simple tooltip, put Tooltip.Trigger inside Tooltip.Root, as the demo at the top of this page does.

When the content of the tooltip cannot be next to its trigger, use a detached trigger. Put Tooltip.Trigger outside Tooltip.Root, and connect the two with a handle from Tooltip.createHandle().

The methods open() and close() of the handle need a mounted Tooltip.Root with the same handle. The handle ignores a call while no root uses it: before a root mounts, and after it unmounts. Each root starts with a new state at its mount. It does not replay an earlier call, and it does not keep the open state of an earlier root.

vue
<script setup lang="ts">
import { Tooltip } from '@vase-ui/vue/tooltip';

const demoTooltip = Tooltip.createHandle();
</script>

<template>
  <Tooltip.Trigger :handle="demoTooltip">Button</Tooltip.Trigger>

  <Tooltip.Root :handle="demoTooltip">
    <Tooltip.Portal>
      <Tooltip.Positioner>
        <Tooltip.Popup>Tooltip</Tooltip.Popup>
      </Tooltip.Positioner>
    </Tooltip.Portal>
  </Tooltip.Root>
</template>
vue
<script setup lang="ts">
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';
import { Tooltip } from '@vase-ui/vue/tooltip';
import styles from '../../index.module.css';

const demoTooltip = Tooltip.createHandle();

const TrashIcon: FunctionalComponent = (_, { attrs }) =>
  h(
    'svg',
    {
      width: '16',
      height: '16',
      viewBox: '0 0 16 16',
      fill: 'none',
      stroke: 'currentColor',
      'stroke-linejoin': 'round',
      ...attrs,
      style: { display: 'block' },
    },
    [
      h('path', { 'stroke-linecap': 'square', d: 'M2.5 4h11' }),
      h('path', {
        'stroke-linecap': 'round',
        d: 'M6.5 4V3c0-.82843.67157-1.5 1.5-1.5s1.5.67157 1.5 1.5v1',
      }),
      h('path', {
        'stroke-linecap': 'square',
        d: 'm3.5 4 .87069 9.1422c.07332.7699.7199 1.3578 1.49324 1.3578h4.27217c.7733 0 1.4199-.5879 1.4932-1.3578L12.5 4',
      }),
    ],
  );

const popupHiddenClass = styles.PopupHidden;
</script>

<template>
  <Tooltip.Provider>
    <Tooltip.Trigger :class="styles.IconButton" :handle="demoTooltip" aria-label="Delete">
      <TrashIcon aria-hidden="true" />
    </Tooltip.Trigger>

    <Tooltip.Root :handle="demoTooltip">
      <Tooltip.Portal>
        <Tooltip.Positioner :side-offset="11">
          <Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
            <Tooltip.Popup :class="styles.Popup">
              <Tooltip.Arrow :class="styles.Arrow" />
              Delete
            </Tooltip.Popup>
          </Transition>
        </Tooltip.Positioner>
      </Tooltip.Portal>
    </Tooltip.Root>
  </Tooltip.Provider>
</template>
css
.Container {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  justify-content: center;
}

.ButtonGroup {
  display: flex;
}

.IconButton {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  padding: 0;
  margin: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &[data-popup-open] {
    background-color: oklch(97% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(26.9% 0 0deg);
    }
  }

  @media (hover: hover) {
    &:hover:not(:disabled) {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active:not(:disabled) {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &:not(:first-child) {
    border-left: none;
  }

  &:disabled {
    color: oklch(55.6% 0 0deg);
    border-color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
      border-color: oklch(70.8% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;
    position: relative;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Positioner {
  height: var(--positioner-height);
  width: var(--positioner-width);
  max-width: var(--available-width);
}

.Popup {
  box-sizing: border-box;
  position: relative;
  font-size: 0.875rem;
  line-height: 1.25rem;
  padding: 0.25rem 0.5rem;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  color: oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
  transform-origin: var(--transform-origin);
  transition:
    scale 100ms ease-out,
    opacity 100ms ease-out;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
    box-shadow: none;
  }

  &[data-instant] {
    transition: none;
  }
}

/* The enter-from and leave-to class of the `<Transition>` around the popup. */
.PopupHidden {
  opacity: 0;
  scale: 0.98;
}

.Arrow {
  display: block;
  position: relative;
  width: 12px;
  height: 6px;
  overflow: clip;

  &[data-side='top'] {
    bottom: -6px;
    rotate: 180deg;
  }

  &[data-side='bottom'] {
    top: -6px;
    rotate: 0deg;
  }

  &[data-side='left'] {
    right: -9px;
    rotate: 90deg;
  }

  &[data-side='right'] {
    left: -9px;
    rotate: -90deg;
  }

  &::before {
    content: '';
    display: block;
    position: absolute;
    bottom: 0;
    left: 50%;
    box-sizing: border-box;
    width: calc(6px * sqrt(2));
    height: calc(6px * sqrt(2));
    background-color: white;
    border: 1px solid oklch(14.5% 0 0deg);
    transform: translate(-50%, 50%) rotate(45deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(14.5% 0 0deg);
      border: 1px solid white;
    }
  }
}

.Button {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.75rem;
  margin: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover:not(:disabled) {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active:not(:disabled) {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &:disabled {
    color: oklch(55.6% 0 0deg);
    border-color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
      border-color: oklch(70.8% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;
    position: relative;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}
vue
<script setup lang="ts">
import { Tooltip } from '@vase-ui/vue/tooltip';
import { TrashIcon } from '../../icons-tw';

const demoTooltip = Tooltip.createHandle();

const triggerClass =
  'flex size-8 items-center justify-center border border-neutral-950 bg-white text-neutral-950 select-none data-popup-open:bg-neutral-100 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white hover:bg-neutral-100 active:bg-neutral-200 dark:border-white dark:bg-neutral-950 dark:text-white dark:data-popup-open:bg-neutral-800 dark:hover:bg-neutral-800 dark:active:bg-neutral-700';
const popupClass =
  'relative flex flex-col border border-neutral-950 bg-white px-2 py-1 text-sm text-neutral-950 origin-[var(--transform-origin)] shadow-[0.25rem_0.25rem_0] shadow-black/12 transition-[transform,opacity] duration-100 ease-out data-instant:transition-none dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none';
const popupHiddenClass = 'opacity-0 [transform:scale(0.98)]';
const arrowClass =
  "relative block w-3 h-1.5 overflow-clip data-[side=bottom]:top-[-6px] data-[side=left]:right-[-9px] data-[side=left]:rotate-90 data-[side=right]:left-[-9px] data-[side=right]:-rotate-90 data-[side=top]:bottom-[-6px] data-[side=top]:rotate-180 before:content-[''] before:absolute before:bottom-0 before:left-1/2 before:w-[calc(6px*sqrt(2))] before:h-[calc(6px*sqrt(2))] before:bg-white dark:before:bg-neutral-950 before:border before:border-neutral-950 dark:before:border-white before:[transform:translate(-50%,50%)_rotate(45deg)]";
</script>

<template>
  <Tooltip.Provider>
    <Tooltip.Trigger :class="triggerClass" :handle="demoTooltip" aria-label="Delete">
      <TrashIcon aria-hidden="true" />
    </Tooltip.Trigger>

    <Tooltip.Root :handle="demoTooltip">
      <Tooltip.Portal>
        <Tooltip.Positioner :side-offset="11">
          <Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
            <Tooltip.Popup :class="popupClass">
              <Tooltip.Arrow :class="arrowClass" />
              Delete
            </Tooltip.Popup>
          </Transition>
        </Tooltip.Positioner>
      </Tooltip.Portal>
    </Tooltip.Root>
  </Tooltip.Provider>
</template>
ts
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';

export const HeadphonesIcon: FunctionalComponent = (_, { attrs }) =>
  h(
    'svg',
    {
      width: '16',
      height: '16',
      viewBox: '0 0 16 16',
      fill: 'none',
      stroke: 'currentColor',
      ...attrs,
      style: { display: 'block' },
    },
    [
      h('path', {
        'stroke-linecap': 'round',
        d: 'M1.5 11V7.5c0-2.5 2.5-6 6.5-6s6.5 3.5 6.5 6V11',
      }),
      h('path', {
        d: 'M12 7.5c1.3807 0 2.5 1.11929 2.5 2.5v2c0 1.3807-1.1193 2.5-2.5 2.5h-1.5v-7zm-8 0h1.5v7H4c-1.38071 0-2.5-1.1193-2.5-2.5v-2c0-1.38071 1.11929-2.5 2.5-2.5Z',
      }),
    ],
  );

export const StopwatchIcon: FunctionalComponent = (_, { attrs }) =>
  h(
    'svg',
    {
      width: '16',
      height: '16',
      viewBox: '0 0 16 16',
      fill: 'none',
      stroke: 'currentColor',
      ...attrs,
      style: { display: 'block' },
    },
    [
      h('circle', { cx: '8', cy: '8.5', r: '6' }),
      h('path', {
        'stroke-linecap': 'square',
        'stroke-linejoin': 'round',
        d: 'M8 9.5v-5m0-2v-2m-2 0h4M12 4l1.5-1.5',
      }),
    ],
  );

export const TrashIcon: FunctionalComponent = (_, { attrs }) =>
  h(
    'svg',
    {
      width: '16',
      height: '16',
      viewBox: '0 0 16 16',
      fill: 'none',
      stroke: 'currentColor',
      'stroke-linejoin': 'round',
      ...attrs,
      style: { display: 'block' },
    },
    [
      h('path', { 'stroke-linecap': 'square', d: 'M2.5 4h11' }),
      h('path', {
        'stroke-linecap': 'round',
        d: 'M6.5 4V3c0-.82843.67157-1.5 1.5-1.5s1.5.67157 1.5 1.5v1',
      }),
      h('path', {
        'stroke-linecap': 'square',
        d: 'm3.5 4 .87069 9.1422c.07332.7699.7199 1.3578 1.49324 1.3578h4.27217c.7733 0 1.4199-.5879 1.4932-1.3578L12.5 4',
      }),
    ],
  );

Multiple triggers ​

One tooltip can have more than one trigger. Give the same handle to more than one detached trigger, or put more than one Tooltip.Trigger in one Tooltip.Root.

vue
<script setup lang="ts">
import { Tooltip } from '@vase-ui/vue/tooltip';
</script>

<template>
  <Tooltip.Root>
    <Tooltip.Trigger>Trigger 1</Tooltip.Trigger>
    <Tooltip.Trigger>Trigger 2</Tooltip.Trigger>
    <Tooltip.Portal>
      <Tooltip.Positioner>
        <Tooltip.Popup>Tooltip</Tooltip.Popup>
      </Tooltip.Positioner>
    </Tooltip.Portal>
  </Tooltip.Root>
</template>
vue
<script setup lang="ts">
import { Tooltip } from '@vase-ui/vue/tooltip';

const demoTooltip = Tooltip.createHandle();
</script>

<template>
  <Tooltip.Trigger :handle="demoTooltip">Trigger 1</Tooltip.Trigger>

  <Tooltip.Trigger :handle="demoTooltip">Trigger 2</Tooltip.Trigger>

  <Tooltip.Root :handle="demoTooltip">
    <Tooltip.Portal>
      <Tooltip.Positioner>
        <Tooltip.Popup>Tooltip</Tooltip.Popup>
      </Tooltip.Positioner>
    </Tooltip.Portal>
  </Tooltip.Root>
</template>

The tooltip can show different content for each trigger. Give a payload to each Tooltip.Trigger. The default slot of Tooltip.Root gets the payload of the active trigger. The type argument of createHandle() gives the payload its type:

vue
<script setup lang="ts">
import { Tooltip } from '@vase-ui/vue/tooltip';

const demoTooltip = Tooltip.createHandle<{ text: string }>();
</script>

<template>
  <Tooltip.Trigger :handle="demoTooltip" :payload="{ text: 'Trigger 1' }">
    Trigger 1
  </Tooltip.Trigger>

  <Tooltip.Trigger :handle="demoTooltip" :payload="{ text: 'Trigger 2' }">
    Trigger 2
  </Tooltip.Trigger>

  <Tooltip.Root v-slot="{ payload }" :handle="demoTooltip">
    <Tooltip.Portal>
      <Tooltip.Positioner :side-offset="8">
        <Tooltip.Popup>
          <Tooltip.Arrow />
          <span v-if="payload !== undefined">Tooltip opened by {{ payload.text }}</span>
        </Tooltip.Popup>
      </Tooltip.Positioner>
    </Tooltip.Portal>
  </Tooltip.Root>
</template>

Controlled mode with multiple triggers ​

To control the open state, use v-model:open on Tooltip.Root, or the open prop and the openChange event. With more than one trigger, control the active trigger with v-model:trigger-id on Tooltip.Root and the id prop of each Tooltip.Trigger.

The second argument of openChange has trigger, the element of the trigger that started the change.

vue
<script setup lang="ts">
import { h, shallowRef } from 'vue';
import type { FunctionalComponent } from 'vue';
import { Tooltip } from '@vase-ui/vue/tooltip';
import styles from '../../index.module.css';

const demoTooltip = Tooltip.createHandle();

const open = shallowRef(false);
const triggerId = shallowRef<string | null>(null);

function handleOpenChange(isOpen: boolean, eventDetails: Tooltip.Root.ChangeEventDetails) {
  open.value = isOpen;
  triggerId.value = eventDetails.trigger?.id ?? null;
}

function openProgrammatically() {
  triggerId.value = 'trigger-2';
  open.value = true;
}

const HeadphonesIcon: FunctionalComponent = (_, { attrs }) =>
  h(
    'svg',
    {
      width: '16',
      height: '16',
      viewBox: '0 0 16 16',
      fill: 'none',
      stroke: 'currentColor',
      ...attrs,
      style: { display: 'block' },
    },
    [
      h('path', {
        'stroke-linecap': 'round',
        d: 'M1.5 11V7.5c0-2.5 2.5-6 6.5-6s6.5 3.5 6.5 6V11',
      }),
      h('path', {
        d: 'M12 7.5c1.3807 0 2.5 1.11929 2.5 2.5v2c0 1.3807-1.1193 2.5-2.5 2.5h-1.5v-7zm-8 0h1.5v7H4c-1.38071 0-2.5-1.1193-2.5-2.5v-2c0-1.38071 1.11929-2.5 2.5-2.5Z',
      }),
    ],
  );

const StopwatchIcon: FunctionalComponent = (_, { attrs }) =>
  h(
    'svg',
    {
      width: '16',
      height: '16',
      viewBox: '0 0 16 16',
      fill: 'none',
      stroke: 'currentColor',
      ...attrs,
      style: { display: 'block' },
    },
    [
      h('circle', { cx: '8', cy: '8.5', r: '6' }),
      h('path', {
        'stroke-linecap': 'square',
        'stroke-linejoin': 'round',
        d: 'M8 9.5v-5m0-2v-2m-2 0h4M12 4l1.5-1.5',
      }),
    ],
  );

const TrashIcon: FunctionalComponent = (_, { attrs }) =>
  h(
    'svg',
    {
      width: '16',
      height: '16',
      viewBox: '0 0 16 16',
      fill: 'none',
      stroke: 'currentColor',
      'stroke-linejoin': 'round',
      ...attrs,
      style: { display: 'block' },
    },
    [
      h('path', { 'stroke-linecap': 'square', d: 'M2.5 4h11' }),
      h('path', {
        'stroke-linecap': 'round',
        d: 'M6.5 4V3c0-.82843.67157-1.5 1.5-1.5s1.5.67157 1.5 1.5v1',
      }),
      h('path', {
        'stroke-linecap': 'square',
        d: 'm3.5 4 .87069 9.1422c.07332.7699.7199 1.3578 1.49324 1.3578h4.27217c.7733 0 1.4199-.5879 1.4932-1.3578L12.5 4',
      }),
    ],
  );

const popupHiddenClass = styles.PopupHidden;
</script>

<template>
  <Tooltip.Provider>
    <div :class="styles.Container">
      <div :class="styles.ButtonGroup">
        <Tooltip.Trigger
          :class="styles.IconButton"
          :handle="demoTooltip"
          id="trigger-1"
          aria-label="Trigger 1"
        >
          <HeadphonesIcon aria-hidden="true" />
        </Tooltip.Trigger>

        <Tooltip.Trigger
          :class="styles.IconButton"
          :handle="demoTooltip"
          id="trigger-2"
          aria-label="Trigger 2"
        >
          <StopwatchIcon aria-hidden="true" />
        </Tooltip.Trigger>

        <Tooltip.Trigger
          :class="styles.IconButton"
          :handle="demoTooltip"
          id="trigger-3"
          aria-label="Trigger 3"
        >
          <TrashIcon aria-hidden="true" />
        </Tooltip.Trigger>
      </div>

      <button type="button" :class="styles.Button" @click="openProgrammatically">
        Open programmatically
      </button>
    </div>

    <Tooltip.Root
      :handle="demoTooltip"
      :open="open"
      :trigger-id="triggerId"
      @open-change="handleOpenChange"
    >
      <Tooltip.Portal>
        <Tooltip.Positioner :side-offset="11" :class="styles.Positioner">
          <Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
            <Tooltip.Popup :class="styles.Popup">
              <Tooltip.Arrow :class="styles.Arrow" />
              Controlled tooltip
            </Tooltip.Popup>
          </Transition>
        </Tooltip.Positioner>
      </Tooltip.Portal>
    </Tooltip.Root>
  </Tooltip.Provider>
</template>
css
.Container {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  justify-content: center;
}

.ButtonGroup {
  display: flex;
}

.IconButton {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  padding: 0;
  margin: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &[data-popup-open] {
    background-color: oklch(97% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(26.9% 0 0deg);
    }
  }

  @media (hover: hover) {
    &:hover:not(:disabled) {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active:not(:disabled) {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &:not(:first-child) {
    border-left: none;
  }

  &:disabled {
    color: oklch(55.6% 0 0deg);
    border-color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
      border-color: oklch(70.8% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;
    position: relative;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Positioner {
  height: var(--positioner-height);
  width: var(--positioner-width);
  max-width: var(--available-width);
}

.Popup {
  box-sizing: border-box;
  position: relative;
  font-size: 0.875rem;
  line-height: 1.25rem;
  padding: 0.25rem 0.5rem;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  color: oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
  transform-origin: var(--transform-origin);
  transition:
    scale 100ms ease-out,
    opacity 100ms ease-out;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
    box-shadow: none;
  }

  &[data-instant] {
    transition: none;
  }
}

/* The enter-from and leave-to class of the `<Transition>` around the popup. */
.PopupHidden {
  opacity: 0;
  scale: 0.98;
}

.Arrow {
  display: block;
  position: relative;
  width: 12px;
  height: 6px;
  overflow: clip;

  &[data-side='top'] {
    bottom: -6px;
    rotate: 180deg;
  }

  &[data-side='bottom'] {
    top: -6px;
    rotate: 0deg;
  }

  &[data-side='left'] {
    right: -9px;
    rotate: 90deg;
  }

  &[data-side='right'] {
    left: -9px;
    rotate: -90deg;
  }

  &::before {
    content: '';
    display: block;
    position: absolute;
    bottom: 0;
    left: 50%;
    box-sizing: border-box;
    width: calc(6px * sqrt(2));
    height: calc(6px * sqrt(2));
    background-color: white;
    border: 1px solid oklch(14.5% 0 0deg);
    transform: translate(-50%, 50%) rotate(45deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(14.5% 0 0deg);
      border: 1px solid white;
    }
  }
}

.Button {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.75rem;
  margin: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover:not(:disabled) {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active:not(:disabled) {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &:disabled {
    color: oklch(55.6% 0 0deg);
    border-color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
      border-color: oklch(70.8% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;
    position: relative;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}
vue
<script setup lang="ts">
import { shallowRef } from 'vue';
import { Tooltip } from '@vase-ui/vue/tooltip';
import { HeadphonesIcon, StopwatchIcon, TrashIcon } from '../../icons-tw';

const demoTooltip = Tooltip.createHandle();

const open = shallowRef(false);
const triggerId = shallowRef<string | null>(null);

function handleOpenChange(isOpen: boolean, eventDetails: Tooltip.Root.ChangeEventDetails) {
  open.value = isOpen;
  triggerId.value = eventDetails.trigger?.id ?? null;
}

function openProgrammatically() {
  triggerId.value = 'trigger-2';
  open.value = true;
}

const iconButtonClass =
  'flex size-8 items-center justify-center border border-neutral-950 bg-white text-neutral-950 select-none data-popup-open:bg-neutral-100 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white focus-visible:relative hover:bg-neutral-100 active:bg-neutral-200 dark:border-white dark:bg-neutral-950 dark:text-white dark:data-popup-open:bg-neutral-800 dark:hover:bg-neutral-800 dark:active:bg-neutral-700';
const buttonClass =
  'flex h-8 items-center justify-center gap-2 border border-neutral-950 bg-white px-3 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 select-none focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white hover:bg-neutral-100 active:bg-neutral-200 dark:border-white dark:bg-neutral-950 dark:text-white dark:hover:bg-neutral-800 dark:active:bg-neutral-700';
const popupClass =
  'relative flex flex-col border border-neutral-950 bg-white px-2 py-1 text-sm text-neutral-950 origin-[var(--transform-origin)] shadow-[0.25rem_0.25rem_0] shadow-black/12 transition-[transform,opacity] duration-100 ease-out data-instant:transition-none dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none';
const popupHiddenClass = 'opacity-0 [transform:scale(0.98)]';
const arrowClass =
  "relative block w-3 h-1.5 overflow-clip data-[side=bottom]:top-[-6px] data-[side=left]:right-[-9px] data-[side=left]:rotate-90 data-[side=right]:left-[-9px] data-[side=right]:-rotate-90 data-[side=top]:bottom-[-6px] data-[side=top]:rotate-180 before:content-[''] before:absolute before:bottom-0 before:left-1/2 before:w-[calc(6px*sqrt(2))] before:h-[calc(6px*sqrt(2))] before:bg-white dark:before:bg-neutral-950 before:border before:border-neutral-950 dark:before:border-white before:[transform:translate(-50%,50%)_rotate(45deg)]";
</script>

<template>
  <Tooltip.Provider>
    <div class="flex gap-2 flex-wrap justify-center">
      <div class="flex">
        <Tooltip.Trigger
          :class="iconButtonClass"
          :handle="demoTooltip"
          id="trigger-1"
          aria-label="Trigger 1"
        >
          <HeadphonesIcon aria-hidden="true" />
        </Tooltip.Trigger>

        <Tooltip.Trigger
          :class="[iconButtonClass, 'border-l-0']"
          :handle="demoTooltip"
          id="trigger-2"
          aria-label="Trigger 2"
        >
          <StopwatchIcon aria-hidden="true" />
        </Tooltip.Trigger>

        <Tooltip.Trigger
          :class="[iconButtonClass, 'border-l-0']"
          :handle="demoTooltip"
          id="trigger-3"
          aria-label="Trigger 3"
        >
          <TrashIcon aria-hidden="true" />
        </Tooltip.Trigger>
      </div>

      <button type="button" :class="buttonClass" @click="openProgrammatically">
        Open programmatically
      </button>
    </div>

    <Tooltip.Root
      :handle="demoTooltip"
      :open="open"
      :trigger-id="triggerId"
      @open-change="handleOpenChange"
    >
      <Tooltip.Portal>
        <Tooltip.Positioner
          :side-offset="11"
          class="h-[var(--positioner-height)] w-[var(--positioner-width)] max-w-[var(--available-width)]"
        >
          <Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
            <Tooltip.Popup :class="popupClass">
              <Tooltip.Arrow :class="arrowClass" />
              Controlled tooltip
            </Tooltip.Popup>
          </Transition>
        </Tooltip.Positioner>
      </Tooltip.Portal>
    </Tooltip.Root>
  </Tooltip.Provider>
</template>
ts
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';

export const HeadphonesIcon: FunctionalComponent = (_, { attrs }) =>
  h(
    'svg',
    {
      width: '16',
      height: '16',
      viewBox: '0 0 16 16',
      fill: 'none',
      stroke: 'currentColor',
      ...attrs,
      style: { display: 'block' },
    },
    [
      h('path', {
        'stroke-linecap': 'round',
        d: 'M1.5 11V7.5c0-2.5 2.5-6 6.5-6s6.5 3.5 6.5 6V11',
      }),
      h('path', {
        d: 'M12 7.5c1.3807 0 2.5 1.11929 2.5 2.5v2c0 1.3807-1.1193 2.5-2.5 2.5h-1.5v-7zm-8 0h1.5v7H4c-1.38071 0-2.5-1.1193-2.5-2.5v-2c0-1.38071 1.11929-2.5 2.5-2.5Z',
      }),
    ],
  );

export const StopwatchIcon: FunctionalComponent = (_, { attrs }) =>
  h(
    'svg',
    {
      width: '16',
      height: '16',
      viewBox: '0 0 16 16',
      fill: 'none',
      stroke: 'currentColor',
      ...attrs,
      style: { display: 'block' },
    },
    [
      h('circle', { cx: '8', cy: '8.5', r: '6' }),
      h('path', {
        'stroke-linecap': 'square',
        'stroke-linejoin': 'round',
        d: 'M8 9.5v-5m0-2v-2m-2 0h4M12 4l1.5-1.5',
      }),
    ],
  );

export const TrashIcon: FunctionalComponent = (_, { attrs }) =>
  h(
    'svg',
    {
      width: '16',
      height: '16',
      viewBox: '0 0 16 16',
      fill: 'none',
      stroke: 'currentColor',
      'stroke-linejoin': 'round',
      ...attrs,
      style: { display: 'block' },
    },
    [
      h('path', { 'stroke-linecap': 'square', d: 'M2.5 4h11' }),
      h('path', {
        'stroke-linecap': 'round',
        d: 'M6.5 4V3c0-.82843.67157-1.5 1.5-1.5s1.5.67157 1.5 1.5v1',
      }),
      h('path', {
        'stroke-linecap': 'square',
        d: 'm3.5 4 .87069 9.1422c.07332.7699.7199 1.3578 1.49324 1.3578h4.27217c.7733 0 1.4199-.5879 1.4932-1.3578L12.5 4',
      }),
    ],
  );

Animating the Tooltip ​

A tooltip can animate its position, its size and its content when it moves from one trigger to another.

Position and size ​

To animate the position, give Tooltip.Positioner a CSS transition of left, right, top and bottom. To animate the size, give Tooltip.Popup a transition of width and height.

Content ​

To animate the content, put it in Tooltip.Viewport. The default slot of the viewport gets triggerId, the id of the active trigger. Put the content in a <Transition>, with triggerId as its key. At a move, the old content leaves while the new content enters. Select the old content with the leave classes, and the viewport while it has old content with :has(> .x-leave-active).

The data-activation-direction attribute of the viewport gives the place of the new trigger from the old trigger. Its value has one or two tokens, one for each axis: left or right, and up or down, such as right down. Match one token with the ~= selector, such as [data-activation-direction~='right'].

Use a viewport only when one popup has more than one trigger, and the content changes with an animation at a move to another trigger. With a viewport, give Tooltip.Positioner the styles width: var(--positioner-width) and height: var(--positioner-height). The positioner then keeps the measured size during the transition. Without these styles, the size of the content can change the size of the popup in each frame, or move the popup to another side.

vue
<script setup lang="ts">
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';
import { Tooltip } from '@vase-ui/vue/tooltip';
import styles from './index.module.css';

const demoTooltip = Tooltip.createHandle<string>();

const HeadphonesIcon: FunctionalComponent = (_, { attrs }) =>
  h(
    'svg',
    {
      width: '16',
      height: '16',
      viewBox: '0 0 16 16',
      fill: 'none',
      stroke: 'currentColor',
      ...attrs,
      style: { display: 'block' },
    },
    [
      h('path', {
        'stroke-linecap': 'round',
        d: 'M1.5 11V7.5c0-2.5 2.5-6 6.5-6s6.5 3.5 6.5 6V11',
      }),
      h('path', {
        d: 'M12 7.5c1.3807 0 2.5 1.11929 2.5 2.5v2c0 1.3807-1.1193 2.5-2.5 2.5h-1.5v-7zm-8 0h1.5v7H4c-1.38071 0-2.5-1.1193-2.5-2.5v-2c0-1.38071 1.11929-2.5 2.5-2.5Z',
      }),
    ],
  );

const StopwatchIcon: FunctionalComponent = (_, { attrs }) =>
  h(
    'svg',
    {
      width: '16',
      height: '16',
      viewBox: '0 0 16 16',
      fill: 'none',
      stroke: 'currentColor',
      ...attrs,
      style: { display: 'block' },
    },
    [
      h('circle', { cx: '8', cy: '8.5', r: '6' }),
      h('path', {
        'stroke-linecap': 'square',
        'stroke-linejoin': 'round',
        d: 'M8 9.5v-5m0-2v-2m-2 0h4M12 4l1.5-1.5',
      }),
    ],
  );

const TrashIcon: FunctionalComponent = (_, { attrs }) =>
  h(
    'svg',
    {
      width: '16',
      height: '16',
      viewBox: '0 0 16 16',
      fill: 'none',
      stroke: 'currentColor',
      'stroke-linejoin': 'round',
      ...attrs,
      style: { display: 'block' },
    },
    [
      h('path', { 'stroke-linecap': 'square', d: 'M2.5 4h11' }),
      h('path', {
        'stroke-linecap': 'round',
        d: 'M6.5 4V3c0-.82843.67157-1.5 1.5-1.5s1.5.67157 1.5 1.5v1',
      }),
      h('path', {
        'stroke-linecap': 'square',
        d: 'm3.5 4 .87069 9.1422c.07332.7699.7199 1.3578 1.49324 1.3578h4.27217c.7733 0 1.4199-.5879 1.4932-1.3578L12.5 4',
      }),
    ],
  );

const popupHiddenClass = styles.PopupHidden;
const contentEnterFromClass = styles.ContentEnterFrom;
const contentLeavingClass = styles.ContentLeaving;
const contentLeaveToClass = styles.ContentLeaveTo;
</script>

<template>
  <Tooltip.Provider>
    <div :class="styles.ButtonGroup">
      <Tooltip.Trigger
        :class="styles.Button"
        :handle="demoTooltip"
        payload="Listen to audio preview"
        aria-label="Listen to audio preview"
      >
        <HeadphonesIcon aria-hidden="true" />
      </Tooltip.Trigger>

      <Tooltip.Trigger
        :class="styles.Button"
        :handle="demoTooltip"
        payload="Set a timer"
        aria-label="Set a timer"
      >
        <StopwatchIcon aria-hidden="true" />
      </Tooltip.Trigger>

      <Tooltip.Trigger
        :class="styles.Button"
        :handle="demoTooltip"
        payload="Delete: This action cannot be undone"
        aria-label="Delete: This action cannot be undone"
      >
        <TrashIcon aria-hidden="true" />
      </Tooltip.Trigger>
    </div>

    <Tooltip.Root v-slot="{ payload }" :handle="demoTooltip">
      <Tooltip.Portal>
        <Tooltip.Positioner :side-offset="11" :class="styles.Positioner">
          <Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
            <Tooltip.Popup :class="styles.Popup">
              <Tooltip.Arrow :class="styles.Arrow" />

              <Tooltip.Viewport v-slot="{ triggerId }" :class="styles.Viewport">
                <Transition
                  :enter-from-class="contentEnterFromClass"
                  :leave-active-class="contentLeavingClass"
                  :leave-to-class="contentLeaveToClass"
                >
                  <div :key="triggerId ?? ''" :class="styles.Content">{{ payload }}</div>
                </Transition>
              </Tooltip.Viewport>
            </Tooltip.Popup>
          </Transition>
        </Tooltip.Positioner>
      </Tooltip.Portal>
    </Tooltip.Root>
  </Tooltip.Provider>
</template>
css
.Container {
  display: flex;
}

.ButtonGroup {
  display: flex;
}

.Button {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  padding: 0;
  margin: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &[data-popup-open] {
    background-color: oklch(97% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(26.9% 0 0deg);
    }
  }

  @media (hover: hover) {
    &:hover:not(:disabled) {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active:not(:disabled) {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &:not(:first-child) {
    border-left: none;
  }

  &:disabled {
    color: oklch(55.6% 0 0deg);
    border-color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
      border-color: oklch(70.8% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;
    position: relative;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Positioner {
  height: var(--positioner-height);
  width: var(--positioner-width);
  max-width: var(--available-width);
  transition:
    top 0.35s cubic-bezier(0.22, 1, 0.36, 1),
    left 0.35s cubic-bezier(0.22, 1, 0.36, 1),
    right 0.35s cubic-bezier(0.22, 1, 0.36, 1),
    bottom 0.35s cubic-bezier(0.22, 1, 0.36, 1),
    transform 0.35s cubic-bezier(0.22, 1, 0.36, 1);

  &[data-instant] {
    transition: none;
  }
}

.Popup {
  box-sizing: border-box;
  position: relative;
  height: var(--popup-height, auto);
  width: var(--popup-width, auto);
  max-width: 500px;
  transform-origin: var(--transform-origin);
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  color: oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);

  font-size: 0.875rem;
  line-height: 1.25rem;
  transition:
    width 0.35s cubic-bezier(0.22, 1, 0.36, 1),
    height 0.35s cubic-bezier(0.22, 1, 0.36, 1),
    opacity 0.35s cubic-bezier(0.22, 1, 0.36, 1),
    scale 0.35s cubic-bezier(0.22, 1, 0.36, 1);

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
    box-shadow: none;
  }

  &[data-instant] {
    transition: none;
  }
}

/* The enter-from and leave-to class of the `<Transition>` around the popup. */
.PopupHidden {
  opacity: 0;
  scale: 0.9;
}

.Arrow {
  display: block;
  position: relative;
  width: 12px;
  height: 6px;
  overflow: clip;
  transition: left 0.35s cubic-bezier(0.22, 1, 0.36, 1);

  &[data-instant] {
    transition: none;
  }

  &[data-side='top'] {
    bottom: -6px;
    rotate: 180deg;
  }

  &[data-side='bottom'] {
    top: -6px;
    rotate: 0deg;
  }

  &[data-side='left'] {
    right: -9px;
    rotate: 90deg;
  }

  &[data-side='right'] {
    left: -9px;
    rotate: -90deg;
  }

  &::before {
    content: '';
    display: block;
    position: absolute;
    bottom: 0;
    left: 50%;
    box-sizing: border-box;
    width: calc(6px * sqrt(2));
    height: calc(6px * sqrt(2));
    background-color: white;
    border: 1px solid oklch(14.5% 0 0deg);
    transform: translate(-50%, 50%) rotate(45deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(14.5% 0 0deg);
      border: 1px solid white;
    }
  }
}

.Viewport {
  --viewport-inline-padding: 0.5rem;
  box-sizing: border-box;
  position: relative;
  height: 100%;
  width: 100%;
  overflow: clip;
  padding: 0.25rem var(--viewport-inline-padding);

  /* The content of a trigger. A `<Transition>` with the trigger id as the key keeps the old content
     for its leave while the new content enters. */
  .Content {
    width: calc(var(--popup-width) - 2 * var(--viewport-inline-padding));
    translate: 0;
    opacity: 1;
    transition:
      translate 350ms cubic-bezier(0.22, 1, 0.36, 1),
      opacity 175ms cubic-bezier(0.22, 1, 0.36, 1);

    [data-instant] & {
      transition: none;
    }
  }

  .ContentLeaving {
    position: absolute;
    top: 0.25rem;
    left: var(--viewport-inline-padding);
  }

  &[data-activation-direction~='right'] .ContentLeaveTo,
  &[data-activation-direction~='left'] .ContentEnterFrom {
    translate: -50% 0;
    opacity: 0;
  }

  &[data-activation-direction~='right'] .ContentEnterFrom,
  &[data-activation-direction~='left'] .ContentLeaveTo {
    translate: 50% 0;
    opacity: 0;
  }
}
vue
<script setup lang="ts">
import { Tooltip } from '@vase-ui/vue/tooltip';
import { HeadphonesIcon, StopwatchIcon, TrashIcon } from '../../icons-tw';

const demoTooltip = Tooltip.createHandle<string>();

const triggerClass =
  'flex size-8 items-center justify-center border border-neutral-950 bg-white text-sm leading-none whitespace-nowrap font-normal text-neutral-950 select-none data-popup-open:bg-neutral-100 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white focus-visible:relative hover:bg-neutral-100 active:bg-neutral-200 dark:border-white dark:bg-neutral-950 dark:text-white dark:data-popup-open:bg-neutral-800 dark:hover:bg-neutral-800 dark:active:bg-neutral-700';
const arrowClass =
  "relative block w-3 h-1.5 overflow-clip transition-[left] duration-[0.35s] ease-[cubic-bezier(0.22,1,0.36,1)] data-instant:transition-none data-[side=bottom]:top-[-6px] data-[side=left]:right-[-9px] data-[side=left]:rotate-90 data-[side=right]:left-[-9px] data-[side=right]:-rotate-90 data-[side=top]:bottom-[-6px] data-[side=top]:rotate-180 before:content-[''] before:absolute before:bottom-0 before:left-1/2 before:w-[calc(6px*sqrt(2))] before:h-[calc(6px*sqrt(2))] before:bg-white dark:before:bg-neutral-950 before:border before:border-neutral-950 dark:before:border-white before:[transform:translate(-50%,50%)_rotate(45deg)]";

// The viewport has the side of the new trigger in `data-activation-direction`.
const contentEnterFromClass =
  'opacity-0 group-data-[activation-direction~=left]:-translate-x-1/2 group-data-[activation-direction~=right]:translate-x-1/2';
const contentLeavingClass = 'absolute top-1 left-2';
const contentLeaveToClass =
  'opacity-0 group-data-[activation-direction~=left]:translate-x-1/2 group-data-[activation-direction~=right]:-translate-x-1/2';
const popupHiddenClass = 'opacity-0 [transform:scale(0.9)]';
</script>

<template>
  <Tooltip.Provider>
    <div class="flex">
      <Tooltip.Trigger
        :class="triggerClass"
        :handle="demoTooltip"
        payload="Listen to audio preview"
        aria-label="Listen to audio preview"
      >
        <HeadphonesIcon aria-hidden="true" />
      </Tooltip.Trigger>

      <Tooltip.Trigger
        :class="[triggerClass, 'border-l-0']"
        :handle="demoTooltip"
        payload="Set a timer"
        aria-label="Set a timer"
      >
        <StopwatchIcon aria-hidden="true" />
      </Tooltip.Trigger>

      <Tooltip.Trigger
        :class="[triggerClass, 'border-l-0']"
        :handle="demoTooltip"
        payload="Delete: This action cannot be undone"
        aria-label="Delete: This action cannot be undone"
      >
        <TrashIcon aria-hidden="true" />
      </Tooltip.Trigger>
    </div>

    <Tooltip.Root v-slot="{ payload }" :handle="demoTooltip">
      <Tooltip.Portal>
        <Tooltip.Positioner
          :side-offset="11"
          class="h-[var(--positioner-height)] w-[var(--positioner-width)] max-w-[var(--available-width)] transition-[top,left,right,bottom,transform] duration-[0.35s] ease-[cubic-bezier(0.22,1,0.36,1)] data-instant:transition-none"
        >
          <Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
            <Tooltip.Popup
              class="relative h-[var(--popup-height,auto)] w-[var(--popup-width,auto)] max-w-[500px] border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 text-sm text-neutral-950 dark:text-white origin-[var(--transform-origin)] shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:shadow-none transition-[width,height,opacity,transform] duration-[0.35s] ease-[cubic-bezier(0.22,1,0.36,1)] data-instant:transition-none"
            >
              <Tooltip.Arrow :class="arrowClass" />

              <Tooltip.Viewport
                v-slot="{ triggerId }"
                class="group relative h-full w-full overflow-clip px-2 py-1"
              >
                <Transition
                  :enter-from-class="contentEnterFromClass"
                  :leave-active-class="contentLeavingClass"
                  :leave-to-class="contentLeaveToClass"
                >
                  <div
                    :key="triggerId ?? ''"
                    class="w-[calc(var(--popup-width)-1rem)] transition-[translate,opacity] duration-[350ms,175ms] ease-[cubic-bezier(0.22,1,0.36,1)] [[data-instant]_&]:transition-none"
                  >
                    {{ payload }}
                  </div>
                </Transition>
              </Tooltip.Viewport>
            </Tooltip.Popup>
          </Transition>
        </Tooltip.Positioner>
      </Tooltip.Portal>
    </Tooltip.Root>
  </Tooltip.Provider>
</template>
ts
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';

export const HeadphonesIcon: FunctionalComponent = (_, { attrs }) =>
  h(
    'svg',
    {
      width: '16',
      height: '16',
      viewBox: '0 0 16 16',
      fill: 'none',
      stroke: 'currentColor',
      ...attrs,
      style: { display: 'block' },
    },
    [
      h('path', {
        'stroke-linecap': 'round',
        d: 'M1.5 11V7.5c0-2.5 2.5-6 6.5-6s6.5 3.5 6.5 6V11',
      }),
      h('path', {
        d: 'M12 7.5c1.3807 0 2.5 1.11929 2.5 2.5v2c0 1.3807-1.1193 2.5-2.5 2.5h-1.5v-7zm-8 0h1.5v7H4c-1.38071 0-2.5-1.1193-2.5-2.5v-2c0-1.38071 1.11929-2.5 2.5-2.5Z',
      }),
    ],
  );

export const StopwatchIcon: FunctionalComponent = (_, { attrs }) =>
  h(
    'svg',
    {
      width: '16',
      height: '16',
      viewBox: '0 0 16 16',
      fill: 'none',
      stroke: 'currentColor',
      ...attrs,
      style: { display: 'block' },
    },
    [
      h('circle', { cx: '8', cy: '8.5', r: '6' }),
      h('path', {
        'stroke-linecap': 'square',
        'stroke-linejoin': 'round',
        d: 'M8 9.5v-5m0-2v-2m-2 0h4M12 4l1.5-1.5',
      }),
    ],
  );

export const TrashIcon: FunctionalComponent = (_, { attrs }) =>
  h(
    'svg',
    {
      width: '16',
      height: '16',
      viewBox: '0 0 16 16',
      fill: 'none',
      stroke: 'currentColor',
      'stroke-linejoin': 'round',
      ...attrs,
      style: { display: 'block' },
    },
    [
      h('path', { 'stroke-linecap': 'square', d: 'M2.5 4h11' }),
      h('path', {
        'stroke-linecap': 'round',
        d: 'M6.5 4V3c0-.82843.67157-1.5 1.5-1.5s1.5.67157 1.5 1.5v1',
      }),
      h('path', {
        'stroke-linecap': 'square',
        d: 'm3.5 4 .87069 9.1422c.07332.7699.7199 1.3578 1.49324 1.3578h4.27217c.7733 0 1.4199-.5879 1.4932-1.3578L12.5 4',
      }),
    ],
  );

API reference ​

Tooltip.Provider ​

Makes the tooltips in it one group. One tooltip of a group is open at a time. While one is open, and for timeout after its close, a hover opens another tooltip of the group at once. The tooltips with no provider are one more group, in which each tooltip waits for its delay. Renders no element.

Props

PropTypeDefaultDescription
closeDelaynumber | undefinedThe time in milliseconds before the close, after the pointer left.
delaynumber | undefinedThe time in milliseconds that the mouse rests on a trigger before the open.
timeoutnumber | undefined400The time in milliseconds after a close in which a hover opens another tooltip of the provider at once.

Slots

SlotSlot propsDescription
defaultThe content of the part.

Tooltip.Root ​

Groups the parts of the tooltip. Renders no element.

Props

PropTypeDefaultDescription
defaultOpenboolean | undefinedfalseThe tooltip is open at the mount of an uncontrolled root.
defaultTriggerIdstring | null | undefinednullThe id of the active trigger at the mount, with defaultOpen.
disabledboolean | undefinedfalseThe tooltip does not open, and an open tooltip closes.
disableHoverablePopupboolean | undefinedfalseThe pointer cannot rest on the popup: the tooltip closes when the pointer leaves the trigger.
handleTooltipHandle<Payload> | undefinedConnects the root to triggers outside it and to code.
openboolean | undefinedThe tooltip is open. v-model:open binds it.
trackCursorAxis"none" | "x" | "y" | "both" | undefined'none'The axis on which the popup follows the mouse pointer on the trigger.
triggerIdstring | null | undefinedThe id of the active trigger. v-model:trigger-id binds it.

Slots

SlotSlot propsDescription
default
payload: Payload | undefined
The content of the tooltip. payload is the payload of the active trigger.

Events

EventArgumentsDescription
openChange[open: boolean, details: TooltipRootChangeEventDetails]The tooltip opens or closes. details.cancel() keeps it as it was.
update:open[open: boolean]Comes after openChange, unless a listener cancels the change. v-model:open uses it.
update:triggerId[id: string | null]The active trigger changes. v-model:trigger-id uses it.

Exposed

MemberTypeDescription
close() => voidCloses the tooltip with the reason 'imperative-action'.

Tooltip.Trigger ​

The element that the tooltip describes. A mouse that rests on it opens the tooltip after the delay, and the focus of a key opens it at once. A touch opens no tooltip. The tooltip of a disabled button also opens at a hover, so it can tell why the button is disabled. A trigger in another trigger opens only its own tooltip. Renders a <button> element.

Props

PropTypeDefaultDescription
closeDelaynumber | undefinedThe time in milliseconds before the close, after the pointer left the trigger and the popup. With no value, the closeDelay of the Tooltip.Provider, else 0.
closeOnClickboolean | undefinedtrueA press on the trigger closes the tooltip.
delaynumber | undefinedThe time in milliseconds that the mouse rests on the trigger before the open. With no value, the delay of the Tooltip.Provider, else 600.
disabledboolean | undefinedfalseThe tooltip does not open from this trigger. The element of the trigger stays in use.
handleTooltipHandle<Payload> | undefinedConnects the trigger to a root outside it.
idstring | undefinedThe id of the element, with a generated id as the default. The triggerId of the root and open() of the handle name the trigger by it.
payloadPayload | undefinedThe payload that the tooltip gets when this trigger opens it.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: TooltipTriggerState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: TooltipTriggerState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-popup-open]The tooltip is open for this trigger.
[data-trigger-disabled]The tooltip does not open from this trigger, because the trigger or the root is disabled.

Tooltip.Portal ​

Moves its content to another place of the DOM, <body> by default. Renders no element.

Props

PropTypeDefaultDescription
deferboolean | undefinedfalseMoves the content after the other content of the same update mounted.
disabledboolean | undefinedfalseRenders the content in place, also on the server.
keepMountedboolean | undefinedfalseThe closed popup stays in the DOM, with hidden and inert.
toPortalTarget | undefinedThe target of the content. With no value, the target of the nearest portal above, else body. While it is null, the portal renders nothing.

Slots

SlotSlot propsDescription
defaultThe content of the part.

Tooltip.Positioner ​

Places the popup at its anchor. Renders a <div> element.

Props

PropTypeDefaultDescription
alignAlign | undefined'center'The alignment of the popup on the side.
alignOffsetnumber | OffsetFunction | undefined0The offset on the align axis in pixels, or a function that gives it.
anchorAnchor | (() => Anchor)The element to place the popup against: an element, a virtual element, a template ref of a part, or a function that gives one. With no value, the active trigger.
arrowPaddingnumber | undefined5The smallest space between the arrow and an inner edge of its nearest positioned ancestor, in pixels.
collisionAvoidanceCollisionAvoidance | undefinedHow the popup avoids the edge of the boundary.
collisionBoundaryBoundary | undefined'clipping-ancestors'The area that holds the popup: an element, elements, or a rectangle.
collisionPaddingPadding | undefined5The space between the popup and the edge of the boundary, in pixels.
disableAnchorTrackingboolean | undefinedfalseThe popup does not follow a move of its anchor.
positionMethod"absolute" | "fixed" | undefined'absolute'The CSS position of the positioner.
sideSide | undefined'bottom'The side of the anchor for the popup. It can change to avoid a collision. inline-start and inline-end follow the CSS direction of the positioner: give the positioner dir when the target of its portal has another direction.
sideOffsetnumber | OffsetFunction | undefined0The distance from the anchor in pixels, or a function that gives it.
stickyboolean | undefinedfalseThe popup stays in the view after its anchor scrolls out of it.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: TooltipPositionerState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: TooltipPositionerState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-side]The side of the anchor that the popup is on: top, bottom, left or right, or inline-start or inline-end for a logical side prop.
[data-align]The alignment of the popup on its side: start, center or end.
[data-anchor-hidden]The anchor is out of view.

CSS variables

VariableDescription
--anchor-widthThe width of the anchor, in pixels.
--anchor-heightThe height of the anchor, in pixels.
--available-widthThe width that the popup can have before it goes over the edge of the collision boundary.
--available-heightThe height that the popup can have before it goes over the edge of the collision boundary.
--transform-originThe point that the popup grows from, for the transform-origin of the popup. It is at the arrow, on the edge of the anchor. With no arrow, it is at the aligned edge of the popup or at the center of the anchor.
--positioner-widthThe width of the popup, in pixels, with a Tooltip.Viewport in the popup. During a move to another trigger, the width of the new content.
--positioner-heightThe height of the popup, in pixels, with a Tooltip.Viewport in the popup. During a move to another trigger, the height of the new content.

Tooltip.Popup ​

The container of the content of the tooltip. It has no role and does not describe the trigger. To describe the trigger with it, set keepMounted on Tooltip.Portal, give the popup role="tooltip" and an id, and give the trigger aria-describedby with that id. Renders a <div> element.

Props

PropTypeDefaultDescription
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: TooltipPopupState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: TooltipPopupState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-side]The side of the anchor that the popup is on, as on the positioner.
[data-align]The alignment of the popup on its side, as on the positioner.
[data-instant]An animation must not run. The value says why: delay (the tooltip opened while another tooltip of its group was open or in the timeout after its close, or it closed because another tooltip of the group opened or its active trigger left the DOM), focus (the focus opened it) or dismiss (Escape or a press on the trigger closed it).
[hidden]The tooltip is closed, and keepMounted keeps the popup in the DOM.

CSS variables

VariableDescription
--popup-widthThe width of the popup, in pixels, during a move to another trigger with a Tooltip.Viewport: first the old width, then the new width.
--popup-heightThe height of the popup, in pixels, during a move to another trigger with a Tooltip.Viewport: first the old height, then the new height.

Tooltip.Arrow ​

An element that points to the center of the anchor. Renders a <div> element. Its place is in the padding box of its nearest positioned ancestor: the popup when the popup has a position, else the positioner.

Props

PropTypeDefaultDescription
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: TooltipArrowState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: TooltipArrowState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-side]The side of the anchor that the popup is on, as on the positioner.
[data-align]The alignment of the popup on its side, as on the positioner.
[data-uncentered]The arrow cannot point at the center of the anchor.
[data-instant]An animation must not run, with the values of the popup.

Tooltip.Viewport ​

The container of content that changes when the tooltip moves to another trigger. Renders a <div> element.

Props

PropTypeDefaultDescription
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: TooltipViewportState
triggerId: string | null
Give the content the triggerId as its key in a <Transition>, so that it leaves and enters at a move.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: TooltipViewportState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-activation-direction]The place of the new trigger from the old trigger, after a move: at most two tokens, left or right and up or down, such as right down.
[data-instant]An animation must not run, with the values of the popup.

createHandle ​

Makes a Tooltip.Handle with the type argument as the type of its payload.

Return value

TooltipHandle<Payload>

Handle ​

A handle opens and closes the tooltip from code, and connects the triggers outside the root to it.

Properties

PropertyTypeModifiersDescription
isOpenbooleanreadonlyThe popup is open. It is false while no root has the handle.

Methods

MethodReturnsDescription
open(triggerId: string)voidOpens the popup for the trigger with this id. With no mounted root, it does nothing, and a development build warns. Throws an error when the root has no trigger with this id.
close()voidCloses the popup. With no mounted root, it does nothing.

Differences from Base UI ​

Vase UI ports Tooltip from Base UI 1.9.0.

API ​

Base UIVase UI
Tooltip.Root prop onOpenChangeComplete@after-enter and @after-leave of a <Transition> around the popup run after its animations.
Tooltip.Root prop actionsRefA template ref of the root has close(). With :css="false", the leave hook of a <Transition> keeps the popup until it calls done, so the root has no unmount action.
Tooltip.Trigger prop delayWith no delay, the trigger takes the delay of Tooltip.Provider, or 600 with no provider. So the prop has no Vue default.
Tooltip.Trigger prop closeDelayWith no closeDelay, the trigger takes the closeDelay of Tooltip.Provider, or 0 with no provider. So the prop has no Vue default.
Tooltip.Portal prop renderThe portal is a <Teleport> with no element of its own, so it has no as and no element slot. Put an element in the portal when you need one.
Tooltip.Popup<Transition> around the popup animates it, so the state has no transitionStatus, and the popup has no data-starting-style and no data-ending-style. CSS selects a closed popup that stays in the DOM with [hidden], so the popup has no data-open and no data-closed.

Added in Vase UI ​

PartMemberWhy
Tooltip.Triggerdisabled state fieldThe state says that the tooltip does not open from this trigger, because the trigger or the root is disabled. data-trigger-disabled shows it. Base UI has no such state.
Tooltip.Portaldisabled propThe portal is a <Teleport>, and disabled renders its content in place, also on the server.
Tooltip.Portaldefer propThe portal is a <Teleport>, and it takes the props of <Teleport>: defer waits for the other content of the update.
Tooltip.Rootupdate:triggerId eventv-model:trigger-id binds the active trigger: Vue binds a second state of a component with a named v-model.

Return types ​

FunctionVase UI returnsWhy
Tooltip.createHandleTooltipHandle<Payload>Tooltip.Handle is the class TooltipHandle. The declaration of the function names the class, and Base UI documents the same class by its name in the namespace.

Behavior ​

Vase UIEvidence
isOpen of a popup handle is reactive. A computed, a watch or a template that reads it follows the open state of the popup, and the root that controls the handle. In Base UI, a component that reads isOpen in its render does not render again when the popup opens or closes.
A popup that unmounts leaves the timer of its delay group as it is. In Base UI, a closed popup that another popup replaced in the group clears the timer of that other popup when it unmounts. The group then stays active until a popup opens and closes again. Until then, each popup of the group opens with no delay and with no animation.
A touch outside an open popup closes it only at a tap. A tap ends with pointerup in less than 1 s and moves 8 px or less. Chromium on Android sends click for a move of 8 px and not for 8.1 px, the smallest limit of the measured browsers. A touch that scrolls the page ends with pointercancel and closes nothing, as a native popover stays open in Chromium and WebKit. A drag of more than 8 px on an element that does not scroll also closes nothing. A tap on a backdrop, or on the element that a modal popup puts over the page, closes the popup and gives the page under it no click. In Base UI, a touch that scrolls the page closes a Popover, a Menu or a Select.
An arrow points at the center of its anchor. Its place is in the padding box of its nearest positioned ancestor, so the borders of a popup with a position do not move it. arrowPadding keeps the same space from each inner edge of that box. In the Popover demo of Base UI, the popup has position: relative and a 1 px border, and the arrow is 1.5 px off the center of its trigger.
--transform-origin is in the pixels of the popup, from its border box: the arrow or the aligned edge or the center of the anchor, on the edge of the anchor. A margin of the popup, a padding of the positioner, a margin that collapses through the popup and a zoom of the popup do not move it. A Select popup over the trigger grows from the middle of the value, also after it grows while the user scrolls the list. In the Popover demo of Base UI, a 16 px margin on the popup moves the origin 16 px off the edge of the trigger.
An arrow in a popup with a CSS zoom points at the center of its anchor. Its place and arrowPadding are in the pixels of its nearest positioned ancestor. A Select popup over the trigger with a zoom puts the text of the selected item over the text of the value. In the Popover demo of Base UI, a zoom: 2 on the popup moves the arrow 225 px off the center of the trigger.
close() of a handle with no mounted root does nothing and gives no warning: the popup is closed, which is what the call asks for. When two mounted roots have one handle, the root that mounted last has the handle, and the other root gets it back when that root unmounts; the library gives no warning. Base UI warns in a development build in both cases.

The handbook gives the differences that all components share: Styling, Animation, Composition, Customization, Forms and TypeScript.