Drawer.vue
3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<script setup>
/**
* Drawer —— 右侧抽屉(v-model:open)。用于详情类内容(如调用日志的一条记录)。
* ESC 关闭、backdrop 关闭、焦点陷阱 —— 焦点陷阱与 `Modal.vue` 共用 `use-focus-trap.js`
* 同一份实现;本组件只多两件自己的事:从右侧滑出、宽度可调。
*/
import { ref } from 'vue'
import { useFocusTrap } from '../composables/use-focus-trap.js'
const props = defineProps({
/** 受控开关。 */
open: { type: Boolean, default: false },
/** 标题文案(不传则不渲染标题栏文字,关闭按钮仍在)。 */
title: { type: String, default: '' },
/** 抽屉宽度(任意 CSS 长度)。 */
width: { type: String, default: '480px' },
})
const emit = defineEmits(['update:open', 'close'])
/** @type {import('vue').Ref<HTMLElement|null>} 抽屉根元素,用于焦点陷阱。 */
const panelEl = ref(null)
/**
* 关闭:同步 v-model 并发 close 事件。
* @returns {void}
*/
function close() {
emit('update:open', false)
emit('close')
}
// ESC 关闭 + Tab 焦点循环 + 打开聚焦 / 关闭还焦点,与 Modal 共用同一份实现。
useFocusTrap(panelEl, () => props.open, close)
</script>
<template>
<Teleport to="body">
<div
v-if="open"
class="drama-drawer__backdrop"
@click.self="close"
>
<aside
ref="panelEl"
class="drama-drawer__panel"
:style="{ width }"
role="dialog"
aria-modal="true"
tabindex="-1"
>
<header class="drama-drawer__header">
<h2 class="drama-drawer__title">
{{ title }}
</h2>
<button
class="drama-drawer__close"
type="button"
aria-label="Close"
@click="close"
>
×
</button>
</header>
<div class="drama-drawer__body">
<slot />
</div>
<footer
v-if="$slots.footer"
class="drama-drawer__footer"
>
<slot name="footer" />
</footer>
</aside>
</div>
</Teleport>
</template>
<style scoped>
.drama-drawer__backdrop {
position: fixed;
inset: 0;
display: flex;
justify-content: flex-end;
background-color: var(--drama-color-overlay);
z-index: 1000;
}
.drama-drawer__panel {
display: flex;
flex-direction: column;
max-width: 100%;
height: 100%;
background-color: var(--drama-color-surface);
color: var(--drama-color-text);
border-left: 1px solid var(--drama-color-border);
box-shadow: var(--drama-shadow-lg);
outline: none;
}
.drama-drawer__header {
display: flex;
align-items: center;
justify-content: space-between;
gap: var(--drama-space-md);
padding: var(--drama-space-lg) var(--drama-space-xl);
border-bottom: 1px solid var(--drama-color-border);
}
.drama-drawer__title {
margin: 0;
font-family: var(--drama-font-family-heading);
font-size: var(--drama-font-size-lg);
font-weight: var(--drama-font-weight-bold);
}
.drama-drawer__close {
background: transparent;
border: none;
color: var(--drama-color-text-muted);
font-size: var(--drama-font-size-xl);
line-height: 1;
cursor: pointer;
padding: 0 var(--drama-space-sm);
}
.drama-drawer__body {
flex: 1;
overflow: auto;
padding: var(--drama-space-xl);
}
.drama-drawer__footer {
display: flex;
justify-content: flex-end;
gap: var(--drama-space-md);
padding: var(--drama-space-lg) var(--drama-space-xl);
border-top: 1px solid var(--drama-color-border);
}
</style>