Skip to content

Commit

Permalink
[WIP] Add keyboard focus border for dropdown options
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Ng <[email protected]>
  • Loading branch information
Pytal committed Jan 7, 2023
1 parent 8577536 commit 2edcb7f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/components/Select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@
isOptionDeselectable(option) && index === typeAheadPointer,
'vs__dropdown-option--selected': isOptionSelected(option),
'vs__dropdown-option--highlight': index === typeAheadPointer,
'vs__dropdown-option--kb-focus':
isKeyboardNavigation && index === typeAheadPointer,
'vs__dropdown-option--disabled': !selectable(option),
}"
:aria-selected="index === typeAheadPointer ? true : null"
@mouseover="selectable(option) ? (typeAheadPointer = index) : null"
@mouseover="onMouseOver(option, index)"
@click.prevent.stop="selectable(option) ? select(option) : null"
>
<slot name="option" v-bind="normalizeOptionForSlot(option)">
Expand Down Expand Up @@ -675,6 +677,7 @@ export default {
search: '',
open: false,
isComposing: false,
isKeyboardNavigation: false,
pushedTags: [],
// eslint-disable-next-line vue/no-reserved-keys
_value: [], // Internal value managed by Vue Select if no `value` prop is passed
Expand Down Expand Up @@ -1309,6 +1312,20 @@ export default {
this.mousedown = false
},
/**
* Event-Handler for option mouseover
* @param {Object|String} option
* @param {Number} index
* @return {void}
*/
onMouseOver(option, index) {
this.isKeyboardNavigation = false
if (!this.selectable(option)) {
return
}
this.typeAheadPointer = index
},
/**
* Search <input> KeyBoardEvent handler.
* @param {KeyboardEvent} e
Expand All @@ -1330,6 +1347,7 @@ export default {
// up.prevent
38: (e) => {
e.preventDefault()
this.isKeyboardNavigation = true
if (!this.open) {
this.open = true
return
Expand All @@ -1339,6 +1357,7 @@ export default {
// down.prevent
40: (e) => {
e.preventDefault()
this.isKeyboardNavigation = true
if (!this.open) {
this.open = true
return
Expand Down
5 changes: 4 additions & 1 deletion src/css/global/variables.css
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@
--vs-dropdown-option-padding: 3px 20px;

/* Active State */
--vs-dropdown-option--active-bg: #5897fb;
--vs-dropdown-option--active-bg: #136CFB;
--vs-dropdown-option--active-color: #fff;

/* Keyboard Focus State */
--vs-dropdown-option--kb-focus-box-shadow: inset 0px 0px 0px 2px #2B2B2B;

/* Deselect State */
--vs-dropdown-option--deselect-bg: #fb5858;
--vs-dropdown-option--deselect-color: #fff;
Expand Down
4 changes: 4 additions & 0 deletions src/css/modules/dropdown-option.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
color: var(--vs-dropdown-option--active-color);
}

.vs__dropdown-option--kb-focus {
box-shadow: var(--vs-dropdown-option--kb-focus-box-shadow);
}

.vs__dropdown-option--deselect {
background: var(--vs-dropdown-option--deselect-bg);
color: var(--vs-dropdown-option--deselect-color);
Expand Down

0 comments on commit 2edcb7f

Please sign in to comment.