Step-by-step instructions on setting up a feature where clicking on a vacant area will automatically hide the block

Is there a way to create a functionality where clicking on an empty space will close a specific block? Here is the link to view the code on codesandbox

<template>
  <div>
    <div class="wrapper"></div>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  props: {
    msg: String,
  },
};
</script>

<style scoped>
.wrapper {
  width: 300px;
  height: 150px;
  background: green;
  margin: auto;
}
</style>

Answer №1

One effective approach may be utilizing an event bus to facilitate communication between the window click event listener and the component.

Feel free to experiment with this codesandbox

Answer №2

Here is one way to approach the task. Utilizing a backdrop that reacts when clicked upon. Does this align with your expectations?

MainComponent.vue

<template>
  <div id="app" class="backdrop" @click.self="showBox = !showBox">
    <img alt="Vue logo" src="./assets/logo.png" width="25%" />
    <HelloWorld :showBox="showBox" msg="Hello Vue in CodeSandbox!" />
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld";

export default {
  name: "MainComponent",
  components: {
    HelloWorld,
  },
  data: function () {
    return {
      showBox: true,
    };
  },
};
</script>

<style>
#app {
  text-align: center;
  margin-top: 40px;
}
.backdrop {
  z-index: 0;
  position: fixed;
  width: 100%;
  height: 100%;
}
</style>

HelloWorldComponent.vue

<template>
  <div>
    <div v-if="showBox" class="wrapper"></div>
  </div>
</template>

<script>
export default {
  name: "HelloWorldComponent",
  props: {
    msg: String,
    showBox: Boolean,
  },
};
</script>

<style scoped>
.wrapper {
  width: 300px;
  height: 150px;
  background: green;
  margin: auto;
}
</style>

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

The issue arises when the view fails to load while simulating a backend

Trying to test a specific element of the user interface requires a particular request to the backend with predefined data while allowing all other requests to pass through. Here's a more readable version in coffee script: describe 'select2 combo ...

"Facing a dilemma with Javascript's Facebox getElementById function - not fetching any

Utilizing facebox, I am initiating a small modal dialog with a form and attempting to retrieve the value from a text field on that form using javascript. Below is the HTML code: <div id="dialog-form95" style="display:none"> <div class="block"> ...

The issue with angular JavaScript in the child component is causing the left side navigation dropdown to malfunction

I'm currently facing an issue with the left side navigation in my home component. The dropdown functionality is not working within one of the routing modules (admin-routing.module.ts). Interestingly, the navigation works perfectly fine in app-routing. ...

Encountering Vue js router issues post login validation

I am a beginner with vue and I recently created a small vue app on codesandbox: https://codesandbox.io/s/vue-template-fjd4i However, I am facing issues with routing. Here is how to reproduce the problem: Click on Login and enter any credentials You sho ...

unable to retrieve element values using class names

I have created multiple h1 elements with the same class names listed below. <h1 class="h1">One</h1> <h1 class="h1">Two</h1> <h1 class="h1">Three</h1> <h1 class="h1">Four</h1> I have also added a button that ...

Is there a way to incorporate a base64 encoded image from a text file into an HTML image?

I have several images stored as base64 blobs in a remote location. Let's focus on one specific image: llorkcir.txt data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxISEhASEBAQDxAQDw8QEA8PEA8PDw0PFRIWFhURFRUYHSggGBolGxUVITEhJSkrLi4uFx8zODMt ...

ES6 arrow function fails to recognize the 'this' operator after being transpiled into JavaScript

Currently, I am developing a node application that undergoes transpilation from TypeScript ES6 to JavaScript ES6. To manage class dependencies, I am employing inversify for dependency injection. However, I encountered an error while trying to access member ...

How to Eliminate the Border on the Final Item within a React List

Hi everyone, I need help with removing the border of the last item in an unordered list when the list reaches a certain size. I initially tried this: document.querySelector('.employee-list-item:last-child').style.border = "none"; However, I enc ...

javascript passing a window object as an argument to a function

In Slider function, I am passing a window object that does not contain the body element. Additionally, my code only functions correctly on mobile screens. While debugging the code below: console.log(windows.document); If (!mySlider) {console.log(windows. ...

PouchDB Live proves to be too swift for real-time chatting

While using the current vuex mutation to sync with my pouch/ couchdb, I've encountered an issue. As I type into a text box quickly, sometimes some letters are not synced, which can be annoying but not a dealbreaker. However, when I edit in the middle ...

What is the best way to showcase this code using HTML?

Looking to create a real-time user counter for my Meteor app using Javascript. Currently, I am using the following code snippet to track the number of users on the site: Presences.find({online: true}).count(); However, I am unsure how to display this inf ...

What is the best way to add a dynamic parameter to the URL?

Here is an example of my URL: https://my-website.com/api/players?countryId=1&clubId=2&playerName=abc The parameter values can vary. This is the code snippet I use: getDataPlayer(payload) { let params if(payload.countryId && payl ...

"Vue component inputs fail to update when used in the same instance

I have reused the same component in both the navigation menu and on the people's page. My problem is that when the SearchComponent inputs in the navigation update their values, the SearchComponent inputs on the people's page do not update, and v ...

Is there a race condition issue with jQuery Ajax?

Here is the JavaScript code I'm using to iterate through a list of textfields on a webpage. It sends the text within each textfield as a price to the server via an AJAX GET request, and then receives the parsed double value back from the server. If an ...

Ensuring the script waits for the complete loading of iframe prior to

I'm faced with an issue on the website I'm currently working on. There's a Live Chat plugin integrated on an iframe, and I need to change an image if no agents are available. Interestingly, my code works perfectly fine when tested on the con ...

Extract the text enclosed by two specific symbols within a string and add it to a new array

I have a string formatted as follows: var str = "-#A This text belongs to A. Dummy Text of A. -#B This text belongs to B. Dummy Text of B. -#C This text belongs to C. Dummy text of C. -#Garbage This string should be ignored" I am looking to convert this ...

The string in the text remains unchanged

Currently, I am attempting to replace specific strings as text is entered into the input field. The strings in question are {b} and {\b}. However, I have noticed that not all instances of these strings are being replaced, leaving some behind. Below is ...

Sending Location Data From JavaScript to PHP via Cookies

I encountered an issue while attempting to send Geolocation coordinates from JavaScript to PHP using Cookies. The error message I am receiving is: Notice: Undefined index: data in /Applications/XAMPP/xamppfiles/htdocs/samepage.php on line 24 The file name ...

Connecting to deeply nested attributes within an object using specified criteria

I apologize if the title of my query is not very descriptive, I couldn't come up with a better one. Please feel free to suggest improvements. I am currently working on developing a reusable "property grid" in Angular. My goal is to create a grid wher ...

Issue with React Native, incapable of modifying value

Having trouble updating the title value in my code. It's not updating as expected. Can someone kindly assist in figuring out what's causing the issue? class EditScreen extends Component { render() { return ( <KeyboardAvoidingView ...