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

(Issue: "Improve code in any way") I improved code readability and reusability for usePostion.js file #384

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
64 changes: 27 additions & 37 deletions hooks/usePosition.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@ import { useCallback, useEffect, useState } from 'react';
export function usePosition(ref) {
const [prevElement, setPrevElement] = useState(null);
const [nextElement, setNextElement] = useState(null);

useEffect(() => {
const element = ref.current;
const update = () => {
const rect = element.getBoundingClientRect();
const visibleElements = Array.from(element.children).filter((child) => {
const containerElement = ref.current;

const handleScroll = () => {
const rect = containerElement.getBoundingClientRect();
const visibleElements = Array.from(containerElement.children).filter((child) => {
const childRect = child.getBoundingClientRect();
return rect.left <= childRect.left && rect.right >= childRect.right;
});

if (visibleElements.length > 0) {
setPrevElement(getPrevElement(visibleElements));
setNextElement(getNextElement(visibleElements));
setPrevElement(getSiblingElement(visibleElements, 'previousElementSibling'));
setNextElement(getSiblingElement(visibleElements, 'nextElementSibling'));
}
};

update();
element.addEventListener('scroll', update, { passive: true });
handleScroll();
containerElement.addEventListener('scroll', handleScroll, { passive: true });

return () => {
element.removeEventListener('scroll', update, { passive: true });
containerElement.removeEventListener('scroll', handleScroll, { passive: true });
};
}, [ref]);

Expand All @@ -30,9 +34,7 @@ export function usePosition(ref) {

if (!currentNode || !element) return;

let newScrollPosition;

newScrollPosition =
const newScrollPosition =
element.offsetLeft +
element.getBoundingClientRect().width / 2 -
currentNode.getBoundingClientRect().width / 2;
Expand All @@ -45,35 +47,23 @@ export function usePosition(ref) {
[ref]
);

function getPrevElement(list) {
const sibling = list[0].previousElementSibling;

if (sibling instanceof HTMLElement) {
return sibling;
}

return sibling;
}
const getSiblingElement = (list, property) => {
const sibling = list.length > 0 ? list[0][property] : null;
return sibling instanceof HTMLElement ? sibling : null;
};

function getNextElement(list) {
const sibling = list[list.length - 1].nextElementSibling;
if (sibling instanceof HTMLElement) {
return sibling;
}
return null;
}
const scrollRight = useCallback(
() => scrollToElement(nextElement),
[scrollToElement, nextElement]
);
const scrollLeft = useCallback(
() => scrollToElement(prevElement),
[scrollToElement, prevElement]
const scroll = useCallback(
(direction) => {
const targetElement = direction === 'right' ? nextElement : prevElement;
scrollToElement(targetElement);
},
[scrollToElement, nextElement, prevElement]
);

return {
hasItemsOnLeft: prevElement !== null,
hasItemsOnRight: nextElement !== null,
scrollLeft,
scrollRight,
scrollLeft: () => scroll('left'),
scrollRight: () => scroll('right'),
};
}