huntabyte / shadcn-svelte

shadcn/ui, but for Svelte. ✨
https://next.shadcn-svelte.com
MIT License
5.53k stars 341 forks source link

Input element sometimes won't get focus (with Superforms) #1014

Closed ciscoheat closed 1 month ago

ciscoheat commented 7 months ago

Describe the bug

As you certainly know, Superforms has an autoFocusOnError option, enabled as default, focusing on an error field when the form is submitted. I noticed that the Input component doesn't always get focus.

I've been debugging Superforms, changing the wait time before focusing from the current await tick() to setTimeout with 100 ms delay, but nothing seems to work, so I don't think it's a Superforms issue.

Reproduction

https://github.com/ciscoheat/shadcn-svelte-not-focusing

Instructions are included there.

Logs

No response

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (16) x64 Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
    Memory: 44.72 GB / 63.93 GB
  Binaries:
    Node: 21.4.0 - C:\Program Files\nodejs\node.EXE       
    Yarn: 1.22.21 - ~\AppData\Local\pnpm\yarn.CMD
    npm: 9.3.1 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.15.5 - ~\AppData\Local\pnpm\pnpm.CMD
    bun: 1.0.32 - ~\.bun\bin\bun.EXE
  Browsers:
    Edge: Chromium (123.0.2420.81)
    Internet Explorer: 11.0.19041.3636
  npmPackages:
    @sveltejs/kit: ^2.0.0 => 2.5.5
    svelte: 5.0.0-next.100 => 5.0.0-next.100
    sveltekit-superforms: ^2.12.4 => 2.12.4

Severity

annoyance

ciscoheat commented 7 months ago

Current workaround:

$effect(() => {
    if ($errors.amount) {
        setTimeout(() => document.querySelector('input[name="amount"]')?.focus());
    }
});
huntabyte commented 7 months ago

Can you reproduce this with Svelte 4? I'm not able to. Or does the same thing happen with a regular (non-component) input?

You're on the money about usually using a tick() or otherwise, but it seems like in this case waiting for the state to update via $effect() then focusing would work but it shouldn't have to be like that.

ciscoheat commented 7 months ago

True, I cannot reproduce it on Svelte 4 either. But it works with a regular input even on Svelte 5.

I could post an issue at the Svelte repo, but maybe a dependency on both Superforms and shadcn-svelte won't be appreciated. :)

huntabyte commented 7 months ago

We could just recreate an <Input /> component and use it with only Superforms to isolate the issue further before bringing it to their attention!

ciscoheat commented 7 months ago

I think I managed to reproduce it without any of the libs, will post an issue at Svelte.

ciscoheat commented 7 months ago

Nope, it was harder than I thought.

huntabyte commented 7 months ago

So you can't reproduce by creating an <Input /> component and using it in place of the one for shadcn-svelte? All it does is wrap a regular input.

huntabyte commented 1 month ago

Gonna close this as I haven't been able to reproduce and from my understanding it shouldn't be an issue with this lib as it's just rendering an HTML input :)