Skip to content

Fix Restoring Focus in Dialog (#3686) #3687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ export let FocusTrap = Object.assign(

useRestoreFocus(
{ ownerDocument },
computed(() => mounted.value && Boolean(props.features & Features.RestoreFocus))
computed(() => Boolean(props.features & Features.RestoreFocus)),
computed(() => mounted.value)
)
let previousActiveElement = useInitialFocus(
{ ownerDocument, container, initialFocus: computed(() => props.initialFocus) },
Expand Down Expand Up @@ -239,15 +240,16 @@ function useRestoreElement(enabled: Ref<boolean>) {

function useRestoreFocus(
{ ownerDocument }: { ownerDocument: Ref<Document | null> },
enabled: Ref<boolean>
enabled: Ref<boolean>,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats happening here is that, if enabled is also calculated from mounted.value, it:

  1. it hits onUnmounted(() => (mounted.value = false))
  2. that caused recalculating of the computed(() => mounted.value && Boolean(props.features & Features.RestoreFocus))
  3. As result inOnmounted the enabled.value was already false and restoration is skipped

mounted: Ref<boolean>
) {
let getRestoreElement = useRestoreElement(enabled)

// Restore the focus to the previous element
onMounted(() => {
watchEffect(
() => {
if (enabled.value) return
if (enabled.value && mounted.value) return

if (ownerDocument.value?.activeElement === ownerDocument.value?.body) {
focusElement(getRestoreElement())
Expand Down