|
| 1 | +<template> |
| 2 | + <transition |
| 3 | + name="el-drawer-fade" |
| 4 | + @after-enter="afterEnter" |
| 5 | + @after-leave="afterLeave"> |
| 6 | + <div |
| 7 | + class="el-drawer__wrapper" |
| 8 | + tabindex="-1" |
| 9 | + v-show="visible"> |
| 10 | + <div |
| 11 | + class="el-drawer__container" |
| 12 | + :class="visible && 'el-drawer__open'" |
| 13 | + @click.self="handleWrapperClick" |
| 14 | + role="document" |
| 15 | + tabindex="-1"> |
| 16 | + <div |
| 17 | + aria-modal="true" |
| 18 | + aria-labelledby="el-drawer__title" |
| 19 | + :aria-label="title" |
| 20 | + class="el-drawer" |
| 21 | + :class="[direction, customClass]" |
| 22 | + :style="isHorizontal ? `width: ${size}` : `height: ${size}`" |
| 23 | + ref="drawer" |
| 24 | + role="dialog" |
| 25 | + tabindex="-1" |
| 26 | + > |
| 27 | + <header class="el-drawer__header" id="el-drawer__title" v-if="withHeader"> |
| 28 | + <slot name="title"> |
| 29 | + <span role="heading" tabindex="0" :title="title">{{ title }}</span> |
| 30 | + </slot> |
| 31 | + <button |
| 32 | + :aria-label="`close ${title || 'drawer'}`" |
| 33 | + class="el-drawer__close-btn" |
| 34 | + type="button" |
| 35 | + v-if="showClose" |
| 36 | + @click="closeDrawer"> |
| 37 | + <i class="el-dialog__close el-icon el-icon-close"></i> |
| 38 | + </button> |
| 39 | + </header> |
| 40 | + <section class="el-drawer__body" v-if="rendered"> |
| 41 | + <slot></slot> |
| 42 | + </section> |
| 43 | + </div> |
| 44 | + </div> |
| 45 | + </div> |
| 46 | + </transition> |
| 47 | +</template> |
| 48 | + |
| 49 | +<script lang="ts"> |
| 50 | +// @ts-ignore |
| 51 | +import Popup from '@/utils/popup' |
| 52 | +// @ts-ignore |
| 53 | +import Utils from '@/utils/aria-utils' |
| 54 | +import { ComponentInternalInstance, computed, defineComponent, getCurrentInstance, nextTick, onMounted, onUnmounted, Prop, ref, watch } from 'vue' |
| 55 | +
|
| 56 | +export default defineComponent({ |
| 57 | + name: 'ElDrawer', |
| 58 | + mixins: [Popup], |
| 59 | + emits: ['open', 'opened', 'close', 'closed', 'update:visible'], |
| 60 | + props: { |
| 61 | + appendToBody: { |
| 62 | + type: Boolean, |
| 63 | + default: false |
| 64 | + }, |
| 65 | + beforeClose: { |
| 66 | + type: Function |
| 67 | + }, |
| 68 | + customClass: { |
| 69 | + type: String, |
| 70 | + default: '' |
| 71 | + }, |
| 72 | + closeOnPressEscape: { |
| 73 | + type: Boolean, |
| 74 | + default: true |
| 75 | + }, |
| 76 | + destroyOnClose: { |
| 77 | + type: Boolean, |
| 78 | + default: false |
| 79 | + }, |
| 80 | + modal: { |
| 81 | + type: Boolean, |
| 82 | + default: true |
| 83 | + }, |
| 84 | + direction: { |
| 85 | + type: String, |
| 86 | + default: 'rtl', |
| 87 | + validator(val: string) { |
| 88 | + return ['ltr', 'rtl', 'ttb', 'btt'].indexOf(val) !== -1; |
| 89 | + } |
| 90 | + } as Prop<String>, |
| 91 | + modalAppendToBody: { |
| 92 | + type: Boolean, |
| 93 | + default: true |
| 94 | + }, |
| 95 | + showClose: { |
| 96 | + type: Boolean, |
| 97 | + default: true |
| 98 | + }, |
| 99 | + size: { |
| 100 | + type: String, |
| 101 | + default: '30%' |
| 102 | + }, |
| 103 | + title: { |
| 104 | + type: String, |
| 105 | + default: '' |
| 106 | + }, |
| 107 | + visible: { |
| 108 | + type: Boolean |
| 109 | + }, |
| 110 | + wrapperClosable: { |
| 111 | + type: Boolean, |
| 112 | + default: true |
| 113 | + }, |
| 114 | + withHeader: { |
| 115 | + type: Boolean, |
| 116 | + default: true |
| 117 | + } |
| 118 | + }, |
| 119 | +
|
| 120 | + setup(props, { emit }) { |
| 121 | + const closed = ref(false) |
| 122 | + const prevActiveElement = ref(null as Element | HTMLElement | null) |
| 123 | + const rendered = ref(false) |
| 124 | + const isHorizontal = computed(() => { |
| 125 | + return props.direction === 'rtl' || props.direction === 'ltr' |
| 126 | + }) |
| 127 | + const instance = getCurrentInstance() as ComponentInternalInstance |
| 128 | + watch(() => props.visible, (val) => { |
| 129 | + const el = instance.vnode.el as HTMLElement |
| 130 | + if (val) { |
| 131 | + closed.value = false |
| 132 | + emit('open') |
| 133 | + if (props.appendToBody) { |
| 134 | + document.body.appendChild(el) |
| 135 | + } |
| 136 | + prevActiveElement.value = document.activeElement |
| 137 | + nextTick(() => { |
| 138 | + Utils.focusFirstDescendant(instance.refs.drawer) |
| 139 | + }) |
| 140 | + } else { |
| 141 | + if (!closed.value) emit('close') |
| 142 | + nextTick(() => { |
| 143 | + if (prevActiveElement.value) { |
| 144 | + (prevActiveElement.value as HTMLElement)?.focus() |
| 145 | + } |
| 146 | + }) |
| 147 | + } |
| 148 | + }) |
| 149 | +
|
| 150 | + const afterEnter = () => { |
| 151 | + emit('opened') |
| 152 | + } |
| 153 | + const afterLeave = () => { |
| 154 | + emit('closed') |
| 155 | + } |
| 156 | + const hide = (cancel?:boolean) => { |
| 157 | + if (cancel !== false) { |
| 158 | + emit('update:visible', false); |
| 159 | + emit('close'); |
| 160 | + if (props.destroyOnClose === true) { |
| 161 | + rendered.value = false |
| 162 | + } |
| 163 | + closed.value = true; |
| 164 | + } |
| 165 | + } |
| 166 | + const handleWrapperClick = () => { |
| 167 | + if (props.wrapperClosable) { |
| 168 | + closeDrawer() |
| 169 | + } |
| 170 | + } |
| 171 | + const closeDrawer = () => { |
| 172 | + if (typeof props.beforeClose === 'function') { |
| 173 | + props.beforeClose(hide) |
| 174 | + } else { |
| 175 | + hide() |
| 176 | + } |
| 177 | + } |
| 178 | + const handleClose = () => { |
| 179 | + // This method here will be called by PopupManger, when the `closeOnPressEscape` was set to true |
| 180 | + // pressing `ESC` will call this method, and also close the drawer. |
| 181 | + // This method also calls `beforeClose` if there was one. |
| 182 | + closeDrawer() |
| 183 | + } |
| 184 | + onMounted(() => { |
| 185 | + if (props.visible) { |
| 186 | + rendered.value = true |
| 187 | + open() |
| 188 | + } |
| 189 | + }) |
| 190 | + onUnmounted(() => { |
| 191 | + const el = instance.vnode.el |
| 192 | + if (props.appendToBody && el?.parentNode) { |
| 193 | + el.parentNode.removeChild(el) |
| 194 | + } |
| 195 | + }) |
| 196 | +
|
| 197 | + return { |
| 198 | + rendered, |
| 199 | + isHorizontal, |
| 200 | + afterEnter, |
| 201 | + afterLeave, |
| 202 | + handleWrapperClick, |
| 203 | + closeDrawer, |
| 204 | + handleClose |
| 205 | + } |
| 206 | + } |
| 207 | +}) |
| 208 | +</script> |
| 209 | + |
| 210 | +<style lang="scss"> |
| 211 | +@import 'theme/drawer.scss'; |
| 212 | +</style> |
0 commit comments