-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
819eff3
commit 2353677
Showing
6 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { cn } from "@/lib/utils"; | ||
import * as React from "react"; | ||
|
||
interface Props { | ||
children?: React.ReactNode; | ||
} | ||
|
||
function useCloseOnOutsideTouch(ref: any, setIsOpen: any) { | ||
React.useEffect(() => { | ||
/** | ||
* Alert if clicked on outside of element | ||
*/ | ||
function handleClickOutside(event: any) { | ||
if (ref.current && !ref.current.contains(event.target)) { | ||
setIsOpen(false); | ||
} | ||
} | ||
// Bind the event listener | ||
document.addEventListener("mousedown", handleClickOutside); | ||
return () => { | ||
// Unbind the event listener on clean up | ||
document.removeEventListener("mousedown", handleClickOutside); | ||
}; | ||
}, [ref, setIsOpen]); | ||
} | ||
|
||
function SidebarToggle(props) { | ||
return ( | ||
<button | ||
className="border-2 p-2 rounded-lg mb-4 block" | ||
onClick={() => props.setIsOpen(props.open ? true : false)} | ||
> | ||
{props.open ? "open" : "close"} | ||
</button> | ||
); | ||
} | ||
|
||
export const SidebarMobile = ({ children }: Props) => { | ||
const [isOpen, setIsOpen] = React.useState(false); | ||
const wrapperRef = React.useRef(null); | ||
useCloseOnOutsideTouch(wrapperRef, setIsOpen); | ||
|
||
return ( | ||
<div className="block sm:hidden"> | ||
<SidebarToggle setIsOpen={setIsOpen} open={true}></SidebarToggle> | ||
<div | ||
className={cn( | ||
"bg-slate-600 w-60 absolute inset-0 p-4 transition ease-in-out duration-200", | ||
isOpen ? "transform translate-x-0" : "transform -translate-x-full" | ||
)} | ||
ref={wrapperRef} | ||
> | ||
<SidebarToggle setIsOpen={setIsOpen}></SidebarToggle> | ||
{children ? children : "SidebarMobile"} | ||
</div> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import * as React from "react"; | ||
|
||
// component exports | ||
export * from "./SidebarMobile"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { clsx, type ClassValue } from "clsx"; | ||
import { twMerge } from "tailwind-merge"; | ||
|
||
export function cn(...inputs: ClassValue[]) { | ||
return twMerge(clsx(inputs)); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.