Enhancing menu item visibility with Typescript and Vue.js 3: A step-by-step guide

How can I dynamically highlight the active menu item in my menu?
I believe that adding v-if(selected) and a function might be the way to go in the first template.

<template>
  <MenuTreeView @selected='collapsedMenuSelected' :items='filteredFirstLevelNavigation' :filter="false"/>
</template>

A glance at MenuTreeView:

<template>
  <Tree :value="items" filterPlaceholder="Search" :expandedKeys="expandedKeys" filterMode="lenient" :filter="filter"
        selectionMode="single" @nodeSelect="onNodeSelect"></Tree>
</template>

Answer №1

When faced with such scenarios, it becomes necessary to keep track of the id of the currently active element within the data of the parent component. This can be achieved by storing the id when a row is selected.
Parent

<template>
  <div class="menu">
    <MenuItem
      v-for="row in rows"
      :active-id="activeId"
      :key="row.id"
      @select="handleSelect"
    />
  </div>
</template>

<script>
import MenuItem from "./MenuItem";
export default {
  name: "TheMenu",
  components: {
    MenuItem,
  },
  data() {
    return {
      activeId: null,
      rows: [
        {
          id: 1,
          label: "First",
        },
        {
          id: 2,
          label: "Second",
        },
        {
          id: 3,
          label: "Third",
        },
      ],
    };
  },
  methods: {
    handleSelect(id) {
      this.activeId = id;
    },
  },
};
</script>

Child

<template>
  <div
    class="menu-item"
    :class="{
      'menu-item_active': activeId === row.id,
    }"
    @click="handleSelect"
  >
    {{ row.label }}
  </div>
</template>

<script>
export default {
  name: "MenuItem",
  props: {
    row: {
      type: Object,
      required: true,
    },
    activeId: {
      type: Number,
      required: true,
    },
  },
  methods: {
    handleSelect() {
      this.$emit("select", this.row.id);
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.menu-item {
  /* your styles */
  display: flex;
  width: 100%;
}
.menu-item_active {
  background-color: red;
}
</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

Utilizing various AngularJS filters with multiple input sources

Looking to enhance my user array filtering process with two input boxes. Here's how the array is structured: $scope.users = [{ id: 1, fname: 'Sophia', lname: 'Smith', email: '<a href="/cdn-cgi/l/email ...

Extract data from a CSV table stored in a variable using node.js

Currently, I am working on a node application that can potentially result in a CSV formatted table being stored in a variable. I am interested in converting this CSV data into a JSON format. I have explored various modules, but it appears that most of th ...

My global variable keeps getting caught by the addEventListener function

My script does not use jQuery and has an addEventListener on a button click. The issue is that after the first run, I want to change the value of "sendingurl" to something else. However, even though sendingurl is updated with the new id value, it doesn&apo ...

Is there a method to retrieve the subdirectories and files currently present on a given URL?

Picture having a file on your system with the following URL: I am curious if there is any software, command, or script that can retrieve subfolders/files based on a given URL. For instance: Input parameter: http://my.page/folder_1/ Output: http://my.p ...

What is the process for disabling the CSS module feature in Next.js?

In Next.js, Global CSS can only be imported in _App.js. However, importing global CSS in every component is not allowed, so we have to use CSS modules to comply with this restriction imposed by Next.js. Currently, I am in the process of migrating a large ...

A pop-up appears, prompting me to redirect to an external URL when clicking on a link that opens in a new tab on my WordPress website

Seeking assistance in dealing with a popup that appears when trying to open a link redirected to an external URL. The popup prompts for permission to open the link in a new tab. Upon inspecting the Element, the code snippet below is revealed: <div cla ...

Place the Material-UI Drawer component beneath the Appbar in the layout

Currently, I am working on developing a single-page application using Material-UI. In this project, I have integrated the use of an AppBar along with a ToolBar and a Drawer. However, I have encountered an issue where the Drawer is overlapping the AppBar an ...

Will the script src onclick change have an effect after the page is fully loaded?

Imagine you have a script that loads right away when a page loads. Now, what happens if the script src changes when you click on a button? Will the new src get executed? Here is some example code: <button> click </button> <script class=" ...

There seems to be an issue as the function reporter.beforeLaunch cannot be identified and

I am currently attempting to incorporate the protractor screenshot reporter for jasmine 2. However, I encountered the following error in the terminal: /usr/local/bin/node lib/cli.js example/conf.js /Users/sadiq/node_modules/protractor/node_modules/q/q.j ...

Retrieving a parameter in NextJS from the request.body when it is not found

In the API code for my nextJS, I have implemented the following logic: export default async function handler(request, response) { if (request.method === "POST") { const type = request.body.type ?? 'body type' const ...

The most basic method for monitoring changes in an object

An application needs to immediately update all clients with a large object when it changes. What is the most basic method to monitor changes in a Node.js object? Consider the following object: var obj = { num: 3, deep: { num: 5 } } A functi ...

Adjusting an item according to a specified pathway

I am currently working on dynamically modifying an object based on a given path, but I am encountering some difficulties in the process. I have managed to create a method that retrieves values at a specified path, and now I need to update values at that pa ...

adjustable canvas dimensions determined by chosen images

I would like to create a canvas image based on the selected image <canvas id="canvas" ></canvas> <input type="file" id="file-input"> Using JavaScript: $(function() { $('#file-input').change(function(e) { var file ...

Text input fields within a grid do not adjust to different screen sizes when placed within a tab

I noticed that my component under a tab is causing the Textfield to become unresponsive on small screens. To demonstrate this, I checked how the Textfield appears on an iPhone 5/SE screen size. https://i.stack.imgur.com/d8Bql.png Is there a way to make t ...

Retrieve any webpage using AJAX

Hey there! I'm a beginner in AJAX and have a question that may seem basic to some. I understand that you can set up a page to respond to an AJAX call, but is it possible to request any page with AJAX? In other words, can all the functionalities of a ...

405 Method Not Allowed - Compatible with all devices except for iOS Safari

Recently, I've been working on an application using AngularJS to track and submit statistics to a Google Apps Script for processing. While everything functions flawlessly on my computer in both Chrome and Firefox, encountering errors on the iPad has b ...

NodeJS: Steps to efficiently transfer data from a master table to two separate tables while maintaining the order of the master table, utilizing asynchronous code wherever applicable

Are promises or async/await being used for this task? For instance: if the master table has columns (id, uuid, op_1, op_2) then the new tables should be table1(id, uuid) table2(id, op_1, op_2) The priority is to maintain the same order as the master ta ...

Conceal the div when the horizontal scroll position of the container is at 0

Struggling with a JavaScript issue in a fluid width page that involves scrolling a div using navigation buttons. I am new to JavaScript and trying to use scrollLeft = 0 to hide the leftmost nav button if there's nothing to scroll, and to show the div ...

Saving data inputted in a form using ReactJS and JavaScript for exporting later

What is the best way to save or export form input in a ReactJS/JS powered website? For example, if I have a form and want to save or export the data in a format like CSV after the user clicks Submit, what method should I use? Appreciate any advice. Thank ...

issue with webpack-dev-server not refreshing after changes to HTML or Sass files

Here is the layout of my project: \root \webpack.config.js \public \ index.html \ ... \ css \ directives \ views \ dist (webpack output) \app.js ...