From b8d3ba85f5507fb78bbf623aa84a66508b7cbfdd Mon Sep 17 00:00:00 2001 From: shadcn Date: Thu, 15 Jun 2023 15:27:41 +0400 Subject: [PATCH] feat: implement auto scroll --- components/button-scroll-to-bottom.tsx | 18 ++-------------- components/chat-scroll-anchor.tsx | 29 ++++++++++++++++++++++++++ components/chat.tsx | 6 +++++- lib/hooks/use-at-bottom.tsx | 23 ++++++++++++++++++++ package.json | 1 + pnpm-lock.yaml | 11 ++++++++++ 6 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 components/chat-scroll-anchor.tsx create mode 100644 lib/hooks/use-at-bottom.tsx diff --git a/components/button-scroll-to-bottom.tsx b/components/button-scroll-to-bottom.tsx index b94f34a..436a6c0 100644 --- a/components/button-scroll-to-bottom.tsx +++ b/components/button-scroll-to-bottom.tsx @@ -3,26 +3,12 @@ import * as React from 'react' import { cn } from '@/lib/utils' +import { useAtBottom } from '@/lib/hooks/use-at-bottom' import { Button, type ButtonProps } from '@/components/ui/button' import { IconArrowDown } from '@/components/ui/icons' export function ButtonScrollToBottom({ className, ...props }: ButtonProps) { - const [isAtBottom, setIsAtBottom] = React.useState(false) - - React.useEffect(() => { - const handleScroll = () => { - setIsAtBottom( - window.innerHeight + window.scrollY > document.body.offsetHeight - 100 - ) - } - - window.addEventListener('scroll', handleScroll, { passive: true }) - handleScroll() - - return () => { - window.removeEventListener('scroll', handleScroll) - } - }, []) + const isAtBottom = useAtBottom() return (