-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathvue3-tabs-chrome.vue
802 lines (716 loc) · 18 KB
/
vue3-tabs-chrome.vue
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
<template>
<div class="vue3-tabs-chrome">
<div class="tabs-content" :ref="setContentRef">
<div
class="tabs-item"
v-for="(tab, i) in tabs"
:class="{ active: tab.key === modelValue }"
:key="tab.key"
:style="{ width: tabWidth + 'px' }"
:ref="e => setTabRef(e, tab)"
@contextmenu="e => handleContextMenu(e, tab, i)"
>
<div class="tabs-background">
<div class="tabs-background-divider"></div>
<div class="tabs-background-content"></div>
<svg class="tabs-background-before" width="7" height="7">
<path d="M 0 7 A 7 7 0 0 0 7 0 L 7 7 Z"></path>
</svg>
<svg class="tabs-background-after" width="7" height="7">
<path d="M 0 0 A 7 7 0 0 0 7 7 L 0 7 Z"></path>
</svg>
</div>
<div class="tabs-close" @click.stop="handleDelete(tab, i)" v-show="showTabCloseIcon(tab)">
<svg class="tabs-close-icon" width="16" height="16" stroke="#595959">
<path d="M 4 4 L 12 12 M 12 4 L 4 12"></path>
</svg>
</div>
<div class="tabs-main" :title="tab.label">
<span class="tabs-favico" v-if="tab.favico">
<render-temp v-if="typeof tab.favico === 'function'" :render="tab.favico" :params="[tab, i]" />
<img v-else-if="tab.favico" :src="tab.favico" alt="" />
</span>
<span class="tabs-label" :class="{ 'no-close': !showTabCloseIcon(tab), 'no-icon': !tab.favico }">
<render-temp v-if="typeof renderLabel === 'function'" :render="renderLabel" :params="[tab, i]" />
<template v-else>{{ tab.label }}</template>
</span>
</div>
</div>
<span class="tabs-after" :ref="setAfterRef" :style="{ left: (tabWidth - gap * 2) * tabs.length + gap * 2 + 'px' }">
<slot name="after" />
</span>
</div>
</div>
</template>
<script lang="ts">
import RenderTemp from './render-temp.vue'
import Draggabilly from 'draggabilly'
import { defineComponent, ref, reactive, onMounted, PropType, nextTick, h, onUnmounted, ComponentPublicInstance } from 'vue'
export interface Tab {
/** 显示名称 */
label: string
/** 唯一 key */
key: string
favico?: string
/**
* 是否可关闭
*/
closable?: boolean
/**
* 是否可被交换
*/
swappable?: boolean
/**
* 是否可拖拽
*/
dragable?: boolean
$el?: HTMLElement
// eslint-disable-next-line
_instance?: any
_x?: number
}
export interface Refs {
[key: string]: Element | null
}
export default defineComponent({
name: 'VueTabsChrome',
components: { RenderTemp },
emits: ['click', 'update:modelValue', 'remove', 'dragstart', 'dragging', 'dragend', 'swap', 'contextmenu'],
props: {
modelValue: {
type: [String, Number],
default: ''
},
tabs: {
type: Array as PropType<Tab[]>,
default: () => []
},
/**
* 当宽度小于设置的值时,会自动隐藏关闭按钮
*/
autoHiddenCloseIconWidth: {
type: Number,
default: 120
},
/**
* tab 的最小宽度
*/
minWidth: {
type: Number,
default: 40
},
/**
* tab 的最大宽度
*/
maxWidth: {
type: Number,
default: 245
},
/**
* 两个相邻的 tab 的空隙大小
*/
gap: {
type: Number,
default: 7
},
/**
* 关闭事件
*/
onClose: {
type: Function
},
/**
* 新 tab 追加时,是否追加到当前 tab 之后
*/
insertToAfter: {
type: Boolean,
default: false
},
/**
* 鼠标按下时,是否自动将命中的 tab 设置为激活状态
*/
isMousedownActive: {
type: Boolean,
default: true
},
/**
* 自定义渲染 label
*/
renderLabel: {
type: Function
}
},
setup(props, context) {
const $refs = reactive<Refs>({})
const tabWidth = ref<number>(0)
/**
* 计算单个 tab 的宽度
*/
const calcTabWidth = () => {
const { tabs, minWidth, maxWidth, gap } = props
const { $content } = $refs
const afterWidth = $refs.$after?.getBoundingClientRect().width || 0
if (!$content) return Math.max(maxWidth, minWidth)
const contentWidth: number = $content.clientWidth - gap * 3 - afterWidth
let width: number = contentWidth / tabs.length
width += gap * 2
if (width > maxWidth) width = maxWidth
if (width < minWidth) width = minWidth
tabWidth.value = width
}
/**
* 拖拽开始事件
* @param e 拖拽事件
* @param tab 当前正在拖拽的 tab
* @param i 当前拖拽的下标
*/
const handlePointerDown = (e: Event, tab: Tab, i: number) => {
const { emit } = context
const { isMousedownActive } = props
// 如果允许按下就 active,才命中
if (isMousedownActive) emit('update:modelValue', tab.key)
emit('update:modelValue', tab.key)
emit('dragstart', e, tab, i)
}
/**
* 拖拽事件监听
* @param e 拖拽事件
* @param tab 当前正在拖拽的 tab
* @param i 当前拖拽的下标
*/
const handleDragMove = (e: Event, tab: Tab, i: number) => {
const { tabs, gap } = props
const { emit } = context
if (tab.swappable === false) {
return
}
// 获取一半 tab 宽度
const halfWidth = (tabWidth.value - gap) / 2
// 获取 tab 当前的 x 值
const { x } = tab._instance.position
let swapTab: Tab | null = null
for (let i = 0; i < tabs.length; i++) {
const currentTab: Tab = tabs[i]
const targetX: number = (currentTab._x || 1) - 1
// 如果命中自己本身,则无需交换
if (tab.key === currentTab.key) {
// eslint-disable-next-line no-continue
continue
}
// 判断是否有重叠的 tab,只需要判定是否在前半部分即可
if (targetX <= x && x < targetX + halfWidth) {
swapTab = currentTab
swapTabs(tab, swapTab)
break
}
}
emit('dragging', e, tab, i)
}
/**
* 交换俩 tab
* @param tab 当前 tab
* @param swapTab 需要交换的 tab
*/
const swapTabs = (tab: Tab, swapTab: Tab) => {
if (swapTab.swappable === false) {
return
}
const { tabs } = props
const { emit } = context
let index = -1
let swapIndex = -1
for (let i = 0; i < tabs.length; i++) {
const obj: Tab = tabs[i]
if (obj.key === tab.key) {
index = i
}
if (obj.key === swapTab.key) {
swapIndex = i
}
}
if (index < 0 || swapIndex < 0 || index === swapIndex) {
return
}
// eslint-disable-next-line
;[tabs[index], tabs[swapIndex]] = [tabs[swapIndex], tabs[index]]
// swap x
const { _x } = tab
tab._x = swapTab._x
swapTab._x = _x
// swap position
const { _instance } = swapTab
setTimeout(() => {
_instance.element.classList.add('move')
_instance.setPosition(_x, _instance.position.y)
}, 50)
setTimeout(() => {
_instance.element.classList.remove('move')
emit('swap', tab, swapTab)
}, 200)
}
/**
* 拖拽完成监听
* @param e 拖拽事件
* @param tab 命中的 tab
* @param i 当前拖拽的下标
*/
const handleDragEnd = (e: Event, tab: Tab, i: number) => {
const { _instance } = tab
const { emit } = context
if (_instance.position.x === 0) return
setTimeout(() => {
_instance.element.classList.add('move')
_instance.setPosition(tab._x, 0)
}, 50)
setTimeout(() => {
_instance.element.classList.remove('move')
emit('dragend', e, tab, i)
}, 200)
return false
}
/**
* 单击事件监听
* @param e 单击事件
* @param tab 命中的 tab
* @param i 当前单击的下标
*/
const handleClick = (e: Event, tab: Tab, i: number) => {
const { emit } = context
emit('click', e, tab, i)
}
/**
* 右键事件监听
* @param e 右键事件
* @param tab 命中的 tab
* @param i 当前右键的下标
*/
const handleContextMenu = (e: Event, tab: Tab, i: number) => {
const { emit } = context
emit('contextmenu', e, tab, i)
}
/**
* 删除事件
* @param tab 当前命中 tab
* @param i 当前命中 tab 的下标
*/
const handleDelete = (tab: Tab, i: number) => {
const { tabs, modelValue, onClose } = props
const { emit } = context
const index = tabs.findIndex(item => item.key === modelValue)
// 可以通过 onClose 返回 false 来主动阻止事件
if (typeof onClose === 'function' && onClose(tab, tab.key, i) === false) {
return false
}
let after, before
if (i === index) {
after = tabs[i + 1]
before = tabs[i - 1]
}
if (after) {
emit('update:modelValue', after.key)
} else if (before) {
emit('update:modelValue', before.key)
} else if (tabs.length <= 1) {
emit('update:modelValue', null)
}
tabs.splice(i, 1)
emit('remove', tab, i)
nextTick(() => {
doLayout()
})
}
/**
* 主动添加 tab
* @param newTabs 用户需要添加的 tab
*/
const addTab = (...newTabs: Array<Tab>) => {
const { insertToAfter, modelValue, tabs } = props
if (insertToAfter) {
const i = tabs.findIndex(tab => tab.key === modelValue)
tabs.splice(i + 1, 0, ...newTabs)
} else {
tabs.push(...newTabs)
}
nextTick(() => {
init()
doLayout()
})
}
/**
* 主动移除 tab
* @param tabKey 如果为数字则判定为用下标删除
*/
const removeTab = (tabKey: string | number) => {
const { tabs } = props
if (typeof tabKey === 'number') {
const index: number = tabKey
const tab = tabs[index]
handleDelete(tab, index)
} else {
const index: number = tabs.findIndex(item => item.key === tabKey)
const tab: Tab | undefined = tabs.find(item => item.key === tabKey)
if (tab) {
handleDelete(tab, index)
}
}
}
// 计时器
let timer: number
/**
* 窗口改变,重新布局
*/
const handleResize = () => {
if (timer) window.clearTimeout(timer)
timer = window.setTimeout(() => {
doLayout()
}, 100)
}
/**
* 判断关闭按钮是否展示
*/
const showTabCloseIcon = (tab: Tab) => {
const { modelValue, autoHiddenCloseIconWidth } = props
if (tab.closable === false) {
return false
}
if (tab.key === modelValue) {
return true
}
if (autoHiddenCloseIconWidth > tabWidth.value) {
return false
}
return true
}
/**
* 渲染文本
*/
const renderLabelText = (tab: Tab) => {
const { renderLabel } = props
if (renderLabel) {
return renderLabel(tab)
}
return h('span', tab.label)
}
/**
* 重新调整 tab 位置
*/
const doLayout = () => {
calcTabWidth()
const { tabs, gap } = props
tabs.forEach((tab, i) => {
const instance = tab._instance
const _x = (tabWidth.value - gap * 2) * i
tab._x = _x
instance.setPosition(_x, 0)
})
}
/**
* 添加 tab 实例
* @param tab 当前命中 tab
* @param i 当前命中 tab 的下标
*/
const addInstance = (tab: Tab, i: number) => {
const { gap } = props
// 如果已经存在实例,则重新设置位置
if (tab._instance) {
tab._instance.setPosition(tab._x, 0)
return
}
// 如果不存在 dom 元素,则无需设置
if (!tab.$el || !$refs.$content) {
return
}
// 添加实例
tab._instance = new Draggabilly(tab.$el, {
axis: 'x',
containment: $refs.$content,
handle: '.tabs-main'
})
if (tab.dragable === false) {
tab._instance.disable()
}
// 计算实际 x 值
const x = (tabWidth.value - gap * 2) * i
// 记录 x 位置到 tab 上
tab._x = x
// 设置位置
tab._instance.setPosition(x, 0)
// 绑定拖拽事件
tab._instance.on('pointerDown', (e: Event) => handlePointerDown(e, tab, i))
tab._instance.on('dragMove', (e: Event) => handleDragMove(e, tab, i))
tab._instance.on('dragEnd', (e: Event) => handleDragEnd(e, tab, i))
tab._instance.on('staticClick', (e: Event) => handleClick(e, tab, i))
}
/**
* 初始化,为 tab 添加实例
*/
const init = () => {
props.tabs.forEach((tab: Tab, i: number) => {
addInstance(tab, i)
})
}
/**
* 为 Tab 添加 dom 节点
* @param el 当前 tab 对应的 dom 元素
* @param tab 当前命中 tab
*/
const setTabRef = (el: Element | ComponentPublicInstance | null, tab: Tab) => {
if (el) {
tab.$el = el as HTMLElement
}
}
/**
* 添加容器 dom 节点
* @param el tab 对应的 dom 父元素
*/
const setContentRef = (el: Element | ComponentPublicInstance | null) => {
if (el) {
$refs.$content = el as Element
}
}
/**
* 添加后缀元素 dom 节点
* @param el 在 tab 后面的元素
*/
const setAfterRef = (el: Element | ComponentPublicInstance | null) => {
if (el) {
$refs.$after = el as Element
}
}
onMounted(() => {
calcTabWidth()
init()
window.addEventListener('resize', handleResize)
})
onUnmounted(() => {
window.removeEventListener('resize', handleResize)
if (timer) window.clearTimeout(timer)
})
return {
setTabRef,
setContentRef,
setAfterRef,
tabWidth,
handleDelete,
handleContextMenu,
showTabCloseIcon,
renderLabelText,
doLayout,
addTab,
removeTab
}
}
})
</script>
<style lang="less">
.vue3-tabs-chrome {
@bg: #dee1e6;
@gap: 7px;
@divider: #a9adb0;
@speed: 150ms;
padding-top: 10px;
background-color: @bg;
position: relative;
.tabs-content {
height: 34px;
position: relative;
overflow: hidden;
}
/* divider */
.tabs-divider {
left: 0;
top: 50%;
width: 1px;
height: 20px;
background-color: @divider;
position: absolute;
transform: translateY(-50%);
}
.tabs-item {
height: 100%;
display: flex;
align-items: center;
user-select: none;
box-sizing: border-box;
// transition: width @speed;
position: absolute;
&:hover {
z-index: 1;
.tabs-background-divider {
display: none;
}
.tabs-background-content {
background-color: #f2f3f5;
}
.tabs-background-before,
.tabs-background-after {
fill: #f2f3f5;
}
}
&.move {
transition: @speed;
}
&.is-dragging {
z-index: 3;
.tabs-background-content {
background-color: #f2f3f5;
}
.tabs-background-divider {
display: none;
}
.tabs-background-before,
.tabs-background-after {
fill: #f2f3f5;
}
}
&.active {
z-index: 2;
.tabs-close {
background-color: #fff;
}
.tabs-background-divider {
display: none;
}
.tabs-background-content {
background-color: #fff;
}
.tabs-background-before,
.tabs-background-after {
fill: #fff;
}
}
&:first-of-type {
.tabs-background-divider::before {
display: none;
}
}
}
.tabs-main {
height: 100%;
left: 0;
right: 0;
margin: 0 @gap * 2;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
transition: @speed;
display: flex;
align-items: center;
position: absolute;
box-sizing: border-box;
overflow: hidden;
}
.tabs-close {
top: 50%;
right: @gap * 2;
width: 16px;
height: 16px;
z-index: 1;
position: absolute;
transform: translateY(-50%);
}
.tabs-close-icon {
width: 100%;
height: 100%;
border-radius: 50%;
&:hover {
stroke: #000;
background-color: #e8eaed;
}
}
.tabs-favico {
height: 16px;
margin-left: 3%;
display: flex;
align-items: center;
overflow: hidden;
img {
height: 100%;
}
}
.tabs-label {
flex: 1;
margin-left: 5%;
margin-right: 16px;
box-sizing: border-box;
overflow: hidden;
white-space: nowrap;
position: relative;
&.no-close {
margin-right: 0;
}
&.no-icon {
margin-left: 0;
}
}
.tabs-background {
width: 100%;
height: 100%;
padding: 0 @gap - 1px;
position: absolute;
box-sizing: border-box;
}
.tabs-background-divider {
left: 0;
width: calc(100% - 14px);
height: 100%;
margin: 0 7px;
position: absolute;
&::before {
content: '';
top: 20%;
right: 100%;
width: 1px;
height: 60%;
background-color: #81878c;
position: absolute;
}
&::after {
content: '';
top: 20%;
left: calc(100% - 1px);
width: 1px;
height: 60%;
background-color: #81878c;
position: absolute;
}
}
.tabs-background-content {
height: 100%;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
transition: @speed;
}
.tabs-background-before,
.tabs-background-after {
bottom: -1px;
position: absolute;
fill: transparent;
transition: @speed;
}
.tabs-background-before {
left: -1px;
}
.tabs-background-after {
right: -1px;
}
.tabs-footer {
height: 4px;
background-color: #fff;
}
.tabs-after {
top: 50%;
display: flex;
position: absolute;
overflow: hidden;
transform: translateY(-50%);
}
@keyframes tab-show {
from {
transform: scaleX(0);
}
to {
transform: scaleX(1);
}
}
}
</style>