2021-10-23 14:03:07 -05:00
|
|
|
<template>
|
|
|
|
<div ref="rootEl">
|
|
|
|
<slot name="header"></slot>
|
2022-06-19 23:20:28 -05:00
|
|
|
<div ref="bodyEl" :data-sticky-container-header-height="headerHeight">
|
2021-10-23 14:03:07 -05:00
|
|
|
<slot></slot>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import { onMounted, onUnmounted } from 'vue';
|
2021-10-23 14:03:07 -05:00
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
const props = withDefaults(defineProps<{
|
|
|
|
autoSticky?: boolean;
|
|
|
|
}>(), {
|
|
|
|
autoSticky: false,
|
|
|
|
});
|
2021-10-23 14:03:07 -05:00
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
const rootEl = $ref<HTMLElement>();
|
|
|
|
const bodyEl = $ref<HTMLElement>();
|
2021-10-23 14:03:07 -05:00
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
let headerHeight = $ref<string | undefined>();
|
2021-10-23 14:03:07 -05:00
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
const calc = () => {
|
|
|
|
const currentStickyTop = getComputedStyle(rootEl).getPropertyValue('--stickyTop') || '0px';
|
2021-10-23 14:03:07 -05:00
|
|
|
|
2022-06-19 23:20:28 -05: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-23 14:03:07 -05:00
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
if (props.autoSticky) {
|
|
|
|
header.style.setProperty('--stickyTop', currentStickyTop);
|
|
|
|
header.style.position = 'sticky';
|
|
|
|
header.style.top = 'var(--stickyTop)';
|
|
|
|
header.style.zIndex = '1';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-10-23 14:03:07 -05:00
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
const observer = new MutationObserver(() => {
|
|
|
|
window.setTimeout(() => {
|
|
|
|
calc();
|
|
|
|
}, 100);
|
|
|
|
});
|
2021-10-23 14:03:07 -05:00
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
onMounted(() => {
|
|
|
|
calc();
|
2021-10-23 14:03:07 -05:00
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
observer.observe(rootEl, {
|
|
|
|
attributes: false,
|
|
|
|
childList: true,
|
|
|
|
subtree: false,
|
|
|
|
});
|
|
|
|
});
|
2021-10-23 14:03:07 -05:00
|
|
|
|
2022-06-19 23:20:28 -05:00
|
|
|
onUnmounted(() => {
|
|
|
|
observer.disconnect();
|
2021-10-23 14:03:07 -05:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" module>
|
|
|
|
|
|
|
|
</style>
|