Skip to content

Commit

Permalink
chore: Optimize SearchBox sticky behavior for better performance
Browse files Browse the repository at this point in the history
This commit optimizes the sticky behavior of the SearchBox component by adding a condition to only attach the scroll event listener when the window width is greater than 768 pixels. This improvement reduces unnecessary event handling and improves performance.

Note: This message includes additional information for clarity. Please remove it before using the commit message.
  • Loading branch information
bramses committed Aug 27, 2024
1 parent 8256b5e commit e2c91e9
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/components/SearchBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ const SearchBox = () => {
const [isSticky, setIsSticky] = useState(false);

useEffect(() => {
const handleScroll = () => {
if (textAreaRef.current) {
const offsetTop = textAreaRef.current.getBoundingClientRect().top;
setIsSticky(offsetTop < 9);
}
};
if (window.innerWidth > 768) {
const handleScroll = () => {
if (textAreaRef.current) {
const offsetTop = textAreaRef.current.getBoundingClientRect().top;
setIsSticky(offsetTop < 9);
}
};

window.addEventListener('scroll', handleScroll);
return () => {
window.removeEventListener('scroll', handleScroll);
};
window.addEventListener('scroll', handleScroll);
return () => {
window.removeEventListener('scroll', handleScroll);
};
}
return undefined;
}, []);

const fetchByID = async (id: string) => {
Expand Down

0 comments on commit e2c91e9

Please sign in to comment.