forked from swiftlang/swift-docc-render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDropdownCustom.vue
321 lines (292 loc) · 8.17 KB
/
DropdownCustom.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
<!--
This source file is part of the Swift.org open source project
Copyright (c) 2021 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
-->
<template>
<BaseDropdown
:value="value"
:class="{ [OpenedClass]: isOpen, 'dropdown-small': isSmall }"
class="dropdown-custom"
>
<template #dropdown="{ dropdownClasses }">
<span :id="`DropdownLabel_${_uid}`" class="visuallyhidden">{{ ariaLabel }}</span>
<button
ref="dropdownToggle"
:id="`DropdownToggle_${_uid}`"
:class="dropdownClasses"
class="form-dropdown-toggle"
:aria-labelledby="`DropdownLabel_${_uid} DropdownToggle_${_uid}`"
:aria-expanded="isOpen ? 'true' : 'false'"
aria-haspopup="true"
@click="toggleDropdown"
@keydown.enter.prevent="openDropdown"
@keydown.esc="closeAndFocusToggler"
@keydown.down.prevent="openDropdown"
@keydown.up.prevent="openDropdown"
>
<span class="form-dropdown-title">{{ value }}</span>
<slot name="toggle-post-content" />
</button>
</template>
<template #eyebrow>
<slot name="eyebrow" />
</template>
<template #after>
<slot
v-bind="{
value,
isOpen,
contentClasses: ['form-dropdown-content', { 'is-open' : isOpen }],
closeDropdown,
onChangeAction,
closeAndFocusToggler,
navigateOverOptions,
OptionClass,
ActiveOptionClass,
}"
/>
</template>
</BaseDropdown>
</template>
<script>
import BaseDropdown from 'docc-render/components/BaseDropdown.vue';
const OpenedClass = 'is-open';
const OptionClass = 'option';
const ActiveOptionClass = 'option-active';
export default {
name: 'DropdownCustom',
components: { BaseDropdown },
constants: {
OpenedClass,
OptionClass,
ActiveOptionClass,
},
props: {
value: {
type: String,
default: '',
},
ariaLabel: {
type: String,
default: '',
},
isSmall: {
type: Boolean,
default: false,
},
},
data() {
return {
isOpen: false,
OpenedClass,
OptionClass,
ActiveOptionClass,
};
},
mounted() {
document.addEventListener('click', this.closeOnLoseFocus);
},
beforeDestroy() {
document.removeEventListener('click', this.closeOnLoseFocus);
},
methods: {
onChangeAction(value) {
this.$emit('input', value);
},
toggleDropdown() {
if (this.isOpen) {
this.closeDropdown();
} else {
this.openDropdown();
}
},
async closeAndFocusToggler() {
this.closeDropdown();
await this.$nextTick();
this.$refs.dropdownToggle.focus({ preventScroll: true });
},
closeDropdown() {
this.isOpen = false;
this.$emit('close');
},
openDropdown() {
this.isOpen = true;
this.$emit('open');
this.focusActiveLink();
},
closeOnLoseFocus(e) {
if (!this.$el.contains(e.target) && this.isOpen) {
this.closeDropdown();
}
},
/**
* Scrolls over the options whe clicking up/down arrows.
* @param {HTMLElement} target
* @param {Number} step
*/
navigateOverOptions({ target }, step) {
// get all options
const allOptions = this.$el.querySelectorAll(`.${OptionClass}`);
// convert to an array
const allOptionsArray = Array.from(allOptions);
// find the current index the target is in
const targetIndex = allOptionsArray.indexOf(target);
// get the next element
const nextElement = allOptionsArray[targetIndex + step];
if (!nextElement) {
return;
}
// focus the new element
nextElement.focus({ preventScroll: true });
},
/**
* Focuses the currently active option
* @return {Promise<void>}
*/
async focusActiveLink() {
const currentOption = this.$el.querySelector(`.${ActiveOptionClass}`);
if (!currentOption) return;
await this.$nextTick();
currentOption.focus({ preventScroll: true });
},
},
};
</script>
<style scoped lang='scss'>
@import "docc-render/styles/_core.scss";
$focus-shadow: 0 0 0 2px var(--color-focus-color);
// applies extra styling to make the dropdown look smaller
.dropdown-small {
height: 30px;
display: flex;
align-items: center;
position: relative;
background: var(--color-fill);
// overwrite the typography and focus styles on the toggle
.form-dropdown-toggle {
line-height: 1.5;
font-size: 12px;
padding-top: 0;
padding-bottom: 0;
padding-left: 20px;
min-height: unset;
height: 30px;
display: flex;
align-items: center;
&:focus {
box-shadow: none;
border-color: var(--color-dropdown-border);
}
// override the outline to a smaller glow on focus from keyboard
@include on-keyboard-focus() {
box-shadow: $focus-shadow;
outline: none;
border-color: var(--color-focus-border-color);
}
}
}
.form-dropdown-toggle {
margin: 0;
// on open remove the bottom border and border radius, so it merges with the content.
.is-open & {
border-radius: $border-radius $border-radius 0 0;
// remove bottom border and compensate for the spacing
border-bottom: none;
padding-bottom: 1px;
// hack to apply focus style to the toggle button, when open but focusing the content.
.fromkeyboard & {
// apply outline everywhere, except the bottom.
box-shadow: 1px -1px 0 1px var(--color-focus-color),
-1px -1px 0 1px var(--color-focus-color);
border-color: var(--color-focus-border-color);
}
}
}
// style the content inside the toggle
.form-dropdown-title {
margin: 0;
padding: 0;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
// the wrapping in `dropdown-custom` is needed to properly apply the :deep() selector
.dropdown-custom {
border-radius: $border-radius;
&.is-open {
border-radius: $border-radius $border-radius 0 0;
}
// style the dropdown content holder
:deep(.form-dropdown-content) {
background: var(--color-fill);
position: absolute;
right: 0;
left: 0;
top: 100%;
border-bottom-left-radius: $border-radius;
border-bottom-right-radius: $border-radius;
border: 1px solid var(--color-dropdown-border);
border-top: none;
display: none;
overflow-y: auto;
&.is-open {
display: block;
.fromkeyboard & {
// hack to apply focus outline everywhere, except the top.
box-shadow: 1px 1px 0 1px var(--color-focus-color),
-1px 1px 0 1px var(--color-focus-color);
border-color: var(--color-focus-border-color);
border-top-color: transparent;
}
}
// if wrapped inside a nav
// add nav-height to bottom-spacing for more space
// in case user has multiple tabs open
$bottom-spacing: 72px;
.nav & {
// At initial page load, we take away 20px along with the height of both
// the nav to make the dropdown not exceed the height of the
// breakpoint (20px is visual space between the bottom of the dropdown and the
// bottom of the page).
max-height: calc(100vh - #{$globalnav-height} - #{$nav-height} - #{$bottom-spacing});
@include nav-is-sticking($nested: true) {
// When the nav is sticking, we no longer need to account for the
// height of the global nav.
max-height: calc(100vh - #{$nav-height} - #{$bottom-spacing});
}
}
}
// style the options
:deep(.options) {
list-style: none;
margin: 0;
padding: 0 0 20px;
}
// style each option
:deep(.option) {
cursor: pointer;
padding: 5px 20px;
font-size: 12px;
line-height: 20px;
outline: none;
&:hover {
background-color: var(--color-fill-tertiary);
}
&.option-active {
font-weight: $font-weight-semibold;
}
.fromkeyboard & {
&:hover {
background-color: transparent;
}
&:focus {
background-color: var(--color-fill-tertiary);
outline: none;
}
}
}
}
</style>