Fuzziness occurrence in distinct vue element

My Situation
I have a custom DropDown with a filter text input above. The DropDown can be opened independently from the filter text input.

My Goal
I want the DropDown to close when the filter input loses focus and also when I click outside of the DropDown.

My Attempts

  • I tried binding to the blur event on my root div element in the control, but it didn't work.
  • I looked for internal component methods to override but couldn't find any.

Code Snippet

<div @blur="onRootLostFocus">
...
</div>

...
...
...

onRootLostFocus() {
console.log('LostFocus');
this.deactivateSearchPanel();
this.deactivateSelectionPanel();
}

The Solution

I realized that a div needs tabindex="0" to be focusable, which solved my problem.

Answer №1

Do something similar to this?

Solution: To ensure it is focusable, you should add tabindex="0".

Check out this customized dropdown example:

Vue.component("dropdown", {
   props: ["value"],
   data(){
      return {
         open: false,
         options: ["BMW", "Fiat", "Citroen", "Audi", "Tesla"]
      }
   },
   methods: {
      toggleDropdown() {
         this.open = !this.open;
      },
      closeDropdown(){
         this.open = false;
      },
      selectOption(option) {
         this.$emit("input", option);
      }
   },
   template: `<div class="dropdown">
   <div @blur="closeDropdown" tabindex="0" ref="dropdown" @click="toggleDropdown" class="select">
   {{ value }}
   </div>
   <div class="options" :style="{'max-height': open ? '300px' : '0px'}">
      <div v-for="option in options" @mousedown="selectOption(option)" class="option">
         {{ option }}
      </div>
   </div>
</div>`
})

new Vue({
   el: "#app",
   data: {
      selectedCar: "BMW"
   }
})
.dropdown {
   width: 200px;
   position: relative;
}

.select {
   height: 40px;
   position: absolute;
   left: 0;
   width: 100%;
   background: green;
   display: flex;
   justify-content: center;
   align-items: center;
   color: white;
   cursor: pointer;
}
.option {
   width: 100%;
   height: 40px;
   background: darkgreen;
   color: white;
   display: flex;
   justify-content: center;
   align-items: center;
   cursor: pointer;
}
.option:hover {
   background: green;
}
.options {
   overflow: hidden;
   transition: max-height 200ms;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app"> <p>
   {{ selectedCar }}</p>
   <dropdown v-model="selectedCar" />
  
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When a property is designated as readonly in a TypeScript class, it can still be modified despite its intended

I'm currently grappling with the concept of the readonly keyword in TypeScript. As far as I understand, a readonly property should not be able to be written to after the constructor is called. However, in my testing, I've found that I can actuall ...

Encountering [object, Object] while attempting to display JSON object retrieved from req.body in the console

While attempting to insert a product's details into my API's PostgreSQL database using Postman, I encountered an issue where the values of the specs key were displayed as [object Object] instead of showing the complete data. As a result, even in ...

Exploring alternative methods for accessing object values in TypeScript using a string object path without relying on the eval function

If we have an object in JS/typescript structured like this: var obj = { a: { b:{ c:1 } } } And a string "b.c" is given, how can we evaluate this using only the variables provided and retrieve the value 1 from the object without rel ...

Angular JS - The controller function does not return after executing the service script

The login process for my app is functioning correctly. It successfully connects to the server and returns a 200 Status, indicating that it is working fine. However, after the service completes its task, the controller does not execute its lines of code: C ...

Utilizing custom form fields with JavaScript in Symfony2

Here is my form field template: {% block test_question_widget %} {% spaceless %} <div {{ block('widget_container_attributes') }}> {% set type = type|default('hidden') %} <input type="{{ typ ...

Steps to delete vue-snotify notifications after unintentionally clicking the back button on the browser

When you open a persistent notification like a snotify confirm notification or others that do not close automatically, and then accidentally click the browser's back button, the notification remains open. Are there any proposed solutions for this iss ...

Modify the website address and show the dynamic content using AJAX

$(function(){ $("a[rel='tab']").click(function(e){ //capture the URL of the link clicked pageurl = $(this).attr('href'); $("#Content").fadeOut(800); setTimeout(function(){ $.ajax({url:pageurl+'?rel=tab&apo ...

What could be the reason for a "Delay" in the state update process within Redux?

I'm currently facing some issues with the Redux and React Native code provided below. The goal is to build a workout tracking application where users can input their progress. I've implemented a 'workoutSessionSlice' to manage the stat ...

Tips for assigning date ranges to arrays and then conducting comparisons with date input information

I'm developing an application with two date input fields. The current function I have calculates the sum of the digits in each field until it reaches a single digit, and then displays the result along with some text. Now, I need to create another fun ...

A technique to execute multiple Ajax calls simultaneously without having to wait for each one to finish in an onClick event

<html> <head></head> <body> <button onclick="ajaxcall()">Run the function</button> </body> <script> function ajaxcall() { $.ajax({ url: 'insertdata.php', ...

Utilizing Firefox and Selenium with Python: A guide to dynamically accessing HTML elements

Currently, I am utilizing a combination of Python, Selenium, Splinter, and Firefox to develop an interactive web crawler. The Python script provides the options, then Selenium launches Firefox and executes certain commands. At this point, I am looking fo ...

Enhance your security with Ember-simple-auth when using ember-cli-simple-auth-token

Currently, I am in the process of utilizing ember-simple-auth alongside ember-cli-simple-auth-token: "ember-cli-simple-auth-token": "^0.7.3", "ember-simple-auth": "1.0.1" Below are my configurations: ENV['simple-auth-token'] = { authoriz ...

How can I include additional lines in a 3D model?

https://i.sstatic.net/NH1sB.png In my latest project using three.js, I created a 3D human model and now I want to enhance it by adding angle lines similar to the red ones I manually drew on the image. Does anyone know how to achieve this effect? ...

Executing php class method through ajax with jQuery without requiring a php handler file

How can I trigger a PHP class method using AJAX with jQuery without the need for a separate PHP handler file? Here is my PHP Class animal.php:- <?php class animal { function getName() { return "lion"; } } ?> jQuery code snippet:- ...

Steps for creating a file selection feature in Chonky.js

I recently integrated the Chonky library into my application to build a file explorer feature. One challenge I am facing is figuring out how to select a file and retrieve its unique ID when the user double clicks on it. const [files, setFiles] ...

ReactJS Redux Provider fails to accept the store

I'm currently in the process of migrating my redux store from using "createStore" to "configureStore" due to the deprecation of "createStore". I am working with ReactJS 17 and TypeScript, with the following versions of Redux / Redux dependencies: &quo ...

Troubleshooting my HTML5 local storage issues for optimal functionality

I've been working on using HTML5's localstorage to save two variables and load them upon page refresh, but I seem to be encountering some issues when trying to load the saved items: Variables in question: var cookies = 0; var cursors = 0; Savi ...

Scroll to the bottom of a textarea in React by automatically shifting focus

I am having issues with appending text to a textarea and then scrolling to the bottom in order to keep the latest content within view. However, it seems that this process is causing the browser to crash or run out of memory. Can someone provide assistance ...

Issue with Vue.js Checkbox functionality in Firefox, functioning smoothly in Chrome and IE

I have implemented a checkbox feature in my project. <template v-for="(item,index) in items"> <div> <input type="checkbox" v-model="item.checked" @click="selectionCheckboxClicked(index,item.checked)" ...

Using Partial function input in TypeScript

I am in need of a function that can accept partial input. The function includes a variable called style, which should only have values of outline or fill, like so: export type TrafficSignalStyle = 'outline' | 'fill' let style: TrafficSi ...