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

Fixes #16 : Add New Hook That Can Be Used By Dropdown/Popover Components #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions components/Dropdowns/IndexDropdown.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import Link from "next/link";
import { createPopper } from "@popperjs/core";
import usePopoverCloseEvents from "hooks/usePopoverCloseEvents";

const IndexDropdown = () => {
// dropdown props
Expand All @@ -16,8 +17,9 @@ const IndexDropdown = () => {
const closeDropdownPopover = () => {
setDropdownPopoverShow(false);
};
const componentRef = usePopoverCloseEvents(closeDropdownPopover)
return (
<>
<div ref={componentRef}>
<a
className="hover:text-blueGray-500 text-blueGray-700 px-3 py-4 lg:py-2 flex items-center text-xs uppercase font-bold"
href="#pablo"
Expand Down Expand Up @@ -140,7 +142,7 @@ const IndexDropdown = () => {
</a>
</Link>
</div>
</>
</div>
);
};

Expand Down
6 changes: 4 additions & 2 deletions components/Dropdowns/NotificationDropdown.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import { createPopper } from "@popperjs/core";
import usePopoverCloseEvents from "hooks/usePopoverCloseEvents";

const NotificationDropdown = () => {
// dropdown props
Expand All @@ -15,8 +16,9 @@ const NotificationDropdown = () => {
const closeDropdownPopover = () => {
setDropdownPopoverShow(false);
};
const componentRef = usePopoverCloseEvents(closeDropdownPopover)
return (
<>
<div ref={componentRef}>
<a
className="text-blueGray-500 block py-1 px-3"
href="#pablo"
Expand Down Expand Up @@ -73,7 +75,7 @@ const NotificationDropdown = () => {
Seprated link
</a>
</div>
</>
</div>
);
};

Expand Down
6 changes: 4 additions & 2 deletions components/Dropdowns/PagesDropdown.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import Link from "next/link";
import { createPopper } from "@popperjs/core";
import usePopoverCloseEvents from "hooks/usePopoverCloseEvents";

const PagesDropdown = () => {
// dropdown props
Expand All @@ -16,8 +17,9 @@ const PagesDropdown = () => {
const closeDropdownPopover = () => {
setDropdownPopoverShow(false);
};
const componentRef = usePopoverCloseEvents(closeDropdownPopover)
return (
<>
<div ref={componentRef}>
<a
className="lg:text-white lg:hover:text-blueGray-200 text-blueGray-700 px-3 py-4 lg:py-2 flex items-center text-xs uppercase font-bold"
href="#pablo"
Expand Down Expand Up @@ -140,7 +142,7 @@ const PagesDropdown = () => {
</a>
</Link>
</div>
</>
</div>
);
};

Expand Down
6 changes: 4 additions & 2 deletions components/Dropdowns/TableDropdown.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import { createPopper } from "@popperjs/core";
import usePopoverCloseEvents from "hooks/usePopoverCloseEvents";

const NotificationDropdown = () => {
// dropdown props
Expand All @@ -15,8 +16,9 @@ const NotificationDropdown = () => {
const closeDropdownPopover = () => {
setDropdownPopoverShow(false);
};
const componentRef = usePopoverCloseEvents(closeDropdownPopover)
return (
<>
<div ref={componentRef}>
<a
className="text-blueGray-500 py-1 px-3"
href="#pablo"
Expand Down Expand Up @@ -63,7 +65,7 @@ const NotificationDropdown = () => {
Something else here
</a>
</div>
</>
</div>
);
};

Expand Down
6 changes: 4 additions & 2 deletions components/Dropdowns/UserDropdown.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import { createPopper } from "@popperjs/core";
import usePopoverCloseEvents from "hooks/usePopoverCloseEvents";

const UserDropdown = () => {
// dropdown props
Expand All @@ -15,8 +16,9 @@ const UserDropdown = () => {
const closeDropdownPopover = () => {
setDropdownPopoverShow(false);
};
const componentRef = usePopoverCloseEvents(closeDropdownPopover)
return (
<>
<div ref={componentRef}>
<a
className="text-blueGray-500 block"
href="#pablo"
Expand Down Expand Up @@ -81,7 +83,7 @@ const UserDropdown = () => {
Seprated link
</a>
</div>
</>
</div>
);
};

Expand Down
28 changes: 28 additions & 0 deletions hooks/usePopoverCloseEvents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useEffect, useRef } from "react";

export default function usePopoverCloseEvents(closeFunction) {
const ref = useRef(null)

const handleHideDropdown = (event) => {
if (event.key === 'Escape') {
closeFunction();
}
};

const handleClickOutside = (event) => {
if (ref.current && !ref.current.contains(event.target)) {
closeFunction();
}
};

useEffect(() => {
document.addEventListener('keydown', handleHideDropdown, true);
document.addEventListener('click', handleClickOutside, true);
return () => {
document.removeEventListener('keydown', handleHideDropdown, true);
document.removeEventListener('click', handleClickOutside, true);
};
});

return ref;
}