-
Notifications
You must be signed in to change notification settings - Fork 53
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
Showing
7 changed files
with
129 additions
and
32 deletions.
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,63 @@ | ||
import { useRef, useState } from "react"; | ||
import { useCookies } from "react-cookie"; | ||
import { ChevronDownIcon } from "@heroicons/react/solid"; | ||
|
||
import { useClickOutside } from "hooks/useClickOutside"; | ||
import classNames from "utils/classNames"; | ||
|
||
const languages = [ | ||
{ | ||
locale: "en", | ||
language: "English", | ||
}, | ||
{ | ||
locale: "hi", | ||
language: "हिंदी", | ||
}, | ||
]; | ||
|
||
type Props = { | ||
align: "left" | "right"; | ||
}; | ||
|
||
function LanguageDropdown(props: Props) { | ||
const { align } = props; | ||
|
||
const [showMenu, setShowMenu] = useState(false); | ||
|
||
const [cookies, setCookie] = useCookies(["locale"]); | ||
|
||
const toggleRef = useRef<HTMLDivElement>(null); | ||
useClickOutside([toggleRef], () => setShowMenu(false)); | ||
|
||
return ( | ||
<div ref={toggleRef} className="relative"> | ||
<div | ||
className="flex cursor-pointer" | ||
onClick={() => setShowMenu(!showMenu)} | ||
> | ||
<div>{(cookies.locale || "en").toUpperCase()}</div> | ||
<ChevronDownIcon className="ml-2 h-5 w-5 group-hover:text-black" /> | ||
</div> | ||
{showMenu && ( | ||
<div | ||
className={classNames( | ||
"absolute top-8 w-24 cursor-pointer border bg-white dark:bg-dark-100", | ||
align === "left" ? "left-0" : "right-0", | ||
)} | ||
> | ||
{languages.map((language) => ( | ||
<div | ||
key={language.locale} | ||
className="border-b px-3 py-2 last:border-b-0 hover:bg-slate-400 dark:hover:bg-slate-700" | ||
> | ||
{language.language} | ||
</div> | ||
))} | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
export default LanguageDropdown; |
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
{ | ||
"Home": "होम पेज" | ||
"Home": "होम पेज", | ||
"Content": "सामग्री", | ||
"Appearance": "उपस्थिति", | ||
"Search": "खोज" | ||
} |
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,32 @@ | ||
import { useEffect } from "react"; | ||
|
||
const isNode = (evt: EventTarget | null): evt is Node => | ||
Boolean(evt && "nodeType" in evt); | ||
|
||
export const useClickOutside = ( | ||
ref: React.RefObject<HTMLElement> | React.RefObject<HTMLElement>[], | ||
callback: () => void, | ||
) => { | ||
const handleClick = (evt: MouseEvent | TouchEvent) => { | ||
const refs = Array.isArray(ref) ? ref : [ref]; | ||
if ( | ||
refs.every( | ||
(refItem) => | ||
!refItem.current || | ||
(isNode(evt.target) && | ||
!refItem.current.contains(evt.target) && | ||
evt.target.nodeName !== "svg"), | ||
) | ||
) { | ||
callback(); | ||
} | ||
}; | ||
useEffect(() => { | ||
document.addEventListener("mousedown", handleClick); | ||
document.addEventListener("touchstart", handleClick); | ||
return () => { | ||
document.removeEventListener("mousedown", handleClick); | ||
document.removeEventListener("touchstart", handleClick); | ||
}; | ||
}); | ||
}; |