Skip to content

Commit 647680f

Browse files
committed
Feature(modeling-commons): Add back to top component
1 parent ccd1df0 commit 647680f

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<template>
2+
<Transition
3+
enter-active-class="transition duration-200 ease-out"
4+
enter-from-class="opacity-0 translate-y-2"
5+
leave-active-class="transition duration-150 ease-in"
6+
leave-to-class="opacity-0 translate-y-2"
7+
>
8+
<UButton
9+
v-if="show"
10+
icon="i-lucide-arrow-up"
11+
color="primary"
12+
size="lg"
13+
:ui="{ base: 'rounded-full shadow-lg' }"
14+
class="fixed bottom-6 right-6 z-50"
15+
aria-label="Back to top"
16+
@click="emit('click')"
17+
/>
18+
</Transition>
19+
</template>
20+
21+
<script setup lang="ts">
22+
defineProps<{ show: boolean }>();
23+
24+
const emit = defineEmits<{ click: [] }>();
25+
</script>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export function useBackToTop(threshold = 600) {
2+
const show = ref(false);
3+
4+
const update = () => {
5+
show.value = window.scrollY > threshold;
6+
};
7+
8+
const scrollToTop = () => {
9+
window.scrollTo({ top: 0, behavior: "smooth" });
10+
};
11+
12+
onMounted(() => {
13+
window.addEventListener("scroll", update, { passive: true });
14+
update();
15+
});
16+
17+
onBeforeUnmount(() => {
18+
window.removeEventListener("scroll", update);
19+
});
20+
21+
return { show, scrollToTop };
22+
}

0 commit comments

Comments
 (0)