-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
app(ui/ux): add scroll-to-top and scroll-to-botton buttons (#13)
- Loading branch information
1 parent
cc4ebf5
commit cd83733
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* @file FCScrollButtons.tsx | ||
* | ||
* The functional component for the scroll to top and scroll to bottom buttons, | ||
* used at the bottom of the chat input. | ||
*/ | ||
|
||
import { IconButton, Tooltip } from "@radix-ui/themes"; | ||
import { RefObject, useEffect, useRef, useState } from "react"; | ||
import { GoMoveToBottom, GoMoveToTop } from "react-icons/go"; | ||
import { scrollToBottom, scrollToTop } from "../utils"; | ||
|
||
interface FCScrollButtonsProps { | ||
containerRef: RefObject<HTMLElement>; | ||
} | ||
|
||
export const FCScrollButtons = ({ containerRef }: FCScrollButtonsProps) => { | ||
const [canScrollUp, setCanScrollUp] = useState<boolean>(false); | ||
const [canScrollDown, setCanScrollDown] = useState<boolean>(false); | ||
const debounceTimer = useRef<number | null>(null); | ||
|
||
// Handler for the container scroll event; it is debounced for better | ||
// performance | ||
const handleScroll = () => { | ||
if (debounceTimer.current !== null) { | ||
clearTimeout(debounceTimer.current); | ||
} | ||
debounceTimer.current = setTimeout(() => { | ||
if (containerRef.current !== null) { | ||
const { scrollTop, scrollHeight, clientHeight } = containerRef.current; | ||
setCanScrollUp(scrollTop > 0); | ||
setCanScrollDown(scrollTop + clientHeight < scrollHeight); | ||
} | ||
}, 300); | ||
}; | ||
|
||
useEffect(() => { | ||
const container = containerRef.current; | ||
if (container !== null) { | ||
container.addEventListener("scroll", handleScroll, { passive: true }); | ||
} | ||
|
||
return () => { | ||
if (container !== null) { | ||
container.removeEventListener("scroll", handleScroll); | ||
} | ||
if (debounceTimer.current !== null) { | ||
clearTimeout(debounceTimer.current); | ||
} | ||
}; | ||
}, [containerRef]); | ||
|
||
return ( | ||
<> | ||
<Tooltip content="Scroll to top" side="top"> | ||
<IconButton | ||
disabled={!canScrollUp} | ||
variant="ghost" | ||
size="1" | ||
onClick={() => scrollToTop(containerRef)} | ||
> | ||
<GoMoveToTop size="20" /> | ||
</IconButton> | ||
</Tooltip> | ||
<Tooltip content="Scroll to bottom" side="top"> | ||
<IconButton | ||
disabled={!canScrollDown} | ||
variant="ghost" | ||
size="1" | ||
onClick={() => scrollToBottom(containerRef)} | ||
> | ||
<GoMoveToBottom size="20" /> | ||
</IconButton> | ||
</Tooltip> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters