组件

Popover

A slip of paper set down next to its trigger. It can be held with a push-pin or a strip of tape, given a hand-drawn arrow to point at what it is about, and cut from any stock. Focus moves into it, Escape puts it away.

安装

terminal
npx shadcn@latest add @wobko/popover

第一次使用?先把 registry 加进 components.json。 如何安装

示例

Dimensions

The shadcn demo on a slip with a push-pin through its top edge. A CSS sheet: it costs no SVG.

Arrow and sides

A felt-tip arrow points at the trigger, whichever side the popover opens on (Radix flips it near the edge of the screen). The arrow's height is added to sideOffset for you.

Stocks and fasteners

Any stock, held down by a pin of any color or by tape. A torn edge is drawn when the popover opens (the only case where it costs SVG).

Share link (composed)

A real use: a share popover with a read-only link and a copy button that confirms. Focus goes to the field, the link is selected on focus, Escape returns you to the trigger.

Demo

Official `popover-demo` with the same markup: a plain heading and paragraph, four label and input rows on a grid. The slip is a CSS sheet. Two small changes: the field ids are prefixed so they cannot clash with another example on the page, and the content is named for screen readers (a popover is a dialog).

API 参考

PopoverContent

The slip: a Paper that is also the Radix popover content (role=dialog, so give it a name with a title or `aria-label`). A CSS sheet, no SVG, unless a torn edge or the arrow is asked for. It takes every Radix Content prop (`side`, `align`, `sideOffset`, ...) and these.

属性类型默认值说明
sideOffsetnumber8Gap between the trigger and the slip (upstream's default is 4: a slip needs room for its shadow and pin). The slip is also capped to the viewport width.
collisionPaddingnumber | Partial<Record<Side, number>>8Room kept between the slip and the viewport edge when it has to shift to fit (upstream's default is 0: a wide slip touched the window).
pinboolean | AccentfalseA push-pin through the top edge: `true` for tomato, or a stationery color (sky, lemon, grass ...).
arrowbooleanfalseA felt-tip arrow between the slip and the trigger. Radix turns it to whichever side the popover opened on and adds its height to `sideOffset`.
tape"corner" | "top" | "both" | falsefalseStrips of masking tape at the corners or the top edge.
stockStockName"slip"What it is made of: slip, paper, kraft, sticky-*, index-card ...
edgeEdgeSpec"cut"Cut (pure CSS), torn, deckle, ...
tiltboolean | numbertrueSmall seeded tilt of the sheet; text stays upright.
seedstring | number-Pins the hand.
data-slotstring"popover-content"For a component built on the popover to rename the slip (a combobox list is `combobox-content` in shadcn). The Command and Calendar that lie on a popover's paper look for `popover-content`, so keep it unless yours brings its own rules.

无障碍

  • Radix Popover: the trigger has aria-expanded and aria-controls; the content is a role=dialog that needs an accessible name (PopoverTitle wired with `aria-labelledby`, or `aria-label`). Focus moves into it on open, Escape and an outside click close it, and focus returns to the trigger.
  • A popover with no trigger (opened by a menu item or a shortcut, positioned by a `PopoverAnchor`) gives the focus back to whoever had it when it opened: Radix only knows the trigger and would drop the focus on the page. It does not when you clicked somewhere else, when the app has already moved the focus on, or when your `onCloseAutoFocus` prevents the default.
  • Tab moves out of a non-modal popover into the page as usual; set `modal` on Popover to trap focus.
  • The pin, tape and arrow are aria-hidden decoration with no pointer events.
  • Touch: works with a tap like any button; nothing depends on hover.
  • Portalled content repeats the InkProvider's `data-density`, `data-motion` and `data-theme`. With reduced motion it appears without the slide and zoom.

源码

CLI 会复制进你项目的文件。

正在加载源码