Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
can't set initialFocus on focus-trap in vue component

I am using the focus-trap package for Vue3 to trap focus inside a form, inside of a modal. I'm having trouble with the :setInitialFocus prop, I think because the element I'm trying to reference there is initially hidden from the page. I've tried using this.$nextTick to remedy that, but haven't had any luck.

In my component I have:

   <focus-trap 
       ref="focusTrap" 
       :active="isFocusTrapActive" 
       :initialFocus="() => $refs.modalForm"
    >
       <Modal @close="closeModal"
               :isOpen="openModal"
       >
          <AccountLoginForm :ref="modalForm"/>
     </Modal>
</focus-trap>

Opening the modal (and the focus trap) are handled by:

    launchModal() {
       this.openModal = !this.openModal;
       this.$nextTick(() => {
          this.isFocusTrapActive = !this.isFocusTrapActive;
       });
     },

With this setup, when the modal opens, I get a maximum call stack exceeded error.

I've also tried just assigning the modal component a ref, and using that for the setInitialFocus prop, such as:

<focus-trap 
       ref="focusTrap" 
       :active="isFocusTrapActive" 
       :initialFocus="() => $refs['modalForm']"
    >
       <Modal @close="closeModal"
               :isOpen="openModal"
               ref="modalForm"
       >
          <AccountLoginForm/>
     </Modal>
</focus-trap>

but get a initialFocus did not return a node error.

The only thing I CAN get to work is if I don't set initialFocus -- then the focus is trapped inside the modal when it appears, just not in the form. I'm not sure what's going on. If any one could offer any help that would be much appreciated.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!