Skip to content

Drawer 抽屉

抽屉组件提供了一个从屏幕边缘滑出的面板,用于展示额外信息或操作,而不离开当前页面。

基础用法

使用 v-model 绑定一个响应式变量控制抽屉的显示和隐藏。可以通过 title 属性设置抽屉标题。使用默认插槽传入抽屉主体内容。

<template>
    <div class="container">
     <ym-drawer v-model="show">
        <p>这是弹框内容</p>
     </ym-drawer>
     <ym-button type="primary" @click="show = true" title="这是弹框标题">打开抽屉</ym-button>
    </div>
  </template>
  
  <script setup>
 import { ref } from 'vue'
 const show = ref(false)
  </script>

自定义头部和底部

使用 headerfooter 具名插槽来自定义头部和底部内容。 在不需要头部或者底部时,可以设置 headerfooter 属性为 false 来隐藏头部和底部显示。

<template>
    <div class="container">
     <ym-drawer v-model="show">
        <template #header>
          <div>
            <h2>这是自定义标题</h2>
          </div>
        </template>
        <template #footer>
          <ym-button type="primary" @click="show = false">关闭</ym-button>
        </template>
        <p>这是弹框内容</p>
     </ym-drawer>
     <ym-button type="primary" @click="show = true" title="这是弹框标题">打开抽屉</ym-button>
    </div>
  </template>
  
  <script setup>
 import { ref } from 'vue'
 const show = ref(false)
  </script>

自定义宽度和高度

可以通过 widthheight 属性来设置抽屉的宽度和高度。这些属性可以是一个 string 的数值类型,还可以传入一个百分比字符串或者一个带单位的字符串。 widthheight 属性默认值是 30%width只有水平方向的抽屉生效,height只有垂直方向的抽屉生效。

<template>
    <div class="container">
     <div class="item">
        <ym-drawer v-model="show1">
        <p>这是弹框内容</p>
     </ym-drawer>
     <ym-button type="primary" @click="show1 = true" title="这是弹框标题">默认(30%)</ym-button>
     </div>
     <div class="item">
        <ym-drawer v-model="show2" width="50%">
        <p>这是弹框内容</p>
     </ym-drawer>
     <ym-button type="success" @click="show2 = true" title="这是弹框标题">50%</ym-button>
     </div>
     <div class="item">
        <ym-drawer v-model="show3" width="500px">
        <p>这是弹框内容</p>
     </ym-drawer>
     <ym-button type="info" @click="show3 = true" title="这是弹框标题">500px</ym-button>
     </div>
    </div>
  </template>
  
  <script setup>
 import { ref } from 'vue'
 const show1 = ref(false)
 const show2 = ref(false)
 const show3 = ref(false)
  </script>
  <style scoped>

.container {
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.item {
  display: flex;
  flex-direction: column;
  align-items: center;
}
</style>

关闭前的回调

使用 beforeClose 属性来设置关闭前的回调函数,在回调函数中可以执行一些逻辑操作,比如确认用户是否真的要关闭抽屉,如果用户确认关闭,则调用 done 函数来关闭抽屉。

<template>
    <div class="container">
     <ym-drawer v-model="show" :beforeClose="beforeClose">
        <p>这是弹框内容</p>
     </ym-drawer>
     <ym-button type="primary" @click="show = true" title="这是弹框标题">打开抽屉</ym-button>
    </div>
  </template>
  
  <script setup>
 import { ref } from 'vue'
 import { YmMessageBox,YmMessage } from 'guyan-ym-ui'
 const show = ref(false)


const beforeClose =(done) => {
  
    YmMessageBox.confirm('确定关闭吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        done()
      }).catch(() => {
        YmMessage.info('取消关闭')
        show.value = true
    }
    )
}
  </script>

抽屉位置

可以使用 position 属性来设置抽屉的位置,默认是 right,还可以设置为 lefttopbottom。 只有当 position 属性为 leftright 时,width 属性才会生效,当 position 属性为 topbottom 时,height 属性才会生效。

<template>
    <div class="container">
        <div class="item">
            <ym-drawer v-model="show1">
            <p>这是弹框内容</p>
        </ym-drawer>
        <ym-button type="primary" @click="show1 = true" title="这是弹框标题">默认(right)</ym-button>
        </div>
        <div class="item">
            <ym-drawer v-model="show2" position="top">
            <p>这是弹框内容</p>
        </ym-drawer>
        <ym-button type="success" @click="show2 = true" title="这是弹框标题">top</ym-button>
        </div>
        <div class="item">
            <ym-drawer v-model="show3" position="left">
            <p>这是弹框内容</p>
        </ym-drawer>
        <ym-button type="info" @click="show3 = true" title="这是弹框标题">left</ym-button>
        </div>
        <div class="item">
            <ym-drawer v-model="show4" position="bottom">
            <p>这是弹框内容</p>
        </ym-drawer>
        <ym-button type="warning" @click="show4 = true" title="这是弹框标题">bottom</ym-button>
        </div>
    </div>
  </template>
  
  <script setup>
 import { ref } from 'vue'
 const show1 = ref(false)
 const show2 = ref(false)
 const show3 = ref(false)
 const show4 = ref(false)
  </script>
  <style scoped>

.container {
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.item {
  display: flex;
  flex-direction: column;
  align-items: center;
}
</style>

Drawer API

Props

NameDescriptionTypeDefault
modelValue是否显示booleanfalse
title标题string-
width宽度stringstring
height高度stringstring
modal是否显示遮罩booleantrue
modalClass遮罩样式string-
closeOnClickModal点击遮罩是否关闭booleantrue
closeOnPressEscape按下ESC是否关闭booleantrue
showClose是否显示关闭按钮booleantrue
beforeClose关闭前的回调(done: () => void) => void-
closeIcon关闭图标string-
zIndex层级number2000
customClass自定义类名string-
appendTo挂载节点string-
header是否显示头部booleantrue
footer是否显示底部booleantrue

Events

NameDescriptionType
open打开抽屉时触发-
close关闭抽屉时触发-
opened打开动画结束时触发-
closed关闭动画结束时触发-
update:modelValuemodelValue 改变时触发(value: boolean) => void

Slots

NameDescriptionSub Component
default默认插槽-
header头部插槽-
footer底部插槽-