Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QCascader, QButton, QBreadcrumbs] Add more accessibility #36

Merged
merged 7 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/qComponents/QBreadcrumbs/src/q-breadcrumbs.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
white-space: nowrap;
text-overflow: ellipsis;

&:focus {
text-decoration: underline;
}

&_last:first-child {
color: var(--color-primary-black);
}
Expand Down
21 changes: 21 additions & 0 deletions src/qComponents/QButton/src/q-button.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@
margin-left: 16px;
}

&:focus {
outline: none;
}

&:hover {
background-color: var(--color-primary);
background-image: none;
box-shadow: -1px -1px 4px rgba(var(--color-rgb-white), 0.25),
1px 1px 4px rgba(var(--color-rgb-blue), 0.4),
Expand Down Expand Up @@ -76,6 +81,13 @@
}

&_theme {
&_primary {
&:focus {
ViZhe marked this conversation as resolved.
Show resolved Hide resolved
background-color: var(--color-primary-darker);
background-image: none;
}
}

&_secondary {
color: var(--color-primary-blue);
background-color: var(--color-tertiary-gray);
Expand All @@ -92,6 +104,11 @@
outline: none;
box-shadow: var(--box-shadow-pressed);
}

&:focus {
color: var(--color-tertiary-white);
background-color: var(--color-primary-darker);
}
}

&_link {
Expand All @@ -103,6 +120,10 @@
background-image: none;
box-shadow: none;

&:focus {
text-decoration: underline;
}

&:hover {
color: var(--color-primary-black);
background-color: transparent;
Expand Down
75 changes: 63 additions & 12 deletions src/qComponents/QCascader/src/QCascader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
:placeholder="calcPlaceholder"
:disabled="isDisabled"
:validate-event="false"
aria-haspopup="true"
role="button"
:aria-controls="id"
:aria-expanded="popper"
:class="{
'q-input_focus': Boolean(popper),
'q-input_hover': areTagsHovered
}"
@focus="handleFocus"
@blur="handleBlur"
@mouseenter.native="handleMouseEnter"
@mouseleave.native="showClose = false"
@focus="handleInputFocus"
@click="togglePopper"
>
<template slot="suffix">
Expand Down Expand Up @@ -103,7 +106,7 @@
<script>
import { createPopper } from '@popperjs/core';
import Emitter from '../../mixins/emitter';
import { addResizeListener, removeResizeListener } from '../../helpers';
import { addResizeListener, removeResizeListener, randId } from '../../helpers';

import QCascaderPanel from './QCascaderPanel';

Expand Down Expand Up @@ -252,7 +255,9 @@ export default {
inputInitialHeight: 0,
popper: null,
showClose: false,
areTagsHovered: false
areTagsHovered: false,
focus: false,
id: null
};
},

Expand Down Expand Up @@ -324,18 +329,63 @@ export default {
}
},

created() {
this.id = randId('q-cascader-');
},

mounted() {
const { input } = this.$refs;
this.inputInitialHeight = input?.$el?.offsetHeight ?? DEFAULT_INPUT_HEIGHT;

addResizeListener(this.$el, this.updateStyle);
document.addEventListener('keyup', this.handleKeyUp, true);
},

beforeDestroy() {
removeResizeListener(this.$el, this.updateStyle);
document.removeEventListener('keyup', this.handleKeyUp, true);
},

methods: {
handleKeyUp(e) {
if (!this.focus) return;
if (e.target.classList.contains('q-input__inner') && e.key === 'Enter') {
this.togglePopper();
}

switch (e.key) {
case 'Escape': {
this.$refs.input.blur();
this.hidePopper();
break;
}

case 'Backspace': {
this.deleteTag();
break;
}

case 'Tab': {
if (!this.$refs.reference.contains(document.activeElement)) {
this.hidePopper();
this.focus = false;
}
break;
}

case 'ArrowRight':
case 'ArrowUp':
case 'ArrowLeft':
case 'ArrowDown': {
this.$refs.panel.navigateFocus(e);
break;
}

default:
break;
}
},

handleTagsHover() {
this.areTagsHovered = true;
this.showClose = true;
Expand All @@ -353,9 +403,14 @@ export default {
}
},

deleteTag({ value }) {
deleteTag({ value } = {}) {
if (!this.checkedValues) return;
const result = new Set(this.checkedValues);
result.delete(value);
if (value) {
result.delete(value);
} else {
result.delete(this.checkedValues[this.checkedValues.length - 1]);
}
const payload = Array.from(result);
this.emit(payload.length ? payload : null);
},
Expand Down Expand Up @@ -417,14 +472,10 @@ export default {
}
},

handleFocus(e) {
handleInputFocus(e) {
this.$emit('focus', e);
this.focus = true;
},

handleBlur(e) {
this.$emit('blur', e);
},

updateStyle() {
const { $el, inputInitialHeight } = this;
if (!$el) return;
Expand Down
23 changes: 21 additions & 2 deletions src/qComponents/QCascader/src/QCascaderMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@
<div
v-for="(node, key) in nodes"
v-else
:id="`${cascader.id}-node-${index}-${key}`"
:key="key"
:class="getNodeClass(node)"
@click="e => handleExpand(e, node)"
role="menuitem"
tabindex="-1"
@click="e => expandMenuItem(e, node)"
@keyup.enter="e => handleEnterKeyUp(e, node, index, key)"
@keyup.right="e => expandMenuItem(e, node)"
>
<q-checkbox
v-if="cascader.multiple"
:ref="`QCheckbox${index}${key}`"
input-tab-index="-1"
:value="getChecked(node)"
:indeterminate="getIndeterminate(node)"
@change="isChecked => handleValueChange(node, isChecked)"
Expand Down Expand Up @@ -110,6 +117,17 @@ export default {
},

methods: {
handleEnterKeyUp(e, node, index, key) {
if (this.cascader.multiple) {
const QCheckboxInstance = this.$refs[`QCheckbox${index}${key}`]?.[0];
if (QCheckboxInstance) {
QCheckboxInstance.nativeClick();
}
return;
}

this.expandMenuItem(e, node);
},
getIndeterminate(node) {
if (
!this.cascader.checkedValues ||
Expand Down Expand Up @@ -220,7 +238,8 @@ export default {
};
},

handleExpand(e, value) {
expandMenuItem(e, value) {
if (e.key === 'ArrowRight' && !value.children) return;
if (!value.children && !this.cascader.multiple) {
this.cascader.emit(value.value);
}
Expand Down
66 changes: 65 additions & 1 deletion src/qComponents/QCascader/src/QCascaderPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
<div class="q-cascader-panel">
<q-cascader-menu
v-for="(menu, index) in menus"
ref="menu"
:key="index"
role="menu"
:aria-labelledby="cascader.id"
:index="index"
:nodes="menu"
@expand="handleExpand"
Expand Down Expand Up @@ -65,6 +66,69 @@ export default {
},

methods: {
navigateFocus(e) {
if (e.target.classList.contains('q-input__inner')) {
const firstNode = this.$el.querySelector(
`#${this.cascader.id}-node-0-0`
);
if (firstNode) {
firstNode.focus();
}
}

if (!e.target.classList.contains('q-cascader-node')) return;
const nodeText = e.target.innerText;
let nodeIndex;
let currentNodePosition;
this.menus.forEach((menu, menuIndex) => {
nodeIndex = menu.findIndex(node => node.label === nodeText);
if (nodeIndex > -1) {
currentNodePosition = [menuIndex, nodeIndex];
}
});

let nextNodePosition;

switch (e.key) {
case 'ArrowRight':
nextNodePosition = [
currentNodePosition[0] + 1,
currentNodePosition[1]
];
break;
case 'ArrowLeft':
nextNodePosition = [
currentNodePosition[0] === 0 ? 0 : currentNodePosition[0] - 1,
currentNodePosition[1]
];

break;

case 'ArrowUp':
nextNodePosition = [
currentNodePosition[0],
currentNodePosition[1] === 0 ? 0 : currentNodePosition[1] - 1
];
break;

case 'ArrowDown':
nextNodePosition = [
currentNodePosition[0],
currentNodePosition[1] + 1
];
break;
default:
break;
}

const node = this.$el.querySelector(
`#${this.cascader.id}-node-${nextNodePosition[0]}-${nextNodePosition[1]}`
);

if (node) {
node.focus();
}
},
handleExpand(index, activeValue) {
if (this.cascader.multiple && !activeValue.children) return;
let nextLevel;
Expand Down
10 changes: 9 additions & 1 deletion src/qComponents/QCascader/src/q-cascader-menu.scss
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@
}
}

&:focus,
&_active {
position: relative;
color: var(--color-primary-black);
background-color: var(--color-tertiary-gray-ultra-light);
box-shadow: -1px -1px 3px rgba(var(--color-rgb-white), 0.25),
1px 1px 3px rgba(var(--color-rgb-blue), 0.4);

Expand All @@ -123,6 +123,14 @@
}
}

&_active {
background-color: var(--color-tertiary-gray-ultra-light);
}

&:focus {
background-color: var(--color-tertiary-gray);
}

&_disabled {
color: var(--color-opacity-gray-dark);
cursor: not-allowed;
Expand Down
Loading