forked from FoundKeyGang/FoundKey
refactor(client): use setup sugar
This commit is contained in:
parent
b946d89ec1
commit
bfc9873fb9
1 changed files with 182 additions and 212 deletions
|
@ -1,5 +1,5 @@
|
||||||
<template>
|
<template>
|
||||||
<transition :name="$store.state.animation ? (type === 'drawer') ? 'modal-drawer' : (type === 'popup') ? 'modal-popup' : 'modal' : ''" :duration="$store.state.animation ? 200 : 0" appear @after-leave="$emit('closed')" @enter="$emit('opening')" @after-enter="childRendered">
|
<transition :name="$store.state.animation ? (type === 'drawer') ? 'modal-drawer' : (type === 'popup') ? 'modal-popup' : 'modal' : ''" :duration="$store.state.animation ? 200 : 0" appear @after-leave="emit('closed')" @enter="emit('opening')" @after-enter="childRendered">
|
||||||
<div v-show="manualShowing != null ? manualShowing : showing" v-hotkey.global="keymap" class="qzhlnise" :class="{ drawer: type === 'drawer', dialog: type === 'dialog' || type === 'dialog:top', popup: type === 'popup' }" :style="{ zIndex, pointerEvents: (manualShowing != null ? manualShowing : showing) ? 'auto' : 'none', '--transformOrigin': transformOrigin }">
|
<div v-show="manualShowing != null ? manualShowing : showing" v-hotkey.global="keymap" class="qzhlnise" :class="{ drawer: type === 'drawer', dialog: type === 'dialog' || type === 'dialog:top', popup: type === 'popup' }" :style="{ zIndex, pointerEvents: (manualShowing != null ? manualShowing : showing) ? 'auto' : 'none', '--transformOrigin': transformOrigin }">
|
||||||
<div class="bg _modalBg" :class="{ transparent: transparentBg && (type === 'popup') }" :style="{ zIndex }" @click="onBgClick" @contextmenu.prevent.stop="() => {}"></div>
|
<div class="bg _modalBg" :class="{ transparent: transparentBg && (type === 'popup') }" :style="{ zIndex }" @click="onBgClick" @contextmenu.prevent.stop="() => {}"></div>
|
||||||
<div ref="content" class="content" :class="{ fixed, top: type === 'dialog:top' }" :style="{ zIndex }" @click.self="onBgClick">
|
<div ref="content" class="content" :class="{ fixed, top: type === 'dialog:top' }" :style="{ zIndex }" @click.self="onBgClick">
|
||||||
|
@ -9,8 +9,8 @@
|
||||||
</transition>
|
</transition>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts" setup>
|
||||||
import { defineComponent, nextTick, onMounted, computed, PropType, ref, watch } from 'vue';
|
import { nextTick, onMounted, computed, ref, watch, provide } from 'vue';
|
||||||
import * as os from '@/os';
|
import * as os from '@/os';
|
||||||
import { isTouchUsing } from '@/scripts/touch';
|
import { isTouchUsing } from '@/scripts/touch';
|
||||||
import { defaultStore } from '@/store';
|
import { defaultStore } from '@/store';
|
||||||
|
@ -25,234 +25,204 @@ function getFixedContainer(el: Element | null): Element | null {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default defineComponent({
|
const props = withDefaults(defineProps<{
|
||||||
provide: {
|
manualShowing?: boolean;
|
||||||
modal: true
|
srcCenter?: boolean;
|
||||||
},
|
src?: HTMLElement;
|
||||||
|
preferType?: string;
|
||||||
|
zPriority?: 'low' | 'middle' | 'high';
|
||||||
|
noOverlap?: boolean;
|
||||||
|
transparentBg?: boolean;
|
||||||
|
}>(), {
|
||||||
|
manualShowing: null,
|
||||||
|
src: null,
|
||||||
|
preferType: 'auto',
|
||||||
|
zPriority: 'low',
|
||||||
|
noOverlap: true,
|
||||||
|
transparentBg: false,
|
||||||
|
});
|
||||||
|
|
||||||
props: {
|
const emit = defineEmits<{
|
||||||
manualShowing: {
|
(ev: 'opening'): void;
|
||||||
type: Boolean,
|
(ev: 'click'): void;
|
||||||
required: false,
|
(ev: 'esc'): void;
|
||||||
default: null,
|
(ev: 'close'): void;
|
||||||
},
|
(ev: 'closed'): void;
|
||||||
srcCenter: {
|
}>();
|
||||||
type: Boolean,
|
|
||||||
required: false
|
|
||||||
},
|
|
||||||
src: {
|
|
||||||
type: Object as PropType<HTMLElement>,
|
|
||||||
required: false,
|
|
||||||
default: null,
|
|
||||||
},
|
|
||||||
preferType: {
|
|
||||||
required: false,
|
|
||||||
type: String,
|
|
||||||
default: 'auto',
|
|
||||||
},
|
|
||||||
zPriority: {
|
|
||||||
type: String as PropType<'low' | 'middle' | 'high'>,
|
|
||||||
required: false,
|
|
||||||
default: 'low',
|
|
||||||
},
|
|
||||||
noOverlap: {
|
|
||||||
type: Boolean,
|
|
||||||
required: false,
|
|
||||||
default: true,
|
|
||||||
},
|
|
||||||
transparentBg: {
|
|
||||||
type: Boolean,
|
|
||||||
required: false,
|
|
||||||
default: false,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
emits: ['opening', 'click', 'esc', 'close', 'closed'],
|
provide('modal', true);
|
||||||
|
|
||||||
setup(props, context) {
|
const maxHeight = ref<number>();
|
||||||
const maxHeight = ref<number>();
|
const fixed = ref(false);
|
||||||
const fixed = ref(false);
|
const transformOrigin = ref('center');
|
||||||
const transformOrigin = ref('center');
|
const showing = ref(true);
|
||||||
const showing = ref(true);
|
const content = ref<HTMLElement>();
|
||||||
const content = ref<HTMLElement>();
|
const zIndex = os.claimZIndex(props.zPriority);
|
||||||
const zIndex = os.claimZIndex(props.zPriority);
|
const type = computed(() => {
|
||||||
const type = computed(() => {
|
if (props.preferType === 'auto') {
|
||||||
if (props.preferType === 'auto') {
|
if (!defaultStore.state.disableDrawer && isTouchUsing && window.innerWidth < 500 && window.innerHeight < 1000) {
|
||||||
if (!defaultStore.state.disableDrawer && isTouchUsing && window.innerWidth < 500 && window.innerHeight < 1000) {
|
return 'drawer';
|
||||||
return 'drawer';
|
} else {
|
||||||
} else {
|
return props.src != null ? 'popup' : 'dialog';
|
||||||
return props.src != null ? 'popup' : 'dialog';
|
}
|
||||||
}
|
} else {
|
||||||
} else {
|
return props.preferType;
|
||||||
return props.preferType;
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
|
|
||||||
let contentClicking = false;
|
|
||||||
|
|
||||||
const close = () => {
|
let contentClicking = false;
|
||||||
// eslint-disable-next-line vue/no-mutating-props
|
|
||||||
if (props.src) props.src.style.pointerEvents = 'auto';
|
|
||||||
showing.value = false;
|
|
||||||
context.emit('close');
|
|
||||||
};
|
|
||||||
|
|
||||||
const onBgClick = () => {
|
const close = () => {
|
||||||
if (contentClicking) return;
|
// eslint-disable-next-line vue/no-mutating-props
|
||||||
context.emit('click');
|
if (props.src) props.src.style.pointerEvents = 'auto';
|
||||||
};
|
showing.value = false;
|
||||||
|
emit('close');
|
||||||
|
};
|
||||||
|
|
||||||
if (type.value === 'drawer') {
|
const onBgClick = () => {
|
||||||
maxHeight.value = window.innerHeight / 2;
|
if (contentClicking) return;
|
||||||
|
emit('click');
|
||||||
|
};
|
||||||
|
|
||||||
|
if (type.value === 'drawer') {
|
||||||
|
maxHeight.value = window.innerHeight / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
const keymap = {
|
||||||
|
'esc': () => emit('esc'),
|
||||||
|
};
|
||||||
|
|
||||||
|
const MARGIN = 16;
|
||||||
|
|
||||||
|
const align = () => {
|
||||||
|
if (props.src == null) return;
|
||||||
|
if (type.value === 'drawer') return;
|
||||||
|
|
||||||
|
const popover = content.value!;
|
||||||
|
|
||||||
|
if (popover == null) return;
|
||||||
|
|
||||||
|
const rect = props.src.getBoundingClientRect();
|
||||||
|
|
||||||
|
const width = popover.offsetWidth;
|
||||||
|
const height = popover.offsetHeight;
|
||||||
|
|
||||||
|
let left;
|
||||||
|
let top;
|
||||||
|
|
||||||
|
if (props.srcCenter) {
|
||||||
|
const x = rect.left + (fixed.value ? 0 : window.pageXOffset) + (props.src.offsetWidth / 2);
|
||||||
|
const y = rect.top + (fixed.value ? 0 : window.pageYOffset) + (props.src.offsetHeight / 2);
|
||||||
|
left = (x - (width / 2));
|
||||||
|
top = (y - (height / 2));
|
||||||
|
} else {
|
||||||
|
const x = rect.left + (fixed.value ? 0 : window.pageXOffset) + (props.src.offsetWidth / 2);
|
||||||
|
const y = rect.top + (fixed.value ? 0 : window.pageYOffset) + props.src.offsetHeight;
|
||||||
|
left = (x - (width / 2));
|
||||||
|
top = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fixed.value) {
|
||||||
|
// 画面から横にはみ出る場合
|
||||||
|
if (left + width > window.innerWidth) {
|
||||||
|
left = window.innerWidth - width;
|
||||||
}
|
}
|
||||||
|
|
||||||
const keymap = {
|
// 画面から縦にはみ出る場合
|
||||||
'esc': () => context.emit('esc'),
|
if (top + height > (window.innerHeight - MARGIN)) {
|
||||||
};
|
if (props.noOverlap) {
|
||||||
|
const underSpace = (window.innerHeight - MARGIN) - top;
|
||||||
const MARGIN = 16;
|
const upperSpace = (rect.top - MARGIN);
|
||||||
|
if (underSpace >= (upperSpace / 3)) {
|
||||||
const align = () => {
|
maxHeight.value = underSpace;
|
||||||
if (props.src == null) return;
|
} else {
|
||||||
if (type.value === 'drawer') return;
|
maxHeight.value = upperSpace;
|
||||||
|
top = (upperSpace + MARGIN) - height;
|
||||||
const popover = content.value!;
|
|
||||||
|
|
||||||
if (popover == null) return;
|
|
||||||
|
|
||||||
const rect = props.src.getBoundingClientRect();
|
|
||||||
|
|
||||||
const width = popover.offsetWidth;
|
|
||||||
const height = popover.offsetHeight;
|
|
||||||
|
|
||||||
let left;
|
|
||||||
let top;
|
|
||||||
|
|
||||||
if (props.srcCenter) {
|
|
||||||
const x = rect.left + (fixed.value ? 0 : window.pageXOffset) + (props.src.offsetWidth / 2);
|
|
||||||
const y = rect.top + (fixed.value ? 0 : window.pageYOffset) + (props.src.offsetHeight / 2);
|
|
||||||
left = (x - (width / 2));
|
|
||||||
top = (y - (height / 2));
|
|
||||||
} else {
|
|
||||||
const x = rect.left + (fixed.value ? 0 : window.pageXOffset) + (props.src.offsetWidth / 2);
|
|
||||||
const y = rect.top + (fixed.value ? 0 : window.pageYOffset) + props.src.offsetHeight;
|
|
||||||
left = (x - (width / 2));
|
|
||||||
top = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fixed.value) {
|
|
||||||
// 画面から横にはみ出る場合
|
|
||||||
if (left + width > window.innerWidth) {
|
|
||||||
left = window.innerWidth - width;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 画面から縦にはみ出る場合
|
|
||||||
if (top + height > (window.innerHeight - MARGIN)) {
|
|
||||||
if (props.noOverlap) {
|
|
||||||
const underSpace = (window.innerHeight - MARGIN) - top;
|
|
||||||
const upperSpace = (rect.top - MARGIN);
|
|
||||||
if (underSpace >= (upperSpace / 3)) {
|
|
||||||
maxHeight.value = underSpace;
|
|
||||||
} else {
|
|
||||||
maxHeight.value = upperSpace;
|
|
||||||
top = (upperSpace + MARGIN) - height;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
top = (window.innerHeight - MARGIN) - height;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 画面から横にはみ出る場合
|
top = (window.innerHeight - MARGIN) - height;
|
||||||
if (left + width - window.pageXOffset > window.innerWidth) {
|
}
|
||||||
left = window.innerWidth - width + window.pageXOffset - 1;
|
}
|
||||||
|
} else {
|
||||||
|
// 画面から横にはみ出る場合
|
||||||
|
if (left + width - window.pageXOffset > window.innerWidth) {
|
||||||
|
left = window.innerWidth - width + window.pageXOffset - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 画面から縦にはみ出る場合
|
||||||
|
if (top + height - window.pageYOffset > (window.innerHeight - MARGIN)) {
|
||||||
|
if (props.noOverlap) {
|
||||||
|
const underSpace = (window.innerHeight - MARGIN) - (top - window.pageYOffset);
|
||||||
|
const upperSpace = (rect.top - MARGIN);
|
||||||
|
if (underSpace >= (upperSpace / 3)) {
|
||||||
|
maxHeight.value = underSpace;
|
||||||
|
} else {
|
||||||
|
maxHeight.value = upperSpace;
|
||||||
|
top = window.pageYOffset + ((upperSpace + MARGIN) - height);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 画面から縦にはみ出る場合
|
|
||||||
if (top + height - window.pageYOffset > (window.innerHeight - MARGIN)) {
|
|
||||||
if (props.noOverlap) {
|
|
||||||
const underSpace = (window.innerHeight - MARGIN) - (top - window.pageYOffset);
|
|
||||||
const upperSpace = (rect.top - MARGIN);
|
|
||||||
if (underSpace >= (upperSpace / 3)) {
|
|
||||||
maxHeight.value = underSpace;
|
|
||||||
} else {
|
|
||||||
maxHeight.value = upperSpace;
|
|
||||||
top = window.pageYOffset + ((upperSpace + MARGIN) - height);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
top = (window.innerHeight - MARGIN) - height + window.pageYOffset - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (top < 0) {
|
|
||||||
top = MARGIN;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (left < 0) {
|
|
||||||
left = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (top > rect.top + (fixed.value ? 0 : window.pageYOffset)) {
|
|
||||||
transformOrigin.value = 'center top';
|
|
||||||
} else if ((top + height) <= rect.top + (fixed.value ? 0 : window.pageYOffset)) {
|
|
||||||
transformOrigin.value = 'center bottom';
|
|
||||||
} else {
|
} else {
|
||||||
transformOrigin.value = 'center';
|
top = (window.innerHeight - MARGIN) - height + window.pageYOffset - 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
popover.style.left = left + 'px';
|
if (top < 0) {
|
||||||
popover.style.top = top + 'px';
|
top = MARGIN;
|
||||||
};
|
}
|
||||||
|
|
||||||
const childRendered = () => {
|
if (left < 0) {
|
||||||
// モーダルコンテンツにマウスボタンが押され、コンテンツ外でマウスボタンが離されたときにモーダルバックグラウンドクリックと判定させないためにマウスイベントを監視しフラグ管理する
|
left = 0;
|
||||||
const el = content.value!.children[0];
|
}
|
||||||
el.addEventListener('mousedown', e => {
|
|
||||||
contentClicking = true;
|
|
||||||
window.addEventListener('mouseup', e => {
|
|
||||||
// click イベントより先に mouseup イベントが発生するかもしれないのでちょっと待つ
|
|
||||||
window.setTimeout(() => {
|
|
||||||
contentClicking = false;
|
|
||||||
}, 100);
|
|
||||||
}, { passive: true, once: true });
|
|
||||||
}, { passive: true });
|
|
||||||
};
|
|
||||||
|
|
||||||
onMounted(() => {
|
if (top > rect.top + (fixed.value ? 0 : window.pageYOffset)) {
|
||||||
watch(() => props.src, async () => {
|
transformOrigin.value = 'center top';
|
||||||
if (props.src) {
|
} else if ((top + height) <= rect.top + (fixed.value ? 0 : window.pageYOffset)) {
|
||||||
// eslint-disable-next-line vue/no-mutating-props
|
transformOrigin.value = 'center bottom';
|
||||||
props.src.style.pointerEvents = 'none';
|
} else {
|
||||||
}
|
transformOrigin.value = 'center';
|
||||||
fixed.value = (type.value === 'drawer') || (getFixedContainer(props.src) != null);
|
}
|
||||||
|
|
||||||
await nextTick()
|
popover.style.left = left + 'px';
|
||||||
|
popover.style.top = top + 'px';
|
||||||
align();
|
};
|
||||||
}, { immediate: true, });
|
|
||||||
|
|
||||||
nextTick(() => {
|
const childRendered = () => {
|
||||||
const popover = content.value;
|
// モーダルコンテンツにマウスボタンが押され、コンテンツ外でマウスボタンが離されたときにモーダルバックグラウンドクリックと判定させないためにマウスイベントを監視しフラグ管理する
|
||||||
new ResizeObserver((entries, observer) => {
|
const el = content.value!.children[0];
|
||||||
align();
|
el.addEventListener('mousedown', ev => {
|
||||||
}).observe(popover!);
|
contentClicking = true;
|
||||||
});
|
window.addEventListener('mouseup', ev => {
|
||||||
});
|
// click イベントより先に mouseup イベントが発生するかもしれないのでちょっと待つ
|
||||||
|
window.setTimeout(() => {
|
||||||
|
contentClicking = false;
|
||||||
|
}, 100);
|
||||||
|
}, { passive: true, once: true });
|
||||||
|
}, { passive: true });
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
onMounted(() => {
|
||||||
showing,
|
watch(() => props.src, async () => {
|
||||||
type,
|
if (props.src) {
|
||||||
fixed,
|
// eslint-disable-next-line vue/no-mutating-props
|
||||||
content,
|
props.src.style.pointerEvents = 'none';
|
||||||
transformOrigin,
|
}
|
||||||
maxHeight,
|
fixed.value = (type.value === 'drawer') || (getFixedContainer(props.src) != null);
|
||||||
close,
|
|
||||||
zIndex,
|
await nextTick()
|
||||||
keymap,
|
|
||||||
onBgClick,
|
align();
|
||||||
childRendered,
|
}, { immediate: true, });
|
||||||
};
|
|
||||||
},
|
nextTick(() => {
|
||||||
|
const popover = content.value;
|
||||||
|
new ResizeObserver((entries, observer) => {
|
||||||
|
align();
|
||||||
|
}).observe(popover!);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
defineExpose({
|
||||||
|
close,
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue