refactor(client): use setup sugar

This commit is contained in:
syuilo 2022-01-29 03:03:23 +09:00
parent c74ecf3a53
commit 7ca456bb45

View file

@ -1,99 +1,82 @@
<template> <template>
<transition :name="$store.state.animation ? 'tooltip' : ''" appear @after-leave="$emit('closed')"> <transition :name="$store.state.animation ? 'tooltip' : ''" appear @after-leave="emit('closed')">
<div v-show="showing" ref="el" class="buebdbiu _acrylic _shadow" :style="{ zIndex, maxWidth: maxWidth + 'px' }"> <div v-show="showing" ref="el" class="buebdbiu _acrylic _shadow" :style="{ zIndex, maxWidth: maxWidth + 'px' }">
<slot>{{ text }}</slot> <slot>{{ text }}</slot>
</div> </div>
</transition> </transition>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { defineComponent, nextTick, onMounted, onUnmounted, ref } from 'vue'; import { nextTick, onMounted, onUnmounted, ref } from 'vue';
import * as os from '@/os'; import * as os from '@/os';
export default defineComponent({ const props = withDefaults(defineProps<{
props: { showing: boolean;
showing: { source: HTMLElement;
type: Boolean, text?: string;
required: true, maxWidth?; number;
}, }>(), {
source: { maxWidth: 250,
required: true, });
},
text: {
type: String,
required: false
},
maxWidth: {
type: Number,
required: false,
default: 250,
},
},
emits: ['closed'], const emit = defineEmits<{
(ev: 'closed'): void;
}>();
setup(props, context) { const el = ref<HTMLElement>();
const el = ref<HTMLElement>(); const zIndex = os.claimZIndex('high');
const zIndex = os.claimZIndex('high');
const setPosition = () => { const setPosition = () => {
if (el.value == null) return; if (el.value == null) return;
const rect = props.source.getBoundingClientRect(); const rect = props.source.getBoundingClientRect();
const contentWidth = el.value.offsetWidth; const contentWidth = el.value.offsetWidth;
const contentHeight = el.value.offsetHeight; const contentHeight = el.value.offsetHeight;
let left = rect.left + window.pageXOffset + (props.source.offsetWidth / 2); let left = rect.left + window.pageXOffset + (props.source.offsetWidth / 2);
let top = rect.top + window.pageYOffset - contentHeight; let top = rect.top + window.pageYOffset - contentHeight;
left -= (el.value.offsetWidth / 2); left -= (el.value.offsetWidth / 2);
if (left + contentWidth - window.pageXOffset > window.innerWidth) { if (left + contentWidth - window.pageXOffset > window.innerWidth) {
left = window.innerWidth - contentWidth + window.pageXOffset - 1; left = window.innerWidth - contentWidth + window.pageXOffset - 1;
} }
if (top - window.pageYOffset < 0) { if (top - window.pageYOffset < 0) {
top = rect.top + window.pageYOffset + props.source.offsetHeight; top = rect.top + window.pageYOffset + props.source.offsetHeight;
el.value.style.transformOrigin = 'center top'; el.value.style.transformOrigin = 'center top';
} }
el.value.style.left = left + 'px'; el.value.style.left = left + 'px';
el.value.style.top = top + 'px'; el.value.style.top = top + 'px';
}; };
onMounted(() => { onMounted(() => {
nextTick(() => { nextTick(() => {
if (props.source == null) { if (props.source == null) {
context.emit('closed'); emit('closed');
return; return;
} }
setPosition();
let loopHandler;
const loop = () => {
loopHandler = window.requestAnimationFrame(() => {
setPosition(); setPosition();
let loopHandler;
const loop = () => {
loopHandler = window.requestAnimationFrame(() => {
setPosition();
loop();
});
};
loop(); loop();
onUnmounted(() => {
window.cancelAnimationFrame(loopHandler);
});
}); });
});
return {
el,
zIndex,
}; };
},
}) loop();
onUnmounted(() => {
window.cancelAnimationFrame(loopHandler);
});
});
});
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>