mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
Usernav to be reused
This commit is contained in:
38
components/ui/ClickOutside/ClickOutside.tsx
Normal file
38
components/ui/ClickOutside/ClickOutside.tsx
Normal file
@@ -0,0 +1,38 @@
|
||||
import React, {
|
||||
FunctionComponent,
|
||||
MutableRefObject,
|
||||
useEffect,
|
||||
useRef,
|
||||
} from "react";
|
||||
import { findDOMNode } from "react-dom";
|
||||
|
||||
export interface ClickOutsideProps {
|
||||
onClickOutside: (e?: MouseEvent) => void;
|
||||
children: React.ReactNode | any;
|
||||
}
|
||||
|
||||
const ClickOutside: FunctionComponent<ClickOutsideProps> = ({
|
||||
children,
|
||||
onClickOutside,
|
||||
}) => {
|
||||
let node = useRef(null);
|
||||
|
||||
const handleClick = (e: MouseEvent) => {
|
||||
console.log("eeee");
|
||||
if (!e || !node.current.contains(e.target as HTMLInputElement)) {
|
||||
console.log("eeee");
|
||||
// onClickOutside && onClickOutside(e);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
document.addEventListener("click", handleClick, true);
|
||||
return () => {
|
||||
document.removeEventListener("click", handleClick);
|
||||
};
|
||||
});
|
||||
|
||||
return children;
|
||||
};
|
||||
|
||||
export default ClickOutside;
|
1
components/ui/ClickOutside/index.ts
Normal file
1
components/ui/ClickOutside/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { default } from "./ClickOutside";
|
@@ -5,13 +5,21 @@ import s from "./Sidebar.module.css";
|
||||
interface Props {
|
||||
className?: string;
|
||||
children?: any;
|
||||
closeSidebar: () => void;
|
||||
}
|
||||
|
||||
const Sidebar: FunctionComponent<Props> = ({ className, children }) => {
|
||||
const Sidebar: FunctionComponent<Props> = ({
|
||||
className,
|
||||
children,
|
||||
closeSidebar,
|
||||
}) => {
|
||||
const rootClassName = cn(s.root, className);
|
||||
return (
|
||||
<div className={rootClassName}>
|
||||
<div className="fixed inset-0 overflow-hidden shadow-sm bg-black bg-opacity-25">
|
||||
<div
|
||||
className="fixed inset-0 overflow-hidden shadow-sm bg-black bg-opacity-25"
|
||||
onClick={closeSidebar}
|
||||
>
|
||||
<div className="absolute inset-0 overflow-hidden">
|
||||
<section className="absolute inset-y-0 right-0 pl-10 max-w-full flex sm:pl-16 ">
|
||||
<div className="w-screen max-w-2xl">
|
||||
|
@@ -2,3 +2,4 @@ export { default as Button } from "./Button";
|
||||
export { default as Container } from "./Container";
|
||||
export { default as Sidebar } from "./Sidebar";
|
||||
export { default as Logo } from "./Logo";
|
||||
export { default as ClickOutside } from "./ClickOutside";
|
||||
|
Reference in New Issue
Block a user