Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix: 修复回到最下方按钮丢失的问题 #36

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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