@veltra/desktop 1.13.3 → 1.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/dialog/dialog.js +47 -19
- package/dist/components/dialog/dialog.js.map +1 -1
- package/dist/components/drawer/drawer.js +78 -43
- package/dist/components/drawer/drawer.js.map +1 -1
- package/dist/components/drawer/style.d.ts +1 -1
- package/dist/components/drawer/style.js +0 -1
- package/dist/components/drawer/style2.css +139 -15
- package/dist/components/multi-select/multi-select.js +6 -2
- package/dist/components/multi-select/multi-select.js.map +1 -1
- package/dist/components/multi-tree-select/multi-tree-select.js +10 -3
- package/dist/components/multi-tree-select/multi-tree-select.js.map +1 -1
- package/dist/components/slider/slider.js +1 -1
- package/dist/components/slider/slider.js.map +1 -1
- package/dist/composables/use-overlay.js +171 -0
- package/dist/composables/use-overlay.js.map +1 -0
- package/dist/style.d.ts +0 -1
- package/dist/types/dialog.d.ts +4 -0
- package/dist/types/drawer.d.ts +4 -0
- package/package.json +1 -1
- package/src/components/dialog/__test__/dialog.test.ts +152 -0
- package/src/components/dialog/dialog.vue +50 -22
- package/src/components/drawer/__test__/drawer.test.ts +169 -5
- package/src/components/drawer/drawer.vue +67 -33
- package/src/components/drawer/style.scss +63 -53
- package/src/components/drawer/style.ts +0 -1
- package/src/components/multi-select/__test__/multi-select.test.ts +47 -0
- package/src/components/multi-select/multi-select.vue +6 -2
- package/src/components/multi-tree-select/__test__/multi-tree-select.test.ts +41 -0
- package/src/components/multi-tree-select/multi-tree-select.vue +12 -3
- package/src/components/slider/slider.vue +1 -1
- package/src/composables/use-overlay.ts +238 -0
- package/src/types/dialog.ts +4 -0
- package/src/types/drawer.ts +4 -0
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from 'vite-plus/test'
|
|
4
|
+
import { createApp, h, nextTick, ref } from 'vue'
|
|
5
|
+
|
|
6
|
+
import UDialog from '../dialog.vue'
|
|
7
|
+
|
|
8
|
+
function mountDialog(props: Record<string, unknown> = {}) {
|
|
9
|
+
const host = document.createElement('div')
|
|
10
|
+
document.body.appendChild(host)
|
|
11
|
+
|
|
12
|
+
const model = ref(true)
|
|
13
|
+
const app = createApp({
|
|
14
|
+
render: () =>
|
|
15
|
+
h(
|
|
16
|
+
UDialog,
|
|
17
|
+
{
|
|
18
|
+
...props,
|
|
19
|
+
modelValue: model.value,
|
|
20
|
+
'onUpdate:modelValue': (value: boolean) => {
|
|
21
|
+
model.value = value
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
() => '弹框内容'
|
|
25
|
+
)
|
|
26
|
+
})
|
|
27
|
+
app.mount(host)
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
host,
|
|
31
|
+
model,
|
|
32
|
+
panel: () => document.body.querySelector<HTMLElement>('.u-dialog'),
|
|
33
|
+
overlay: () => document.body.querySelector<HTMLElement>('.u-dialog-overlay'),
|
|
34
|
+
unmount() {
|
|
35
|
+
app.unmount()
|
|
36
|
+
host.remove()
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
afterEach(() => {
|
|
42
|
+
document.body.style.overflow = ''
|
|
43
|
+
document.body.style.paddingRight = ''
|
|
44
|
+
document.body.innerHTML = ''
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
describe('UDialog 无障碍属性', () => {
|
|
48
|
+
it('面板带 role="dialog"、aria-modal="true" 与 tabindex="-1"', async () => {
|
|
49
|
+
const ctx = mountDialog({ title: '提示' })
|
|
50
|
+
await nextTick()
|
|
51
|
+
|
|
52
|
+
const panel = ctx.panel()!
|
|
53
|
+
const title = panel.querySelector<HTMLElement>('.u-dialog__title')!
|
|
54
|
+
|
|
55
|
+
expect(panel.getAttribute('role')).toBe('dialog')
|
|
56
|
+
expect(panel.getAttribute('aria-modal')).toBe('true')
|
|
57
|
+
expect(panel.getAttribute('tabindex')).toBe('-1')
|
|
58
|
+
expect(panel.getAttribute('aria-labelledby')).toBe(title.id)
|
|
59
|
+
|
|
60
|
+
ctx.unmount()
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('modal 为 false 时不输出 aria-modal', async () => {
|
|
64
|
+
const ctx = mountDialog({ modal: false })
|
|
65
|
+
await nextTick()
|
|
66
|
+
|
|
67
|
+
expect(ctx.panel()!.getAttribute('aria-modal')).toBeNull()
|
|
68
|
+
|
|
69
|
+
ctx.unmount()
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('modal 为 false 时不锁滚动、不抢焦点', async () => {
|
|
73
|
+
const trigger = document.createElement('button')
|
|
74
|
+
document.body.appendChild(trigger)
|
|
75
|
+
trigger.focus()
|
|
76
|
+
|
|
77
|
+
const ctx = mountDialog({ modal: false })
|
|
78
|
+
await nextTick()
|
|
79
|
+
await nextTick()
|
|
80
|
+
|
|
81
|
+
expect(document.body.style.overflow).toBe('')
|
|
82
|
+
expect(document.activeElement).toBe(trigger)
|
|
83
|
+
|
|
84
|
+
ctx.unmount()
|
|
85
|
+
trigger.remove()
|
|
86
|
+
})
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
describe('UDialog 遮罩通用行为', () => {
|
|
90
|
+
it('按 Esc 关闭并同步 v-model', async () => {
|
|
91
|
+
const ctx = mountDialog()
|
|
92
|
+
await nextTick()
|
|
93
|
+
|
|
94
|
+
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }))
|
|
95
|
+
|
|
96
|
+
await vi.waitFor(() => {
|
|
97
|
+
expect(ctx.model.value).toBe(false)
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
ctx.unmount()
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
it('closeOnEsc 为 false 时按 Esc 不关闭', async () => {
|
|
104
|
+
const ctx = mountDialog({ closeOnEsc: false })
|
|
105
|
+
await nextTick()
|
|
106
|
+
|
|
107
|
+
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }))
|
|
108
|
+
await nextTick()
|
|
109
|
+
await nextTick()
|
|
110
|
+
|
|
111
|
+
expect(ctx.model.value).toBe(true)
|
|
112
|
+
expect(ctx.panel()).toBeTruthy()
|
|
113
|
+
|
|
114
|
+
ctx.unmount()
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
it('打开时锁定 body 滚动,关闭后恢复', async () => {
|
|
118
|
+
const ctx = mountDialog()
|
|
119
|
+
await nextTick()
|
|
120
|
+
|
|
121
|
+
expect(document.body.style.overflow).toBe('hidden')
|
|
122
|
+
|
|
123
|
+
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }))
|
|
124
|
+
|
|
125
|
+
await vi.waitFor(() => {
|
|
126
|
+
expect(ctx.model.value).toBe(false)
|
|
127
|
+
expect(document.body.style.overflow).toBe('')
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
ctx.unmount()
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
it('打开时把焦点移入面板,关闭后恢复打开前焦点', async () => {
|
|
134
|
+
const trigger = document.createElement('button')
|
|
135
|
+
document.body.appendChild(trigger)
|
|
136
|
+
trigger.focus()
|
|
137
|
+
|
|
138
|
+
const ctx = mountDialog({ title: '焦点' })
|
|
139
|
+
|
|
140
|
+
await vi.waitFor(() => {
|
|
141
|
+
expect(document.activeElement).toBe(ctx.panel())
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }))
|
|
145
|
+
|
|
146
|
+
await vi.waitFor(() => {
|
|
147
|
+
expect(document.activeElement).toBe(trigger)
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
ctx.unmount()
|
|
151
|
+
})
|
|
152
|
+
})
|
|
@@ -7,19 +7,21 @@
|
|
|
7
7
|
:class="[cls.e('overlay'), bem.is('modal', modal)]"
|
|
8
8
|
ref="overlayRef"
|
|
9
9
|
@mousedown="modal && close()"
|
|
10
|
-
@keyup.esc="close"
|
|
11
|
-
tabindex="0"
|
|
12
10
|
>
|
|
13
11
|
<transition :name="transition" @after-leave="onAfterDialogLeave">
|
|
14
12
|
<div
|
|
15
13
|
v-if="dialogVisible"
|
|
16
|
-
v-bind="$attrs"
|
|
17
|
-
:class="className"
|
|
18
14
|
ref="dialogRef"
|
|
15
|
+
:class="className"
|
|
16
|
+
role="dialog"
|
|
17
|
+
:aria-modal="modal ? 'true' : undefined"
|
|
18
|
+
:aria-labelledby="hasTitle ? titleId : undefined"
|
|
19
|
+
tabindex="-1"
|
|
19
20
|
@mousedown.stop="handleIncreaseZIndex"
|
|
21
|
+
v-bind="$attrs"
|
|
20
22
|
>
|
|
21
23
|
<section :class="headerCls" ref="headerRef" @transitionend.stop @transitioncancel.stop>
|
|
22
|
-
<div :class="cls.e('title')" @mousedown.stop>
|
|
24
|
+
<div :id="titleId" :class="cls.e('title')" @mousedown.stop>
|
|
23
25
|
<slot name="header">
|
|
24
26
|
{{ header || title }}
|
|
25
27
|
</slot>
|
|
@@ -75,8 +77,9 @@
|
|
|
75
77
|
import { useDrag, useFallbackProps } from '@veltra/compositions'
|
|
76
78
|
import { Close, Maximum, Recover } from '@veltra/icons/normal'
|
|
77
79
|
import { bem, extractNormalVNodes, setStyles, zIndex } from '@veltra/utils'
|
|
78
|
-
import { shallowRef, watch, computed, provide, nextTick } from 'vue'
|
|
80
|
+
import { shallowRef, watch, computed, provide, nextTick, useId } from 'vue'
|
|
79
81
|
|
|
82
|
+
import { useOverlay } from '../../composables/use-overlay'
|
|
80
83
|
import type { DialogProps, DialogEmits, DialogExposed, ComponentSize } from '../../types'
|
|
81
84
|
import { UIcon } from '../icon'
|
|
82
85
|
import { UNodeRender } from '../node-render'
|
|
@@ -90,7 +93,9 @@ const props = withDefaults(defineProps<DialogProps>(), {
|
|
|
90
93
|
modal: true,
|
|
91
94
|
modelValue: true,
|
|
92
95
|
autoScroll: true,
|
|
93
|
-
transition: 'fade-scale'
|
|
96
|
+
transition: 'fade-scale',
|
|
97
|
+
closeOnEsc: true,
|
|
98
|
+
lockScroll: true
|
|
94
99
|
})
|
|
95
100
|
const emit = defineEmits<DialogEmits>()
|
|
96
101
|
|
|
@@ -108,6 +113,12 @@ const cls = bem('dialog')
|
|
|
108
113
|
const headerCls = [cls.e('header')]
|
|
109
114
|
const footerCls = [cls.e('footer')]
|
|
110
115
|
|
|
116
|
+
/** 标题元素 id,供 aria-labelledby 引用 */
|
|
117
|
+
const titleId = useId()
|
|
118
|
+
|
|
119
|
+
/** 是否渲染了标题,用于决定 aria-labelledby */
|
|
120
|
+
const hasTitle = computed(() => Boolean(props.title || props.header || slots.header))
|
|
121
|
+
|
|
111
122
|
const { size } = useFallbackProps([props], { size: 'default' as ComponentSize })
|
|
112
123
|
|
|
113
124
|
const overlayRef = shallowRef<HTMLDivElement>()
|
|
@@ -120,8 +131,9 @@ const headerRef = shallowRef<HTMLDivElement>()
|
|
|
120
131
|
|
|
121
132
|
const visible = defineModel<boolean>({ default: false })
|
|
122
133
|
|
|
123
|
-
|
|
124
|
-
const
|
|
134
|
+
// 初始即打开时没有进入过渡钩子,直接按打开状态渲染(初始挂载不播进入动画)
|
|
135
|
+
const overlayVisible = shallowRef(visible.value)
|
|
136
|
+
const dialogVisible = shallowRef(visible.value)
|
|
125
137
|
|
|
126
138
|
watch(visible, (v) => {
|
|
127
139
|
if (v) {
|
|
@@ -153,27 +165,32 @@ const className = computed(() => {
|
|
|
153
165
|
return [cls.b, cls.m(size.value)]
|
|
154
166
|
})
|
|
155
167
|
|
|
168
|
+
/** dialog位移的位置 */
|
|
169
|
+
const translated = { x: 0, y: 0 }
|
|
170
|
+
|
|
156
171
|
/**
|
|
157
172
|
* 是否弹出过, 用于控制下一次弹出时是否采用v-show的方式
|
|
158
173
|
*/
|
|
159
174
|
let opened = false
|
|
160
175
|
|
|
161
|
-
watch(
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
176
|
+
watch(
|
|
177
|
+
visible,
|
|
178
|
+
(v) => {
|
|
179
|
+
if (!v) return
|
|
165
180
|
|
|
166
|
-
|
|
167
|
-
translated.x = 0
|
|
168
|
-
translated.y = 0
|
|
181
|
+
if (!opened) opened = true
|
|
169
182
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
})
|
|
183
|
+
// 初始化位置偏移量
|
|
184
|
+
translated.x = 0
|
|
185
|
+
translated.y = 0
|
|
174
186
|
|
|
175
|
-
|
|
176
|
-
const
|
|
187
|
+
nextTick(() => {
|
|
188
|
+
const overlay = overlayRef.value
|
|
189
|
+
if (overlay) setStyles(overlay, { zIndex: zIndex() })
|
|
190
|
+
})
|
|
191
|
+
},
|
|
192
|
+
{ immediate: true }
|
|
193
|
+
)
|
|
177
194
|
|
|
178
195
|
/**
|
|
179
196
|
* 更新弹框位置
|
|
@@ -225,6 +242,17 @@ const close = () => {
|
|
|
225
242
|
visible.value = false
|
|
226
243
|
}
|
|
227
244
|
|
|
245
|
+
// 通用遮罩行为:Esc 关闭、body 滚动锁、焦点圈与焦点恢复
|
|
246
|
+
// 非模态弹框不锁滚动、不做焦点圈,避免页面无法滚动或键盘无法离开弹框
|
|
247
|
+
useOverlay({
|
|
248
|
+
visible: () => visible.value,
|
|
249
|
+
panel: () => dialogRef.value,
|
|
250
|
+
onClose: close,
|
|
251
|
+
closeOnEsc: () => props.closeOnEsc,
|
|
252
|
+
lockScroll: () => props.lockScroll && props.modal,
|
|
253
|
+
trapFocus: () => props.modal
|
|
254
|
+
})
|
|
255
|
+
|
|
228
256
|
provide(DialogDIKey, { visible })
|
|
229
257
|
|
|
230
258
|
defineExpose<DialogExposed>({ close })
|
|
@@ -1,25 +1,27 @@
|
|
|
1
1
|
// @vitest-environment happy-dom
|
|
2
2
|
|
|
3
|
-
import { describe, expect, it, vi } from 'vite-plus/test'
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from 'vite-plus/test'
|
|
4
4
|
import { createApp, h, nextTick, ref } from 'vue'
|
|
5
5
|
|
|
6
6
|
import UDrawer from '../drawer.vue'
|
|
7
7
|
|
|
8
8
|
function mountDrawer(
|
|
9
9
|
props: Record<string, unknown> = {},
|
|
10
|
-
handlers: { onClose?: () => void; onClosed?: () => void } = {}
|
|
10
|
+
handlers: { onClose?: () => void; onClosed?: () => void } = {},
|
|
11
|
+
slot: () => any = () => '抽屉内容'
|
|
11
12
|
) {
|
|
12
13
|
const host = document.createElement('div')
|
|
13
14
|
document.body.appendChild(host)
|
|
14
15
|
|
|
15
|
-
const
|
|
16
|
+
const { modelValue = false, ...rest } = props
|
|
17
|
+
const model = ref(modelValue === true)
|
|
16
18
|
const closedCount = ref(0)
|
|
17
19
|
const app = createApp({
|
|
18
20
|
render: () =>
|
|
19
21
|
h(
|
|
20
22
|
UDrawer,
|
|
21
23
|
{
|
|
22
|
-
...
|
|
24
|
+
...rest,
|
|
23
25
|
modelValue: model.value,
|
|
24
26
|
'onUpdate:modelValue': (value: boolean) => {
|
|
25
27
|
model.value = value
|
|
@@ -30,7 +32,7 @@ function mountDrawer(
|
|
|
30
32
|
handlers.onClosed?.()
|
|
31
33
|
}
|
|
32
34
|
},
|
|
33
|
-
|
|
35
|
+
slot
|
|
34
36
|
)
|
|
35
37
|
})
|
|
36
38
|
app.mount(host)
|
|
@@ -53,8 +55,15 @@ async function openDrawer(ctx: ReturnType<typeof mountDrawer>) {
|
|
|
53
55
|
await vi.waitFor(() => {
|
|
54
56
|
expect(ctx.panel()).toBeTruthy()
|
|
55
57
|
})
|
|
58
|
+
await nextTick()
|
|
56
59
|
}
|
|
57
60
|
|
|
61
|
+
afterEach(() => {
|
|
62
|
+
document.body.style.overflow = ''
|
|
63
|
+
document.body.style.paddingRight = ''
|
|
64
|
+
document.body.innerHTML = ''
|
|
65
|
+
})
|
|
66
|
+
|
|
58
67
|
describe('UDrawer title', () => {
|
|
59
68
|
it('传入 title 时渲染标题栏', async () => {
|
|
60
69
|
const ctx = mountDrawer({ title: '用户详情' })
|
|
@@ -127,6 +136,10 @@ describe('UDrawer closed', () => {
|
|
|
127
136
|
await openDrawer(ctx)
|
|
128
137
|
|
|
129
138
|
ctx.panel()!.querySelector<HTMLElement>('.u-drawer__close')!.click()
|
|
139
|
+
// v-model 在开始关闭时就同步,closed 要等退出动画结束
|
|
140
|
+
expect(ctx.model.value).toBe(false)
|
|
141
|
+
expect(order).toEqual(['close'])
|
|
142
|
+
|
|
130
143
|
await vi.waitFor(() => {
|
|
131
144
|
expect(order).toContain('closed')
|
|
132
145
|
})
|
|
@@ -138,6 +151,22 @@ describe('UDrawer closed', () => {
|
|
|
138
151
|
ctx.unmount()
|
|
139
152
|
})
|
|
140
153
|
|
|
154
|
+
it('关闭过程中再次打开不会停在关掉的状态', async () => {
|
|
155
|
+
const ctx = mountDrawer({ showClose: true })
|
|
156
|
+
await openDrawer(ctx)
|
|
157
|
+
|
|
158
|
+
ctx.panel()!.querySelector<HTMLElement>('.u-drawer__close')!.click()
|
|
159
|
+
expect(ctx.model.value).toBe(false)
|
|
160
|
+
|
|
161
|
+
ctx.model.value = true
|
|
162
|
+
await nextTick()
|
|
163
|
+
|
|
164
|
+
expect(ctx.panel()).toBeTruthy()
|
|
165
|
+
expect(ctx.model.value).toBe(true)
|
|
166
|
+
|
|
167
|
+
ctx.unmount()
|
|
168
|
+
})
|
|
169
|
+
|
|
141
170
|
it('点遮罩关闭也触发 closed', async () => {
|
|
142
171
|
const ctx = mountDrawer()
|
|
143
172
|
await openDrawer(ctx)
|
|
@@ -160,3 +189,138 @@ describe('UDrawer closed', () => {
|
|
|
160
189
|
ctx.unmount()
|
|
161
190
|
})
|
|
162
191
|
})
|
|
192
|
+
|
|
193
|
+
describe('UDrawer 遮罩通用行为', () => {
|
|
194
|
+
it('按 Esc 关闭抽屉并同步 v-model', async () => {
|
|
195
|
+
const onClose = vi.fn()
|
|
196
|
+
const ctx = mountDrawer({}, { onClose })
|
|
197
|
+
await openDrawer(ctx)
|
|
198
|
+
|
|
199
|
+
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }))
|
|
200
|
+
|
|
201
|
+
await vi.waitFor(() => {
|
|
202
|
+
expect(onClose).toHaveBeenCalledTimes(1)
|
|
203
|
+
expect(ctx.model.value).toBe(false)
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
ctx.unmount()
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
it('closeOnEsc 为 false 时按 Esc 不关闭', async () => {
|
|
210
|
+
const onClose = vi.fn()
|
|
211
|
+
const ctx = mountDrawer({ closeOnEsc: false }, { onClose })
|
|
212
|
+
await openDrawer(ctx)
|
|
213
|
+
|
|
214
|
+
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }))
|
|
215
|
+
await nextTick()
|
|
216
|
+
await nextTick()
|
|
217
|
+
|
|
218
|
+
expect(onClose).not.toHaveBeenCalled()
|
|
219
|
+
expect(ctx.model.value).toBe(true)
|
|
220
|
+
expect(ctx.panel()).toBeTruthy()
|
|
221
|
+
|
|
222
|
+
ctx.unmount()
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
it('打开时锁定 body 滚动,关闭后恢复', async () => {
|
|
226
|
+
const ctx = mountDrawer()
|
|
227
|
+
await openDrawer(ctx)
|
|
228
|
+
|
|
229
|
+
expect(document.body.style.overflow).toBe('hidden')
|
|
230
|
+
|
|
231
|
+
ctx.overlay()!.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true }))
|
|
232
|
+
|
|
233
|
+
await vi.waitFor(() => {
|
|
234
|
+
expect(document.body.style.overflow).toBe('')
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
ctx.unmount()
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
it('面板带 role="dialog"、aria-modal,标题通过 aria-labelledby 关联', async () => {
|
|
241
|
+
const ctx = mountDrawer({ title: '用户详情' })
|
|
242
|
+
await openDrawer(ctx)
|
|
243
|
+
|
|
244
|
+
const panel = ctx.panel()!
|
|
245
|
+
const title = panel.querySelector<HTMLElement>('.u-drawer__title')!
|
|
246
|
+
|
|
247
|
+
expect(panel.getAttribute('role')).toBe('dialog')
|
|
248
|
+
expect(panel.getAttribute('aria-modal')).toBe('true')
|
|
249
|
+
expect(panel.getAttribute('aria-labelledby')).toBe(title.id)
|
|
250
|
+
expect(panel.getAttribute('tabindex')).toBe('-1')
|
|
251
|
+
|
|
252
|
+
ctx.unmount()
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
it('打开时把焦点移入面板,关闭后恢复打开前焦点', async () => {
|
|
256
|
+
const trigger = document.createElement('button')
|
|
257
|
+
document.body.appendChild(trigger)
|
|
258
|
+
trigger.focus()
|
|
259
|
+
|
|
260
|
+
const ctx = mountDrawer({ showClose: true })
|
|
261
|
+
await openDrawer(ctx)
|
|
262
|
+
|
|
263
|
+
await vi.waitFor(() => {
|
|
264
|
+
expect(document.activeElement).toBe(ctx.panel())
|
|
265
|
+
})
|
|
266
|
+
|
|
267
|
+
ctx.panel()!.querySelector<HTMLElement>('.u-drawer__close')!.click()
|
|
268
|
+
|
|
269
|
+
await vi.waitFor(() => {
|
|
270
|
+
expect(document.activeElement).toBe(trigger)
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
ctx.unmount()
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
it('Tab / Shift+Tab 在面板内循环', async () => {
|
|
277
|
+
const ctx = mountDrawer({}, {}, () => [
|
|
278
|
+
h('button', { id: 'first' }, '第一个'),
|
|
279
|
+
h('button', { id: 'second' }, '第二个')
|
|
280
|
+
])
|
|
281
|
+
await openDrawer(ctx)
|
|
282
|
+
|
|
283
|
+
const first = ctx.panel()!.querySelector<HTMLElement>('#first')!
|
|
284
|
+
const second = ctx.panel()!.querySelector<HTMLElement>('#second')!
|
|
285
|
+
|
|
286
|
+
await vi.waitFor(() => {
|
|
287
|
+
expect(document.activeElement).toBe(first)
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
second.focus()
|
|
291
|
+
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Tab' }))
|
|
292
|
+
expect(document.activeElement).toBe(first)
|
|
293
|
+
|
|
294
|
+
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Tab', shiftKey: true }))
|
|
295
|
+
expect(document.activeElement).toBe(second)
|
|
296
|
+
|
|
297
|
+
ctx.unmount()
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
it('多层叠加时 Esc 只关闭最上层', async () => {
|
|
301
|
+
const bottom = mountDrawer()
|
|
302
|
+
await openDrawer(bottom)
|
|
303
|
+
|
|
304
|
+
const top = mountDrawer()
|
|
305
|
+
top.model.value = true
|
|
306
|
+
await vi.waitFor(() => {
|
|
307
|
+
expect(document.body.querySelectorAll('.u-drawer')).toHaveLength(2)
|
|
308
|
+
})
|
|
309
|
+
await nextTick()
|
|
310
|
+
|
|
311
|
+
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }))
|
|
312
|
+
await vi.waitFor(() => {
|
|
313
|
+
expect(top.model.value).toBe(false)
|
|
314
|
+
})
|
|
315
|
+
expect(bottom.model.value).toBe(true)
|
|
316
|
+
|
|
317
|
+
// 最上层关闭后,再按 Esc 才关闭下一层
|
|
318
|
+
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }))
|
|
319
|
+
await vi.waitFor(() => {
|
|
320
|
+
expect(bottom.model.value).toBe(false)
|
|
321
|
+
})
|
|
322
|
+
|
|
323
|
+
top.unmount()
|
|
324
|
+
bottom.unmount()
|
|
325
|
+
})
|
|
326
|
+
})
|
|
@@ -1,26 +1,29 @@
|
|
|
1
1
|
<template>
|
|
2
2
|
<Teleport to="body">
|
|
3
|
-
<transition name="fade"
|
|
4
|
-
<div v-if="
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
</
|
|
3
|
+
<transition name="drawer-fade" appear>
|
|
4
|
+
<div v-if="visible" :class="overlayCls.b" :style="{ zIndex: layer }" @click="close" />
|
|
5
|
+
</transition>
|
|
6
|
+
<transition :name="transitionName" appear @after-leave="onAfterLeave">
|
|
7
|
+
<div
|
|
8
|
+
v-if="visible"
|
|
9
|
+
ref="panelRef"
|
|
10
|
+
:class="drawerClass"
|
|
11
|
+
:style="[drawerStyle, { zIndex: layer }]"
|
|
12
|
+
role="dialog"
|
|
13
|
+
aria-modal="true"
|
|
14
|
+
:aria-labelledby="title ? titleId : undefined"
|
|
15
|
+
tabindex="-1"
|
|
16
|
+
v-bind="$attrs"
|
|
17
|
+
>
|
|
18
|
+
<div v-if="title" :class="cls.e('header')">
|
|
19
|
+
<span :id="titleId" :class="cls.e('title')">{{ title }}</span>
|
|
20
|
+
</div>
|
|
21
|
+
<span :class="cls.e('close')" @click="close" v-if="showClose">
|
|
22
|
+
<u-icon><Close /></u-icon>
|
|
23
|
+
</span>
|
|
24
|
+
<div :class="cls.e('body')">
|
|
25
|
+
<slot />
|
|
26
|
+
</div>
|
|
24
27
|
</div>
|
|
25
28
|
</transition>
|
|
26
29
|
</Teleport>
|
|
@@ -29,23 +32,46 @@
|
|
|
29
32
|
<script lang="ts" setup>
|
|
30
33
|
import { Close } from '@veltra/icons/normal'
|
|
31
34
|
import { bem, zIndex } from '@veltra/utils'
|
|
32
|
-
import { computed, shallowRef } from 'vue'
|
|
35
|
+
import { computed, shallowRef, useId, useTemplateRef, watch } from 'vue'
|
|
33
36
|
|
|
37
|
+
import { useOverlay } from '../../composables/use-overlay'
|
|
34
38
|
import type { DrawerProps, DrawerEmits } from '../../types'
|
|
35
39
|
import { UIcon } from '../icon'
|
|
36
40
|
|
|
37
41
|
defineOptions({ name: 'UDrawer', inheritAttrs: false })
|
|
38
42
|
|
|
39
|
-
const props = withDefaults(defineProps<DrawerProps>(), {
|
|
43
|
+
const props = withDefaults(defineProps<DrawerProps>(), {
|
|
44
|
+
direction: 'right',
|
|
45
|
+
closeOnEsc: true,
|
|
46
|
+
lockScroll: true
|
|
47
|
+
})
|
|
40
48
|
|
|
41
49
|
const emit = defineEmits<DrawerEmits>()
|
|
42
50
|
|
|
43
51
|
const cls = bem('drawer')
|
|
44
52
|
const overlayCls = bem('drawer-overlay')
|
|
45
53
|
|
|
46
|
-
const
|
|
54
|
+
const visible = defineModel<boolean>({ default: false })
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 遮罩与面板共用的层级,每次打开时取一次。
|
|
58
|
+
* 面板与插槽同处一个 transition,模板里直接调 zIndex() 会随插槽重渲染不断递增
|
|
59
|
+
*/
|
|
60
|
+
const layer = shallowRef(0)
|
|
61
|
+
|
|
62
|
+
watch(
|
|
63
|
+
visible,
|
|
64
|
+
(v) => {
|
|
65
|
+
if (v) layer.value = zIndex()
|
|
66
|
+
},
|
|
67
|
+
{ immediate: true }
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
/** 标题元素 id,供 aria-labelledby 引用 */
|
|
71
|
+
const titleId = useId()
|
|
47
72
|
|
|
48
|
-
|
|
73
|
+
/** 抽屉面板模板引用,用于初始聚焦与焦点圈 */
|
|
74
|
+
const panelRef = useTemplateRef<HTMLElement>('panelRef')
|
|
49
75
|
|
|
50
76
|
// 根据方向计算过渡动画名称
|
|
51
77
|
const transitionName = computed(() => {
|
|
@@ -67,17 +93,25 @@ const drawerStyle = computed(() => {
|
|
|
67
93
|
return { [axis]: dimension }
|
|
68
94
|
})
|
|
69
95
|
|
|
70
|
-
const
|
|
71
|
-
|
|
96
|
+
const onAfterLeave = () => {
|
|
97
|
+
// 退出过程中再次打开会取消 leave,不应把仍打开的抽屉当成已关闭
|
|
98
|
+
if (visible.value) return
|
|
99
|
+
emit('closed')
|
|
72
100
|
}
|
|
73
101
|
|
|
74
|
-
|
|
75
|
-
overlayVisible.value = false
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// 关闭抽屉
|
|
102
|
+
// 立刻同步 v-model,退出动画由 v-if 变 false 驱动;动画期间再次打开能取消退出
|
|
79
103
|
const close = () => {
|
|
80
|
-
|
|
104
|
+
if (!visible.value) return
|
|
105
|
+
visible.value = false
|
|
81
106
|
emit('close')
|
|
82
107
|
}
|
|
108
|
+
|
|
109
|
+
// 通用遮罩行为:Esc 关闭、body 滚动锁、焦点圈与焦点恢复
|
|
110
|
+
useOverlay({
|
|
111
|
+
visible: () => visible.value,
|
|
112
|
+
panel: () => panelRef.value ?? undefined,
|
|
113
|
+
onClose: close,
|
|
114
|
+
closeOnEsc: () => props.closeOnEsc,
|
|
115
|
+
lockScroll: () => props.lockScroll
|
|
116
|
+
})
|
|
83
117
|
</script>
|