Skip to content

ActionSheet 动作面板

介绍

底部弹起的模态面板,包含与当前情境相关的多个选项。

引入

通过以下方式来引入组件。

js
import { createApp } from 'vue';
import { ActionSheet } from '@szhn/dh-design-mobile';

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

代码演示

基础用法

动作面板通过 actions 属性来定义选项,actions 属性是一个由对象构成的数组,数组中的每个对象配置一列,对象格式见文档下方表格。

html
<van-cell is-link title="基础用法" @click="show = true" />
<van-action-sheet v-model:show="show" :actions="actions" @select="onSelect" />
js
import { ref } from 'vue';
import { showToast } from '@szhn/dh-design-mobile';

export default {
  setup() {
    const show = ref(false);
    const actions = [
      { name: '选项一' },
      { name: '选项二' },
      { name: '选项三' },
    ];
    const onSelect = (item) => {
      // 默认情况下点击选项时不会自动收起
      // 可以通过 close-on-click-action 属性开启自动收起
      show.value = false;
      showToast(item.name);
    };

    return {
      show,
      actions,
      onSelect,
    };
  },
};

展示图标

使用 actionsicon 字段可以为选项设置图标。

html
<van-cell is-link title="展示图标" @click="show = true" />
<van-action-sheet v-model:show="show" :actions="actions" @select="onSelect" />
js
import { ref } from 'vue';
import { showToast } from '@szhn/dh-design-mobile';

export default {
  setup() {
    const show = ref(false);
    const actions = [
      { name: '选项一', icon: 'cart-o' },
      { name: '选项二', icon: 'shop-o' },
      { name: '选项三', icon: 'star-o' },
    ];
    const onSelect = (item) => {
      show.value = false;
      showToast(item.name);
    };

    return {
      show,
      actions,
      onSelect,
    };
  },
};

展示取消按钮

设置 cancel-text 属性后,会在底部展示取消按钮,点击后关闭当前面板并触发 cancel 事件。

html
<van-action-sheet
  v-model:show="show"
  :actions="actions"
  cancel-text="取消"
  close-on-click-action
  @cancel="onCancel"
/>
js
import { ref } from 'vue';
import { showToast } from '@szhn/dh-design-mobile';

export default {
  setup() {
    const show = ref(false);
    const actions = [
      { name: '选项一' },
      { name: '选项二' },
      { name: '选项三' },
    ];
    const onCancel = () => showToast('取消');

    return {
      show,
      actions,
      onCancel,
    };
  },
};

展示描述信息

通过 description 可以在菜单顶部显示描述信息,通过选项的 subname 属性可以在选项文字的右侧展示描述信息。

html
<van-action-sheet
  v-model:show="show"
  :actions="actions"
  cancel-text="取消"
  description="这是一段描述信息"
  close-on-click-action
/>
js
import { ref } from 'vue';

export default {
  setup() {
    const show = ref(false);
    const actions = [
      { name: '选项一' },
      { name: '选项二' },
      { name: '选项三', subname: '描述信息' },
    ];

    return {
      show,
      actions,
    };
  },
};

选项状态

可以通过 loadingdisabled 将选项设置为加载状态或禁用状态,或者通过color设置选项的颜色

html
<van-action-sheet
  v-model:show="show"
  :actions="actions"
  cancel-text="取消"
  close-on-click-action
/>
js
import { ref } from 'vue';

export default {
  setup() {
    const show = ref(false);
    const actions = [
      { name: '着色选项', color: '#ee0a24' },
      { name: '禁用选项', disabled: true },
      { name: '加载选项', loading: true },
    ];

    return {
      show,
      actions,
    };
  },
};

自定义面板

通过插槽可以自定义面板的展示内容,同时可以使用title属性展示标题栏

html
<van-action-sheet v-model:show="show" title="标题">
  <div class="content">内容</div>
</van-action-sheet>

<style>
  .content {
    padding: 16px 16px 160px;
  }
</style>

API

Props

参数说明类型默认值
v-model:show是否显示动作面板booleanfalse
actions面板选项列表ActionSheetAction[][]
title顶部标题string-
cancel-text取消按钮文字string-
description选项上方的描述信息string-
closeable是否显示关闭图标booleantrue
close-icon关闭图标名称或图片链接,等同于 Icon 组件的 name 属性stringcross
duration动画时长,单位秒,设置为 0 可以禁用动画number | string0.3
z-index将面板的 z-index 层级设置为一个固定值number | string2000+
round是否显示圆角booleantrue
overlay是否显示遮罩层booleantrue
overlay-class自定义遮罩层类名string | Array | object-
overlay-style自定义遮罩层样式object-
lock-scroll是否锁定背景滚动booleantrue
lazy-render是否在显示弹层时才渲染节点booleantrue
close-on-popstate是否在页面回退时自动关闭booleantrue
close-on-click-action是否在点击选项后关闭booleanfalse
close-on-click-overlay是否在点击遮罩层后关闭booleantrue
safe-area-inset-bottom是否开启底部安全区适配booleantrue
teleport指定挂载的节点,等同于 Teleport 组件的 to 属性string | Element-
before-close关闭前的回调函数,返回 false 可阻止关闭,支持返回 Promise(action: string) => boolean | Promise<boolean>-

Action 数据结构

actions 属性是一个由对象构成的数组,数组中的每个对象配置一列,对象可以包含以下值:

键名说明类型
name标题string
subname二级标题string
color选项文字颜色string
icon v4.8.6选项图标名称或图片链接string
className为对应列添加额外的 classstring | Array | object
loading是否为加载状态boolean
disabled是否为禁用状态boolean
callback点击时触发的回调函数action: ActionSheetAction

Events

事件名说明回调参数
select点击选项时触发,禁用或加载状态下不会触发action: ActionSheetAction, index: number
cancel点击取消按钮时触发-
open打开面板时触发-
close关闭面板时触发-
opened打开面板且动画结束后触发-
closed关闭面板且动画结束后触发-
click-overlay点击遮罩层时触发event: MouseEvent

Slots

名称说明参数
default自定义面板的展示内容-
description自定义描述文案-
cancel自定义取消按钮内容-
action自定义选项内容{ action: ActionSheetAction, index: number }

类型定义

组件导出以下类型定义:

ts
import type { ActionSheetProps, ActionSheetAction } from '@szhn/dh-design-mobile';

主题定制

样式变量

组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 ConfigProvider 组件

名称默认值描述
--van-action-sheet-max-height80%动作面板的最大高度
--van-action-sheet-header-height48px动作面板标题的高度
--van-action-sheet-header-font-sizevar(--van-font-size-lg)动作面板标题的字体大小
--van-action-sheet-description-colorvar(--van-text-color-2)描述信息的文字颜色
--van-action-sheet-description-font-sizevar(--van-font-size-md)描述信息的字体大小
--van-action-sheet-description-line-heightvar(--van-line-height-md)描述信息的行高
--van-action-sheet-item-backgroundvar(--van-background-2)选项的背景色
--van-action-sheet-item-font-sizevar(--van-font-size-lg)选项的字体大小
--van-action-sheet-item-line-heightvar(--van-line-height-lg)选项的行高
--van-action-sheet-item-text-colorvar(--van-text-color)选项的文字颜色
--van-action-sheet-item-disabled-text-colorvar(--van-text-color-3)禁用状态下选项的文字颜色
--van-action-sheet-item-icon-size18px选项图标的大小
--van-action-sheet-item-icon-margin-rightvar(--van-padding-xs)选项图标的右外边距
--van-action-sheet-subname-colorvar(--van-text-color-2)副标题的文字颜色
--van-action-sheet-subname-font-sizevar(--van-font-size-sm)副标题的字体大小
--van-action-sheet-subname-line-heightvar(--van-line-height-sm)副标题的行高
--van-action-sheet-close-icon-size22px关闭图标的大小
--van-action-sheet-close-icon-colorvar(--van-gray-5)关闭图标的颜色
--van-action-sheet-close-icon-padding0 var(--van-padding-md)关闭图标的内边距
--van-action-sheet-cancel-text-colorvar(--van-gray-7)取消按钮的文字颜色
--van-action-sheet-cancel-padding-topvar(--van-padding-xs)取消按钮的上内边距
--van-action-sheet-cancel-padding-colorvar(--van-background)取消按钮上方间隙的颜色
--van-action-sheet-loading-icon-size22px加载图标的大小