-
Notifications
You must be signed in to change notification settings - Fork 922
/
CodeGroup.ts
139 lines (123 loc) · 4.08 KB
/
CodeGroup.ts
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
import { defineComponent, h, onBeforeUpdate, ref } from 'vue'
import type { Component, SlotsType, VNode } from 'vue'
export const CodeGroup = defineComponent({
name: 'CodeGroup',
slots: Object as SlotsType<{
default: () => VNode[]
}>,
setup(_, { slots }) {
// index of current active item
const activeIndex = ref(-1)
// refs of the tab buttons
const tabRefs = ref<HTMLButtonElement[]>([])
if (__VUEPRESS_DEV__) {
// after removing a code-group-item, we need to clear the ref
// of the removed item to avoid issues caused by HMR
onBeforeUpdate(() => {
tabRefs.value = []
})
}
// activate next tab
const activateNext = (i = activeIndex.value): void => {
if (i < tabRefs.value.length - 1) {
activeIndex.value = i + 1
} else {
activeIndex.value = 0
}
tabRefs.value[activeIndex.value].focus()
}
// activate previous tab
const activatePrev = (i = activeIndex.value): void => {
if (i > 0) {
activeIndex.value = i - 1
} else {
activeIndex.value = tabRefs.value.length - 1
}
tabRefs.value[activeIndex.value].focus()
}
// handle keyboard event
const keyboardHandler = (event: KeyboardEvent, i: number): void => {
if (event.key === ' ' || event.key === 'Enter') {
event.preventDefault()
activeIndex.value = i
} else if (event.key === 'ArrowRight') {
event.preventDefault()
activateNext(i)
} else if (event.key === 'ArrowLeft') {
event.preventDefault()
activatePrev(i)
}
}
return () => {
// NOTICE: here we put the `slots.default()` inside the render function to make
// the slots reactive, otherwise the slot content won't be changed once the
// `setup()` function of current component is called
// get children code-group-item
const items = (slots.default?.() || [])
.filter((vnode) => (vnode.type as Component).name === 'CodeGroupItem')
.map((vnode) => {
if (vnode.props === null) {
vnode.props = {}
}
return vnode as VNode & { props: Exclude<VNode['props'], null> }
})
// do not render anything if there is no code-group-item
if (items.length === 0) {
return null
}
if (activeIndex.value < 0 || activeIndex.value > items.length - 1) {
// if `activeIndex` is invalid
// find the index of the code-group-item with `active` props
activeIndex.value = items.findIndex(
(vnode) => vnode.props.active === '' || vnode.props.active === true
)
// if there is no `active` props on code-group-item, set the first item active
if (activeIndex.value === -1) {
activeIndex.value = 0
}
} else {
// set the active item
items.forEach((vnode, i) => {
vnode.props.active = i === activeIndex.value
})
}
return h('div', { class: 'code-group' }, [
h(
'div',
{ class: 'code-group__nav' },
h(
'ul',
{ class: 'code-group__ul' },
items.map((vnode, i) => {
const isActive = i === activeIndex.value
return h(
'li',
{ class: 'code-group__li' },
h(
'button',
{
ref: (element) => {
if (element) {
tabRefs.value[i] = element as HTMLButtonElement
}
},
class: {
'code-group__nav-tab': true,
'code-group__nav-tab-active': isActive,
},
ariaPressed: isActive,
ariaExpanded: isActive,
onClick: () => (activeIndex.value = i),
onKeydown: (e) => keyboardHandler(e, i),
},
vnode.props.title
)
)
})
)
),
items,
])
}
},
})