feat: FaScrollArea 组件新增 onScroll 事件

This commit is contained in:
Hooray
2025-05-01 17:07:21 +08:00
parent cbef1bb52e
commit 9b5003fad8
2 changed files with 11 additions and 2 deletions
+9 -1
View File
@@ -25,6 +25,10 @@ const props = withDefaults(
},
)
const emits = defineEmits<{
onScroll: [Event]
}>()
const scrollAreaRef = useTemplateRef('scrollAreaRef')
const arrivedState = ref<{
@@ -46,6 +50,10 @@ const showMaskEnd = computed(() => {
return !arrivedState.value?.bottom
})
function onScroll(event: Event) {
emits('onScroll', event)
}
function onWheel(event: WheelEvent) {
if (props.horizontal) {
scrollAreaRef.value?.el?.viewportElement?.scrollBy({
@@ -108,7 +116,7 @@ defineExpose({
'--mask-scroll-container-gradient-color': props.gradientColor,
} : {}"
>
<ScrollArea ref="scrollAreaRef" :class="cn('relative z-0 flex-1', props.contentClass)" :scrollbar="props.scrollbar" :on-wheel="onWheel">
<ScrollArea ref="scrollAreaRef" :class="cn('relative z-0 flex-1', props.contentClass)" :scrollbar="props.scrollbar" :on-scroll="onScroll" :on-wheel="onWheel">
<slot />
<ScrollBar v-if="props.horizontal" orientation="horizontal" :class="{ 'opacity-0 pointer-events-none': !props.scrollbar }" />
</ScrollArea>
@@ -13,6 +13,7 @@ import ScrollBar from './ScrollBar.vue'
const props = defineProps<ScrollAreaRootProps & {
class?: HTMLAttributes['class']
scrollbar?: boolean
onScroll?: (event: Event) => void
onWheel?: (event: WheelEvent) => void
}>()
@@ -31,7 +32,7 @@ defineExpose({
<template>
<ScrollAreaRoot v-bind="delegatedProps" :class="cn('relative overflow-hidden', props.class)">
<ScrollAreaViewport ref="viewportRef" class="scroll-area-viewport h-full w-full rounded-[inherit]" @wheel.prevent="onWheel">
<ScrollAreaViewport ref="viewportRef" class="scroll-area-viewport h-full w-full rounded-[inherit]" @scroll="onScroll" @wheel="onWheel">
<slot />
</ScrollAreaViewport>
<ScrollBar :class="{ 'opacity-0 pointer-events-none': !props.scrollbar }" />