Skip to content

Popover 气泡卡片

点击/鼠标移入元素,弹出气泡式的卡片浮层。

前置条件

该组件依赖于useEventListener,需要先下载 useEventListener 文件至src/hooks文件夹中。

下载Popover组件文件,并将文件放入src/components/tools文件夹下。

src/components/tools/index.ts写入以下代码

tsx
export * from './Popover';

基础用法

tsx
import { Popover } from '@/components/tools'

function App() {
  return
    <Popover content={'这是一段内容'}>
      <div>Hover</div>
    </Popover>
}

export default App;

点击触发

tsx
import { Button } from 'ono-react-element'
import { Popover } from '@/components/tools'

function App() {
  return
    <Popover content={'这是一段内容'} trigger="click">
      <Button>Click</Button>
    </Popover>
}

export default App;

默认打开

tsx
import { Popover } from '@/components/tools'

function App() {
  return
    <Popover content={'这是一段内容'} defaultOpen>
      <div>Hover</div>
    </Popover>
}

export default App;

隐藏箭头

tsx
import { Popover } from '@/components/tools'

function App() {
  return
    <Popover content={'这是一段内容'} isShowArrow={false}>
      <div>Hover</div>
    </Popover>
}

export default App;

自定义位置

tsx
import { List } from 'ono-react-element'
import { Popover, PositionType } from '@/components/tools'

function App() {
  const position = [
    {
      pos: '',
      label: ''
    },
    {
      pos: 'topLeft',
      label: '上左'
    },
    {
      pos: 'top',
      label: '上'
    },
    {
      pos: 'topRight',
      label: '上右'
    },
    {
      pos: '',
      label: ''
    },
    {
      pos: 'leftTop',
      label: '左上'
    },
    {
      pos: '',
      label: ''
    },
    {
      pos: '',
      label: ''
    },
    {
      pos: '',
      label: ''
    },
    {
      pos: 'rightTop',
      label: '右上'
    },
    {
      pos: 'left',
      label: '左'
    },
    {
      pos: '',
      label: ''
    },
    {
      pos: '',
      label: ''
    },
    {
      pos: '',
      label: ''
    },
    {
      pos: 'right',
      label: '右'
    },
    {
      pos: 'leftBottom',
      label: '左下'
    },
    {
      pos: '',
      label: ''
    },
    {
      pos: '',
      label: ''
    },
    {
      pos: '',
      label: ''
    },
    {
      pos: 'rightBottom',
      label: '右下'
    },
    {
      pos: '',
      label: ''
    },
    {
      pos: 'bottomLeft',
      label: '下左'
    },
    {
      pos: 'bottom',
      label: '下'
    },
    {
      pos: 'bottomRight',
      label: '下右'
    },
    {
      pos: '',
      label: ''
    }
  ]

  return (
    <div style={{ display: 'flex', justifyContent: 'center', padding: '20px' }}>
      <div
        style={{
          display: 'grid',
          gridTemplateColumns: 'repeat(5, 1fr)',
          width: '800px',
          gap: '10px'
        }}
      >
        <List list={position}>
          {({ pos, label }, i) =>
            pos === '' ? (
              <div key={i}></div>
            ) : (
              <Popover
                key={i}
                overlayStyle={{ width: '300px' }}
                content="Lorem ipsum dolor sit amet consectetur adipisicing elit. Maxime voluptatibus doloribus iure quia excepturi iste sunt esse quibusdam omnis nesciunt ullam laborum accusamus amet recusandae, soluta velit! Amet, maiores nisi."
                position={pos as PositionType}
              >
                <div style={{ padding: '8px', border: '1px solid #333' }}>
                  {label}
                </div>
              </Popover>
            )
          }
        </List>
      </div>
    </div>
  )
}

export default App;

API

通用属性参考:通用属性

参数说明类型默认值是否必填
children需要触发气泡卡片的元素ReactNode-
color气泡背景色string'#fff'
borderRadius圆角大小number|string'5px'
content气泡内容ReactNode|() => ReactNode-
padding气泡内边距number|string10
trigger触发方式'hover'|'click''hover'
zIndex层级number9999
defaultOpen是否默认显示booleanfalse
overlayClassName浮层类名string-
overlayStyle浮层样式CSSProperties-
open控制浮层显示状态booleanfalse
onOpenChange浮层显示状态改变回调函数(open: boolean) => void() => {}
isShowArrow是否显示箭头booleantrue
position气泡位置positionParams'top'

positionParams

类型说明
'top'上方
'bottom'下方
'left'左侧
'right'右侧
'topLeft'上左方
'topRight'上右方
'bottomLeft'下左方
'bottomRight'下右方
'leftTop'左上角
'leftBottom'左下角
'rightTop'右上角
'rightBottom'右下角