2021-10-14 20:55:59 +09:00
|
|
|
<template>
|
|
|
|
<div ref="root" :class="$style.root" :style="{ padding: margin + 'px' }">
|
|
|
|
<div ref="content" :class="$style.content">
|
|
|
|
<slot></slot>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
|
|
|
import { defineComponent, onMounted, onUnmounted, ref } from 'vue';
|
|
|
|
|
|
|
|
export default defineComponent({
|
|
|
|
props: {
|
|
|
|
contentMax: {
|
|
|
|
type: Number,
|
|
|
|
required: false,
|
|
|
|
default: null,
|
2021-11-28 20:07:37 +09:00
|
|
|
},
|
|
|
|
marginMin: {
|
|
|
|
type: Number,
|
|
|
|
required: false,
|
|
|
|
default: 12,
|
|
|
|
},
|
|
|
|
marginMax: {
|
|
|
|
type: Number,
|
|
|
|
required: false,
|
|
|
|
default: 32,
|
|
|
|
},
|
2021-10-14 20:55:59 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
setup(props, context) {
|
|
|
|
let ro: ResizeObserver;
|
2021-11-28 20:07:37 +09:00
|
|
|
const root = ref<HTMLElement>();
|
|
|
|
const content = ref<HTMLElement>();
|
2021-10-14 20:55:59 +09:00
|
|
|
const margin = ref(0);
|
|
|
|
const adjust = (rect: { width: number; height: number; }) => {
|
|
|
|
if (rect.width > (props.contentMax || 500)) {
|
2021-11-28 20:07:37 +09:00
|
|
|
margin.value = props.marginMax;
|
2021-10-14 20:55:59 +09:00
|
|
|
} else {
|
2021-11-28 20:07:37 +09:00
|
|
|
margin.value = props.marginMin;
|
2021-10-14 20:55:59 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
ro = new ResizeObserver((entries) => {
|
2021-10-16 19:18:46 +09:00
|
|
|
/* iOSが対応していない
|
2021-10-14 20:55:59 +09:00
|
|
|
adjust({
|
|
|
|
width: entries[0].borderBoxSize[0].inlineSize,
|
|
|
|
height: entries[0].borderBoxSize[0].blockSize,
|
|
|
|
});
|
2021-10-16 19:18:46 +09:00
|
|
|
*/
|
|
|
|
adjust({
|
2021-11-28 20:07:37 +09:00
|
|
|
width: root.value!.offsetWidth,
|
|
|
|
height: root.value!.offsetHeight,
|
2021-10-16 19:18:46 +09:00
|
|
|
});
|
2021-10-14 20:55:59 +09:00
|
|
|
});
|
2021-11-28 20:07:37 +09:00
|
|
|
ro.observe(root.value!);
|
2021-10-14 20:55:59 +09:00
|
|
|
|
|
|
|
if (props.contentMax) {
|
2021-11-28 20:07:37 +09:00
|
|
|
content.value!.style.maxWidth = `${props.contentMax}px`;
|
2021-10-14 20:55:59 +09:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
|
|
|
ro.disconnect();
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
root,
|
|
|
|
content,
|
|
|
|
margin,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" module>
|
|
|
|
.root {
|
|
|
|
box-sizing: border-box;
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
|
|
|
|
.content {
|
|
|
|
margin: 0 auto;
|
|
|
|
}
|
|
|
|
</style>
|