From d8b5d3a0d8dcd43ac5594176146571e4eb79486c Mon Sep 17 00:00:00 2001 From: ItzNotABug Date: Wed, 14 Aug 2024 19:46:07 +0530 Subject: [PATCH] fix: mfa digits losing focus. --- src/lib/elements/forms/inputDigits.svelte | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/lib/elements/forms/inputDigits.svelte b/src/lib/elements/forms/inputDigits.svelte index c822d50bb..4e872d121 100644 --- a/src/lib/elements/forms/inputDigits.svelte +++ b/src/lib/elements/forms/inputDigits.svelte @@ -2,6 +2,8 @@ import { onMount } from 'svelte'; import { FormItem } from '.'; import { createPinInput, melt } from '@melt-ui/svelte'; + import { page } from '$app/stores'; + import { sleep } from '$lib/helpers/promises'; export let length: number = 6; export let value: string = ''; @@ -38,7 +40,13 @@ } }); - onMount(() => { + async function focusDigitInputs() { + /** + * this is to re-focus the inputs that feels natural + * as the blinking of the cursor takes around 250ms. + */ + await sleep(250); + const interval = setInterval(() => { const input = element.querySelector('input'); if (element) { @@ -48,6 +56,18 @@ clearInterval(interval); } }, 10); + } + + onMount(() => { + /** + * Inputs will lose focus on any change. + * Changes include the url & query params like `?redirect=`. + * + * This subscription ensures that focus is restored when needed. + */ + return page.subscribe(() => { + focusDigitInputs(); + }); });