|
@@ -6,7 +6,11 @@
|
|
|
:class="[popperClass, content && 'el-popover--plain']"
|
|
|
ref="popper"
|
|
|
v-show="!disabled && showPopper"
|
|
|
- :style="{ width: width + 'px' }">
|
|
|
+ :style="{ width: width + 'px' }"
|
|
|
+ role="tooltip"
|
|
|
+ :id="tooltipId"
|
|
|
+ :aria-hidden="(disabled || !showPopper) ? 'true' : 'false'"
|
|
|
+ >
|
|
|
<div class="el-popover__title" v-if="title" v-text="title"></div>
|
|
|
<slot>{{ content }}</slot>
|
|
|
</div>
|
|
@@ -14,10 +18,10 @@
|
|
|
<slot name="reference"></slot>
|
|
|
</span>
|
|
|
</template>
|
|
|
-
|
|
|
<script>
|
|
|
import Popper from 'element-ui/src/utils/vue-popper';
|
|
|
import { on, off } from 'element-ui/src/utils/dom';
|
|
|
+import { generateId } from 'element-ui/src/utils/util';
|
|
|
|
|
|
export default {
|
|
|
name: 'ElPopover',
|
|
@@ -49,6 +53,11 @@ export default {
|
|
|
}
|
|
|
},
|
|
|
|
|
|
+ computed: {
|
|
|
+ tooltipId() {
|
|
|
+ return `el-popover-${generateId()}`;
|
|
|
+ }
|
|
|
+ },
|
|
|
watch: {
|
|
|
showPopper(newVal, oldVal) {
|
|
|
newVal ? this.$emit('show') : this.$emit('hide');
|
|
@@ -62,12 +71,23 @@ export default {
|
|
|
},
|
|
|
|
|
|
mounted() {
|
|
|
- let reference = this.reference || this.$refs.reference;
|
|
|
+ let reference = this.referenceElm = this.reference || this.$refs.reference;
|
|
|
const popper = this.popper || this.$refs.popper;
|
|
|
|
|
|
if (!reference && this.$slots.reference && this.$slots.reference[0]) {
|
|
|
reference = this.referenceElm = this.$slots.reference[0].elm;
|
|
|
}
|
|
|
+ // 可访问性
|
|
|
+ if (reference) {
|
|
|
+ reference.className += ' el-tooltip';
|
|
|
+ reference.setAttribute('aria-describedby', this.tooltipId);
|
|
|
+ reference.setAttribute('tabindex', 0); // tab序列
|
|
|
+
|
|
|
+ on(reference, 'focus', this.handleFocus);
|
|
|
+ on(reference, 'blur', this.handleBlur);
|
|
|
+ on(reference, 'keydown', this.handleKeydown);
|
|
|
+ on(reference, 'click', this.handleClick);
|
|
|
+ }
|
|
|
if (this.trigger === 'click') {
|
|
|
on(reference, 'click', this.doToggle);
|
|
|
on(document, 'click', this.handleDocumentClick);
|
|
@@ -114,6 +134,20 @@ export default {
|
|
|
doClose() {
|
|
|
this.showPopper = false;
|
|
|
},
|
|
|
+ handleFocus() {
|
|
|
+ const reference = this.referenceElm;
|
|
|
+ reference.className += ' focusing';
|
|
|
+ this.showPopper = true;
|
|
|
+ },
|
|
|
+ handleClick() {
|
|
|
+ const reference = this.referenceElm;
|
|
|
+ reference.className = reference.className.replace(/\s*focusing\s*/, ' ');
|
|
|
+ },
|
|
|
+ handleBlur() {
|
|
|
+ const reference = this.referenceElm;
|
|
|
+ reference.className = reference.className.replace(/\s*focusing\s*/, ' ');
|
|
|
+ this.showPopper = false;
|
|
|
+ },
|
|
|
handleMouseEnter() {
|
|
|
clearTimeout(this._timer);
|
|
|
if (this.openDelay) {
|
|
@@ -124,6 +158,11 @@ export default {
|
|
|
this.showPopper = true;
|
|
|
}
|
|
|
},
|
|
|
+ handleKeydown(ev) {
|
|
|
+ if (ev.keyCode === 27) { // esc
|
|
|
+ this.doClose();
|
|
|
+ }
|
|
|
+ },
|
|
|
handleMouseLeave() {
|
|
|
clearTimeout(this._timer);
|
|
|
this._timer = setTimeout(() => {
|