본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

툴팁 (Tooltip)

원문 보기 위키 갱신

요소가 키보드 포커스를 받거나 마우스가 그 요소 위에 놓일 때, 해당 요소와 관련된 정보를 표시하는 팝업이에요. Kobalte의 Tooltip 컴포넌트 소개와 사용법, API 참조를 다룹니다.

출처: 문서

본문

요소가 키보드 포커스를 받거나 마우스가 그 위에 놓일 때(hover) 해당 요소와 관련된 정보를 표시하는 팝업이에요.

임포트 (Import)

import { Tooltip } from "@kobalte/core/tooltip";
// or
import { Root, Trigger, ... } from "@kobalte/core/tooltip";
// or (deprecated)
import { Tooltip } from "@kobalte/core";

기능 (Features)

  • ARIA를 통해 보조 기술(assistive technology)에 툴팁으로 노출됩니다.
  • 트리거가 포커스되거나 호버되면 열려요.
  • 트리거가 활성화되거나 Escape 키를 누르면 닫혀요.
  • 한 번에 하나의 툴팁만 표시돼요.
  • aria-describedby를 통한 스크린 리더 라벨링을 지원해요.
  • 커스텀 표시/숨김 지연(delay)을 지원해요.
  • 첫 번째 툴팁은 호버 시 지연이 있고, 이후 툴팁은 지연이 없는 네이티브 툴팁 동작과 일치해요.

구조 (Anatomy)

툴팁은 다음으로 구성됩니다:

  • Tooltip: 툴팁의 루트 컨테이너예요.
  • Tooltip.Trigger: 툴팁을 토글하는 버튼이에요.
  • Tooltip.Portal: 툴팁이 열려 있을 때 자식을 body로 포털(portal)해요.
  • Tooltip.Content: 툴팁이 열려 있을 때 렌더링할 콘텐츠를 담아요.
  • Tooltip.Arrow: 툴팁과 함께 렌더링할 선택적 화살표 요소예요.
<Tooltip>
  <Tooltip.Trigger />
  <Tooltip.Portal>
    <Tooltip.Content>
      <Tooltip.Arrow />
    </Tooltip.Content>
  </Tooltip.Portal>
</Tooltip>

예시 (Example)

import { Tooltip } from "@kobalte/core/tooltip";

export function BasicExample() {
  return (
    <Tooltip>
      <Tooltip.Trigger class={style.tooltip__trigger}>Trigger</Tooltip.Trigger>
      <Tooltip.Portal>
        <Tooltip.Content class={style.tooltip__content}>
          <Tooltip.Arrow />
          <p>Tooltip content</p>
        </Tooltip.Content>
      </Tooltip.Portal>
    </Tooltip>
  );
}
.tooltip__trigger {
  appearance: none;
  border: none;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  height: 40px;
  width: auto;
  outline: none;
  border-radius: 6px;
  padding: 0 16px;
  background-color: hsl(200 98% 39%);
  color: white;
  font-size: 16px;
  line-height: 0;
  transition: 250ms background-color;
}

.tooltip__trigger:hover {
  background-color: hsl(201 96% 32%);
}

.tooltip__trigger:focus-visible {
  outline: 2px solid hsl(200 98% 39%);
  outline-offset: 2px;
}

.tooltip__trigger:active {
  background-color: hsl(201 90% 27%);
}

.tooltip__content {
  z-index: 50;
  max-width: min(calc(100vw - 16px), 380px);
  border: 1px solid hsl(240 5% 84%);
  border-radius: 6px;
  padding: 8px;
  background-color: hsl(240 4% 16%);
  color: white;
  font-size: 14px;
  box-shadow:
    0 10px 15px -3px rgb(0 0 0 / 0.1),
    0 4px 6px -4px rgb(0 0 0 / 0.1);
  transform-origin: var(--kb-tooltip-content-transform-origin);
  animation: contentHide 250ms ease-in forwards;
}

.tooltip__content[data-expanded] {
  animation: contentShow 250ms ease-out;
}

@keyframes contentShow {
  from {
    opacity: 0;
    transform: scale(0.96);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes contentHide {
  from {
    opacity: 1;
    transform: scale(1);
  }
  to {
    opacity: 0;
    transform: scale(0.96);
  }
}

[data-theme*="dark"] .tooltip__trigger {
  background-color: hsl(201 96% 32%);
  color: hsla(0 100% 100% / 0.9);
}

[data-theme*="dark"] .tooltip__trigger:hover {
  background-color: hsl(200 98% 39%);
}

[data-theme*="dark"] .tooltip__trigger:active {
  background-color: hsl(199 89% 48%);
}

[data-theme*="dark"] .tooltip__content {
  border: 1px solid hsl(240 5% 26%);
  background-color: hsl(0 0% 100% / 0.9);
  color: hsl(240 4% 16%);
  box-shadow: none;
}

사용법 (Usage)

기본 열림 (Default open)

defaultOpen prop을 사용해 초기 비제어(uncontrolled) 열림 값을 제공할 수 있어요.

<Tooltip defaultOpen>...</Tooltip>

제어된 열림 (Controlled open)

open prop을 사용해 열림 상태를 제어(controlled)할 수 있어요. onOpenChange 이벤트는 사용자가 트리거나 닫기 버튼을 누르거나 외부와 상호작용할 때 발생하며, 새 값을 받아요.

import { createSignal } from "solid-js";

function ControlledExample() {
  const [open, setOpen] = createSignal(false);

  return (
    <>
      <p>Tooltip is {open() ? "showing" : "not showing"}.</p>
      <Tooltip open={open()} onOpenChange={setOpen}>
        <Tooltip.Trigger>Trigger</Tooltip.Trigger>
        <Tooltip.Portal>
          <Tooltip.Content>
            <Tooltip.Arrow />
            <p>Tooltip content</p>
          </Tooltip.Content>
        </Tooltip.Portal>
      </Tooltip>
    </>
  );
}

원점 인식 애니메이션 (Origin-aware animations)

콘텐츠를 계산된 원점(computed origin)에서 애니메이션할 수 있도록 CSS 커스텀 속성 --kb-tooltip-content-transform-origin을 노출해요.

.tooltip__content {
  transform-origin: var(--kb-tooltip-content-transform-origin);
  animation: contentHide 250ms ease-in forwards;
}

.tooltip__content[data-expanded] {
  animation: contentShow 250ms ease-out;
}

@keyframes contentShow {
  from {
    opacity: 0;
    transform: scale(0.96);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes contentHide {
  from {
    opacity: 1;
    transform: scale(1);
  }
  to {
    opacity: 0;
    transform: scale(0.96);
  }
}

API 참조 (API Reference)

Tooltip

Tooltip은 @kobalte/core/tooltip의 Root 임포트(및 더 이상 사용되지 않는 Tooltip.Root)와 동일해요.

Prop Description
open boolean The controlled open state of the tooltip.
defaultOpen boolean The default open state when initially rendered. Useful when you do not need to control the open state.
onOpenChange (open: boolean) => void Event handler called when the open state of the tooltip changes.
triggerOnFocusOnly boolean Whether to open the tooltip only when the trigger is focused. By default, opens for both focus and hover.
openDelay number The duration from when the mouse enters the trigger until the tooltip opens.
skipDelayDuration number The duration from when the mouse leaves the trigger or content and moves to another tooltip trigger or content without incurring another openDelay
closeDelay number The duration from when the mouse leaves the trigger or content until the tooltip closes.
ignoreSafeArea boolean Whether to close the tooltip even if the user cursor is inside the safe area between the trigger and tooltip.
id string A unique identifier for the component. The id is used to generate id attributes for nested components. If no id prop is provided, a generated id will be used.
forceMount boolean Used to force mounting the tooltip (portal and content) when more control is needed. Useful when controlling animation with SolidJS animation libraries.

Tooltip은 Tooltip.Content의 배치(placement)를 커스터마이즈하기 위한 다음 props도 허용해요.

Prop Description
getAnchorRect (anchor?: HTMLElement) => AnchorRect | undefined Function that returns the anchor element's DOMRect.
placement Placement The placement of the tooltip.
gutter number The distance between the tooltip and the trigger/anchor element. By default, it's 0 plus half of the arrow offset, if it exists.
shift number The skidding of the tooltip along the anchor element.
flip boolean | string Controls the behavior of the tooltip when it overflows the viewport: - If a boolean , specifies whether the tooltip should flip to the opposite side when it overflows. - If a string , indicates the preferred fallback placements when it overflows. The placements must be spaced-delimited, e.g. "top left".
slide boolean Whether the tooltip should slide when it overflows.
overlap boolean Whether the tooltip can overlap the anchor element when it overflows.
sameWidth boolean Whether the tooltip should have the same width as the anchor element. This will be exposed to CSS as --kb-popper-anchor-width .
fitViewport boolean Whether the tooltip should fit the viewport. If this is set to true, the tooltip content will have maxWidth and maxHeight set to the viewport size. This will be exposed to CSS as --kb-popper-available-width and --kb-popper-available-height .
hideWhenDetached boolean Whether to hide the tooltip when the anchor element becomes occluded.
detachedPadding number The minimum padding in order to consider the anchor element occluded.
arrowPadding number The minimum padding between the arrow and the tooltip corner.
overflowPadding number The minimum padding between the tooltip and the viewport edge. This will be exposed to CSS as --kb-popper-overflow-padding .

Tooltip.Trigger

Data attribute Description
data-expanded Present when the tooltip is open.
data-closed Present when the tooltip is close.

Tooltip.Content와 Tooltip.Arrow는 동일한 data-속성을 공유해요.

Tooltip.Content

popper positioner는 Tooltip.Content와 동일한 z-index를 복사해요.

Prop Description
onEscapeKeyDown (event: KeyboardEvent) => void Event handler called when the escape key is down. It can be prevented by calling event.preventDefault .
onPointerDownOutside (event: PointerDownOutsideEvent) => void Event handler called when a pointer event occurs outside the bounds of the component. It can be prevented by calling event.preventDefault .

Tooltip.Arrow

Prop Description
size number The size of the arrow.

렌더링되는 요소 (Rendered elements)

Component Default rendered element
Tooltip none
Tooltip.Trigger button
Tooltip.Portal Portal
Tooltip.Content div
Tooltip.Arrow div

접근성 (Accessibility)

키보드 상호작용 (Keyboard Interactions)

Key Description
Tab Opens/closes the tooltip without delay.
Space When open, closes the tooltip without delay.
Enter When open, closes the tooltip without delay.
Esc When open, closes the tooltip without delay.

더 알아보기 (Learn more)