Skip to content

Commit

Permalink
Now the menu position is automatically recalculated depending on wher…
Browse files Browse the repository at this point in the history
…e the parent element is located. Deleted an unused hook.
  • Loading branch information
RaayNoff committed Sep 4, 2022
1 parent b0ea322 commit 853630c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 46 deletions.
11 changes: 4 additions & 7 deletions src/components/UI/list/additional/listMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import React, { Dispatch, FC } from "react";
import { useActions } from "../../../../hooks/useActions";
import { useMenuPositionRef } from "../../../../hooks/useMenuPositionRef";
import { useMenuPosition } from "../../../../hooks/useMenuPosition";
import { contentApi } from "../../../../services/contentApi";

interface IListMenuProps {
isEnabled: boolean;
setIsEnabled: Dispatch<React.SetStateAction<boolean>>;
listId: number;
menuPosition: {
top: number;
left: number;
};
menuPositionParent: React.RefObject<HTMLDivElement>;
}

const ListMenu: FC<IListMenuProps> = ({
isEnabled,
listId,
setIsEnabled,
menuPosition,
menuPositionParent,
}) => {
const menuRef = useMenuPositionRef(menuPosition, 110, 15);
const menuRef = useMenuPosition(menuPositionParent, 30, 110);
const [deleteList] = contentApi.useFetchListsDeleteMutation();
const { listDelete } = useActions();
const { shareListToggleOn, editListToggleOn } = useActions();
Expand Down
4 changes: 1 addition & 3 deletions src/components/UI/list/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { FC, useEffect, useRef, useState } from "react";
import { useActions } from "../../../hooks/useActions";
import { useListById } from "../../../hooks/useListById";
import { useMenuPosition } from "../../../hooks/useMenuPosition";
import ListMenu from "./additional/listMenu";
import s from "./list.module.scss";

Expand All @@ -12,7 +11,6 @@ interface IListProps {

const List: FC<IListProps> = ({ listId, clickCallback }) => {
const dotRef = useRef<HTMLDivElement>(null);
const menuPosition = useMenuPosition(dotRef);
const [isMenuOpened, SetIsMenuOpened] = useState<boolean>(false);
const { color, listName } = useListById(listId);
const { displayList } = useActions();
Expand Down Expand Up @@ -54,7 +52,7 @@ const List: FC<IListProps> = ({ listId, clickCallback }) => {
</div>

<ListMenu
menuPosition={menuPosition}
menuPositionParent={dotRef}
isEnabled={isMenuOpened}
listId={listId}
setIsEnabled={SetIsMenuOpened}
Expand Down
27 changes: 13 additions & 14 deletions src/hooks/useMenuPosition.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import React, { useEffect, useState } from "react";
import React, { useEffect, useRef } from "react";

export const useMenuPosition = (
elementRef: React.MutableRefObject<HTMLElement | HTMLDivElement | null>
elementRef: React.RefObject<HTMLDivElement>,
offsetTop: number,
offsetLeft: number
) => {
const [menuPosition, setMenuPosition] = useState({
top: 0,
left: 0,
});
const menuRef = useRef<HTMLMenuElement>(null);

useEffect(() => {
if (elementRef.current) {
const elementNode = elementRef.current;
setMenuPosition({
top: elementNode.offsetTop,
left: elementNode.offsetLeft,
});
if (elementRef.current && menuRef.current) {
const top = elementRef.current.offsetTop;
const left = elementRef.current.offsetLeft;

menuRef.current.style.top = top + offsetTop + "px";
menuRef.current.style.left = left + offsetLeft + "px";
}
}, [elementRef]);
}, [elementRef.current?.offsetTop, elementRef.current?.offsetLeft]);

return menuPosition;
return menuRef;
};
22 changes: 0 additions & 22 deletions src/hooks/useMenuPositionRef.ts

This file was deleted.

0 comments on commit 853630c

Please sign in to comment.