GithubHelp home page GithubHelp logo

Comments (5)

RobinMalfait avatar RobinMalfait commented on May 28, 2024

Hey!

The Dialog should already restore focus to the correct element once it closes without you having to do anything special.

The reproduction URL points to our template, can you update your reproduction URL so that we can take a look?

from headlessui.

widavies avatar widavies commented on May 28, 2024

@RobinMalfait

Whoops! Try this link

from headlessui.

reinink avatar reinink commented on May 28, 2024

Hey!

So this is actually intended behavior, as @RobinMalfait suggested. When the dialog is closed and unmounts, Headless UI automatically restores focus to the element that was focused at the point the dialog was opened.

In your particular use-case this focus restoration is happening basically at the exact same time as the code in your afterLeave callback, and Headless UI just happens to be winning out. You can fix this by delaying the focus of your input by one macrotask, which you can do using a setTimeout():

afterLeave={() => {
  setTimeout(() => inputRef.current.focus())
}}

Hope that helps!

from headlessui.

widavies avatar widavies commented on May 28, 2024

@reinink Thanks! I had got it working with setTimeout having a delay specified, but I didn't realize it had a no-args call for one-macrotask as well, that's useful.

One interesting bit - this is working for me in the web browser (chrome), but not in Electron, any ideas?

from headlessui.

widavies avatar widavies commented on May 28, 2024

This seems to be the root issue: electron/electron#28163

from headlessui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.