refactor(client): use composition api

This commit is contained in:
syuilo 2022-06-22 16:29:31 +09:00
parent 85365da69e
commit be383aa5b2
1 changed files with 46 additions and 63 deletions

View File

@ -6,78 +6,61 @@
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { inject, onMounted, onUnmounted, ref } from 'vue';
import { deviceKind } from '@/scripts/device-kind'; import { deviceKind } from '@/scripts/device-kind';
import { defineComponent, inject, onMounted, onUnmounted, ref } from 'vue';
export default defineComponent({ const props = withDefaults(defineProps<{
props: { contentMax?: number | null;
contentMax: { marginMin?: number;
type: Number, marginMax?: number;
required: false, }>(), {
default: null, contentMax: null,
}, marginMin: 12,
marginMin: { marginMax: 24,
type: Number, });
required: false,
default: 12,
},
marginMax: {
type: Number,
required: false,
default: 24,
},
},
setup(props, context) { let ro: ResizeObserver;
let ro: ResizeObserver; let root = $ref<HTMLElement>();
const root = ref<HTMLElement>(); let content = $ref<HTMLElement>();
const content = ref<HTMLElement>(); let margin = $ref(0);
const margin = ref(0); const shouldSpacerMin = inject('shouldSpacerMin', false);
const shouldSpacerMin = inject('shouldSpacerMin', false);
const adjust = (rect: { width: number; height: number; }) => {
if (shouldSpacerMin || deviceKind === 'smartphone') {
margin.value = props.marginMin;
return;
}
if (rect.width > props.contentMax || (rect.width > 360 && window.innerWidth > 400)) { const adjust = (rect: { width: number; height: number; }) => {
margin.value = props.marginMax; if (shouldSpacerMin || deviceKind === 'smartphone') {
} else { margin = props.marginMin;
margin.value = props.marginMin; return;
} }
};
onMounted(() => { if (rect.width > (props.contentMax ?? 0) || (rect.width > 360 && window.innerWidth > 400)) {
ro = new ResizeObserver((entries) => { margin = props.marginMax;
/* iOS } else {
adjust({ margin = props.marginMin;
width: entries[0].borderBoxSize[0].inlineSize, }
height: entries[0].borderBoxSize[0].blockSize, };
});
*/
adjust({
width: root.value!.offsetWidth,
height: root.value!.offsetHeight,
});
});
ro.observe(root.value!);
if (props.contentMax) { onMounted(() => {
content.value!.style.maxWidth = `${props.contentMax}px`; ro = new ResizeObserver((entries) => {
} /* iOS
adjust({
width: entries[0].borderBoxSize[0].inlineSize,
height: entries[0].borderBoxSize[0].blockSize,
}); });
*/
onUnmounted(() => { adjust({
ro.disconnect(); width: root!.offsetWidth,
height: root!.offsetHeight,
}); });
});
ro.observe(root!);
return { if (props.contentMax) {
root, content!.style.maxWidth = `${props.contentMax}px`;
content, }
margin, });
};
}, onUnmounted(() => {
ro.disconnect();
}); });
</script> </script>