summaryrefslogtreecommitdiff
path: root/packages/client/src/components/global/MkSpacer.vue
blob: c7d53f2ee78430eb31f588b018a5a687a43fd2c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<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" setup>
import { inject, onMounted, onUnmounted, ref } from 'vue';
import { deviceKind } from '@/scripts/device-kind';

const props = withDefaults(defineProps<{
	contentMax?: number | null;
	marginMin?: number;
	marginMax?: number;
}>(), {
	contentMax: null,
	marginMin: 12,
	marginMax: 24,
});

let ro: ResizeObserver;
let root = $ref<HTMLElement>();
let content = $ref<HTMLElement>();
let margin = $ref(0);
const shouldSpacerMin = inject('shouldSpacerMin', false);

const adjust = (rect: { width: number; height: number; }) => {
	if (shouldSpacerMin || deviceKind === 'smartphone') {
		margin = props.marginMin;
		return;
	}

	if (rect.width > (props.contentMax ?? 0) || (rect.width > 360 && window.innerWidth > 400)) {
		margin = props.marginMax;
	} else {
		margin = props.marginMin;
	}
};

onMounted(() => {
	ro = new ResizeObserver((entries) => {
		/* iOSが対応していない
		adjust({
			width: entries[0].borderBoxSize[0].inlineSize,
			height: entries[0].borderBoxSize[0].blockSize,
		});
		*/
		adjust({
			width: root!.offsetWidth,
			height: root!.offsetHeight,
		});
	});
	ro.observe(root!);

	if (props.contentMax) {
		content!.style.maxWidth = `${props.contentMax}px`;
	}
});

onUnmounted(() => {
	ro.disconnect();
});
</script>

<style lang="scss" module>
.root {
	box-sizing: border-box;
	width: 100%;
}

.content {
	margin: 0 auto;
	container-type: inline-size;
}
</style>