@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
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
@use 'pkg:@veltra/styles/mixins' as m;
|
|
2
2
|
@use 'pkg:@veltra/styles/functions' as fn;
|
|
3
3
|
@use 'pkg:@veltra/styles/vars';
|
|
4
|
-
@use 'sass:map';
|
|
5
|
-
@use 'sass:string';
|
|
6
4
|
|
|
7
5
|
// 方便拼接
|
|
8
6
|
$root-name: drawer;
|
|
@@ -10,17 +8,20 @@ $overlay-name: #{$root-name}-overlay;
|
|
|
10
8
|
|
|
11
9
|
$edge-offset: 4px;
|
|
12
10
|
$close-size: 48px;
|
|
11
|
+
// 关闭按钮悬浮在面板外沿的距离
|
|
12
|
+
$close-offset: $close-size + 4px;
|
|
13
13
|
|
|
14
14
|
$directions: (right, left, top, bottom);
|
|
15
15
|
|
|
16
|
+
// 只多走贴边间距。外置关闭按钮不计入行程,进出时单独淡入淡出
|
|
16
17
|
$direction-map: (
|
|
17
|
-
right: translateX(100%),
|
|
18
|
-
left: translateX(-100%),
|
|
19
|
-
top: translateY(-100%),
|
|
20
|
-
bottom: translateY(100%)
|
|
18
|
+
right: translateX(calc(100% + #{$edge-offset})),
|
|
19
|
+
left: translateX(calc(-100% - #{$edge-offset})),
|
|
20
|
+
top: translateY(calc(-100% - #{$edge-offset})),
|
|
21
|
+
bottom: translateY(calc(100% + #{$edge-offset}))
|
|
21
22
|
);
|
|
22
23
|
@include m.b($root-name) {
|
|
23
|
-
position:
|
|
24
|
+
position: fixed;
|
|
24
25
|
display: flex;
|
|
25
26
|
flex-direction: column;
|
|
26
27
|
border-radius: fn.use-var(radius, large);
|
|
@@ -92,7 +93,7 @@ $direction-map: (
|
|
|
92
93
|
right: $edge-offset;
|
|
93
94
|
|
|
94
95
|
@include m.bem($root-name, close) {
|
|
95
|
-
left:
|
|
96
|
+
left: -$close-offset;
|
|
96
97
|
}
|
|
97
98
|
}
|
|
98
99
|
|
|
@@ -100,7 +101,7 @@ $direction-map: (
|
|
|
100
101
|
left: $edge-offset;
|
|
101
102
|
|
|
102
103
|
@include m.bem($root-name, close) {
|
|
103
|
-
right:
|
|
104
|
+
right: -$close-offset;
|
|
104
105
|
}
|
|
105
106
|
}
|
|
106
107
|
|
|
@@ -108,7 +109,7 @@ $direction-map: (
|
|
|
108
109
|
top: $edge-offset;
|
|
109
110
|
|
|
110
111
|
@include m.bem($root-name, close) {
|
|
111
|
-
bottom:
|
|
112
|
+
bottom: -$close-offset;
|
|
112
113
|
}
|
|
113
114
|
}
|
|
114
115
|
|
|
@@ -116,7 +117,7 @@ $direction-map: (
|
|
|
116
117
|
bottom: $edge-offset;
|
|
117
118
|
|
|
118
119
|
@include m.bem($root-name, close) {
|
|
119
|
-
top:
|
|
120
|
+
top: -$close-offset;
|
|
120
121
|
}
|
|
121
122
|
}
|
|
122
123
|
}
|
|
@@ -128,61 +129,70 @@ $direction-map: (
|
|
|
128
129
|
width: 100vw;
|
|
129
130
|
height: 100vh;
|
|
130
131
|
background-color: rgba($color: #000000, $alpha: 0.32);
|
|
131
|
-
|
|
132
|
+
}
|
|
132
133
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
134
|
+
// 位移不跟主题时长和 ease-out:slow 偏长,部分主题的 ease-out 会过冲
|
|
135
|
+
$enter-duration: 0.28s;
|
|
136
|
+
$leave-duration: 0.2s;
|
|
137
|
+
$enter-ease: cubic-bezier(0.16, 1, 0.3, 1);
|
|
138
|
+
$leave-ease: cubic-bezier(0.4, 0, 1, 1);
|
|
138
139
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
filter: none;
|
|
144
|
-
}
|
|
140
|
+
// 遮罩只淡透明度,不跟面板的位移曲线
|
|
141
|
+
.drawer-fade-enter-active,
|
|
142
|
+
.drawer-fade-leave-active {
|
|
143
|
+
transition: opacity $leave-duration linear;
|
|
145
144
|
}
|
|
146
145
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
@return '.' + drawer-slide-#{$direction}-#{$type}-active;
|
|
146
|
+
.drawer-fade-enter-from,
|
|
147
|
+
.drawer-fade-leave-to {
|
|
148
|
+
opacity: 0;
|
|
151
149
|
}
|
|
152
150
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
$active
|
|
156
|
-
|
|
157
|
-
|
|
151
|
+
@each $direction in $directions {
|
|
152
|
+
.drawer-slide-#{$direction}-enter-active,
|
|
153
|
+
.drawer-slide-#{$direction}-leave-active {
|
|
154
|
+
will-change: transform;
|
|
155
|
+
// 模糊滤镜和 transform 在同一层会掉帧,动画期间关掉,结束后回到主题滤镜
|
|
156
|
+
backdrop-filter: none;
|
|
157
|
+
-webkit-backdrop-filter: none;
|
|
158
|
+
|
|
159
|
+
#{fn.bem($root-name, close)} {
|
|
160
|
+
transition: opacity $leave-duration linear;
|
|
161
|
+
}
|
|
158
162
|
}
|
|
159
|
-
@return string.slice($active, 2, -1);
|
|
160
|
-
}
|
|
161
163
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
164
|
+
.drawer-slide-#{$direction}-enter-active {
|
|
165
|
+
transition: transform $enter-duration $enter-ease;
|
|
166
|
+
}
|
|
165
167
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
+
.drawer-slide-#{$direction}-leave-active {
|
|
169
|
+
transition: transform $leave-duration $leave-ease;
|
|
170
|
+
}
|
|
168
171
|
}
|
|
169
172
|
|
|
170
|
-
|
|
171
|
-
.drawer-slide-
|
|
172
|
-
|
|
173
|
-
|
|
173
|
+
@each $direction, $transform in $direction-map {
|
|
174
|
+
.drawer-slide-#{$direction}-enter-from,
|
|
175
|
+
.drawer-slide-#{$direction}-leave-to {
|
|
176
|
+
transform: $transform;
|
|
174
177
|
|
|
175
|
-
.
|
|
176
|
-
|
|
177
|
-
|
|
178
|
+
#{fn.bem($root-name, close)} {
|
|
179
|
+
opacity: 0;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
178
182
|
}
|
|
179
183
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
}
|
|
184
|
+
// 减弱动态效果:面板不位移,只随遮罩淡入淡出
|
|
185
|
+
@media (prefers-reduced-motion: reduce) {
|
|
186
|
+
@each $direction in $directions {
|
|
187
|
+
.drawer-slide-#{$direction}-enter-active,
|
|
188
|
+
.drawer-slide-#{$direction}-leave-active {
|
|
189
|
+
transition-property: opacity;
|
|
190
|
+
}
|
|
184
191
|
|
|
185
|
-
.drawer-slide-
|
|
186
|
-
.drawer-slide-
|
|
187
|
-
|
|
192
|
+
.drawer-slide-#{$direction}-enter-from,
|
|
193
|
+
.drawer-slide-#{$direction}-leave-to {
|
|
194
|
+
transform: none;
|
|
195
|
+
opacity: 0;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
188
198
|
}
|
|
@@ -133,3 +133,50 @@ describe('UMultiSelect labelTemplate', () => {
|
|
|
133
133
|
}
|
|
134
134
|
})
|
|
135
135
|
})
|
|
136
|
+
|
|
137
|
+
describe('UMultiSelect dropdown opening', () => {
|
|
138
|
+
const options = [
|
|
139
|
+
{ label: '启用', code: 'ENABLED', value: 'enabled' },
|
|
140
|
+
{ label: '禁用', value: 'disabled' }
|
|
141
|
+
]
|
|
142
|
+
|
|
143
|
+
it('does not open the panel when the filter input is focused programmatically', async () => {
|
|
144
|
+
const { host, unmount } = mountMultiSelect({ filterable: true, options })
|
|
145
|
+
|
|
146
|
+
try {
|
|
147
|
+
const input = host.querySelector<HTMLInputElement>('.u-multi-select__input')!
|
|
148
|
+
expect(input).toBeTruthy()
|
|
149
|
+
|
|
150
|
+
// 模态弹框打开时会把焦点移入面板内第一个可聚焦元素(即本输入框),不应连带展开面板
|
|
151
|
+
input.dispatchEvent(new FocusEvent('focus'))
|
|
152
|
+
for (let attempt = 0; attempt < 6; attempt++) {
|
|
153
|
+
await nextTick()
|
|
154
|
+
await sleep(25)
|
|
155
|
+
}
|
|
156
|
+
expect(queryOptions().length).toBe(0)
|
|
157
|
+
} finally {
|
|
158
|
+
unmount()
|
|
159
|
+
}
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
it('opens the panel when typing into the filter input', async () => {
|
|
163
|
+
const { host, unmount } = mountMultiSelect({ filterable: true, options })
|
|
164
|
+
|
|
165
|
+
try {
|
|
166
|
+
const input = host.querySelector<HTMLInputElement>('.u-multi-select__input')!
|
|
167
|
+
input.value = '启'
|
|
168
|
+
input.dispatchEvent(
|
|
169
|
+
new InputEvent('input', { bubbles: true, data: '启', inputType: 'insertText' })
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
for (let attempt = 0; attempt < 20; attempt++) {
|
|
173
|
+
await nextTick()
|
|
174
|
+
await sleep(25)
|
|
175
|
+
if (queryOptions().length > 0) break
|
|
176
|
+
}
|
|
177
|
+
expect(queryOptions().map((el) => el.textContent)).toEqual(['启用'])
|
|
178
|
+
} finally {
|
|
179
|
+
unmount()
|
|
180
|
+
}
|
|
181
|
+
})
|
|
182
|
+
})
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
:placeholder="model?.length ? '' : placeholder"
|
|
42
42
|
:disabled="disabled"
|
|
43
43
|
@click.stop="handleInputClick"
|
|
44
|
-
@
|
|
44
|
+
@input="handleInput"
|
|
45
45
|
@keydown.enter.prevent="handleCreateByEnter"
|
|
46
46
|
/>
|
|
47
47
|
</div>
|
|
@@ -360,7 +360,11 @@ const handleInputClick = () => {
|
|
|
360
360
|
if (!dropdownVisible.value) dropdownRef.value?.open()
|
|
361
361
|
}
|
|
362
362
|
|
|
363
|
-
|
|
363
|
+
/**
|
|
364
|
+
* 键入过滤词时确保面板展开。不监听 focus:模态弹框打开时会把焦点移入面板内
|
|
365
|
+
* 第一个可聚焦元素(正是本输入框),监听 focus 会让下拉随弹框打开而自动展开。
|
|
366
|
+
*/
|
|
367
|
+
const handleInput = () => {
|
|
364
368
|
if (!dropdownVisible.value) dropdownRef.value?.open()
|
|
365
369
|
}
|
|
366
370
|
|
|
@@ -249,3 +249,44 @@ describe('UMultiTreeSelect labelTemplate', () => {
|
|
|
249
249
|
}
|
|
250
250
|
})
|
|
251
251
|
})
|
|
252
|
+
|
|
253
|
+
describe('UMultiTreeSelect dropdown opening', () => {
|
|
254
|
+
it('does not open the panel when the filter input is focused programmatically', async () => {
|
|
255
|
+
const { host, unmount } = mountMultiTreeSelect({ filterable: true })
|
|
256
|
+
|
|
257
|
+
try {
|
|
258
|
+
const input = host.querySelector<HTMLInputElement>('.u-multi-tree-select__input')!
|
|
259
|
+
expect(input).toBeTruthy()
|
|
260
|
+
|
|
261
|
+
// 模态弹框打开时会把焦点移入面板内第一个可聚焦元素(即本输入框),不应连带展开面板
|
|
262
|
+
input.dispatchEvent(new FocusEvent('focus'))
|
|
263
|
+
for (let attempt = 0; attempt < 6; attempt++) {
|
|
264
|
+
await nextTick()
|
|
265
|
+
await sleep(25)
|
|
266
|
+
}
|
|
267
|
+
expect(queryNodeContents().length).toBe(0)
|
|
268
|
+
} finally {
|
|
269
|
+
unmount()
|
|
270
|
+
}
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
it('opens the panel and keeps the first-keystroke filter when typing into the input', async () => {
|
|
274
|
+
const { host, unmount } = mountMultiTreeSelect({ filterable: true })
|
|
275
|
+
|
|
276
|
+
try {
|
|
277
|
+
const input = host.querySelector<HTMLInputElement>('.u-multi-tree-select__input')!
|
|
278
|
+
input.value = '亚'
|
|
279
|
+
input.dispatchEvent(new Event('input', { bubbles: true }))
|
|
280
|
+
|
|
281
|
+
// 面板懒渲染:键入后自动展开,且首字符过滤在展开后补一次,不被丢失
|
|
282
|
+
for (let attempt = 0; attempt < 20; attempt++) {
|
|
283
|
+
await nextTick()
|
|
284
|
+
await sleep(25)
|
|
285
|
+
if (queryNodeContents().length > 0) break
|
|
286
|
+
}
|
|
287
|
+
expect(queryNodeContents().map((el) => el.textContent)).toEqual(['亚洲'])
|
|
288
|
+
} finally {
|
|
289
|
+
unmount()
|
|
290
|
+
}
|
|
291
|
+
})
|
|
292
|
+
})
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
:placeholder="tags.length ? '' : placeholder"
|
|
42
42
|
:disabled="disabled"
|
|
43
43
|
@click.stop="handleInputClick"
|
|
44
|
-
@
|
|
44
|
+
@input="handleInput"
|
|
45
45
|
/>
|
|
46
46
|
</div>
|
|
47
47
|
<!-- 清空 icon -->
|
|
@@ -385,7 +385,16 @@ const handleInputClick = () => {
|
|
|
385
385
|
if (!dropdownVisible.value) dropdownRef.value?.open()
|
|
386
386
|
}
|
|
387
387
|
|
|
388
|
-
|
|
389
|
-
|
|
388
|
+
/**
|
|
389
|
+
* 键入过滤词时确保面板展开。不监听 focus:模态弹框打开时会把焦点移入面板内
|
|
390
|
+
* 第一个可聚焦元素(正是本输入框),监听 focus 会让下拉随弹框打开而自动展开。
|
|
391
|
+
* 面板为懒渲染,展开后需补一次过滤,否则首字符的过滤条件会丢失。
|
|
392
|
+
*/
|
|
393
|
+
const handleInput = () => {
|
|
394
|
+
if (dropdownVisible.value) return
|
|
395
|
+
dropdownRef.value?.open()
|
|
396
|
+
if (qs.value && typeof props.data !== 'function') {
|
|
397
|
+
nextTick(() => treeRef.value?.filter(qs.value))
|
|
398
|
+
}
|
|
390
399
|
}
|
|
391
400
|
</script>
|
|
@@ -116,7 +116,7 @@ watch(
|
|
|
116
116
|
if (sliderSize.value === 0) return
|
|
117
117
|
|
|
118
118
|
if (props.range) {
|
|
119
|
-
const next = v.map(sliderOffset2Value).
|
|
119
|
+
const next = v.map(sliderOffset2Value).sort((a, b) => a - b) as [number, number]
|
|
120
120
|
const current = props.modelValue as [number, number] | undefined
|
|
121
121
|
if (current) {
|
|
122
122
|
const [a, b] = [...current].sort((x, y) => x - y)
|
|
@@ -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