Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

remove(accounts):removed-react-otp-input

Aswin Asoka982123d3c5a
diff --git a/web/package-lock.json b/web/package-lock.json
index 7c8b84618f..efb6e84e8f 100644
--- a/web/package-lock.json
+++ b/web/package-lock.json
@@ -9056,16 +9056,6 @@
"react": ">=18"
}
},
- "node_modules/react-otp-input": {
- "version": "3.1.1",
- "resolved": "https://registry.npmjs.org/react-otp-input/-/react-otp-input-3.1.1.tgz",
- "integrity": "sha512-bjPavgJ0/Zmf/AYi4onj8FbH93IjeD+e8pWwxIJreDEWsU1ILR5fs8jEJmMGWSBe/yyvPP6X/W6Mk9UkOCkTPw==",
- "license": "MIT",
- "peerDependencies": {
- "react": ">=16.8.6 || ^17.0.0 || ^18.0.0",
- "react-dom": ">=16.8.6 || ^17.0.0 || ^18.0.0"
- }
- },
"node_modules/react-refresh": {
"version": "0.18.0",
"resolved": "https://registry.npmjs.org/react-refresh/-/react-refresh-0.18.0.tgz",
@@ -11161,7 +11151,6 @@
"ente-core-wasm": "*",
"react": "19.2.7",
"react-dom": "19.2.7",
- "react-otp-input": "3.1.1",
"uuid": "13.0.2",
"zxcvbn": "4.4.2"
},
diff --git a/web/packages/accounts/components/Verify2FACodeForm.tsx b/web/packages/accounts/components/Verify2FACodeForm.tsx
index 60800c5997..b361cae61c 100644
--- a/web/packages/accounts/components/Verify2FACodeForm.tsx
+++ b/web/packages/accounts/components/Verify2FACodeForm.tsx
@@ -4,8 +4,165 @@ import { isHTTP401Error } from "ente-base/http";
import log from "ente-base/log";
import { useFormik } from "formik";
import { t } from "i18next";
-import React, { useEffect, useState } from "react";
-import OtpInput from "react-otp-input";
+import React, { useEffect, useRef, useState } from "react";
+
+const otpLength = 6;
+
+interface OTPInputProps {
+ onChange: (value: string) => void;
+ shouldAutoFocus: boolean;
+ value: string;
+}
+
+const OTPInput: React.FC<OTPInputProps> = ({
+ onChange,
+ shouldAutoFocus,
+ value,
+}) => {
+ const inputRefs = useRef<(HTMLInputElement | null)[]>([]);
+ const pendingFocusIndex = useRef<number | undefined>(undefined);
+
+ useEffect(() => {
+ if (shouldAutoFocus) inputRefs.current[0]?.focus();
+ }, [shouldAutoFocus]);
+
+ const focusInput = (index: number) =>
+ inputRefs.current[Math.max(0, Math.min(index, otpLength - 1))]?.focus();
+
+ useEffect(() => {
+ const index = pendingFocusIndex.current;
+ if (index == undefined) return;
+
+ pendingFocusIndex.current = undefined;
+ inputRefs.current[Math.max(0, Math.min(index, otpLength - 1))]?.focus();
+ }, [value]);
+
+ const replaceDigits = (index: number, input: string) => {
+ const digits = input.replace(/\D/g, "");
+ if (!digits) return;
+
+ const startIndex = Math.min(index, value.length);
+ const nextValue =
+ value.slice(0, startIndex) +
+ digits +
+ value.slice(startIndex + digits.length);
+ const clampedValue = nextValue.slice(0, otpLength);
+ const nextFocusIndex = startIndex + digits.length;
+ if (clampedValue == value) {
+ focusInput(nextFocusIndex);
+ } else {
+ pendingFocusIndex.current = nextFocusIndex;
+ onChange(clampedValue);
+ }
+ };
+
+ const removeDigit = (index: number) => {
+ if (index < 0 || index >= value.length) return;
+ pendingFocusIndex.current = index;
+ onChange(value.slice(0, index) + value.slice(index + 1));
+ };
+
+ const handleChange = (
+ index: number,
+ event: React.ChangeEvent<HTMLInputElement>,
+ ) => {
+ const input = event.currentTarget.value;
+ let digits = input.replace(/\D/g, "");
+ if (
+ digits.length === 2 &&
+ value[index] &&
+ digits.startsWith(value[index])
+ ) {
+ digits = digits[1]!;
+ }
+
+ if (digits) {
+ replaceDigits(index, digits);
+ } else if (!input) {
+ removeDigit(index);
+ }
+ };
+
+ const handleKeyDown = (
+ index: number,
+ event: React.KeyboardEvent<HTMLInputElement>,
+ ) => {
+ if (event.metaKey || event.ctrlKey || event.altKey) return;
+
+ if (/^\d$/.test(event.key)) {
+ event.preventDefault();
+ replaceDigits(index, event.key);
+ return;
+ }
+
+ switch (event.key) {
+ case "Backspace":
+ event.preventDefault();
+ if (value[index]) {
+ removeDigit(index);
+ } else {
+ removeDigit(index - 1);
+ }
+ break;
+ case "Delete":
+ event.preventDefault();
+ removeDigit(index);
+ break;
+ case "ArrowLeft":
+ event.preventDefault();
+ focusInput(index - 1);
+ break;
+ case "ArrowRight":
+ event.preventDefault();
+ focusInput(index + 1);
+ break;
+ }
+ };
+
+ return (
+ <Stack
+ direction="row"
+ role="group"
+ aria-label={t("verification_code")}
+ sx={{ alignItems: "center", justifyContent: "center" }}
+ >
+ {Array.from({ length: otpLength }, (_, index) => (
+ <React.Fragment key={index}>
+ {index > 0 && <span>-</span>}
+ <IndividualInput
+ ref={(element) => {
+ inputRefs.current[index] = element;
+ }}
+ aria-label={`${t("verification_code")} ${index + 1}`}
+ autoComplete={index === 0 ? "one-time-code" : "off"}
+ inputMode="numeric"
+ maxLength={otpLength}
+ name={index === 0 ? "otp" : undefined}
+ onChange={(event) => handleChange(index, event)}
+ onFocus={(event) => {
+ if (index > value.length) {
+ focusInput(value.length);
+ } else {
+ event.currentTarget.select();
+ }
+ }}
+ onKeyDown={(event) => handleKeyDown(index, event)}
+ onPaste={(event) => {
+ event.preventDefault();
+ replaceDigits(
+ index,
+ event.clipboardData.getData("text"),
+ );
+ }}
+ pattern="[0-9]*"
+ type="tel"
+ value={value[index] ?? ""}
+ />
+ </React.Fragment>
+ ))}
+ </Stack>
+ );
+};
interface Verify2FACodeFormProps {
onSubmit: (otp: string) => Promise<void>;
@@ -44,9 +201,7 @@ export const Verify2FACodeForm: React.FC<Verify2FACodeFormProps> = ({
? t("incorrect_code")
: t("generic_error"),
);
- // Workaround (toggling shouldAutoFocus) to reset the focus back
- // to the first input field in case of errors.
- // https://github.com/devfolioco/react-otp-input/issues/420
+ // Reset focus back to the first input field in case of errors.
setShouldAutoFocus(false);
setTimeout(() => setShouldAutoFocus(true), 100);
}
@@ -63,22 +218,10 @@ export const Verify2FACodeForm: React.FC<Verify2FACodeFormProps> = ({
<Typography variant="small" sx={{ color: "text.muted" }}>
{t("enter_two_factor_otp")}
</Typography>
- <OtpInput
- containerStyle={{ justifyContent: "center" }}
+ <OTPInput
shouldAutoFocus={shouldAutoFocus}
value={values.otp}
onChange={handleChange("otp")}
- numInputs={6}
- inputType="tel"
- renderSeparator={<span>-</span>}
- renderInput={(props, index) => (
- <IndividualInput
- {...props}
- autoComplete={index === 0 ? "one-time-code" : "off"}
- name={index === 0 ? "otp" : undefined}
- pattern="[0-9]*"
- />
- )}
/>
{errors.otp && (
<Typography variant="mini" sx={{ color: "critical.main" }}>
@@ -101,15 +244,26 @@ export const Verify2FACodeForm: React.FC<Verify2FACodeFormProps> = ({
const IndividualInput = styled("input")(
({ theme }) => `
+ appearance: none;
+ box-sizing: border-box;
+ font-family: inherit;
font-size: 1.5rem;
+ line-height: normal;
+ text-align: center;
+ color: ${theme.vars.palette.text.base};
+ background-color: ${theme.vars.palette.fill.faint};
padding: 4px;
width: 40px !important;
aspect-ratio: 1;
margin-inline: 6px;
border: 1px solid ${theme.vars.palette.accent.main};
border-radius: 1px;
- outline-color: ${theme.vars.palette.accent.light};
- transition: 0.5s;
+ outline: none;
+ transition: border-color 150ms ease, box-shadow 150ms ease;
+ &:focus-visible {
+ border-color: ${theme.vars.palette.accent.light};
+ box-shadow: inset 0 0 0 1px ${theme.vars.palette.accent.light};
+ }
${theme.breakpoints.down("sm")} {
font-size: 1rem;
padding: 4px;
diff --git a/web/packages/accounts/package.json b/web/packages/accounts/package.json
index caff688642..4a03c83a97 100644
--- a/web/packages/accounts/package.json
+++ b/web/packages/accounts/package.json
@@ -8,7 +8,6 @@
"ente-core-wasm": "*",
"react": "19.2.7",
"react-dom": "19.2.7",
- "react-otp-input": "3.1.1",
"uuid": "13.0.2",
"zxcvbn": "4.4.2"
},