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(slider): scroll to a new item #342

Merged
merged 2 commits into from
Jan 17, 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
94 changes: 59 additions & 35 deletions src/components/slider/slider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRef } from "react";
import { useRef, useEffect } from "react";
import styled, { css } from "styled-components";
import ChevronIcon from "../../../public/icons/chevron.svg";
import { SliderListItem } from "./slider-list-item";
Expand Down Expand Up @@ -94,9 +94,6 @@ type SliderProps = {
onDelete?: (id: string) => void;
};

const BOOKMARKS_OFFSET = 150;
const OFFSET = 54;

export const Slider = ({
data,
editingMode,
Expand All @@ -114,57 +111,84 @@ export const Slider = ({
}
};

const disableLeftArrow = selectedItemId === data[0]?.id || !selectedItemId;
const disableRightArrow =
selectedItemId === data[data.length - 1]?.id || !selectedItemId;

const isBookmarkSlider = sliderType === SliderType.Bookmarks;
const isPhaseSlider = sliderType === SliderType.Phase;
const isFloorsSlider = sliderType === SliderType.Floors;

const layout = useAppLayout();

const handleLeftArrowClick = () => {
const currentSelectedIndex = data.findIndex(
(item) => item.id === selectedItemId
);
const prevItemId = data[currentSelectedIndex - 1].id;
onSelect(prevItemId);

sliderItemsListRef?.current?.scrollBy({
top: isFloorsSlider ? OFFSET : 0,
left: isBookmarkSlider ? -BOOKMARKS_OFFSET : isPhaseSlider ? -OFFSET : 0,
behavior: "smooth",
});
const prevItemId = data[currentSelectedIndex - 1]?.id;
onSelectHandler(prevItemId);
};

const handleRightArrowClick = () => {
const currentSelectedIndex = data.findIndex(
(item) => item.id === selectedItemId
);
const nextItemId = data[currentSelectedIndex + 1].id;
onSelect(nextItemId);

sliderItemsListRef?.current?.scrollBy({
top: isFloorsSlider ? -OFFSET : 0,
left: isBookmarkSlider ? BOOKMARKS_OFFSET : isPhaseSlider ? OFFSET : 0,
behavior: "smooth",
});
const nextItemId = data[currentSelectedIndex + 1]?.id;
onSelectHandler(nextItemId);
};

const onSelectHandler = (id: string): void => {
onSelect(id);
if (id) {
scrollItemIntoView(id);
onSelect(id);
}
};

const isItemVisible = (item: HTMLDivElement | undefined): boolean => {
const listElement = sliderItemsListRef?.current;
if (!item || !listElement) {
return false;
}
const listLeft = listElement.offsetLeft;
const listTop = listElement.offsetTop;
const listRight = listLeft + listElement.offsetWidth;
const listBottom = listTop + listElement.offsetHeight;

const itemLeft = item.offsetLeft - listElement.scrollLeft;
const itemTop = item.offsetTop - listElement.scrollTop;
const itemRight = itemLeft + item.offsetWidth;
const itemBottom = itemTop + item.offsetHeight;

const isVisible =
itemLeft >= listLeft &&
itemRight <= listRight &&
itemTop >= listTop &&
itemBottom <= listBottom;
return isVisible;
};

const scrollItemIntoView = (id: string): void => {
if (!id) {
return;
}
const listItem = listItems.current?.find(
(item: HTMLDivElement) => item.id === id
);

if (isBookmarkSlider || isPhaseSlider) {
listItem?.scrollIntoView({ behavior: "smooth", inline: "center" });
} else {
listItem?.scrollIntoView({ behavior: "smooth", block: "center" });
const isVisible = isItemVisible(listItem);
if (!isVisible) {
if (isBookmarkSlider || isPhaseSlider) {
listItem?.scrollIntoView({ behavior: "smooth", inline: "nearest" });
} else {
listItem?.scrollIntoView({ behavior: "smooth", block: "nearest" });
}
}
};

useEffect(() => {
scrollItemIntoView(selectedItemId);
}, [selectedItemId]);

const disableLeftArrow = selectedItemId === data[0]?.id || !selectedItemId;
const disableRightArrow =
selectedItemId === data[data.length - 1]?.id || !selectedItemId;

const isBookmarkSlider = sliderType === SliderType.Bookmarks;
const isPhaseSlider = sliderType === SliderType.Phase;
const isFloorsSlider = sliderType === SliderType.Floors;

const layout = useAppLayout();

return (
<>
<ArrowIconLeft
Expand Down
2 changes: 1 addition & 1 deletion src/pages/comparison/comparison.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,6 @@ export const Comparison = ({ mode }: ComparisonPageProps) => {

const addBookmarkHandler = () => {
const newBookmarkId = uuidv4();
setSelectedBookmarkId(newBookmarkId);
makeScreenshot().then((imageUrl) => {
setBookmarks((prev) => [
...prev,
Expand All @@ -380,6 +379,7 @@ export const Comparison = ({ mode }: ComparisonPageProps) => {
activeLayersIdsRightSide: [...activeLayersIdsRightSide],
},
]);
setSelectedBookmarkId(newBookmarkId);
});
};

Expand Down
2 changes: 1 addition & 1 deletion src/pages/debug-app/debug-app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,6 @@ export const DebugApp = () => {

const addBookmarkHandler = () => {
const newBookmarkId = uuidv4();
setSelectedBookmarkId(newBookmarkId);
makeScreenshot().then((imageUrl) => {
setBookmarks((prev) => [
...prev,
Expand All @@ -583,6 +582,7 @@ export const DebugApp = () => {
activeLayersIdsRightSide: [],
},
]);
setSelectedBookmarkId(newBookmarkId);
});
};

Expand Down
2 changes: 1 addition & 1 deletion src/pages/viewer-app/viewer-app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,6 @@ export const ViewerApp = () => {

const addBookmarkHandler = () => {
const newBookmarkId = uuidv4();
setSelectedBookmarkId(newBookmarkId);
makeScreenshot().then((imageUrl) => {
setBookmarks((prev) => [
...prev,
Expand All @@ -452,6 +451,7 @@ export const ViewerApp = () => {
activeLayersIdsRightSide: [],
},
]);
setSelectedBookmarkId(newBookmarkId);
});
};

Expand Down