iceshrimp/packages/client/src/components/global/MkSpacer.vue

88 lines
1.6 KiB
Vue
Raw Normal View History

2021-10-14 20:55:59 +09:00
<template>
2023-04-08 09:01:42 +09:00
<div ref="root" :class="$style.root" :style="{ padding: margin + 'px' }">
<div ref="content" :class="$style.content">
<slot></slot>
</div>
2021-10-14 20:55:59 +09:00
</div>
</template>
2022-06-22 16:29:31 +09:00
<script lang="ts" setup>
2023-04-08 09:01:42 +09:00
import { inject, onMounted, onUnmounted, ref } from "vue";
import { deviceKind } from "@/scripts/device-kind";
2023-05-15 08:19:00 +09:00
import { ui } from "@/config";
2021-10-14 20:55:59 +09:00
2023-04-08 09:01:42 +09:00
const props = withDefaults(
defineProps<{
contentMax?: number | null;
marginMin?: number;
marginMax?: number;
}>(),
{
contentMax: null,
marginMin: 12,
marginMax: 24,
}
);
2021-10-14 20:55:59 +09:00
2022-06-22 16:29:31 +09:00
let ro: ResizeObserver;
let root = $ref<HTMLElement>();
let content = $ref<HTMLElement>();
let margin = $ref(0);
2023-04-08 09:01:42 +09:00
const shouldSpacerMin = inject("shouldSpacerMin", false);
2021-12-03 13:52:57 +09:00
2023-04-08 09:01:42 +09:00
const adjust = (rect: { width: number; height: number }) => {
if (shouldSpacerMin || deviceKind === "smartphone") {
2022-06-22 16:29:31 +09:00
margin = props.marginMin;
return;
}
if (ui === "classic") {
2023-05-15 08:19:00 +09:00
margin = 12;
return;
}
2021-10-14 20:55:59 +09:00
2023-04-08 09:01:42 +09:00
if (
rect.width > (props.contentMax ?? 0) ||
(rect.width > 360 && window.innerWidth > 400)
) {
2022-06-22 16:29:31 +09:00
margin = props.marginMax;
} else {
margin = props.marginMin;
}
};
2021-10-14 20:55:59 +09:00
2022-06-22 16:29:31 +09:00
onMounted(() => {
ro = new ResizeObserver((entries) => {
/* iOS
adjust({
width: entries[0].borderBoxSize[0].inlineSize,
height: entries[0].borderBoxSize[0].blockSize,
2021-10-14 20:55:59 +09:00
});
2022-06-22 16:29:31 +09:00
*/
adjust({
width: root!.offsetWidth,
height: root!.offsetHeight,
2021-10-14 20:55:59 +09:00
});
2022-06-22 16:29:31 +09:00
});
ro.observe(root!);
if (props.contentMax) {
content!.style.maxWidth = `${props.contentMax}px`;
}
});
2021-10-14 20:55:59 +09:00
2022-06-22 16:29:31 +09:00
onUnmounted(() => {
ro.disconnect();
2021-10-14 20:55:59 +09:00
});
</script>
<style lang="scss" module>
.root {
box-sizing: border-box;
width: 100%;
}
.content {
margin: 0 auto;
}
</style>