Guide on how to trigger the opening of a side panel with a button click in Vue.js

Embarking on my first Vue app development journey, I find myself in need of guidance on how to trigger the opening of a panel by clicking a button within the header.

Starting off with a simple HTML template, my goal is to add some interactivity upon clicking a specific button located in the header section.

Unfortunately, the console throws an error message when the button is clicked:

vue.js:597 [Vue warn]: Invalid handler for event "click": got undefined

(found in )

The snippet of code I've implemented looks like this:

HTML:

<!-- Doctype HTML5 -->
<!DOCTYPE html>
<html lang="en" />

<head>

  <link rel="stylesheet" href="css/normalize.css">

  {{ $style := resources.Get "scss/main.scss" | toCSS | minify | fingerprint }}
  <link rel="stylesheet" href="{{ $style.Permalink }}">

  <script src="js/vue.js"></script>

  <script type="text/javascript" src="js/vue-proj-info.js"></script>

</head>

<body>
      <header>

      <div class="h-branding">
        <div id="h-nLogo">
          <img src="img/med-logo-rev.png" height="70" />
        </div>
        <div id="h-title">
          <h1>Executive Blueprint</h1>
          <p class="subheader"><span class="tr-tier">Tier 1</span> - <span class="tr-service">Executive</span></p>
        </div>
      </div>

      <div id="h-toggles">
        <div class="buttonGroup">
          <button type="" name="tier1">Tier 1</button>
          <button type="" name="tier2">Tier 2</button>
          <button type="" name="tier3">Tier 3</button>
        </div>
        <div class="buttonGroup">
          <button type="" name="executive">Executive</button>
          <button type="" name="concierge">Concierge</button>
        </div>

          </div>

  <proj-slideout ref="proj-slideout" id="proj-slideout" :class="{ isOpen: isOpen }">></proj-slideout>


      <div id="h-infoButton">
        <div class="buttonGroup">
          <button type="button" name="projInfo" class="proj-slideout-opener"
             @click="open">Project Information</button>
        </div>
      </div>

      </header>

JS:

    Vue.component('proj-slideout', {
  template: '#proj-slideout',
  props: ['show'],
  data: () => ({
    isOpen: false,
    projContent: 'overview' /* overview, jtbd, tiers, files */
  }),
  methods: {
    close() {
      this.isOpen = false;
    },
    open() {
      this.isOpen = true;
      console.log('Open Panel');
    }
  }
});

document.addEventListener("DOMContentLoaded", function(event) {
  var app = new Vue({
    el: 'header'
  })
});

SCSS:

#proj-slideout {
  position: fixed;
  top: 0;
  right: 0;
  width: 90vw;
  height: 100vh;
  padding: 30px;
  display: flex;
  flex-direction: row;
  background-color: white;
  transform: translateX(-100%);
  transition: transform 0.6s ease(out-cubic);
  display: none;

  &.isOpen {
    transform: translateX(0);
  }

If you have any insights or suggestions regarding this issue, please share!

Answer №1

When using @click="open" in your code, it refers to the Vue parent component scope. Therefore, you need to define both isOpen and open in the parent Vue component.

document.addEventListener("DOMContentLoaded", function(event) {
  var app = new Vue({
    el: 'header',
    data: () => ({
      isOpen: false,
    }),
    methods: {
      close() {
        this.isOpen = false;
      },
      open() {
        this.isOpen = true;
        console.log('Open Panel');
      }
    }
  })
});

Answer №2

To implement this functionality, simply utilize the v-show directive.

Within your component's template, include either the v-if or v-show directive like so :

<projContent v-show="isOpen" ... />

For the trigger button, use the click event to toggle the value of isOpen between false and true:

<button @click="isOpen = !isOpen" ... >Project Information</button>//or even better @click="isOpen ^= 1"

By clicking the button, the visibility of the projContent component will automatically alternate between hidden and shown without the need for additional methods.

Ensure that you remove any instances of display:none from your scss.

If desired, refer to this link for guidance on implementing transition animations: "Vue transitions"

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

Enhance User Experience with a Responsive Website Dropdown Menu

Currently, I am focused on enhancing the responsiveness of my website and I realized that having a well-designed menu for mobile view is essential. To address this need, I added a button that only appears when the screen size is 480px or lower, which seems ...

Exploring the power of Vue by processing components on a local

I have come across this code: <my-messages> <message>Hello</message> <message>World</message> </my-messages> Currently, my <my-messages> component is rendered as: <div class="Messages"> <!-- ...

What is the best way to access an excel file using JavaScript and Protractor?

Is it possible to read an Excel file dynamically in JavaScript and iterate through cell values using row and column counts, without converting it into a JSON object? I have searched through several posts on Stack Overflow but have not found a solution yet. ...

Managing OAuth2 redirections on the frontend: Best practices

I am currently working on implementing an OAuth2 flow for a Single Page Webapp, but I am facing challenges in dealing with Frontend/JavaScript redirects. Regarding the backend setup, I have it all sorted out: utilizing a library that takes care of everyth ...

Class does not have the capability to deserialize an array

I encountered this issue with my code (see image): Here is the snippet of my code: function CheckLoginData() { var user = []; user.Email = $("#tbEmail").val(); user.Password = $("#tbPassword").val(); $.ajax({ type: "POST", contentType: "applic ...

How can I make a drop-down field in AngularJS show the current value after populating options in a select control using ng-options?

This conversation centers around an app that can be visualized as, https://i.stack.imgur.com/Y1Nvv.png When a user clicks on one of the stories on the left side, the corresponding content is displayed on the right-hand side. Each story includes a title ...

Tips for transferring localstorage values from the view to the controller in order to utilize them as a PHP variable in a separate view

How can I pass the value from local storage as a PHP variable when using location.href in the controller after clicking a button? In the view: echo Html::button('Proceed', [ 'onclick' => " var dataVal = localStorage.g ...

What is the process for transmitting data in JSON format generated by Python to JavaScript?

Utilizing Python libraries cherrypy and Jinja, my web pages are being served by two Python files: Main.py (responsible for handling web pages) and search.py (containing server-side functions). I have implemented a dynamic dropdown list using JavaScript w ...

The command 'run-s' is not valid and cannot be found as an internal or external command. Please make sure it is a recognized program or batch file

Unexpectedly, I encountered an issue while attempting to utilize the npm link command to test my local package. Any ideas on how to resolve this? Operating System: Windows 10 Node version: 15.9.0 NPM version: 8.12.2 ...

Sending data using jQuery to a web API

One thing on my mind: 1. Is it necessary for the names to match when transmitting data from client to my webapi controller? In case my model is structured like this: public class Donation { public string DonorType { get; set; } //etc } But the f ...

Troubleshooting Vue CLI installation

Currently in the process of learning Vue.JS and I've encountered an issue while attempting to install Vue CLI. My current versions are: NodeJS - v13.8.0 Vue CLI - v4.2.2 I had no trouble installing NodeJS, however, when I navigated to my folder in ...

Exploring the power of Vue i18n for prop translations in Vue applications

My goal is to translate the titles passed to my component through props. However, it seems that these strings are not being translated like the rest of my code due to being passed as props. Below are the two components I am currently working with: Parent ...

Encountering problems when transforming Next.js server components into client components

I've been working on a blog site using next.js. Initially, I had a home page that was a server component, but I wanted to convert it into a client component to add interactivity like pagination. However, after converting the code to a client componen ...

What could be the reason for the Checkbox's value not showing up after making changes?

In my React and Material UI project, I am facing an issue where I want to check all checkboxes in a list by simply checking one checkbox in a parent component. Despite passing down the correct value of the parent checkbox through props, the visual changes ...

Pattern to identify a JSON string with Regular Expressions

Currently, I am working on developing a JSON validator from the ground up and have hit a roadblock when it comes to the string component. My original plan was to create a regex pattern that aligns with the sequence specified on JSON.org: Here is the regex ...

displaying pictures exclusively from Amazon S3 bucket using JavaScript

Considering my situation, I have a large number of images stored in various folders within an Amazon S3 bucket. My goal is to create a slideshow for unregistered users without relying on databases or risking server performance issues due to high traffic. I ...

Issues with pop-up windows on YII2 gridview

I am currently developing a web application using the Yii2 framework for my company to efficiently manage their storage. I have incorporated various plugins from kartik-v to enhance the functionality of the application. However, I am facing an issue with i ...

Using Angular to create a dynamic form with looping inputs that reactively responds to user

I need to implement reactive form validation for a form that has dynamic inputs created through looping data: This is what my form builder setup would be like : constructor(private formBuilder: FormBuilder) { this.userForm = this.formBuilder.group({ ...

Under specific circumstances, it is not possible to reset a property in vue.js

In Vue.js, I have developed a 'mini-game' that allows players to 'fight'. After someone 'dies', the game declares the winner and prompts if you want to play again. However, I am facing an issue where resetting the health of bo ...

Unexpected behavior: getElementById returning URL instead of element

I created a function that accepts a thumbnail path as an argument, waits for the bootstrap modal to open, and then assigns the correct path to the thumbnail href attribute within the modal. However, when I use console.log with the element(el), it displays ...