Skip to content

useFocusTrap | Implement updateContainerElements() #4848

@lbineau

Description

@lbineau

Clear and concise description of the problem

Summary

The focus-trap library provides a useful trap.updateContainerElements() method, allowing consumers to update the container elements of a focus trap without needing to destroy and recreate it. This is especially helpful in dynamic UIs where the trap boundaries may change during the component lifecycle.

Thank you for considering this enhancement!

Use Case

In applications where the content within a modal, drawer, or any focus-trapped container changes (e.g., conditional rendering, dynamic portals), being able to manually update the container elements is essential to maintain correct focus behavior.

Suggested solution

Expose the updateContainerElements method via the useFocusTrap hook so users can call it directly when needed. https://github.com/focus-trap/focus-trap?tab=readme-ov-file#trapupdatecontainerelements

Suggested API

const { activate, deactivate, updateContainerElements } = useFocusTrap(ref, options);

Alternative

Currently, developers must recreate the trap if they need to update the container elements, which is not efficient and can lead to undesirable side effects.

Additional context

Benefits

  • Aligns useFocusTrap closer to the full capabilities of the underlying focus-trap library.
  • Reduces boilerplate and avoids unnecessary unmounting/mounting cycles.
  • Improves accessibility in dynamic content scenarios.

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy