Skip to content
Snippets Groups Projects
Commit 4533addc authored by Aggarwal Himanshu's avatar Aggarwal Himanshu
Browse files

Add hook to detect click outside a ref

parent f2be55e0
No related branches found
No related tags found
2 merge requests!17Merge develop into main,!9Add support for filtering cryptocurrencies using the filter components
export * from "./useFilters";
export * from "./useOnClickOutside";
import React, { useState } from "react";
import { useState } from "react";
/**
* Format of each filter should be:
......@@ -8,7 +8,7 @@ import React, { useState } from "react";
* value: ""
* }
*/
export default function useFilters(initialArray) {
export function useFilters(initialArray) {
const [filters, setFilters] = useState(initialArray);
const checkIfFilterExists = (filter) => {
......
import { useEffect } from "react";
export function useOnClickOutside(ref, handler) {
useEffect(() => {
const listener = (event) => {
if (ref.current && ref.current?.contains(event.target)) {
return;
}
handler(event);
};
document.addEventListener("mousedown", listener);
document.addEventListener("touchstart", listener);
return () => {
document.removeEventListener("mousedown", listener);
document.removeEventListener("touchstart", listener);
};
}, [ref, handler]);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment