Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Add focus when visible utility (#2413)

Leendert de Borst852180f97052
diff --git a/apps/web/src/pages/auth/Login.tsx b/apps/web/src/pages/auth/Login.tsx
index 565f35300..1a6d0a7e5 100644
--- a/apps/web/src/pages/auth/Login.tsx
+++ b/apps/web/src/pages/auth/Login.tsx
@@ -22,6 +22,7 @@ import { useNotifications } from '@/context/NotificationContext';
import { useWebApi } from '@/context/WebApiContext';
import { usePageTitle } from '@/hooks/usePageTitle';
import { apiErrorMessage } from '@/utils/ApiErrors';
+import { focusWhenVisible } from '@/utils/FocusWhenVisible';
import { StorageKeys } from '@/utils/StorageKeys';
import { vaultStore } from '@/vault/VaultStore';
@@ -75,8 +76,7 @@ const Login: React.FC = () => {
setUsername(current => current || saved);
}
});
- const timer = setTimeout(() => usernameRef.current?.focus(), 300);
- return (): void => clearTimeout(timer);
+ return focusWhenVisible(() => usernameRef.current);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
diff --git a/apps/web/src/pages/auth/Unlock.tsx b/apps/web/src/pages/auth/Unlock.tsx
index 41099f893..0e00ded7a 100644
--- a/apps/web/src/pages/auth/Unlock.tsx
+++ b/apps/web/src/pages/auth/Unlock.tsx
@@ -19,6 +19,7 @@ import { useLoading } from '@/context/LoadingContext';
import { useNotifications } from '@/context/NotificationContext';
import { useWebApi } from '@/context/WebApiContext';
import { usePageTitle } from '@/hooks/usePageTitle';
+import { focusWhenVisible } from '@/utils/FocusWhenVisible';
import { WebAuthnNotSupportedError, WebAuthnService } from '@/utils/WebAuthnService';
import { vaultStore } from '@/vault/VaultStore';
@@ -121,11 +122,18 @@ const Unlock: React.FC = () => {
}, [auth, dbContext, webApi, notifications, navigate, t, skipWebAuthn, unlockWithWebAuthn]);
useEffect(() => {
- if (!isLoading && !isWebAuthnLoading && !showWebAuthnButton) {
- const timer = setTimeout(() => passwordRef.current?.focus(), 100);
- return (): void => clearTimeout(timer);
+ if (!isLoading && !isWebAuthnLoading) {
+ return focusWhenVisible(() => passwordRef.current);
}
- }, [isLoading, isWebAuthnLoading, showWebAuthnButton]);
+ }, [isLoading, isWebAuthnLoading]);
+
+ /**
+ * Hide the passkey option and move focus to the password field.
+ */
+ const showPasswordUnlock = (): void => {
+ setShowWebAuthnButton(false);
+ passwordRef.current?.focus();
+ };
/**
* Derive the KEK from the password and open the vault key chain.
@@ -216,7 +224,7 @@ const Unlock: React.FC = () => {
<svg className="w-5 h-5 mr-2 -ml-1" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M17.414 2.586a2 2 0 00-2.828 0L7 10.172V13h2.828l7.586-7.586a2 2 0 000-2.828z"></path><path fillRule="evenodd" d="M2 6a2 2 0 012-2h4a1 1 0 010 2H4v10h10v-4a1 1 0 112 0v4a2 2 0 01-2 2H4a2 2 0 01-2-2V6z" clipRule="evenodd"></path></svg>
{t('pages.auth.unlock.UnlockWithWebAuthn')}
</button>
- <button type="button" onClick={() => setShowWebAuthnButton(false)} className="inline-flex items-center justify-center px-5 py-2 text-base font-medium text-center text-gray-900 rounded-lg border border-gray-300 hover:bg-gray-100 focus:ring-4 focus:ring-gray-100 dark:text-white dark:border-gray-700 dark:hover:bg-gray-700 dark:focus:ring-gray-800">
+ <button type="button" onClick={showPasswordUnlock} className="inline-flex items-center justify-center px-5 py-2 text-base font-medium text-center text-gray-900 rounded-lg border border-gray-300 hover:bg-gray-100 focus:ring-4 focus:ring-gray-100 dark:text-white dark:border-gray-700 dark:hover:bg-gray-700 dark:focus:ring-gray-800">
{t('pages.auth.unlock.UnlockWithPassword')}
</button>
</div>
diff --git a/apps/web/src/utils/FocusWhenVisible.ts b/apps/web/src/utils/FocusWhenVisible.ts
new file mode 100644
index 000000000..07a97c69a
--- /dev/null
+++ b/apps/web/src/utils/FocusWhenVisible.ts
@@ -0,0 +1,24 @@
+/**
+ * Focus an element, retrying each frame until it takes focus. On a full page load index.html keeps #app hidden
+ * behind the loading screen for at least a second, and a hidden element ignores focus().
+ * Returns a cancel function for use as an effect cleanup.
+ */
+export function focusWhenVisible(getElement: () => HTMLElement | null, timeoutMs = 5000): () => void {
+ const start = performance.now();
+ let frame = 0;
+
+ /**
+ * Try to focus once; schedule another try while the element does not have focus yet.
+ */
+ const attempt = (): void => {
+ const element = getElement();
+ element?.focus();
+ if ((element && document.activeElement === element) || performance.now() - start > timeoutMs) {
+ return;
+ }
+ frame = requestAnimationFrame(attempt);
+ };
+
+ attempt();
+ return (): void => cancelAnimationFrame(frame);
+}