From e40452dd3f5d2a552b33e1976843eeed4863799a Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Sun, 25 Feb 2024 17:59:58 +0100 Subject: [PATCH] fix: input --- digit-autofocus.patch | 35 +++++++++++++++++++++++ src/lib/elements/forms/inputDigits.svelte | 32 ++++++--------------- src/routes/+layout.svelte | 3 +- 3 files changed, 46 insertions(+), 24 deletions(-) create mode 100644 digit-autofocus.patch diff --git a/digit-autofocus.patch b/digit-autofocus.patch new file mode 100644 index 000000000..8aef6b1fd --- /dev/null +++ b/digit-autofocus.patch @@ -0,0 +1,35 @@ +diff --git a/src/lib/elements/forms/inputDigits.svelte b/src/lib/elements/forms/inputDigits.svelte +index dee662d63..7d54930f5 100644 +--- a/src/lib/elements/forms/inputDigits.svelte ++++ b/src/lib/elements/forms/inputDigits.svelte +@@ -25,9 +25,15 @@ + }); + + onMount(() => { +- if (element && autofocus) { +- element.querySelector('input')?.focus(); +- } ++ const interval = setInterval(() => { ++ const input = element.querySelector('input'); ++ if (element) { ++ if (input && autofocus) { ++ input.focus(); ++ } ++ clearInterval(interval); ++ } ++ }, 10); + }); + + +diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte +index 99cc475ff..c8c0239c7 100644 +--- a/src/routes/+layout.svelte ++++ b/src/routes/+layout.svelte +@@ -54,6 +54,7 @@ + '/recover', + '/invite', + '/card', ++ '/mfa', + '/hackathon' + ]; + if ($user) { diff --git a/src/lib/elements/forms/inputDigits.svelte b/src/lib/elements/forms/inputDigits.svelte index f688aff17..e15828197 100644 --- a/src/lib/elements/forms/inputDigits.svelte +++ b/src/lib/elements/forms/inputDigits.svelte @@ -33,30 +33,16 @@ } }); - /** - * TODO: Find proper solution. - * This spams focus for half a second. - * Autofocus works properly if +layout.ts is ssr = false. Seems like Svelte Kit bug. - * Using document.hasFocus doesnt help here - */ - async function focusInput(i = 0) { - if (i >= 10) { - return; - } - - if (element && autofocus) { - element.querySelector('input')?.focus(); - } - - await tick(); - - setTimeout(() => { - focusInput(i + 1); - }, 50); - } - onMount(() => { - focusInput(); + const interval = setInterval(() => { + const input = element.querySelector('input'); + if (element) { + if (input && autofocus) { + input.focus(); + } + clearInterval(interval); + } + }, 10); }); diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 99cc475ff..8c8e27929 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -54,7 +54,8 @@ '/recover', '/invite', '/card', - '/hackathon' + '/hackathon', + '/mfa' ]; if ($user) { if (