iceshrimp/packages/client/src/components/global/sticky-container.vue

67 lines
1.4 KiB
Vue
Raw Normal View History

2021-10-24 04:03:07 +09:00
<template>
<div ref="rootEl">
<slot name="header"></slot>
<div ref="bodyEl" :data-sticky-container-header-height="headerHeight">
2021-10-24 04:03:07 +09:00
<slot></slot>
</div>
</div>
</template>
<script lang="ts" setup>
import { onMounted, onUnmounted } from 'vue';
2021-10-24 04:03:07 +09:00
const props = withDefaults(defineProps<{
autoSticky?: boolean;
}>(), {
autoSticky: false,
});
2021-10-24 04:03:07 +09:00
const rootEl = $ref<HTMLElement>();
const bodyEl = $ref<HTMLElement>();
2021-10-24 04:03:07 +09:00
let headerHeight = $ref<string | undefined>();
2021-10-24 04:03:07 +09:00
const calc = () => {
const currentStickyTop = getComputedStyle(rootEl).getPropertyValue('--stickyTop') || '0px';
2021-10-24 04:03:07 +09:00
const header = rootEl.children[0] as HTMLElement;
if (header === bodyEl) {
bodyEl.style.setProperty('--stickyTop', currentStickyTop);
} else {
bodyEl.style.setProperty('--stickyTop', `calc(${currentStickyTop} + ${header.offsetHeight}px)`);
headerHeight = header.offsetHeight.toString();
2021-10-24 04:03:07 +09:00
if (props.autoSticky) {
header.style.setProperty('--stickyTop', currentStickyTop);
header.style.position = 'sticky';
header.style.top = 'var(--stickyTop)';
header.style.zIndex = '1';
}
}
};
2021-10-24 04:03:07 +09:00
const observer = new MutationObserver(() => {
window.setTimeout(() => {
calc();
}, 100);
});
2021-10-24 04:03:07 +09:00
onMounted(() => {
calc();
2021-10-24 04:03:07 +09:00
observer.observe(rootEl, {
attributes: false,
childList: true,
subtree: false,
});
});
2021-10-24 04:03:07 +09:00
onUnmounted(() => {
observer.disconnect();
2021-10-24 04:03:07 +09:00
});
</script>
<style lang="scss" module>
</style>