Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

@focus event is not bubble by default

Vue version:
3.2.39

While click on the button, the focus does not bubble to the wrapper, and that is why it does not show the "wrapper focus". How to fix this?

<template>
  <div tabindex="-1" @focus="onWrapperFocus" @blur="onWrapperBlur">
    <button @focus="onInnerFocus" @blur="onInnerBlur">Hello</button>
  </div>
</template>

<script >
export default {
  setup() {
    return {
      onWrapperFocus() {
        console.log("wrapper focus");
      },

      onWrapperBlur() {
        console.log("wrapper blur");
      },

      onInnerFocus() {
        console.log("inner focus");
      },

      onInnerBlur() {
        console.log("inner blur");
      },
    };
  },
};
</script>

Expected behavior:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

inner focus 
wrapper focus

>Solution :

There is another phase of event called "capturing". It is rarely used in real code, but can be usefull in this case.

You can add the capturing phase by adding .capture after the event name like shown below:

<div tabindex="-1" @focus.capture="onWrapperFocus" @blur.capture="onWrapperBlur">
  <button @focus="onInnerFocus" @blur="onInnerBlur">Hello</button>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading