Skip to content

Commit

Permalink
Merge pull request #1945 from tijlleenders/vin/1936/add-keyboard-supp…
Browse files Browse the repository at this point in the history
…ort-intro-screens

add keyboard support intro screens
  • Loading branch information
Tushar-4781 authored Jul 21, 2024
2 parents b8a987a + 065fff2 commit 62b67aa
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 25 deletions.
51 changes: 34 additions & 17 deletions src/components/LanguageChoice/LanguagesList.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import React from "react";
import React, { useEffect, useRef } from "react";
import i18n from "i18next";

import { useRecoilState } from "recoil";
import { useSetRecoilState } from "recoil";
import { ILanguageListProps, ILanguage } from "@src/Interfaces/ILanguage";
import { vibrateWorks } from "@src/constants/vibrateCheck";
import { languageSelectionState } from "@src/store";
import { useLanguageSelection } from "../../hooks/useLanguageSelection";

export const LanguagesList = (props: ILanguageListProps) => {
const { languages, navigationCallback, type, hideSelected } = props;
const [, setIsLanguageChosen] = useRecoilState(languageSelectionState);
const sortedLanguages = [languages[0], ...languages.slice(1).sort((a, b) => a.title.localeCompare(b.title))];
export const LanguagesList = ({ languages, navigationCallback, type, hideSelected }: ILanguageListProps) => {
const setIsLanguageChosen = useSetRecoilState(languageSelectionState);
const labelRefs = useRef<HTMLLabelElement[]>([]);

const handleClick = (langId: string) => {
const handleLanguageSelect = (langId: string) => {
if (vibrateWorks) {
navigator.vibrate(100);
}
Expand All @@ -21,20 +20,38 @@ export const LanguagesList = (props: ILanguageListProps) => {
if (type === "fragment" && navigationCallback) navigationCallback("/ZinZenFAQ");
else window.history.back();
};

const handleClick = (event: React.MouseEvent, langId: string) => {
event.preventDefault();
handleLanguageSelect(langId);
};

const focusedIndex = useLanguageSelection(languages, handleLanguageSelect);

useEffect(() => {
labelRefs.current[focusedIndex]?.focus();
}, [focusedIndex]);

return (
<div className="containerLang">
{sortedLanguages.map((lang: ILanguage) => (
<button
key={String(lang.sno)}
type="button"
className={lang.selected && !hideSelected ? "selected" : ""}
onClick={() => {
handleClick(lang.langId);
{languages.map((lang: ILanguage, index: number) => (
<label
key={lang.sno}
ref={(ref) => {
labelRefs.current[index] = ref!;
}}
htmlFor={lang.sno.toString()}
className={`${lang.selected && !hideSelected ? "selected" : ""} ${focusedIndex === index ? "focused" : ""}`}
>
{lang.title}
<input type="radio" checked={lang.selected} readOnly />
</button>
<input
type="radio"
onMouseDown={(e) => handleClick(e, lang.langId)}
checked={lang.selected}
readOnly
id={lang.sno.toString()}
/>
</label>
))}
</div>
);
Expand Down
26 changes: 26 additions & 0 deletions src/hooks/useKeyPress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useEffect, useState, useCallback } from "react";

export const useKeyPress = (targetKey: string) => {
const [keyPressed, setKeyPressed] = useState(false);

const keyHandler = useCallback(
(event: KeyboardEvent) => {
if (event.key === targetKey) {
setKeyPressed(event.type === "keydown");
}
},
[targetKey],
);

useEffect(() => {
window.addEventListener("keydown", keyHandler);
window.addEventListener("keyup", keyHandler);

return () => {
window.removeEventListener("keydown", keyHandler);
window.removeEventListener("keyup", keyHandler);
};
}, [keyHandler]);

return keyPressed;
};
32 changes: 32 additions & 0 deletions src/hooks/useLanguageSelection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ILanguage } from "@src/Interfaces";
import { useEffect, useState } from "react";
import { useKeyPress } from "./useKeyPress";

export const useLanguageSelection = (languages: ILanguage[], handleClick: (langId: string) => void): number => {
const [focusedIndex, setFocusedIndex] = useState<number>(0);

const upPress = useKeyPress("ArrowUp");
const downPress = useKeyPress("ArrowDown");
const enterPress = useKeyPress("Enter");

useEffect(() => {
if (downPress) {
setFocusedIndex((prevIndex) => (prevIndex + 1) % languages.length);
}
}, [downPress]);

useEffect(() => {
if (upPress) {
setFocusedIndex((prevIndex) => (prevIndex - 1 + languages.length) % languages.length);
}
}, [upPress]);

useEffect(() => {
if (enterPress) {
const { langId } = languages[focusedIndex];
handleClick(langId);
}
}, [enterPress]);

return focusedIndex;
};
23 changes: 16 additions & 7 deletions src/pages/FAQPage/FAQPage.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* eslint-disable no-unused-expressions */
import React from "react";
import React, { useEffect } from "react";
import { useNavigate } from "react-router-dom";
import { useTranslation } from "react-i18next";

import { useRecoilValue } from "recoil";
import { darkModeState } from "@src/store";
import chevronLeftIcon from "@assets/images/chevronLeft.svg";

import ZAccordion from "@src/common/Accordion";
import { vibrateWorks } from "@src/constants/vibrateCheck";
import OnboardingLayout from "@src/layouts/OnboardingLayout";
import { useRecoilValue } from "recoil";
import { darkModeState } from "@src/store";
import { vibrateWorks } from "@src/constants/vibrateCheck";
import { useKeyPress } from "@src/hooks/useKeyPress";

import "./FAQPage.scss";
import "@translations/i18n";

Expand All @@ -24,6 +24,8 @@ export const FAQPage = () => {
{ header: t("qTooGoodToBeTrue"), body: t("ansTooGoodToBeTrue") },
];

const enterPressed = useKeyPress("Enter");

const handleClick = () => {
localStorage.setItem("checkedIn", "yes");
const invite = localStorage.getItem("pendingInvite");
Expand All @@ -36,6 +38,12 @@ export const FAQPage = () => {
}
};

useEffect(() => {
if (enterPressed) {
handleClick();
}
}, [enterPressed]);

return (
<OnboardingLayout>
<p className={`faqpage-about${darkModeStatus ? "-dark" : ""}`}>
Expand All @@ -46,7 +54,8 @@ export const FAQPage = () => {
defaultActiveKey={["1"]}
showCount={false}
style={{ background: "var(--bottom-nav-color)" }}
panels={QnA.map((ele) => ({
panels={QnA.map((ele, index) => ({
key: index.toString(),
header: ele.header,
body: (
<p className="faq-question-content">
Expand Down
8 changes: 7 additions & 1 deletion src/pages/LandingPage/LandingPage.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
border-radius: 0.5rem;
box-shadow: 0px 5px 6px rgba(0, 0, 0, 0.25);

button {
label {
width: 100%;
display: flex;
justify-content: space-between;
Expand All @@ -27,8 +27,14 @@
border-bottom: 1px solid var(--default-border-color);
}

.focused {
border: var(--icon-grad-2) 2px solid;
border-radius: 4px;
}

input[type="radio"] {
accent-color: #cd6e51 !important;
outline: none;
}

.selected {
Expand Down

0 comments on commit 62b67aa

Please sign in to comment.