Skip to content

Commit

Permalink
🐛 fix: 修复回到最下方按钮丢失的问题 (#36)
Browse files Browse the repository at this point in the history
* 🐛 fix: slove backtoBottom not work

* 💄 fix: change padding styles
  • Loading branch information
ONLY-yours authored Jan 15, 2024
1 parent 52e2aa6 commit dc268da
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
23 changes: 17 additions & 6 deletions src/BackBottom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,22 @@ const BackBottom = memo<BackBottomProps>(
const [visible, setVisible] = useState<boolean>(false);
const { styles, cx } = useStyles(visible);
const ref = useRef<HTMLAnchorElement | HTMLButtonElement>(null);

const [isWindowAvailable, setIsWindowAvailable] = useState(false);

useEffect(() => {
// 检查window对象是否已经可用
if (typeof window !== 'undefined') {
setIsWindowAvailable(true);
}
}, []);

const current = useMemo(() => {
if (target.current && target.current.scrollHeight > target.current.clientHeight) {
return target.current;
}
return document.body;
}, []);
}, [isWindowAvailable]);

const scrollHeight = current?.scrollHeight || 0;
const clientHeight = current?.clientHeight || 0;
Expand All @@ -42,12 +52,13 @@ const BackBottom = memo<BackBottomProps>(

useEffect(() => {
if (typeof window === 'undefined') return;
const scroll = (e: any) => {
if (typeof current === 'undefined') return;
const scroll = () => {
timeRef.current = window.setTimeout(() => {
setVisible(current?.scrollTop + clientHeight + visibilityHeight < scrollHeight);
setScroll({
top: e?.scrollTop,
left: e?.scrollLeft,
top: current?.scrollTop,
left: current?.scrollLeft,
});
}, 60);
};
Expand All @@ -60,13 +71,13 @@ const BackBottom = memo<BackBottomProps>(
}
current?.removeEventListener?.('scroll', scroll);
};
}, []);
}, [current]);

useEffect(() => {
if (scroll?.top) {
setVisible(scroll?.top + clientHeight + visibilityHeight < scrollHeight);
}
}, [scrollHeight, scroll, visibilityHeight]);
}, [scrollHeight, scroll, visibilityHeight, current]);

const scrollToBottom: MouseEventHandler<HTMLDivElement> = (e) => {
(target as any)?.current?.scrollTo({ behavior: 'smooth', left: 0, top: scrollHeight });
Expand Down
2 changes: 1 addition & 1 deletion src/ChatItem/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const useStyles = createStyles(
position: relative;
width: 100%;
max-width: 100vw;
padding: 12px 16px;
padding: 16px;
content-visibility: auto;
contain-intrinsic-size: 100px;
time {
Expand Down

0 comments on commit dc268da

Please sign in to comment.