🎨
This commit is contained in:
70
src/client/components/global/spacer.vue
Normal file
70
src/client/components/global/spacer.vue
Normal file
@@ -0,0 +1,70 @@
|
||||
<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,
|
||||
}
|
||||
},
|
||||
|
||||
setup(props, context) {
|
||||
let ro: ResizeObserver;
|
||||
const root = ref<HTMLElement>(null);
|
||||
const content = ref<HTMLElement>(null);
|
||||
const margin = ref(0);
|
||||
const adjust = (rect: { width: number; height: number; }) => {
|
||||
if (rect.width > (props.contentMax || 500)) {
|
||||
margin.value = 32;
|
||||
} else {
|
||||
margin.value = 12;
|
||||
}
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
ro = new ResizeObserver((entries) => {
|
||||
adjust({
|
||||
width: entries[0].borderBoxSize[0].inlineSize,
|
||||
height: entries[0].borderBoxSize[0].blockSize,
|
||||
});
|
||||
});
|
||||
ro.observe(root.value);
|
||||
|
||||
if (props.contentMax) {
|
||||
content.value.style.maxWidth = `${props.contentMax}px`;
|
||||
}
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
ro.disconnect();
|
||||
});
|
||||
|
||||
return {
|
||||
root,
|
||||
content,
|
||||
margin,
|
||||
};
|
||||
},
|
||||
});
|
||||
</script>
|
||||
|
||||
<style lang="scss" module>
|
||||
.root {
|
||||
box-sizing: border-box;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.content {
|
||||
margin: 0 auto;
|
||||
}
|
||||
</style>
|
@@ -14,6 +14,7 @@ import loading from './global/loading.vue';
|
||||
import error from './global/error.vue';
|
||||
import ad from './global/ad.vue';
|
||||
import header from './global/header.vue';
|
||||
import spacer from './global/spacer.vue';
|
||||
|
||||
export default function(app: App) {
|
||||
app.component('I18n', i18n);
|
||||
@@ -30,4 +31,5 @@ export default function(app: App) {
|
||||
app.component('MkError', error);
|
||||
app.component('MkAd', ad);
|
||||
app.component('MkHeader', header);
|
||||
app.component('MkSpacer', spacer);
|
||||
}
|
||||
|
Reference in New Issue
Block a user