46 lines
1.5 KiB
Vue
46 lines
1.5 KiB
Vue
<script setup lang="ts">
|
|
import { ArrowDownIcon } from "@lucide/vue";
|
|
import { computed, onMounted, onUnmounted, ref, watch } from "vue";
|
|
|
|
import { MessageScrollerButton, useMessageScrollerScrollable } from "@/components/ui/message-scroller";
|
|
import { setAtEnd, unseenCount } from "./unseen";
|
|
|
|
/**
|
|
* The scroller's jump-to-latest button. It reads where the reader is for the
|
|
* unseen count and says how many replies are waiting below.
|
|
*
|
|
* The scroller measures on the frames after it mounts, and until then it
|
|
* reports "not scrollable", which would look like the end. The reader counts
|
|
* as at the end only once it has measured.
|
|
*/
|
|
const scrollable = useMessageScrollerScrollable();
|
|
const measured = ref(false);
|
|
let frame = 0;
|
|
onMounted(() => {
|
|
frame = requestAnimationFrame(() => {
|
|
frame = requestAnimationFrame(() => {
|
|
measured.value = true;
|
|
});
|
|
});
|
|
});
|
|
onUnmounted(() => {
|
|
cancelAnimationFrame(frame);
|
|
setAtEnd(false);
|
|
});
|
|
watch(
|
|
() => measured.value && !scrollable.value.end,
|
|
(end) => setAtEnd(end),
|
|
{ immediate: true },
|
|
);
|
|
|
|
const label = computed(() => `${unseenCount.value} new ${unseenCount.value === 1 ? "reply" : "replies"}`);
|
|
</script>
|
|
|
|
<template>
|
|
<MessageScrollerButton :size="unseenCount ? 'sm' : 'icon-sm'" :class="unseenCount ? 'gap-1.5 whitespace-nowrap' : undefined">
|
|
<ArrowDownIcon />
|
|
<template v-if="unseenCount">{{ label }}</template>
|
|
<span v-else class="sr-only">Scroll to end</span>
|
|
</MessageScrollerButton>
|
|
</template>
|