---
localeCode: zh-CN
order: 58
category: 展示类
title: Popover 气泡卡片
icon: doc-popover
brief: 点击/鼠标移入元素,弹出气泡式的卡片浮层。
---
## 使用场景
Popover 气泡卡片是由用户自主打开的临时性浮层卡片,能够承载一些额外内容和交互行为而不影响原页面。
和 Tooltip 的区别是,它可以承载更复杂的内容,而不仅仅是提示文本。
## 代码演示
### 如何引入
```jsx import
import { Popover } from '@douyinfe/semi-ui';
```
### 基本使用
将浮层的触发器 Trigger 作为`children`,使用 Popover 包裹(如下的例子中触发器为 Tag 元素)。浮层内容通过`content`传入
```jsx live=true
import React from 'react';
import { Popover, Tag } from '@douyinfe/semi-ui';
function Demo() {
return (
Hi ByteDancer, this is a popover.
We have 2 lines.
}
>
悬停此处
);
}
```
### 弹出位置
支持通过`position`设置浮层弹出方向,共支持十二个方向。
```jsx live=true
import React from 'react';
import { Popover, Tag } from '@douyinfe/semi-ui';
function Demo() {
const tops = [
['topLeft', 'TL'],
['top', 'Top'],
['topRight', 'TR'],
];
const lefts = [
['leftTop', 'LT'],
['left', 'Left'],
['leftBottom', 'LB'],
];
const rights = [
['rightTop', 'RT'],
['right', 'Right'],
['rightBottom', 'RB'],
];
const bottoms = [
['bottomLeft', 'BL'],
['bottom', 'Bottom'],
['bottomRight', 'BR'],
];
return (
{tops.map((pos, index) => (
Hi ByteDancer, this is a popover.
We have 2 lines.
}
position={Array.isArray(pos) ? pos[0] : pos}
key={index}
>
{Array.isArray(pos) ? pos[1] : pos}
))}
{lefts.map((pos, index) => (
Hi ByteDancer, this is a popover.
We have 2 lines.
}
position={Array.isArray(pos) ? pos[0] : pos}
key={index}
>
{Array.isArray(pos) ? pos[1] : pos}
))}
{rights.map((pos, index) => (
Hi ByteDancer, this is a popover.
We have 2 lines.
}
position={Array.isArray(pos) ? pos[0] : pos}
key={index}
>
{Array.isArray(pos) ? pos[1] : pos}
))}
{bottoms.map((pos, index) => (
Hi ByteDancer, this is a popover.
We have 2 lines.
}
position={Array.isArray(pos) ? pos[0] : pos}
key={index}
>
{Array.isArray(pos) ? pos[1] : pos}
))}
);
}
```
### 受控显示
设置`trigger='custom'`,此场景下,Popover 的显示与否完全受到参数 `visible` 的控制。
```jsx live=true
import React from 'react';
import { Popover, Button } from '@douyinfe/semi-ui';
class App extends React.Component {
constructor(props = {}) {
super(props);
this.state = {
visible: false,
};
this.content = (
Hi ByteDancer, this is a popover.
We have 2 lines.
);
this.toggleShow = this.toggleShow.bind(this);
}
toggleShow() {
this.setState({
visible: !this.state.visible,
});
}
render() {
const content = this.content;
const { visible } = this.state;
return (
{tops.map((pos, index) => (
Hi ByteDancer, this is a popover.
We have 2 lines.
}
position={Array.isArray(pos) ? pos[0] : pos}
key={index}
>
{Array.isArray(pos) ? pos[1] : pos}
))}
{lefts.map((pos, index) => (
Hi ByteDancer, this is a popover.
We have 2 lines.
}
position={Array.isArray(pos) ? pos[0] : pos}
key={index}
>
{Array.isArray(pos) ? pos[1] : pos}
))}
{rights.map((pos, index) => (
Hi ByteDancer, this is a popover.
We have 2 lines.
}
position={Array.isArray(pos) ? pos[0] : pos}
key={index}
>
{Array.isArray(pos) ? pos[1] : pos}
))}
{bottoms.map((pos, index) => (
Hi ByteDancer, this is a popover.
We have 2 lines.
}
position={Array.isArray(pos) ? pos[0] : pos}
key={index}
>
{Array.isArray(pos) ? pos[1] : pos}
))}
{tops.map((pos, index) => (
Hi ByteDancer, this is a popover.
We have 2 lines.
}
position={Array.isArray(pos) ? pos[0] : pos}
key={index}
>
{Array.isArray(pos) ? pos[1] : pos}
))}
{lefts.map((pos, index) => (
Hi ByteDancer, this is a popover.
We have 2 lines.
}
position={Array.isArray(pos) ? pos[0] : pos}
key={index}
>
{Array.isArray(pos) ? pos[1] : pos}
))}
{rights.map((pos, index) => (
Hi ByteDancer, this is a popover.
We have 2 lines.
}
position={Array.isArray(pos) ? pos[0] : pos}
key={index}
>
{Array.isArray(pos) ? pos[1] : pos}
))}
{bottoms.map((pos, index) => (
Hi ByteDancer, this is a popover.
We have 2 lines.
}
position={Array.isArray(pos) ? pos[0] : pos}
key={index}
>
{Array.isArray(pos) ? pos[1] : pos}
))}
);
}
```
### 设置浮层背景色
如果你需要定制浮层的背景色或边框颜色,请**务必单独声明 `style` 中的 `backgroundColor` 和 `borderColor` 属性**,这样能够使得“小三角”也能应用相同的背景色和边框颜色。
```jsx live=true
import React from 'react';
import { Popover, Tag } from '@douyinfe/semi-ui';
function Demo() {
return (
Hi ByteDancer, this is a popover.
We have 2 lines.
}
trigger="custom"
position='right'
visible
showArrow
style={{
backgroundColor: 'rgba(var(--semi-blue-4),1)',
borderColor: 'rgba(var(--semi-blue-4),1)',
color: 'var(--semi-color-white)',
borderWidth: 1,
borderStyle: 'solid',
}}
>
点击此处
);
}
```
### 初始化弹出层焦点位置
Popover content 支持传入函数,它的入参是一个对象,将 `initialFocusRef` 绑定在可聚焦 DOM 或组件上,打开面板时会自动聚焦在该位置。
```jsx live=true
import React from 'react';
import { Button, Input, Popover, Space } from '@douyinfe/semi-ui';
() => {
const renderContent = ({ initialFocusRef }) => {
return (