Vant4 Popover 氣泡彈出框

2023-02-16 17:56 更新

介紹

彈出式的氣泡菜單。

引入

通過以下方式來全局注冊組件,更多注冊方式請參考組件注冊

import { createApp } from 'vue';
import { Popover } from 'vant';

const app = createApp();
app.use(Popover);

代碼演示

基礎用法

當 Popover 彈出時,會基于 ?reference? 插槽的內(nèi)容進行定位。

<van-popover v-model:show="showPopover" :actions="actions" @select="onSelect">
  <template #reference>
    <van-button type="primary">淺色風格</van-button>
  </template>
</van-popover>
import { ref } from 'vue';
import { showToast } from 'vant';

export default {
  setup() {
    const showPopover = ref(false);

    // 通過 actions 屬性來定義菜單選項
    const actions = [
      { text: '選項一' },
      { text: '選項二' },
      { text: '選項三' },
    ];
    const onSelect = (action) => showToast(action.text);

    return {
      actions,
      onSelect,
      showPopover,
    };
  },
};

深色風格

Popover 支持淺色和深色兩種風格,默認為淺色風格,將 ?theme? 屬性設置為 ?dark? 可切換為深色風格。

<van-popover v-model:show="showPopover" theme="dark" :actions="actions">
  <template #reference>
    <van-button type="primary">深色風格</van-button>
  </template>
</van-popover>
import { ref } from 'vue';

export default {
  setup() {
    const showPopover = ref(false);
    const actions = [
      { text: '選項一' },
      { text: '選項二' },
      { text: '選項三' },
    ];

    return {
      actions,
      showPopover,
    };
  },
};

彈出位置

通過 ?placement? 屬性來控制氣泡的彈出位置。

<van-popover placement="top" />

?placement? 支持以下值:

top           # 頂部中間位置
top-start     # 頂部左側(cè)位置
top-end       # 頂部右側(cè)位置
left          # 左側(cè)中間位置
left-start    # 左側(cè)上方位置
left-end      # 左側(cè)下方位置
right         # 右側(cè)中間位置
right-start   # 右側(cè)上方位置
right-end     # 右側(cè)下方位置
bottom        # 底部中間位置
bottom-start  # 底部左側(cè)位置
bottom-end    # 底部右側(cè)位置

展示圖標

在 ?actions? 數(shù)組中,可以通過 ?icon? 字段來定義選項的圖標,支持傳入圖標名稱或圖片鏈接,等同于 Icon 組件的 name 屬性。

<van-popover v-model:show="showPopover" :actions="actions">
  <template #reference>
    <van-button type="primary">展示圖標</van-button>
  </template>
</van-popover>
import { ref } from 'vue';

export default {
  setup() {
    const showPopover = ref(false);
    const actions = [
      { text: '選項一', icon: 'add-o' },
      { text: '選項二', icon: 'music-o' },
      { text: '選項三', icon: 'more-o' },
    ];

    return {
      actions,
      showPopover,
    };
  },
};

禁用選項

在 ?actions? 數(shù)組中,可以通過 ?disabled? 字段來禁用某個選項。

<van-popover v-model:show="showPopover" :actions="actions">
  <template #reference>
    <van-button type="primary">禁用選項</van-button>
  </template>
</van-popover>
import { ref } from 'vue';

export default {
  setup() {
    const showPopover = ref(false);
    const actions = [
      { text: '選項一', disabled: true },
      { text: '選項二', disabled: true },
      { text: '選項三' },
    ];

    return {
      actions,
      showPopover,
    };
  },
};

自定義內(nèi)容

通過默認插槽,可以在 Popover 內(nèi)部放置任意內(nèi)容。

<van-popover v-model:show="showPopover">
  <van-grid
    square
    clickable
    :border="false"
    column-num="3"
    style="width: 240px;"
  >
    <van-grid-item
      v-for="i in 6"
      :key="i"
      text="選項"
      icon="photo-o"
      @click="showPopover = false"
    />
  </van-grid>
  <template #reference>
    <van-button type="primary">自定義內(nèi)容</van-button>
  </template>
</van-popover>
import { ref } from 'vue';

export default {
  setup() {
    const showPopover = ref(false);
    return { showPopover };
  },
};

非受控模式

你可以把 Popover 當做受控組件或非受控組件使用:

  • 當綁定 ?v-model:show? 時,Popover 為受控組件,此時組件的顯示完全由 ?v-model:show? 的值決定。
  • 當未綁定 ?v-model:show? 時,Popover 為非受控組件,此時你可以通過 ?show? 屬性傳入一個默認值,組件值的顯示由組件自身控制。
<van-popover :actions="actions" position="top-start" @select="onSelect">
  <template #reference>
    <van-button type="primary">非受控模式</van-button>
  </template>
</van-popover>
import { ref } from 'vue';
import { showToast } from 'vant';

export default {
  setup() {
    const actions = [
      { text: '選項一' },
      { text: '選項二' },
      { text: '選項三' },
    ];
    const onSelect = (action) => showToast(action.text);
    return {
      actions,
      onSelect,
    };
  },
};

API

Props

參數(shù) 說明 類型 默認值
v-model:show 是否展示氣泡彈出層 boolean false
actions 選項列表 PopoverAction[] []
placement 彈出位置 PopoverPlacement bottom
theme 主題風格,可選值為 dark PopoverTheme light
trigger 觸發(fā)方式,可選值為 manual PopoverTrigger click
duration 動畫時長,單位秒,設置為 0 可以禁用動畫 number | string 0.3
offset 出現(xiàn)位置的偏移量 [number, number] [0, 8]
overlay 是否顯示遮罩層 boolean false
overlay-class v3.0.10 自定義遮罩層類名 string | Array | object -
overlay-style v3.0.10 自定義遮罩層樣式 object -
show-arrow v3.2.2 是否展示小箭頭 boolean true
close-on-click-action 是否在點擊選項后關閉 boolean true
close-on-click-outside 是否在點擊外部元素后關閉菜單 boolean true
close-on-click-overlay v3.0.10 是否在點擊遮罩層后關閉菜單 boolean true
teleport 指定掛載的節(jié)點,等同于 Teleport 組件的 to 屬性 string | Element body
icon-prefix v3.0.17 圖標類名前綴,等同于 Icon 組件的 class-prefix 屬性 string van-icon

PopoverAction 數(shù)據(jù)結構

?actions? 屬性是一個由對象構成的數(shù)組,數(shù)組中的每個對象配置一列,對象可以包含以下值:

鍵名 說明 類型
text 選項文字 string
icon 文字左側(cè)的圖標,支持傳入圖標名稱或圖片鏈接,等同于 Icon 組件的 name 屬性 string
color 選項文字顏色 string
disabled 是否為禁用狀態(tài) boolean
className 為對應選項添加額外的類名 string | Array | object

Events

事件名 說明 回調(diào)參數(shù)
select 點擊選項時觸發(fā) action: PopoverAction, index: number
open 打開菜單時觸發(fā) -
close 關閉菜單時觸發(fā) -
opened 打開菜單且動畫結束后觸發(fā) -
closed 關閉菜單且動畫結束后觸發(fā) -
click-overlay 點擊遮罩層時觸發(fā) event: MouseEvent

Slots

名稱 說明 參數(shù)
default 自定義菜單內(nèi)容 -
reference 觸發(fā) Popover 顯示的元素內(nèi)容 -
action v3.4.0 自定義選項內(nèi)容 { action: PopoverAction, index: number }

類型定義

組件導出以下類型定義:

import type {
  PopoverProps,
  PopoverTheme,
  PopoverAction,
  PopoverTrigger,
  PopoverPlacement,
} from 'vant';

主題定制

樣式變量

組件提供了下列 CSS 變量,可用于自定義樣式,使用方法請參考 ConfigProvider 組件。

名稱 默認值 描述
--van-popover-arrow-size 6px -
--van-popover-radius var(--van-radius-lg) -
--van-popover-action-width 128px -
--van-popover-action-height 44px -
--van-popover-action-font-size var(--van-font-size-md) -
--van-popover-action-line-height var(--van-line-height-md) -
--van-popover-action-icon-size 20px -
--van-popover-light-text-color var(--van-text-color) -
--van-popover-light-background var(--van-background-2) -
--van-popover-light-action-disabled-text-color var(--van-text-color-3) -
--van-popover-dark-text-color var(--van-white) -
--van-popover-dark-background #4a4a4a -
--van-popover-dark-action-disabled-text-color var(--van-text-color-2) -

常見問題

Popover 的點擊事件無法正確觸發(fā)?

這種情況通常是由于項目中引入了 ?fastclick? 庫導致的。建議移除 ?fastclick?,或者配置 ?fastclick? 的 ignore 規(guī)則


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號