uView Pro Xuni-app x Vapor-mode component library is releasedLimited-time source license offer · 80+ components ready to use

View now
Skip to content

Popover ​

10:27

Description ​

Opens a menu panel next to the trigger element. The menu content comes entirely from the #menu slot: the component renders no built-in items and ships no default item styles. Visibility is controlled with v-model:show, and the six placement variants decide whether the panel sits above or below the trigger and how it aligns left / centre / right.

Once open, the panel snaps to the trigger element and stays inside the viewport: it flips to the other side when there is no room vertically, and is pushed inwards when it touches a horizontal edge. On Web, scrolling while the panel is open closes it; on App and Mini Program the mask blocks touch scrolling. Re-measure with refresh() or by changing refreshKey after the content resizes asynchronously; a viewport size change re-measures automatically.

The default look is a 1px outline plus a single soft shadow, and the arrow shares the panel's background colour so the two join seamlessly. Combine show-border and show-shadow to go from an outlined card to a flat WeChat-style bubble, and set the fill with bg-color. See Notes for the finer edge rules.

Compatibility ​

PlatformSupport
App✅ Supported (no page-scroll listener; the mask blocks touch scrolling. If your code calls scrollTo, the panel will not close by itself - call close() or refresh() yourself)
H5✅ Supported (any scroll while open closes the panel; first-frame measurement relies on nextTick + a 50ms tick)
WeChat Mini Program✅ Supported (same as App: the mask intercepts touch scrolling; first-frame measurement needs a tick, and the panel max-height + overflow-y fallback applies)

Examples ​

Basic usage ​

Tap to expand the menu at the bottom-end, then tap any item to close it by setting show to false in the caller.

View source
vue
<template>
    <view class="popover-row">
        <ux-popover v-model:show="show" placement="bottom-end">
            <ux-button shape="round" size="small" type="primary" text="点我展开" @click="show = true"></ux-button>
            <template #menu>
                <view class="popover-menu">
                    <ux-cell-item title="编辑资料" :arrow="false" @click="onPick()"></ux-cell-item>
                    <ux-cell-item title="复制链接" :arrow="false" @click="onPick()"></ux-cell-item>
                    <ux-cell-item title="删除" :arrow="false" :border-bottom="false" @click="onPick()"></ux-cell-item>
                </view>
            </template>
        </ux-popover>
    </view>
</template>

<script setup lang="ts">
const show = ref<boolean>(false);

// 点菜单项后由调用方自己关:组件不猜插槽里的点击语义
function onPick(): void {
    show.value = false;
}
</script>

<style lang="scss">
/* 触发区必须是 flex 容器:组件只给触发区 align-self: flex-start,非 flex 父级会让它占满一行 */
.popover-row {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
}
/* 面板宽度由内容撑开,给菜单一个最小宽度 */
.popover-menu {
    min-width: 180px;
}
</style>

Six placements ​

Each button drives its own instance; open them one by one and compare the arrow direction and alignment point.

View source
vue
<template>
    <view class="popover-grid">
        <view class="popover-grid-cell">
            <ux-popover v-model:show="bottomEndShow" placement="bottom-end">
                <ux-button shape="round" size="small" text="下方-末" @click="bottomEndShow = true"></ux-button>
                <template #menu>
                    <view class="popover-menu--compact">
                        <text class="popover-menu-text">placement = bottom-end</text>
                    </view>
                </template>
            </ux-popover>
        </view>
        <view class="popover-grid-cell">
            <ux-popover v-model:show="topShow" placement="top">
                <ux-button shape="round" size="small" text="上方-中" @click="topShow = true"></ux-button>
                <template #menu>
                    <view class="popover-menu--compact">
                        <text class="popover-menu-text">placement = top</text>
                    </view>
                </template>
            </ux-popover>
        </view>
        <!-- bottom / bottom-start / top-end / top-start 同理,一个方位一个实例 -->
    </view>
</template>

<script setup lang="ts">
// 触发元素必须落在自己实例的默认插槽里,所以一个方位一个实例
const bottomEndShow = ref<boolean>(false);
const topShow = ref<boolean>(false);
</script>

<style lang="scss">
/* 三等分格子:按钮宽度跟内容,格子里居中 */
.popover-grid {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    margin: 0 -4px;
}
.popover-grid-cell {
    box-sizing: border-box;
    width: 33.3333%;
    padding: 4px;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
}
.popover-menu--compact {
    min-width: 140px;
    padding: 10px 14px;
}
.popover-menu-text {
    /* #ifndef APP */
    display: block;
    /* #endif */
    font-size: 12px;
}
</style>

Arrow and appearance ​

Change the switches and tiers, then tap "open with the current appearance"; the echo lines inside the panel are the values actually in effect.

View source
vue
<template>
    <ux-cell-group>
        <ux-cell-item title="显示箭头" :arrow="false">
            <ux-switch v-model="arrowOn"></ux-switch>
        </ux-cell-item>
        <ux-cell-item title="显示描边" :arrow="false">
            <ux-switch v-model="borderOn"></ux-switch>
        </ux-cell-item>
        <ux-cell-item title="箭头边长" :arrow="false">
            <view class="popover-segments">
                <ux-button
                    v-for="(item, index) in sizeList"
                    :key="index"
                    :type="arrowSize == item.value ? 'primary' : 'default'"
                    custom-class="popover-segment"
                    size="small"
                    shape="round"
                    :text="item.label"
                    @click="arrowSize = item.value"
                ></ux-button>
            </view>
        </ux-cell-item>
        <ux-cell-item title="面板圆角" :arrow="false" :border-bottom="false">
            <view class="popover-segments">
                <ux-button
                    v-for="(item, index) in radiusList"
                    :key="index"
                    :type="radius == item.value ? 'primary' : 'default'"
                    custom-class="popover-segment"
                    size="small"
                    shape="round"
                    :text="item.label"
                    @click="radius = item.value"
                ></ux-button>
            </view>
        </ux-cell-item>
    </ux-cell-group>
    <!-- 动作单独成行:和档位胶囊排在一起就看不出哪个是参数、哪个是动作 -->
    <ux-gap :height="12" />
    <view class="popover-row">
        <ux-popover
            v-model:show="show"
            :arrow-size="arrowSize"
            :border-radius="radius"
            :show-arrow="arrowOn"
            :show-border="borderOn"
            placement="bottom-start"
        >
            <ux-button shape="round" size="small" type="primary" text="按当前外观打开" @click="show = true"></ux-button>
            <template #menu>
                <view class="popover-menu">
                    <view class="popover-menu-body">
                        <text class="popover-menu-text">arrow-size = {{ arrowSize }}</text>
                        <text class="popover-menu-text">border-radius = {{ radius }}</text>
                    </view>
                    <!-- 按钮包进带内边距的容器:block 的 100% 只算容器内容宽,直接叠 margin 会顶破面板右边缘 -->
                    <view class="popover-menu-footer">
                        <ux-button block size="small" shape="round" text="关闭" @click="show = false"></ux-button>
                    </view>
                </view>
            </template>
        </ux-popover>
    </view>
</template>

<script setup lang="ts">
const show = ref<boolean>(false);
const arrowOn = ref<boolean>(true);
const borderOn = ref<boolean>(true);
const arrowSize = ref<number>(8);
const radius = ref<number>(8);

const sizeList: { label: string; value: number }[] = [
    { label: '6', value: 6 },
    { label: '8', value: 8 },
    { label: '12', value: 12 }
];

const radiusList: { label: string; value: number }[] = [
    { label: '0', value: 0 },
    { label: '8', value: 8 },
    { label: '16', value: 16 }
];
</script>

<style lang="scss">
/* 触发按钮必须在 flex 容器里:组件只给触发区 align-self: flex-start */
.popover-row {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
}
/* 档位分段贴单元格右侧,项间 6px */
.popover-segments {
    display: flex;
    flex-direction: row;
    align-items: center;
}
.popover-segment {
    margin-left: 6px;
}
.popover-menu {
    min-width: 180px;
}
.popover-menu-body {
    padding: 10px 14px;
}
/* 菜单底部动作区:内边距留给容器,里面的 block 按钮才是安全的 100% 宽 */
.popover-menu-footer {
    padding: 6px 14px 12px 14px;
}
.popover-menu-text {
    /* #ifndef APP */
    display: block;
    /* #endif */
    font-size: 12px;
    line-height: 1.8;
}
</style>

WeChat-style bubble ​

Fixed dark grey background, no border, no shadow, small radius: the arrow merges completely into the panel. Text colour on the dark background is up to the slot content.

View source
vue
<template>
    <view class="popover-row">
        <ux-popover
            v-model:show="show"
            bg-color="#4c4c4c"
            placement="bottom-end"
            :arrow-size="12"
            :border-radius="8"
            :offset="10"
            :show-border="false"
            :show-shadow="false"
        >
            <ux-button shape="round" size="small" type="primary" text="点我展开" @click="show = true"></ux-button>
            <template #menu>
                <view class="popover-wechat-menu">
                    <view v-for="(item, index) in items" :key="index" class="popover-wechat-item" @click="onPick()">
                        <view class="popover-wechat-icon">
                            <ux-icon :name="item.icon" size="22" color="#ffffff"></ux-icon>
                        </view>
                        <text class="popover-wechat-text">{{ item.label }}</text>
                    </view>
                </view>
            </template>
        </ux-popover>
    </view>
</template>

<script setup lang="ts">
const show = ref<boolean>(false);

const items: { icon: string; label: string }[] = [
    { icon: 'chat-fill', label: '发起群聊' },
    { icon: 'account-fill', label: '添加朋友' },
    { icon: 'scan', label: '扫一扫' },
    { icon: 'rmb', label: '收付款' }
];

// 气泡式菜单选完即关
function onPick(): void {
    show.value = false;
}
</script>

<style lang="scss">
.popover-row {
    display: flex;
    flex-direction: row;
    align-items: center;
}
.popover-wechat-menu {
    min-width: 180px;
    padding: 6px 0;
}
.popover-wechat-item {
    display: flex;
    flex-direction: row;
    align-items: center;
    padding: 12px 18px;
}
/* 图标固定占位:同一个横向基线,文字不会因为图标宽窄而错位 */
.popover-wechat-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 22px;
}
/* 深底白字:颜色必须落在 text 自身,App 端 text 不继承父级颜色 */
.popover-wechat-text {
    /* #ifndef APP */
    display: block;
    /* #endif */
    font-size: 14px;
    color: #ffffff;
    margin-left: 12px;
}
</style>

Comment action bubble ​

A row of icon-plus-label actions with the panel above the trigger: no border, no shadow, arrow in the panel colour; each item splits the width evenly via flex.

View source
vue
<template>
    <view class="popover-comment">
        <view class="popover-comment-main">
            <text class="popover-comment-name">前端梦工厂</text>
            <text class="popover-comment-text">TypeScript 2.0 版已发布,欢迎体验。</text>
        </view>
        <ux-popover
            v-model:show="show"
            bg-color="#3d3d3d"
            placement="top-end"
            :arrow-size="14"
            :border-radius="10"
            :offset="10"
            :show-border="false"
            :show-shadow="false"
        >
            <view class="popover-more" @click="show = true">
                <ux-icon name="more-dot-fill" size="20" color="#909399"></ux-icon>
            </view>
            <template #menu>
                <view class="popover-action-bar">
                    <view v-for="(item, index) in actions" :key="index" class="popover-action-item" @click="onPick()">
                        <ux-icon :name="item.icon" size="22" color="#ffffff"></ux-icon>
                        <text class="popover-action-text">{{ item.label }}</text>
                    </view>
                </view>
            </template>
        </ux-popover>
    </view>
</template>

<script setup lang="ts">
const show = ref<boolean>(false);

const actions: { icon: string; label: string }[] = [
    { icon: 'file-text', label: '复制' },
    { icon: 'share', label: '转发' },
    { icon: 'chat', label: '翻译' },
    { icon: 'heart', label: '不喜欢' },
    { icon: 'warning', label: '投诉' }
];

// 操作类气泡点完一项即关闭
function onPick(): void {
    show.value = false;
}
</script>

<style lang="scss">
.popover-comment {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    padding: 12px 14px;
    background-color: #f5f6f7;
    border-radius: 10px;
}
.popover-comment-main {
    flex: 1;
    margin-right: 12px;
}
.popover-comment-name {
    /* #ifndef APP */
    display: block;
    /* #endif */
    font-size: 14px;
    font-weight: bold;
    color: #303133;
}
.popover-comment-text {
    /* #ifndef APP */
    display: block;
    /* #endif */
    font-size: 13px;
    color: #606266;
    margin-top: 4px;
}
.popover-more {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 6px;
}
/* 面板宽度由内容撑开:min-width 撑出五等分段,各操作项 flex: 1 均分 */
.popover-action-bar {
    display: flex;
    flex-direction: row;
    align-items: center;
    min-width: 300px;
    padding: 16px 6px 14px 6px;
}
.popover-action-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
}
/* 深底白字:颜色必须落在 text 自身,App 端 text 不继承父级颜色 */
.popover-action-text {
    /* #ifndef APP */
    display: block;
    /* #endif */
    font-size: 13px;
    color: #ffffff;
    margin-top: 8px;
}
</style>

Mask and closing behaviour ​

Open it, then try tapping the mask, tapping a menu item, or appending several items and reopening to see whether the panel re-fits the trigger.

View source
vue
<template>
    <ux-cell-group>
        <ux-cell-item title="半透明遮罩" :arrow="false">
            <ux-switch v-model="dim"></ux-switch>
        </ux-cell-item>
        <ux-cell-item title="点遮罩关闭" :arrow="false">
            <ux-switch v-model="maskCloseAble"></ux-switch>
        </ux-cell-item>
        <ux-cell-item title="点内容关闭" :arrow="false">
            <ux-switch v-model="contentCloseAble"></ux-switch>
        </ux-cell-item>
        <ux-cell-item title="层级" :arrow="false" :border-bottom="false">
            <view class="popover-segments">
                <ux-button
                    v-for="(item, index) in zIndexList"
                    :key="index"
                    :type="zIndex == item.value ? 'primary' : 'default'"
                    custom-class="popover-segment"
                    size="small"
                    shape="round"
                    :text="item.label"
                    @click="zIndex = item.value"
                ></ux-button>
            </view>
        </ux-cell-item>
    </ux-cell-group>
    <!-- 主动作在前、次动作在后,两个动作统一小尺寸圆角按钮 -->
    <ux-gap :height="12" />
    <view class="popover-row">
        <ux-popover
            v-model:show="show"
            :content-close-able="contentCloseAble"
            :mask-bg-color="dim ? 'rgba(0,0,0,0.45)' : 'rgba(0,0,0,0)'"
            :mask-close-able="maskCloseAble"
            :refresh-key="refreshKey"
            :z-index="zIndex"
            placement="bottom-end"
        >
            <ux-button shape="round" size="small" type="primary" text="打开遮罩演示" @click="show = true"></ux-button>
            <template #menu>
                <view class="popover-menu">
                    <ux-cell-item title="编辑资料" :arrow="false" @click="onPick()"></ux-cell-item>
                    <ux-cell-item v-for="(item, index) in extraLabels" :key="index" :title="item.title" :arrow="false" :border-bottom="!item.last"></ux-cell-item>
                </view>
            </template>
        </ux-popover>
        <ux-button shape="round" size="small" text="追加菜单项" @click="addItem()"></ux-button>
    </view>
</template>

<script setup lang="ts">
const show = ref<boolean>(false);
const dim = ref<boolean>(true);
const maskCloseAble = ref<boolean>(true);
const contentCloseAble = ref<boolean>(false);
// 留空取主题 dropdown 档,也能传具体数值
const zIndex = ref<string>('');
const refreshKey = ref<number>(0);
const extraItems = ref<number>(0);

const zIndexList: { label: string; value: string }[] = [
    { label: '默认', value: '' },
    { label: '2000', value: '2000' }
];

// 最后一项的分隔线由数据层给:模板里不读数组长度,否则原生端可能读到越界值
const extraLabels = computed<{ title: string; last: boolean }[]>((): { title: string; last: boolean }[] => {
    const list: { title: string; last: boolean }[] = [];
    for (let i = 0; i < extraItems.value; i++) {
        list.push({ title: '新增菜单项 ' + (i + 1).toString(), last: i == extraItems.value - 1 });
    }
    return list;
});

// 菜单内容尺寸变了要改 refreshKey:组件不会自己重新测位,面板会停在旧位置
function addItem(): void {
    extraItems.value = extraItems.value + 1;
    refreshKey.value = refreshKey.value + 1;
}

// content-close-able 为 false 时组件不关,由调用方自己决定
function onPick(): void {
    show.value = false;
}
</script>

<style lang="scss">
.popover-row {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
}
.popover-segments {
    display: flex;
    flex-direction: row;
    align-items: center;
}
.popover-segment {
    margin-left: 6px;
}
.popover-menu {
    min-width: 180px;
}
</style>

Usage notes ​

Check these four points before wiring it up; they cover most wrong-position, cannot-close and does-not-follow problems.

API ​

Props ​

PropertyDescriptionTypeDefaultVersion
customStyleCustom style for the panel containerstring | JsonObject--
customClassCustom style class for the panel containerstring''-
placementPanel placementPopoverPlacement'bottom-end'-
offsetGap between the panel and the trigger elementstring | number8-
showArrowWhether to show the arrowbooleantrue-
arrowSizeArrow edge lengthstring | number8-
bgColorPanel background colour; empty falls back to the theme whitestring''-
borderRadiusPanel corner radiusstring | number8-
showBorderWhether the panel draws its outline: same recipe as Ant Design / Element Plus, a 1px stroke defines the edgebooleantrue-
showShadowWhether the panel draws its diffuse shadow; turn off for a flat solid bubblebooleantrue-
maskBgColorMask background colour, fully transparent by defaultstring'rgba(0,0,0,0)'-
maskCloseAbleWhether tapping the mask closes the panelbooleantrue-
contentCloseAbleWhether tapping the panel content closes itbooleanfalse-
durationShow/hide animation durationstring | number200-
zIndexStacking level; empty falls back to the theme levelstring | number''-
refreshKeyRe-measure switch for content size changesstring | number0-

Events ​

EventDescriptionParamsVersion
openOpening starts (panel mounted, before measurement)--
closeClosing starts (final state written back to the entry state)--
clickMaskThe mask layer was tapped--

Slots ​

SlotDescriptionVersion
defaultTrigger area content (required)-
menuPanel content-

Methods ​

Call them on the ux-popover instance obtained via ref:

MethodDescriptionParamsReturns
openOpen the panel and run the measurement pass again; same as setting v-model:show to true--
closePlay the exit animation and unmount the panel; same as setting v-model:show to false--
refreshRe-measure the trigger element and the panel and rewrite the geometry without changing visibility--
vue
<template>
    <ux-popover ref="popoverRef" v-model:show="show" placement="bottom-end">
        <ux-button text="打开" @click="popoverRef?.open()"></ux-button>
        <template #menu>
            <view class="popover-menu">
                <ux-cell-item title="刷新位置" :arrow="false" @click="popoverRef?.refresh()"></ux-cell-item>
            </view>
        </template>
    </ux-popover>
</template>

Notes ​

  1. The panel is measured once on open and does not follow scrolling. On Web any scroll while it is open closes it automatically (page scroll and scrolling inside a scroll-view alike), so it can never drift out of place. App and Mini Program do not listen to page scrolling; they rely on the mask to block touch scrolling instead, so a programmatic scrollTo leaves the panel at its old position - call close() or refresh() yourself.
  2. The component wraps the trigger in a view with align-self: flex-start, which is what lets it shrink to the content width. In a non-flex parent it degrades to full-row width, so the measurement basis widens with it. Keep the trigger inside a display: flex container.
  3. Content that changes size after the first paint (image load, appended list items, conditional rendering) is not re-measured automatically. Change refreshKey to trigger a re-measure, or call refresh() imperatively.
  4. contentCloseAble defaults to false: tapping a menu item does not close the panel by itself. Set v-model:show to false in your own click handler, otherwise the panel stays on screen.
  5. maskBgColor is fully transparent by default, which means the mask hides nothing and only captures clicks. To make the empty area tap-to-close, just keep maskCloseAble at its default; no extra styling is needed.
  6. The panel ships with a 1px outline plus a single shadow: the outline uses the theme border colour to define the edge, while 0 4px 12px only lifts it off the page. App does not guarantee box-shadow, in which case the outline alone carries the boundary and the arrow is stroked as well, so nothing disappears. For a flat solid bubble (WeChat style) set show-border="false" + show-shadow="false" together with bg-color.
  7. A dark background does not invert your text: the component owns the panel colour while the text colour comes from the #menu slot, so give every text its own white colour (on App, text does not inherit color from its parent).
  8. The arrow is painted above the panel (higher z-index): it only strokes its two exposed bevelled edges, while the part reaching inside the panel is covered by the panel's own background, so no extra line is drawn inside.
  9. When a block element inside the menu (ux-button or anything else) should fill the panel width, put the horizontal padding on a wrapper (for example padding: 0 14px) instead of adding margin to the block element: block is width: 100% of the content box, so extra margins push it past the panel's right edge and it looks clipped.
  10. Overflow handling has exactly two rules: vertically the panel flips to the other side first, and only clamps to the 8px inset if it still overflows; horizontally it never flips, it only clamps the left edge (when the panel is wider than the available width it is squeezed to viewport width minus 16). A trigger sitting near a screen edge therefore gets its panel pushed inwards with the arrow following, rather than a flipped panel. The panel is position: fixed but lives where the component sits, so an ancestor with transform / filter becomes its containing block - coordinates are then relative to that ancestor and the ancestor's overflow: hidden may clip it; use an overlay built for that case (such as ux-popup) instead.