Skip to content

Commit

Permalink
LanguageDropdown scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
elmeister committed Aug 24, 2023
1 parent e0ae8b7 commit c5b4215
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 32 deletions.
2 changes: 2 additions & 0 deletions src/components/Headers/AudioPlayer.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";

import { Fragment, useEffect, useRef, useState } from "react";
import { connect } from "react-redux";
import { Dialog, Transition } from "@headlessui/react";
Expand Down
2 changes: 2 additions & 0 deletions src/components/Headers/ContentModal.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";

import { Fragment, useState } from "react";
import { Dialog, Listbox, Transition } from "@headlessui/react";
import {
Expand Down
63 changes: 63 additions & 0 deletions src/components/Headers/LanguageDropdown.tsx
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;
56 changes: 25 additions & 31 deletions src/components/Headers/PageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Settings from "../Shared/Settings";
import AudioPlayer from "./AudioPlayer";
import ContentModal from "./ContentModal";
import DarkModeToggle from "./DarkModeToggle";
import LanguageDropdown from "./LanguageDropdown";

const noop = () => {};

Expand Down Expand Up @@ -131,7 +132,7 @@ const PageHeader = ({
width={24}
height={24}
/>
Content
{translate("Content")}
</button>
<button
type="button"
Expand All @@ -148,7 +149,7 @@ const PageHeader = ({
width={28}
height={24}
/>
Appearance
{translate("Appearance")}
</button>
<button
type="button"
Expand All @@ -167,17 +168,8 @@ const PageHeader = ({
width={24}
height={24}
/>
Source
{translate("Source")}
</button>
{/*<Link href="/verse-parallel">
<a
href="#"
className="border-transparent text-current flex flex-col items-center p-2 rounded border-b-2 text-sm font-medium hover:bg-nav-hover dark:hover:bg-dark-bg"
>
<img className="w-6 h-6 mb-1" src="/Parellel.svg" />
Parallel Mode
</a>
</Link> */}

<button
type="button"
Expand All @@ -194,7 +186,7 @@ const PageHeader = ({
width={24}
height={24}
/>
Play Audio
{translate("Play Audio")}
</button>

<button
Expand All @@ -214,7 +206,7 @@ const PageHeader = ({
width={24}
height={24}
/>
Advanced View
{translate("Advanced View")}
</button>
{isProduction ? null : (
<button
Expand All @@ -234,7 +226,7 @@ const PageHeader = ({
width={24}
height={24}
/>
Notes
{translate("Notes")}
</button>
)}
{isProduction ? null : (
Expand All @@ -249,15 +241,15 @@ const PageHeader = ({
width={24}
height={24}
/>
Bookmark
{translate("Bookmark")}
</Link>
)}
</div>
</div>
<div className="flex flex-1 items-center justify-start pr-2 lg:ml-6 lg:justify-end">
<div className="w-full max-w-lg lg:max-w-xs">
<label htmlFor="search" className="sr-only">
Search
{translate("Search")}
</label>
<div className="relative">
<div className="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3"></div>
Expand All @@ -279,7 +271,7 @@ const PageHeader = ({
id="search"
name="search"
className="block w-full rounded-md border border-gray-300 bg-white py-2 pl-10 pr-3 leading-5 placeholder:text-gray-500 focus:border-my-orange focus:outline-none focus:ring-1 focus:ring-my-orange focus:placeholder:text-gray-400 dark:bg-dark-100 dark:placeholder:text-gray-50 sm:text-sm"
placeholder="Search"
placeholder={translate("Search")}
type="search"
value={input}
onChange={(e) => {
Expand All @@ -290,6 +282,7 @@ const PageHeader = ({
</div>
</div>
<DarkModeToggle />
<LanguageDropdown align="right" />
</div>
<div className="flex items-center lg:hidden">
{/* Mobile menu button */}
Expand All @@ -311,52 +304,53 @@ const PageHeader = ({
href="/"
className="block border-transparent py-2 pl-3 pr-4 text-base font-medium text-gray-500 hover:border-l-4 hover:border-my-orange hover:bg-yellow-100 hover:text-gray-900 focus:border-l-4 focus:border-my-orange focus:bg-yellow-100 focus:text-gray-900 dark:text-white dark:hover:text-gray-900 dark:focus:text-gray-900"
>
Home
{translate("Home")}
</Link>
<button
type="button"
onClick={openContentModal}
className="block border-transparent py-2 pl-3 pr-4 text-base font-medium text-gray-500 hover:border-l-4 hover:border-my-orange hover:bg-yellow-100 hover:text-gray-900 focus:border-l-4 focus:border-my-orange focus:bg-yellow-100 focus:text-gray-900 dark:text-white dark:hover:text-gray-900 dark:focus:text-gray-900"
>
Content
{translate("Content")}
</button>
<button
type="button"
onClick={openSettingsModal}
className="block border-transparent py-2 pl-3 pr-4 text-base font-medium text-gray-500 hover:border-l-4 hover:border-my-orange hover:bg-yellow-100 hover:text-gray-900 focus:border-l-4 focus:border-my-orange focus:bg-yellow-100 focus:text-gray-900 dark:text-white dark:hover:text-gray-900 dark:focus:text-gray-900"
>
Appearance
{translate("Appearance")}
</button>
<button
type="button"
onClick={openAuthorSettingsModal}
className="block border-transparent py-2 pl-3 pr-4 text-base font-medium text-gray-500 hover:border-l-4 hover:border-my-orange hover:bg-yellow-100 hover:text-gray-900 focus:border-l-4 focus:border-my-orange focus:bg-yellow-100 focus:text-gray-900 dark:text-white dark:hover:text-gray-900 dark:focus:text-gray-900"
>
Language
{translate("Source")}
</button>
<button
type="button"
onClick={openPlayerModal}
className="block border-transparent py-2 pl-3 pr-4 text-base font-medium text-gray-500 hover:border-l-4 hover:border-my-orange hover:bg-yellow-100 hover:text-gray-900 focus:border-l-4 focus:border-my-orange focus:bg-yellow-100 focus:text-gray-900 dark:text-white dark:hover:text-gray-900 dark:focus:text-gray-900"
>
Play Audio
{translate("Play Audio")}
</button>
<button
type="button"
className="block border-transparent py-2 pl-3 pr-4 text-base font-medium text-gray-500 hover:border-l-4 hover:border-my-orange hover:bg-yellow-100 hover:text-gray-900 focus:border-l-4 focus:border-my-orange focus:bg-yellow-100 focus:text-gray-900 dark:text-white dark:hover:text-gray-900 dark:focus:text-gray-900"
>
Notes
{translate("Notes")}
</button>
<button
type="button"
className="block border-transparent py-2 pl-3 pr-4 text-base font-medium text-gray-500 hover:border-l-4 hover:border-my-orange hover:bg-yellow-100 hover:text-gray-900 focus:border-l-4 focus:border-my-orange focus:bg-yellow-100 focus:text-gray-900 dark:text-white dark:hover:text-gray-900 dark:focus:text-gray-900"
>
Bookmark
{translate("Bookmark")}
</button>
</div>

<Disclosure>
<Disclosure.Button className="flex w-full justify-between border-t-2 border-gray-300 py-2 pl-3 pr-4 text-base font-medium text-black hover:border-l-4 hover:border-my-orange hover:bg-yellow-100 hover:text-gray-900 focus:border-l-4 focus:border-my-orange focus:bg-yellow-100 focus:text-gray-900 dark:text-gray-400 dark:hover:text-gray-900 dark:focus:text-gray-900">
Advanced View
{translate("Advanced View")}
<ChevronDownIcon className="ml-2 h-5 w-5 group-hover:text-black" />
</Disclosure.Button>
<Disclosure.Panel className="z-50 text-gray-500">
Expand All @@ -370,7 +364,7 @@ const PageHeader = ({
className=" font-medium text-gray-600 dark:text-gray-50"
passive
>
Devnagari
{translate("Devnagari")}
</Switch.Label>
</span>
<Switch
Expand Down Expand Up @@ -410,7 +404,7 @@ const PageHeader = ({
className=" font-medium text-gray-600 dark:text-gray-50"
passive
>
Verse Text
{translate("Verse Text")}
</Switch.Label>
</span>
<Switch
Expand Down Expand Up @@ -450,7 +444,7 @@ const PageHeader = ({
className=" font-medium text-gray-600 dark:text-gray-50"
passive
>
Synonyms
{translate("Synonyms")}
</Switch.Label>
</span>
<Switch
Expand Down Expand Up @@ -489,7 +483,7 @@ const PageHeader = ({
className=" font-medium text-gray-600 dark:text-gray-50"
passive
>
Translation
{translate("Translation")}
</Switch.Label>
</span>
<Switch
Expand Down Expand Up @@ -528,7 +522,7 @@ const PageHeader = ({
className=" font-medium text-gray-600 dark:text-gray-50"
passive
>
Purport
{translate("Purport")}
</Switch.Label>
</span>
<Switch
Expand Down
5 changes: 4 additions & 1 deletion src/components/Headers/hi.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"Home": "होम पेज"
"Home": "होम पेज",
"Content": "सामग्री",
"Appearance": "उपस्थिति",
"Search": "खोज"
}
1 change: 1 addition & 0 deletions src/components/Shared/Author.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"use client";
import { Fragment, useEffect, useState } from "react";
import { useCookies } from "react-cookie";
import { Dialog, Listbox, Switch, Transition } from "@headlessui/react";
Expand Down
32 changes: 32 additions & 0 deletions src/hooks/useClickOutside.ts
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);
};
});
};

0 comments on commit c5b4215

Please sign in to comment.