@veltra/desktop 1.13.3 → 1.14.0
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 +38 -6
- package/dist/components/drawer/drawer.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/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 +146 -3
- package/src/components/drawer/drawer.vue +29 -4
- 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,238 @@
|
|
|
1
|
+
import { nextTick, onScopeDispose, watch } from 'vue'
|
|
2
|
+
|
|
3
|
+
/** 遮罩层通用行为配置 */
|
|
4
|
+
export interface UseOverlayOptions {
|
|
5
|
+
/** 当前是否处于打开状态 */
|
|
6
|
+
visible: () => boolean
|
|
7
|
+
/** 遮罩面板元素,用于初始聚焦与焦点圈 */
|
|
8
|
+
panel: () => HTMLElement | undefined
|
|
9
|
+
/** 关闭回调 */
|
|
10
|
+
onClose: () => void
|
|
11
|
+
/** 是否允许按 Esc 关闭,缺省允许 */
|
|
12
|
+
closeOnEsc?: () => boolean
|
|
13
|
+
/** 是否锁定 body 滚动,缺省锁定 */
|
|
14
|
+
lockScroll?: () => boolean
|
|
15
|
+
/** 是否把焦点限制在面板内,缺省限制 */
|
|
16
|
+
trapFocus?: () => boolean
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** 面板内可聚焦元素选择器 */
|
|
20
|
+
const FOCUSABLE_SELECTOR = [
|
|
21
|
+
'a[href]',
|
|
22
|
+
'area[href]',
|
|
23
|
+
'button:not([disabled])',
|
|
24
|
+
'input:not([disabled]):not([type="hidden"])',
|
|
25
|
+
'select:not([disabled])',
|
|
26
|
+
'textarea:not([disabled])',
|
|
27
|
+
'iframe',
|
|
28
|
+
'object',
|
|
29
|
+
'embed',
|
|
30
|
+
'[contenteditable="true"]',
|
|
31
|
+
'[tabindex]:not([tabindex="-1"])'
|
|
32
|
+
].join(',')
|
|
33
|
+
|
|
34
|
+
/** 收集面板内可聚焦元素 */
|
|
35
|
+
function getFocusableElements(panel: HTMLElement): HTMLElement[] {
|
|
36
|
+
return Array.from(panel.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR)).filter((el) => {
|
|
37
|
+
return el.getAttribute('aria-hidden') !== 'true' && el.tabIndex >= 0
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// body 滚动锁采用模块级计数,支持多个遮罩层嵌套打开
|
|
42
|
+
let lockCount = 0
|
|
43
|
+
let originalOverflow = ''
|
|
44
|
+
let originalPaddingRight = ''
|
|
45
|
+
|
|
46
|
+
/** 锁定 body 滚动 */
|
|
47
|
+
function lockBodyScroll(): void {
|
|
48
|
+
if (typeof document === 'undefined' || typeof window === 'undefined') return
|
|
49
|
+
|
|
50
|
+
lockCount += 1
|
|
51
|
+
if (lockCount > 1) return
|
|
52
|
+
|
|
53
|
+
const { body, documentElement } = document
|
|
54
|
+
originalOverflow = body.style.overflow
|
|
55
|
+
originalPaddingRight = body.style.paddingRight
|
|
56
|
+
|
|
57
|
+
// 滚动条消失会让页面右移,用等宽 padding 补偿
|
|
58
|
+
const scrollbarWidth = window.innerWidth - documentElement.clientWidth
|
|
59
|
+
if (scrollbarWidth > 0) {
|
|
60
|
+
const paddingRight = Number.parseFloat(window.getComputedStyle(body).paddingRight) || 0
|
|
61
|
+
body.style.paddingRight = `${paddingRight + scrollbarWidth}px`
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
body.style.overflow = 'hidden'
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** 解除 body 滚动锁 */
|
|
68
|
+
function unlockBodyScroll(): void {
|
|
69
|
+
if (typeof document === 'undefined' || lockCount === 0) return
|
|
70
|
+
|
|
71
|
+
lockCount -= 1
|
|
72
|
+
if (lockCount > 0) return
|
|
73
|
+
|
|
74
|
+
document.body.style.overflow = originalOverflow
|
|
75
|
+
document.body.style.paddingRight = originalPaddingRight
|
|
76
|
+
originalOverflow = ''
|
|
77
|
+
originalPaddingRight = ''
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** 已打开的遮罩层栈,按打开顺序排列 */
|
|
81
|
+
const overlayStack: symbol[] = []
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 遮罩层通用行为:Esc 关闭、body 滚动锁、焦点圈与焦点恢复。
|
|
85
|
+
* 仅包内部使用,不对外导出。
|
|
86
|
+
*/
|
|
87
|
+
export function useOverlay(options: UseOverlayOptions): void {
|
|
88
|
+
const stackToken = Symbol('overlay')
|
|
89
|
+
|
|
90
|
+
let listening = false
|
|
91
|
+
let stacked = false
|
|
92
|
+
let scrollLocked = false
|
|
93
|
+
let previousActive: HTMLElement | null = null
|
|
94
|
+
let openToken = 0
|
|
95
|
+
|
|
96
|
+
const isCloseOnEsc = () => options.closeOnEsc?.() ?? true
|
|
97
|
+
const isLockScroll = () => options.lockScroll?.() ?? true
|
|
98
|
+
const isTrapFocus = () => options.trapFocus?.() ?? true
|
|
99
|
+
const isTopmost = () => overlayStack[overlayStack.length - 1] === stackToken
|
|
100
|
+
|
|
101
|
+
function pushStack(): void {
|
|
102
|
+
if (stacked) return
|
|
103
|
+
overlayStack.push(stackToken)
|
|
104
|
+
stacked = true
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function popStack(): void {
|
|
108
|
+
if (!stacked) return
|
|
109
|
+
const index = overlayStack.indexOf(stackToken)
|
|
110
|
+
if (index !== -1) overlayStack.splice(index, 1)
|
|
111
|
+
stacked = false
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function handleKeydown(event: KeyboardEvent): void {
|
|
115
|
+
// 多层遮罩叠加时只由最上层响应,避免一次 Esc 连关多层
|
|
116
|
+
if (!options.visible() || !isTopmost()) return
|
|
117
|
+
|
|
118
|
+
if (event.key === 'Escape') {
|
|
119
|
+
if (event.defaultPrevented || !isCloseOnEsc()) return
|
|
120
|
+
options.onClose()
|
|
121
|
+
return
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (event.key !== 'Tab' || !isTrapFocus()) return
|
|
125
|
+
|
|
126
|
+
const panel = options.panel()
|
|
127
|
+
if (!panel) return
|
|
128
|
+
|
|
129
|
+
const focusables = getFocusableElements(panel)
|
|
130
|
+
if (!focusables.length) {
|
|
131
|
+
event.preventDefault()
|
|
132
|
+
panel.focus()
|
|
133
|
+
return
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const first = focusables[0]
|
|
137
|
+
const last = focusables[focusables.length - 1]
|
|
138
|
+
if (!first || !last) return
|
|
139
|
+
|
|
140
|
+
const active = document.activeElement
|
|
141
|
+
const inside = active ? panel.contains(active) : false
|
|
142
|
+
|
|
143
|
+
// 焦点不在面板内或落在面板本身时,按 Tab 方向拉回两端
|
|
144
|
+
if (!inside || active === panel) {
|
|
145
|
+
event.preventDefault()
|
|
146
|
+
const target = event.shiftKey ? last : first
|
|
147
|
+
target.focus()
|
|
148
|
+
return
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (event.shiftKey && active === first) {
|
|
152
|
+
event.preventDefault()
|
|
153
|
+
last.focus()
|
|
154
|
+
} else if (!event.shiftKey && active === last) {
|
|
155
|
+
event.preventDefault()
|
|
156
|
+
first.focus()
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function addKeydownListener(): void {
|
|
161
|
+
if (listening || typeof document === 'undefined') return
|
|
162
|
+
document.addEventListener('keydown', handleKeydown)
|
|
163
|
+
listening = true
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function removeKeydownListener(): void {
|
|
167
|
+
if (!listening || typeof document === 'undefined') return
|
|
168
|
+
document.removeEventListener('keydown', handleKeydown)
|
|
169
|
+
listening = false
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** 把焦点移入面板:优先第一个可聚焦元素,否则面板自身 */
|
|
173
|
+
function focusPanel(): void {
|
|
174
|
+
const panel = options.panel()
|
|
175
|
+
if (!panel) return
|
|
176
|
+
const target = getFocusableElements(panel)[0] ?? panel
|
|
177
|
+
target.focus()
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** 恢复打开前的焦点 */
|
|
181
|
+
function restoreFocus(): void {
|
|
182
|
+
if (!previousActive) return
|
|
183
|
+
const target = previousActive
|
|
184
|
+
previousActive = null
|
|
185
|
+
if (target.isConnected) target.focus()
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
watch(
|
|
189
|
+
options.visible,
|
|
190
|
+
async (visible) => {
|
|
191
|
+
if (!visible) {
|
|
192
|
+
removeKeydownListener()
|
|
193
|
+
popStack()
|
|
194
|
+
restoreFocus()
|
|
195
|
+
return
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (typeof document === 'undefined') return
|
|
199
|
+
|
|
200
|
+
pushStack()
|
|
201
|
+
addKeydownListener()
|
|
202
|
+
|
|
203
|
+
// 非模态遮罩不做焦点圈:不抢焦点,也不在关闭时回焦
|
|
204
|
+
if (!isTrapFocus()) return
|
|
205
|
+
|
|
206
|
+
const active = document.activeElement
|
|
207
|
+
previousActive = active instanceof HTMLElement && active !== document.body ? active : null
|
|
208
|
+
|
|
209
|
+
const token = ++openToken
|
|
210
|
+
await nextTick()
|
|
211
|
+
|
|
212
|
+
// 打开后立即关闭(或再次打开)时不再抢焦点
|
|
213
|
+
if (token !== openToken || !options.visible()) return
|
|
214
|
+
focusPanel()
|
|
215
|
+
},
|
|
216
|
+
{ immediate: true }
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
watch(
|
|
220
|
+
() => options.visible() && isLockScroll(),
|
|
221
|
+
(locked) => {
|
|
222
|
+
if (locked === scrollLocked) return
|
|
223
|
+
scrollLocked = locked
|
|
224
|
+
if (locked) lockBodyScroll()
|
|
225
|
+
else unlockBodyScroll()
|
|
226
|
+
},
|
|
227
|
+
{ immediate: true }
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
onScopeDispose(() => {
|
|
231
|
+
removeKeydownListener()
|
|
232
|
+
popStack()
|
|
233
|
+
if (scrollLocked) {
|
|
234
|
+
scrollLocked = false
|
|
235
|
+
unlockBodyScroll()
|
|
236
|
+
}
|
|
237
|
+
})
|
|
238
|
+
}
|
package/src/types/dialog.ts
CHANGED
|
@@ -19,6 +19,10 @@ export interface DialogProps {
|
|
|
19
19
|
fullscreen?: boolean
|
|
20
20
|
/** 弹框过渡动画,默认为 spring */
|
|
21
21
|
transition?: DialogTransition
|
|
22
|
+
/** 是否允许按 Esc 关闭弹框,默认 true */
|
|
23
|
+
closeOnEsc?: boolean
|
|
24
|
+
/** 打开时是否锁定 body 滚动,默认 true */
|
|
25
|
+
lockScroll?: boolean
|
|
22
26
|
}
|
|
23
27
|
|
|
24
28
|
/** 对话框组件定义的事件 */
|
package/src/types/drawer.ts
CHANGED