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

fix: click outside if mounted in shadow DOM #578

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import HideSeconds from './demo/HideSeconds.vue';
import MinuteStep from './demo/MinuteStep.vue';
import FixedTimeList from './demo/FixedTimeList.vue';
import Disabled from './demo/Disabled.vue';
import ShadowDom from './demo/ShadowDom.vue';

import docEn from './en.md';
import docZhCN from './zh-cn.md';
Expand Down Expand Up @@ -74,6 +75,11 @@ const components = [
component: ControlOpen,
code: fs.readFileSync(`${__dirname}/demo/ControlOpen.vue`, 'utf8'),
},
{
id: 'ShadowDom',
component: ShadowDom,
code: fs.readFileSync(`${__dirname}/demo/ShadowDom.vue`, 'utf8'),
},
];

function transformMd(text) {
Expand Down
28 changes: 28 additions & 0 deletions example/demo/ShadowDom.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<template>
<div>
<date-picker
ref="datePicker"
v-model="value"
value-type="format"
type="date"
placeholder="Select date"
:append-to-body="false"
></date-picker>
</div>
</template>

<script>
export default {
name: 'ShadowDom',
data() {
return {
value: null,
};
},
mounted() {
const shadow = this.$el.attachShadow({ mode: 'open' });

shadow.appendChild(this.$refs.datePicker.$el);
},
};
</script>
6 changes: 6 additions & 0 deletions example/en.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,9 @@ Set custom time options by `hourOptions` `minuteOptions` `secondOptions`.
### Select fixed time list

You can provide a list of fixed time for users to choose by `timePickerOptions`

<!-- ShadowDom -->

### Shadow DOM

If you use the component inside shadow DOM, you should set the `:append-to-body="false"`.
6 changes: 6 additions & 0 deletions example/zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,9 @@
### 固定的时间列表

可以通过 `timePickerOptions` 提供一个固定的时间列表选择

<!-- ShadowDom -->

### Shadow DOM

If you use the component inside shadow DOM, you should set the `:append-to-body="false"`.
29 changes: 28 additions & 1 deletion src/popup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ export default {
left: '',
};
},
computed: {
hasShadowParent() {
if (!document.head.attachShadow) {
return false;
}

let element = this.$el;

while (element) {
if (element instanceof ShadowRoot) {
return true;
}

element = element.parentNode;
}

return false;
},
},
watch: {
visible: {
immediate: true,
Expand Down Expand Up @@ -76,10 +95,18 @@ export default {
},
methods: {
handleClickOutside(evt) {
if (!this.visible) return;
if (!this.visible) {
return;
}

const { target } = evt;
const el = this.$el;

if (el && !el.contains(target)) {
if (this.hasShadowParent && evt.composedPath().includes(el)) {
return;
}

this.$emit('clickoutside', evt);
}
},
Expand Down