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

팝오버(Popover)

원문 보기 위키 갱신

앵커(anchor) 요소를 기준으로 위치가 정해지는 팝오버로, 팝오버 내용을 앵커에 상대적으로 배치해요.

출처: 문서

본문

Import

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

Features

  • 팝오버 콘텐츠에 접근 가능한 다이얼로그(dialog) 의미론을 사용해요.
  • 모달(modal) 및 비모달(non-modal) 모드를 지원해요.
  • 렌더링된 제목과 설명을 통해 스크린 리더 발표를 제공해요.
  • 포커스가 완전히 관리되며 커스터마이즈할 수 있어요.
  • 선택적으로 가리키는 화살표를 렌더링할 수 있어요.
  • 제어(controlled) 또는 비제어(uncontrolled) 방식으로 사용할 수 있어요.

Anatomy

팝오버는 다음 요소들로 구성돼요:

  • Popover: 팝오버의 루트 컨테이너예요.
  • Popover.Trigger: 팝오버를 여는 버튼이에요.
  • Popover.Anchor: Popover.Content를 배치할 기준이 되는 선택적 요소예요.
  • Popover.Portal: 팝오버가 열려 있을 때 자식 요소를 body로 포털해요.
  • Popover.Content: 팝오버가 열려 있을 때 렌더링할 콘텐츠를 담아요.
  • Popover.Arrow: 팝오버 옆에 렌더링되는 선택적 화살표 요소예요.
  • Popover.CloseButton: 팝오버를 닫는 버튼이에요.
  • Popover.Title: 팝오버가 열려 있을 때 발표되는 접근 가능한 제목이에요.
  • Popover.Description: 팝오버가 열려 있을 때 발표되는 선택적 접근 가능한 설명이에요.
<Popover>
	<Popover.Trigger />
	<Popover.Anchor />
	<Popover.Portal>
		<Popover.Content>
			<Popover.Arrow />
			<Popover.CloseButton />
			<Popover.Title />
			<Popover.Description />
		</Popover.Content>
	</Popover.Portal>
</Popover>

Example

Usage

Default open

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

<Popover defaultOpen>...</Popover>

Controlled open

open prop을 사용해 열림 상태를 제어(controlled) 방식으로 만들 수 있어요. 사용자가 트리거, 닫기 버튼을 누르거나 바깥 영역을 상호작용하면 onOpenChange 이벤트가 발생하며 새 값을 받아요.

import { createSignal } from "solid-js";

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

	return (
		<Popover open={open()} onOpenChange={setOpen}>
			<Popover.Trigger>{open() ? "Close" : "Open"}</Popover.Trigger>
			<Popover.Portal>
				<Popover.Content>...</Popover.Content>
			</Popover.Portal>
		</Popover>
	);
}

With custom anchor

트리거를 앵커로 쓰고 싶지 않다면 Popover.Anchor 컴포넌트를 사용해 콘텐츠를 다른 요소에 앵커할 수 있어요.

<Popover>
	<Popover.Anchor>
		<p>
			The popover opens when you click <Popover.Trigger>here</Popover.Trigger>.
		</p>
		<p>But it's anchored to the whole div.</p>
	</Popover.Anchor>
	<Popover.Portal>
		<Popover.Content>...</Popover.Content>
	</Popover.Portal>
</Popover>

Origin-aware animations

CSS 커스텀 프로퍼티 --kb-popover-content-transform-origin을 노출하며, 이를 사용해 계산된 원점(origin)에서 콘텐츠를 애니메이션할 수 있어요.

/* style.css */
.popover__content {
	transform-origin: var(--kb-popover-content-transform-origin);
	animation: contentHide 250ms ease-in forwards;
}

.popover__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

Popover

Popover는 @kobalte/core/popover의 Root import와 동일해요(그리고 폐기(deprecated)된 Popover.Root).

Prop Description
open boolean
The controlled open state of the popover.
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 popover changes.
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.
modal boolean
Whether the popover should be the only visible content for screen readers, when set to true:
- interaction with outside elements will be disabled.
- scroll will be locked.
- focus will be locked inside the popover content.
- elements outside the popover content will not be visible for screen readers.
preventScroll boolean
Whether the scroll should be locked even if the popover is not modal.
forceMount boolean
Used to force mounting the popover (portal and content) when more control is needed. Useful when controlling animation with SolidJS animation libraries.
translations PopoverIntlTranslations
Localization strings.

Popover also accepts the following props to customize the placement of the Popover.Content.

Prop Description
getAnchorRect (anchor?: HTMLElement) => AnchorRect | undefined
Function that returns the anchor element's DOMRect.
anchorRef Accessor<HTMLElement | undefined>
A ref for the anchor element. Useful if you want to use an element outside Popover as the popover anchor.
placement Placement
The placement of the popover.
gutter number
The distance between the popover 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 popover along the anchor element.
flip boolean | string
Controls the behavior of the popover when it overflows the viewport:
- If a boolean, specifies whether the popover 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 popover should slide when it overflows.
overlap boolean
Whether the popover can overlap the anchor element when it overflows.
sameWidth boolean
Whether the popover should have the same width as the anchor element. This will be exposed to CSS as --kb-popper-anchor-width.
fitViewport boolean
Whether the popover should fit the viewport. If this is set to true, the popover 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 popover 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 popover corner.
overflowPadding number
The minimum padding between the popover and the viewport edge. This will be exposed to CSS as --kb-popper-overflow-padding.

Popover.Trigger

Popover.Trigger는 Button으로 구성돼요.

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

Popover.Anchor , Popover.Content , Popover.Arrow, Popover.Title, Popover.Description and Popover.CloseButton share the same data-attributes.

Popover.Content

포퍼 포지셔너(popper positioner)는 Popover.Content와 동일한 z-index를 복사해요.

Prop Description
onOpenAutoFocus (event: Event) => void
Event handler called when focus moves into the component after opening. It can be prevented by calling event.preventDefault.
onCloseAutoFocus (event: Event) => void
Event handler called when focus moves to the trigger after closing. It can be prevented by calling event.preventDefault.
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.
onFocusOutside (event: FocusOutsideEvent) => void
Event handler called when the focus moves outside the bounds of the component. It can be prevented by calling event.preventDefault.
onInteractOutside (event: InteractOutsideEvent) => void
Event handler called when an interaction (pointer or focus event) happens outside the bounds of the component. It can be prevented by calling event.preventDefault.

Popover.Arrow

Prop Description
size number
The size of the arrow.

Rendered elements

Component Default rendered element
Popover none
Popover.Trigger button
Popover.Anchor div
Popover.Portal Portal
Popover.Content div
Popover.Arrow div
Popover.CloseButton button
Popover.Title h2
Popover.Description p

Accessibility

Keyboard Interactions

Key Description
Space When focus is on the trigger, opens/closes the popover.
Enter When focus is on the trigger, opens/closes the popover.
Tab Moves focus to the next focusable element.
Shift + Tab Moves focus to the previous focusable element.
Esc Closes the popover and moves focus to the trigger.

더 알아보기 (Learn more)