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

image: fix keyboard events handling logic #18416

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
30 changes: 20 additions & 10 deletions packages/image/src/image-viewer.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<transition name="viewer-fade">
<div tabindex="-1" ref="el-image-viewer__wrapper" class="el-image-viewer__wrapper" :style="{ 'z-index': zIndex }">
<div v-show="visible" tabindex="-1" ref="el-image-viewer__wrapper" class="el-image-viewer__wrapper" :style="{ 'z-index': zIndex }">
<div class="el-image-viewer__mask"></div>
<!-- CLOSE -->
<span class="el-image-viewer__btn el-image-viewer__close" @click="hide">
Expand Down Expand Up @@ -91,7 +91,8 @@ export default {
initialIndex: {
type: Number,
default: 0
}
},
visible: Boolean
},

data() {
Expand Down Expand Up @@ -151,15 +152,30 @@ export default {
this.loading = true;
}
});
},
visible(val) {
if (val) {
this.show();
}
}
},
methods: {
show() {
this.deviceSupportInstall();
this.$nextTick(_ => {
// add tabindex then wrapper can be focusable via Javascript
// focus wrapper so arrow key can't cause inner scroll behavior underneath
this.$refs['el-image-viewer__wrapper'].focus();
});
},
hide() {
this.deviceSupportUninstall();
this.onClose();
},
deviceSupportInstall() {
this._keyDownHandler = rafThrottle(e => {
// prevent scroll behavior
e.preventDefault();
const keyCode = e.keyCode;
switch (keyCode) {
// ESC
Expand Down Expand Up @@ -202,11 +218,11 @@ export default {
});
}
});
on(document, 'keydown', this._keyDownHandler);
on(this.$refs['el-image-viewer__wrapper'], 'keydown', this._keyDownHandler);
on(document, mousewheelEventName, this._mouseWheelHandler);
},
deviceSupportUninstall() {
off(document, 'keydown', this._keyDownHandler);
off(this.$refs['el-image-viewer__wrapper'], 'keydown', this._keyDownHandler);
off(document, mousewheelEventName, this._mouseWheelHandler);
this._keyDownHandler = null;
this._mouseWheelHandler = null;
Expand Down Expand Up @@ -291,12 +307,6 @@ export default {
}
transform.enableTransition = enableTransition;
}
},
mounted() {
this.deviceSupportInstall();
// add tabindex then wrapper can be focusable via Javascript
// focus wrapper so arrow key can't cause inner scroll behavior underneath
this.$refs['el-image-viewer__wrapper'].focus();
}
};
</script>
8 changes: 1 addition & 7 deletions packages/image/src/main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
:style="imageStyle"
:class="{ 'el-image__inner--center': alignCenter, 'el-image__preview': preview }">
<template v-if="preview">
<image-viewer :z-index="zIndex" :initial-index="imageIndex" v-show="showViewer" :on-close="closeViewer" :url-list="previewSrcList"/>
<image-viewer :z-index="zIndex" :initial-index="imageIndex" :visible.sync="showViewer" :on-close="closeViewer" :url-list="previewSrcList"/>
</template>
</div>
</template>
Expand All @@ -38,8 +38,6 @@
SCALE_DOWN: 'scale-down'
};

let prevOverflow = '';

export default {
name: 'ElImage',

Expand Down Expand Up @@ -217,13 +215,9 @@
}
},
clickHandler() {
// prevent body scroll
prevOverflow = document.body.style.overflow;
document.body.style.overflow = 'hidden';
this.showViewer = true;
},
closeViewer() {
document.body.style.overflow = prevOverflow;
this.showViewer = false;
}
}
Expand Down