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

툴팁 (Tooltip)

원문 보기 위키 갱신

sl-tooltip은 특정 동작에 따라 추가 정보를 보여주는 컴포넌트예요. 호버, 클릭, 포커스 같은 트리거로 작동하며, content 슬롯이나 content 속성으로 내용을 넣을 수 있어요.

출처: 문서

본문

툴팁은 특정 동작에 따라 추가 정보를 보여줘요.

툴팁의 대상(target)은 첫 번째 자식 요소예요. 그래서 툴팁 안에는 요소를 하나만 감싸야 해요. 여러 요소에 툴팁을 보여주려면 먼저 그 요소들을 컨테이너 안에 중첩하세요.

툴팁은 display: contents를 사용하므로 flex나 grid 레이아웃에서 요소가 배치되는 방식을 방해하지 않아요.

<sl-tooltip content="This is a tooltip">
  <sl-button>Hover Me</sl-button>
</sl-tooltip>
import SlButton from '@shoelace-style/shoelace/dist/react/button';
import SlTooltip from '@shoelace-style/shoelace/dist/react/tooltip';
const App = () => (
  <SlTooltip content="This is a tooltip">
    <SlButton>Hover Me</SlButton>
  </SlTooltip>
);

Examples

Placement

placement 속성을 사용해 툴팁의 선호 위치를 설정해요.

<div class="tooltip-placement-example">
  <div class="tooltip-placement-example-row">
    <sl-tooltip content="top-start" placement="top-start">
      <sl-button></sl-button>
    </sl-tooltip>
    <sl-tooltip content="top" placement="top">
      <sl-button></sl-button>
    </sl-tooltip>
    <sl-tooltip content="top-end" placement="top-end">
      <sl-button></sl-button>
    </sl-tooltip>
  </div>
  <div class="tooltip-placement-example-row">
    <sl-tooltip content="left-start" placement="left-start">
      <sl-button></sl-button>
    </sl-tooltip>
    <sl-tooltip content="right-start" placement="right-start">
      <sl-button></sl-button>
    </sl-tooltip>
  </div>
  <div class="tooltip-placement-example-row">
    <sl-tooltip content="left" placement="left">
      <sl-button></sl-button>
    </sl-tooltip>
    <sl-tooltip content="right" placement="right">
      <sl-button></sl-button>
    </sl-tooltip>
  </div>
  <div class="tooltip-placement-example-row">
    <sl-tooltip content="left-end" placement="left-end">
      <sl-button></sl-button>
    </sl-tooltip>
    <sl-tooltip content="right-end" placement="right-end">
      <sl-button></sl-button>
    </sl-tooltip>
  </div>
  <div class="tooltip-placement-example-row">
    <sl-tooltip content="bottom-start" placement="bottom-start">
      <sl-button></sl-button>
    </sl-tooltip>
    <sl-tooltip content="bottom" placement="bottom">
      <sl-button></sl-button>
    </sl-tooltip>
    <sl-tooltip content="bottom-end" placement="bottom-end">
      <sl-button></sl-button>
    </sl-tooltip>
  </div>
</div>
<style>
  .tooltip-placement-example {
    width: 250px;
    margin: 1rem;
  }
  .tooltip-placement-example-row:after {
    content: '';
    display: table;
    clear: both;
  }
  .tooltip-placement-example sl-button {
    float: left;
    width: 2.5rem;
    margin-right: 0.25rem;
    margin-bottom: 0.25rem;
  }
  .tooltip-placement-example-row:nth-child(1) sl-tooltip:first-child sl-button,
  .tooltip-placement-example-row:nth-child(5) sl-tooltip:first-child sl-button {
    margin-left: calc(40px + 0.25rem);
  }
  .tooltip-placement-example-row:nth-child(2) sl-tooltip:nth-child(2) sl-button,
  .tooltip-placement-example-row:nth-child(3) sl-tooltip:nth-child(2) sl-button,
  .tooltip-placement-example-row:nth-child(4) sl-tooltip:nth-child(2) sl-button {
    margin-left: calc((40px * 3) + (0.25rem * 3));
  }
</style>
import SlButton from '@shoelace-style/shoelace/dist/react/button';
import SlTooltip from '@shoelace-style/shoelace/dist/react/tooltip';
const css = `
  .tooltip-placement-example {
    width: 250px;
  }
  .tooltip-placement-example-row:after {
    content: '';
    display: table;
    clear: both;
  }
  .tooltip-placement-example sl-button {
    float: left;
    width: 2.5rem;
    margin-right: 0.25rem;
    margin-bottom: 0.25rem;
  }
  .tooltip-placement-example-row:nth-child(1) sl-tooltip:first-child sl-button,
  .tooltip-placement-example-row:nth-child(5) sl-tooltip:first-child sl-button {
    margin-left: calc(40px + 0.25rem);
  }
  .tooltip-placement-example-row:nth-child(2) sl-tooltip:nth-child(2) sl-button,
  .tooltip-placement-example-row:nth-child(3) sl-tooltip:nth-child(2) sl-button,
  .tooltip-placement-example-row:nth-child(4) sl-tooltip:nth-child(2) sl-button {
    margin-left: calc((40px * 3) + (0.25rem * 3));
  }
`;
const App = () => (
  <>
    <div className="tooltip-placement-example">
      <div className="tooltip-placement-example-row">
        <SlTooltip content="top-start" placement="top-start">
          <SlButton />
        </SlTooltip>
        <SlTooltip content="top" placement="top">
          <SlButton />
        </SlTooltip>
        <SlTooltip content="top-end" placement="top-end">
          <SlButton />
        </SlTooltip>
      </div>
      <div className="tooltip-placement-example-row">
        <SlTooltip content="left-start" placement="left-start">
          <SlButton />
        </SlTooltip>
        <SlTooltip content="right-start" placement="right-start">
          <SlButton />
        </SlTooltip>
      </div>
      <div className="tooltip-placement-example-row">
        <SlTooltip content="left" placement="left">
          <SlButton />
        </SlTooltip>
        <SlTooltip content="right" placement="right">
          <SlButton />
        </SlTooltip>
      </div>
      <div className="tooltip-placement-example-row">
        <SlTooltip content="left-end" placement="left-end">
          <SlButton />
        </SlTooltip>
        <SlTooltip content="right-end" placement="right-end">
          <SlButton />
        </SlTooltip>
      </div>
      <div className="tooltip-placement-example-row">
        <SlTooltip content="bottom-start" placement="bottom-start">
          <SlButton />
        </SlTooltip>
        <SlTooltip content="bottom" placement="bottom">
          <SlButton />
        </SlTooltip>
        <SlTooltip content="bottom-end" placement="bottom-end">
          <SlButton />
        </SlTooltip>
      </div>
    </div>
    <style>{css}</style>
  </>
);

Click Trigger

trigger 속성을 click으로 설정하면 호버 대신 클릭으로 툴팁을 토글할 수 있어요.

<sl-tooltip content="Click again to dismiss" trigger="click">
  <sl-button>Click to Toggle</sl-button>
</sl-tooltip>
import SlButton from '@shoelace-style/shoelace/dist/react/button';
import SlTooltip from '@shoelace-style/shoelace/dist/react/tooltip';
const App = () => (
  <SlTooltip content="Click again to dismiss" trigger="click">
    <SlButton>Click to Toggle</SlButton>
  </SlTooltip>
);

Manual Trigger

trigger 속성을 manual로 설정하면 툴팁을 프로그래밍 방식으로 제어할 수 있어요. open 속성으로 툴팁이 언제 보일지 제어하세요.

<sl-button style="margin-right: 4rem;">Toggle Manually</sl-button>
<sl-tooltip content="This is an avatar" trigger="manual" class="manual-tooltip">
  <sl-avatar label="User"></sl-avatar>
</sl-tooltip>
<script>
  const tooltip = document.querySelector('.manual-tooltip');
  const toggle = tooltip.previousElementSibling;
  toggle.addEventListener('click', () => (tooltip.open = !tooltip.open));
</script>
import { useState } from 'react';
import SlAvatar from '@shoelace-style/shoelace/dist/react/avatar';
import SlButton from '@shoelace-style/shoelace/dist/react/button';
import SlTooltip from '@shoelace-style/shoelace/dist/react/tooltip';
const App = () => {
  const [open, setOpen] = useState(false);
  return (
    <>
      <SlButton style={{ marginRight: '4rem' }} onClick={() => setOpen(!open)}>
        Toggle Manually
      </SlButton>
      <SlTooltip open={open} content="This is an avatar" trigger="manual">
        <SlAvatar />
      </SlTooltip>
    </>
  );
};

Removing Arrows

--sl-tooltip-arrow-size 디자인 토큰을 오버라이드해서 툴팁 화살표의 크기를 제어할 수 있어요. 화살표를 없애려면 아래처럼 값을 0으로 설정하세요.

<sl-tooltip content="This is a tooltip" style="--sl-tooltip-arrow-size: 0;">
  <sl-button>No Arrow</sl-button>
</sl-tooltip>
import SlButton from '@shoelace-style/shoelace/dist/react/button';
import SlTooltip from '@shoelace-style/shoelace/dist/react/tooltip';
const App = () => (
  <div style={{ '--sl-tooltip-arrow-size': '0' }}>
    <SlTooltip content="This is a tooltip">
      <SlButton>Above</SlButton>
    </SlTooltip>
    <SlTooltip content="This is a tooltip" placement="bottom">
      <SlButton>Below</SlButton>
    </SlTooltip>
  </div>
);

전역으로 오버라이드하려면 Shoelace 스타일시트가 로드된 뒤 스타일시트의 root 블록에 설정하세요.

:root {
  --sl-tooltip-arrow-size: 0;
}

HTML in Tooltips

content 슬롯을 사용하면 HTML 콘텐츠를 가진 툴팁을 만들 수 있어요. 툴팁은 텍스트와 표현용(presentational) 요소만을 위해 설계됐어요. 버튼, 링크, 폼 컨트롤 같은 상호작용 요소를 툴팁 안에 두는 건 피하세요.

<sl-tooltip>
  <div slot="content">I'm not <strong>just</strong> a tooltip, I'm a <em>tooltip</em> with HTML!</div>
  <sl-button>Hover me</sl-button>
</sl-tooltip>
import SlButton from '@shoelace-style/shoelace/dist/react/button';
import SlTooltip from '@shoelace-style/shoelace/dist/react/tooltip';
const App = () => (
  <SlTooltip>
    <div slot="content">
      I'm not <strong>just</strong> a tooltip, I'm a <em>tooltip</em> with HTML!
    </div>
    <SlButton>Hover Me</SlButton>
  </SlTooltip>
);

Setting a Maximum Width

--max-width 커스텀 프로퍼티를 사용해 툴팁이 줄바꿈되기 전까지 커질 수 있는 너비를 바꿀 수 있어요.

<sl-tooltip style="--max-width: 80px;" content="This tooltip will wrap after only 80 pixels.">
  <sl-button>Hover me</sl-button>
</sl-tooltip>
import SlButton from '@shoelace-style/shoelace/dist/react/button';
import SlTooltip from '@shoelace-style/shoelace/dist/react/tooltip';
const App = () => (
  <SlTooltip style={{ '--max-width': '80px' }} content="This tooltip will wrap after only 80 pixels.">
    <SlButton>Hover Me</SlButton>
  </SlTooltip>
);

Hoisting

툴팁이 overflow: auto|hidden|scroll을 가진 컨테이너 안에 있으면 잘릴 수 있어요. hoist 속성은 툴팁이 고정(fixed) 포지셔닝 전략을 사용하도록 강제해서 컨테이너 밖으로 나갈 수 있게 해줘요. 이 경우 툴팁은 포함 블록(containing block)을 기준으로 배치되는데, 조상 중 하나가 transform, perspective, filter를 사용하지 않는 한 보통 뷰포트예요.

자세한 내용은 이 페이지를 참고하세요.

<div class="tooltip-hoist">
  <sl-tooltip content="This is a tooltip">
    <sl-button>No Hoist</sl-button>
  </sl-tooltip>
  <sl-tooltip content="This is a tooltip" hoist>
    <sl-button>Hoist</sl-button>
  </sl-tooltip>
</div>
<style>
  .tooltip-hoist {
    position: relative;
    border: solid 2px var(--sl-panel-border-color);
    overflow: hidden;
    padding: var(--sl-spacing-medium);
  }
</style>
import SlButton from '@shoelace-style/shoelace/dist/react/button';
import SlTooltip from '@shoelace-style/shoelace/dist/react/tooltip';
const css = `
  .tooltip-hoist {
    border: solid 2px var(--sl-panel-border-color);
    overflow: hidden;
    padding: var(--sl-spacing-medium);
    position: relative;
  }
`;
const App = () => (
  <>
    <div class="tooltip-hoist">
      <SlTooltip content="This is a tooltip">
        <SlButton>No Hoist</SlButton>
      </SlTooltip>
      <SlTooltip content="This is a tooltip" hoist>
        <SlButton>Hoist</SlButton>
      </SlTooltip>
    </div>
    <style>{css}</style>
  </>
);

Importing

autoloader나 전통적인 loader를 사용하고 있다면 이 섹션은 무시해도 돼요. 그 외에는 아래 스니펫 중 하나로 이 컴포넌트를 골라(cherry pick) 가져올 수 있어요.

Script / Import / Bundler / React

CDN에서 script 태그로 이 컴포넌트를 가져오려면:

<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/tooltip/tooltip.js"></script>

CDN에서 JavaScript import로 이 컴포넌트를 가져오려면:

import 'https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/tooltip/tooltip.js';

bundler를 사용해 이 컴포넌트를 가져오려면:

import '@shoelace-style/shoelace/dist/components/tooltip/tooltip.js';

이 컴포넌트를 React 컴포넌트로 가져오려면:

import SlTooltip from '@shoelace-style/shoelace/dist/react/tooltip';

Slots

Name Description
(default) The tooltip’s target element. Avoid slotting in more than one element, as subsequent ones will be ignored.
content The content to render in the tooltip. Alternatively, you can use the content attribute.

슬롯(slots) 사용에 대해 더 알아보기.

Properties

Name Description Reflects Type Default
content The tooltip’s content. If you need to display HTML, use the content slot instead. string ''
placement The preferred placement of the tooltip. Note that the actual placement may vary as needed to keep the tooltip inside of the viewport. 'top' | 'top-start' | 'top-end' | 'right' | 'right-start' | 'right-end' | 'bottom' | 'bottom-start' | 'bottom-end' | 'left' | 'left-start' | 'left-end' 'top'
disabled Disables the tooltip so it won’t show when triggered. boolean false
distance The distance in pixels from which to offset the tooltip away from its target. number 8
open Indicates whether or not the tooltip is open. You can use this in lieu of the show/hide methods. boolean false
skidding The distance in pixels from which to offset the tooltip along its target. number 0
trigger Controls how the tooltip is activated. Possible options include click, hover, focus, and manual. Multiple options can be passed by separating them with a space. When manual is used, the tooltip must be activated programmatically. string 'hover focus'
hoist Enable this option to prevent the tooltip from being clipped when the component is placed inside a container with overflow: auto|hidden|scroll. Hoisting uses a fixed positioning strategy that works in many, but not all, scenarios. boolean false
updateComplete A read-only promise that resolves when the component has finished updating.

속성과 프로퍼티(attributes and properties)에 대해 더 알아보기.

Events

Name React Event Description Event Detail
sl-show onSlShow Emitted when the tooltip begins to show. -
sl-after-show onSlAfterShow Emitted after the tooltip has shown and all animations are complete. -
sl-hide onSlHide Emitted when the tooltip begins to hide. -
sl-after-hide onSlAfterHide Emitted after the tooltip has hidden and all animations are complete. -

이벤트(events)에 대해 더 알아보기.

Methods

Name Description Arguments
show() Shows the tooltip. -
hide() Hides the tooltip -

메서드(methods)에 대해 더 알아보기.

Custom Properties

Name Description Default
--max-width The maximum width of the tooltip before its content will wrap.
--hide-delay The amount of time to wait before hiding the tooltip when hovering.
--show-delay The amount of time to wait before showing the tooltip when hovering.

CSS 커스텀 프로퍼티 커스터마이징에 대해 더 알아보기.

Parts

Name Description
base The component’s base wrapper, an `` element.
base__popup The popup’s exported popup part. Use this to target the tooltip’s popup container.
base__arrow The popup’s exported arrow part. Use this to target the tooltip’s arrow.
body The tooltip’s body where its content is rendered.

CSS 파트(parts) 커스터마이징에 대해 더 알아보기.

Animations

Name Description
tooltip.show The animation to use when showing the tooltip.
tooltip.hide The animation to use when hiding the tooltip.

애니메이션 커스터마이징에 대해 더 알아보기.

Dependencies

이 컴포넌트는 다음 의존성을 자동으로 import 해요.

  • ``

더 알아보기 (Learn more)