¥Popover
在门户中显示富文本内容,由按钮触发。
import * as React from "react";import { Popover } from "radix-ui";import { MixerHorizontalIcon, Cross2Icon } from "@radix-ui/react-icons";import "./styles.css";const PopoverDemo = () => (<Popover.Root><Popover.Trigger asChild><button className="IconButton" aria-label="Update dimensions"><MixerHorizontalIcon /></button></Popover.Trigger><Popover.Portal><Popover.Content className="PopoverContent" sideOffset={5}><div style={{ display: "flex", flexDirection: "column", gap: 10 }}><p className="Text" style={{ marginBottom: 10 }}>Dimensions</p><fieldset className="Fieldset"><label className="Label" htmlFor="width">Width</label><input className="Input" id="width" defaultValue="100%" /></fieldset><fieldset className="Fieldset"><label className="Label" htmlFor="maxWidth">Max. width</label><input className="Input" id="maxWidth" defaultValue="300px" /></fieldset><fieldset className="Fieldset"><label className="Label" htmlFor="height">Height</label><input className="Input" id="height" defaultValue="25px" /></fieldset><fieldset className="Fieldset"><label className="Label" htmlFor="maxHeight">Max. height</label><input className="Input" id="maxHeight" defaultValue="none" /></fieldset></div><Popover.Close className="PopoverClose" aria-label="Close"><Cross2Icon /></Popover.Close><Popover.Arrow className="PopoverArrow" /></Popover.Content></Popover.Portal></Popover.Root>);export default PopoverDemo;
Can be controlled or uncontrolled.
Customize side, alignment, offsets, collision handling.
Optionally render a pointing arrow.
Focus is fully managed and customizable.
Supports modal and non-modal modes.
Dismissing and layering behavior is highly customizable.
¥Installation
从命令行安装组件。
¥Install the component from your command line.
¥Anatomy
导入所有部分并将它们组合在一起。
¥Import all parts and piece them together.
¥API Reference
¥Root
包含弹出窗口的所有部分。
¥Contains all the parts of a popover.
¥Trigger
切换弹出窗口的按钮。默认情况下,Popover.Content
将根据触发器进行定位。
¥The button that toggles the popover. By default, the Popover.Content
will position itself against the trigger.
¥Anchor
用于定位 Popover.Content
的可选元素。如果不使用 RSC,内容将与 Popover.Trigger
并排放置。
¥An optional element to position the Popover.Content
against. If this part is not used, the content will position alongside the Popover.Trigger
.
¥Portal
使用时,将内容部分传送到 body
中。
¥When used, portals the content part into the body
.
¥Content
弹出窗口打开时弹出的组件。
¥The component that pops out when the popover is open.
¥Arrow
一个可选的箭头元素,用于与弹出窗口一起渲染。这可以用来帮助在视觉上将锚点与 Popover.Content
链接起来。必须在 Popover.Content
中渲染。
¥An optional arrow element to render alongside the popover. This can be used to help visually link the anchor with the Popover.Content
. Must be rendered inside Popover.Content
.
¥Close
关闭打开的弹出窗口的按钮。
¥The button that closes an open popover.
¥Examples
¥Constrain the content size
你可能需要限制内容的宽度,使其与触发器的宽度匹配。你可能还需要限制其高度,使其不超过视口。
¥You may want to constrain the width of the content so that it matches the trigger width. You may also want to constrain its height to not exceed the viewport.
为了支持此功能,我们公开了几个 CSS 自定义属性,例如 --radix-popover-trigger-width
和 --radix-popover-content-available-height
。使用它们来限制内容尺寸。
¥We expose several CSS custom properties such as --radix-popover-trigger-width
and --radix-popover-content-available-height
to support this. Use them to constrain the content dimensions.
¥Origin-aware animations
我们公开了一个 CSS 自定义属性 --radix-popover-content-transform-origin
。使用它来根据 side
、sideOffset
、align
、alignOffset
以及任何碰撞,从其计算出的原点为内容设置动画。
¥We expose a CSS custom property --radix-popover-content-transform-origin
. Use it to animate the content from its computed origin based on side
, sideOffset
, align
, alignOffset
and any collisions.
¥Collision-aware animations
我们公开了 data-side
和 data-align
属性。它们的值将在运行时更改以反映碰撞。使用它们来创建碰撞和方向感知动画。
¥We expose data-side
and data-align
attributes. Their values will change at runtime to reflect collisions. Use them to create collision and direction-aware animations.
¥With custom anchor
如果你不想使用触发器作为锚点,可以将内容锚定到另一个元素。
¥You can anchor the content to another element if you do not want to use the trigger as the anchor.
¥Accessibility
¥Adheres to the Dialog WAI-ARIA design pattern.
¥Keyboard Interactions
¥Custom APIs
通过将原始部分抽象到你自己的组件中来创建你自己的 API。
¥Create your own API by abstracting the primitive parts into your own component.
¥Abstract the arrow and set default configuration
此示例抽象了 Popover.Arrow
部分并设置了默认的 sideOffset
配置。
¥This example abstracts the Popover.Arrow
part and sets a default sideOffset
configuration.
¥Usage
¥Implementation