import {computed, reactive, SimpleFunction} from "@peryl/react-compose"; import {delay} from "@peryl/utils/delay"; import {iTableScrollRefer} from "./useTableScroll"; import {PlainScroll} from "../../../Scroll"; export function useFixedShadow( { scrollRefer, }: { scrollRefer: iTableScrollRefer }) { const state = reactive({ showFixedLeft: false, showFixedRight: false, }); const freeState = { scroll: null as null | PlainScroll, dispose: null as null | SimpleFunction, onScroll: () => { const { freezeState: { wrapperScrollLeft }, state: { hostWidth, contentWidth } } = freeState.scroll!; const showFixedLeft = contentWidth > hostWidth && wrapperScrollLeft > 0; const showFixedRight = contentWidth > hostWidth && Math.abs(wrapperScrollLeft + hostWidth - contentWidth) > 5; state.showFixedLeft !== showFixedLeft && (state.showFixedLeft = showFixedLeft); state.showFixedRight !== showFixedRight && (state.showFixedRight = showFixedRight); } }; scrollRefer.onTableScrollRefer(scroll => { freeState.dispose?.(); if (!scroll) { return; } freeState.scroll = scroll; scroll.on.onScroll(freeState.onScroll); freeState.dispose = () => scroll.off.onScroll(freeState.onScroll); delay().then(() => {freeState.onScroll();}); }); const classes = computed(() => [ { 'plt-table-hide-fixed-left': !state.showFixedLeft, 'plt-table-hide-fixed-right': !state.showFixedRight, } ]); return { fixedShadowClass: classes, }; }