Hover Card (호버 카드)
시력이 있는 사용자가 링크 뒤에 있는 콘텐츠를 미리 볼 수 있게 해주는 컴포넌트예요. 마우스를 트리거에 올리면(호버) 카드 형태로 콘텐츠가 나타나는 패턴이에요.
출처: 문서
본문
Import (불러오기)
import { HoverCard } from "@kobalte/core/hover-card";
// or
import { Root, Trigger, ... } from "@kobalte/core/hover-card";
// or (deprecated)
import { HoverCard } from "@kobalte/core";
Features (기능)
- 호버 시에만 열려요.
- 열림과 닫힘 지연 시간을 커스텀할 수 있어요.
- 선택적으로 화살표(arrow)를 렌더링할 수 있어요.
- 스크린 리더에서는 무시돼요.
- 제어 방식(controlled) 또는 비제어 방식(uncontrolled)으로 사용할 수 있어요.
Anatomy (구조)
호버 카드는 다음의 부분들로 이루어져요:
- HoverCard: 호버 카드의 루트 컨테이너예요.
- HoverCard.Trigger: 호버 카드를 여는 링크예요.
- HoverCard.Portal: 호버 카드가 열려 있을 때 children을
body로 포털(portal)해요. - HoverCard.Content: 호버 카드가 열려 있을 때 렌더링할 콘텐츠를 담아요.
- HoverCard.Arrow: 호버 카드 옆에 렌더링되는 선택적 화살표 요소예요.
<HoverCard>
<HoverCard.Trigger />
<HoverCard.Portal>
<HoverCard.Content>
<HoverCard.Arrow />
</HoverCard.Content>
</HoverCard.Portal>
</HoverCard>
Example (예제)
기본 예제를 통해 호버 카드의 구조와 스타일링 방식을 확인해 보세요. 인터랙티브한 미리보기는 출처 문서에서 직접 확인할 수 있어요.
Usage (사용법)
Default open (기본으로 열림)
초기 비제어 방식의 열림 값을 defaultOpen prop으로 제공할 수 있어요.
<HoverCard defaultOpen>...</HoverCard>
Controlled open (제어 방식 열림)
open prop을 사용하면 열림 상태를 제어할 수 있어요. 사용자가 트리거에 마우스를 올리거나 떼면 onOpenChange 이벤트가 발생하며, 새로운 값을 전달받아요.
import { createSignal } from "solid-js";
function ControlledExample() {
const [open, setOpen] = createSignal(false);
return (
<HoverCard open={open()} onOpenChange={setOpen}>
...
</HoverCard>
);
}
Origin-aware animations (원점 기준 애니메이션)
계산된 원점(origin)에서 콘텐츠를 애니메이션 처리할 수 있도록, CSS 커스텀 프로퍼티 --kb-hovercard-content-transform-origin을 제공해요.
/* style.css */
.hovercard__content {
transform-origin: var(--kb-hovercard-content-transform-origin);
animation: contentHide 250ms ease-in forwards;
}
.hovercard__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 Reference (API 참조)
HoverCard
HoverCard는 @kobalte/core/hover-card의 Root 임포트(그리고 더 이상 사용되지 않는 HoverCard.Root)와 동일해요.
| Prop | Description |
|---|---|
| open | boolean The controlled open state of the hovercard. |
| 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 hovercard changes. |
| openDelay | number The duration from when the mouse enters the trigger until the hovercard opens. |
| closeDelay | number The duration from when the mouse leaves the trigger or content until the hovercard closes. |
| ignoreSafeArea | boolean Whether to close the hovercard even if the user cursor is inside the safe area between the trigger and hovercard. |
| 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 hovercard (portal and content) when more control is needed. Useful when controlling animation with SolidJS animation libraries. |
HoverCard는 HoverCard.Content의 배치(placement)를 커스텀하기 위해 다음의 prop들을 추가로 받아요.
| Prop | Description |
|---|---|
| getAnchorRect | (anchor?: HTMLElement) => AnchorRect | undefined Function that returns the anchor element's DOMRect. |
| placement | Placement The placement of the hovercard. |
| gutter | number The distance between the hovercard and the trigger element. By default, it's 0 plus half of the arrow offset, if it exists. |
| shift | number The skidding of the hovercard along the trigger element. |
| flip | boolean | string Controls the behavior of the hovercard when it overflows the viewport: - If a boolean, specifies whether the hovercard 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 hovercard should slide when it overflows. |
| overlap | boolean Whether the hovercard can overlap the trigger element when it overflows. |
| sameWidth | boolean Whether the hovercard should have the same width as the trigger element. This will be exposed to CSS as --kb-popper-anchor-width. |
| fitViewport | boolean Whether the hovercard should fit the viewport. If this is set to true, the hovercard 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 hovercard when the trigger element becomes occluded. |
| detachedPadding | number The minimum padding in order to consider the trigger element occluded. |
| arrowPadding | number The minimum padding between the arrow and the hovercard corner. |
| overflowPadding | number The minimum padding between the hovercard and the viewport edge. This will be exposed to CSS as --kb-popper-overflow-padding. |
HoverCard.Content
포퍼 위치 지정자(popper positioner)는 HoverCard.Content와 동일한 z-index를 복사해요.
HoverCard.Trigger
HoverCard.Trigger는 Link로 구성돼요.
| Data attribute | Description |
|---|---|
| data-expanded | Present when the hovercard is open. |
| data-closed | Present when the hovercard is close. |
HoverCard.Content는 동일한 data-attribute를 공유해요.
Rendered elements (렌더링되는 요소)
| Component | Default rendered element |
|---|---|
HoverCard |
none |
HoverCard.Trigger |
a |
HoverCard.Portal |
Portal |
HoverCard.Content |
div |
HoverCard.Arrow |
div |
Accessibility (접근성)
Keyboard Interactions (키보드 상호작용)
호버 카드는 마우스 사용자만을 위한 것이므로 키보드 내비게이션에는 반응하지 않아요.
더 알아보기 (Learn more)
- Link 컴포넌트 — HoverCard.Trigger의 기반
- Hover Card 컴포넌트 소스 — GitHub 저장소
- Kobalte 시작하기 — 프로젝트 설정