"Emphasize menu items with an underline as you navigate through the

I am using Gatsby with React and have a navigation menu with links. I would like to make it so that when a link is clicked, a border bottom appears to indicate the current page, rather than only on hover.

       <ul className="men" id="menu">
            <li className="menuSob">
              <Link className="menuItem" to="/about/">
                About Magnet
              </Link>
             </li>
             <li className="menuRep">
              <Link className="menuItem" to="/representacoes/">
                Representations
              </Link>
             </li>
             <li className="menuServ">
              <Link className="menuItem" to="/services/">
               Services
              </Link>
             </li>
             <li className="menuContent">
              <Link className="menuItem" to="/content/">
                Content
              </Link>
             </li>
             <li className="menuCont">
              <Link className="menuItem" to="/contact/">
                Contact
              </Link>
             </li>
       </ul>

Answer №1

With Gatsby Link, you can easily achieve this using either the activeClassName or activeStyle property. Here's an example:

   <li className="menuSob">
     <Link className="menuItem" to="/about/" activeClassName={`active`}>
            About Magnet
     </Link>
   </li>

In your .scss file, add the following:

.active{
  text-decoration: underline
} 

To learn more, visit the Gatsby Link documentation. Gatsby builds on React routing, offering all its benefits along with additional features unique to Gatsby. For instance, by setting partiallyActive={true} in each <Link>, all paths under the route path will be considered "active". For example:

      <Link className="menuItem" to="/contact/" activeClassName={`active`} partiallyActive={true}>
        Contact
      </Link>

This will mark as "active" all paths under /contact (e.g., /contact/give-me-five, /contact/etc, and so forth).

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

What separates npm run dev from npm run start when working with Next.js?

I'm curious about the distinction between running npm run dev and npm run start. Surprisingly, there isn't much information available online regarding this topic. Specifically, I'm interested in understanding this difference within React an ...

Encountering a problem while setting up a Node JS project with npm/npx using vite/react js, resulting in the error message: "node:internal/modules/cjs/loader:120

Currently, I have Node.js version v22.0.0 installed on my system. I am working on creating a new React JS project using Vite technology. However, when I try to initiate the project, I encounter an error in the terminal. npm create vite@latest node:interna ...

Custom shaped corrugated sheet in three dimensions

While constructing a house-like structure with corrugated sheets, I utilized BoxGeometry to outline the structure and adjusted vertices to create a corrugated sheet wall. I have been experimenting with creating a facade using corrugated sheets in the sh ...

Stretch element to reach the edge of the container

I need help with positioning an input and a textarea inside a container that has a height of 52.5vh. I want the textarea to start right below the input and expand all the way to the end of the container. How can I achieve this layout? Both elements should ...

When Apache proxypass is used, NextJS encounters unexpected errors

I have been trying to deploy my Next.js app to my server. I started the server using "npm run start" and it is running on localhost:3000. To make it accessible on my website's domain, I set up a proxy pass from localhost:3000. I made changes to the Ap ...

Passing parameters from a child component and router using ReactJS: A convenient method

Within my React MainComponent, I have a constructor that receives an id parameter from the router when passing it as <Link to={"/main/108"}>. The console then displays id: 108. constructor(props) { super(props); this.state ...

Form Input Field with Real-Time JavaScript Validation

Is there a way to validate the content of a textarea using pure JavaScript, without using jQuery? I need assistance with this issue. <script> function validate() { // 1. Only allow alphanumeric characters, dash(-), comma(,) and no spaces ...

Is JSON.stringify failing to function correctly in Mozilla Firefox?

Currently, I am attempting to convert an object into a string in javascript. After stringifying the object, I have noticed some discrepancies between different browsers. {"jobTypeArray":"[CONTRACT -W2]"} In Firefox and Chrome, the values appear as follow ...

Creating a visually appealing layout by dividing HTML content into two columns within a WebView

Utilizing WebView in my application to display html content for reading ebooks presented a challenge during development. After parsing the ebook and converting it into an html string, I load this string into the WebView component. myView.setVerticalScro ...

Initiate monitoring for child component modifications

I'm looking to disable 'changeDetection' for the parent component while enabling it for the child component. Can you provide an example of how this can be achieved? The parent component contains static data, meaning change detection is not ...

I am currently attempting to extract data from a JSON file by using key names for reference, but I am running into issues when dealing with nested keys

Is there a better way to retrieve values from a JSON file by matching key names? The current method I am using does not seem to work with nested keys, so any suggestions on alternative approaches would be appreciated. // Sample .JSON file { "ro ...

Having trouble sending data through a jQuery AJAX request?

When I make a post request using jQuery AJAX to an ActionResult method, I encounter the following issue: $("#itemTextbox, #itemTextboxNew, #quantity1Textbox").on("keydown", function (e) { if ((e.keyCode == 120){ var GetReportLast5SellAndBuyURL="/Trd/Se ...

Transform Ajax response into dropdown menu option

I have made an ajax call and received HTML as a response. Now, I need to convert this output into options and add them to select tags on my webpage. <div class="views-element-container"> <div class="view view-contact-view-id-conta ...

Exploring textboxes with jQuery loops

Is there a way to clear all these textboxes by iterating through them using Jquery? <div class="col-md-4" id="RegexInsert"> <h4>New Regex Pattern</h4> <form role="form"> <div class="form-group"> &l ...

ReactJS state not being updated due to a nested Axios call

Attempting to fetch data from nested axios calls, successfully retrieving responses from both calls. Struggling to update the prize_pool value within the second axios call. Any assistance would be greatly appreciated. getAllLeague() { axios.get(BA ...

Alert classes are not included in bootstrap 5 npm package

In my Angular 13 project, I am utilizing Bootstrap version 5.2.1. While most components are working as expected, the alert component seems to be missing some styling elements. Specifically, I have noticed that alerts with the classes alert-success and aler ...

How to use mousedown event in Three.js to create line drawings

I've tried multiple approaches to achieve this effect. I'm looking to draw a line on mouse down event, and despite researching various resources, I haven't been able to come up with a solution. Currently, I'm utilizing the RayCaster met ...

Troubleshooting Safari compatibility issues with Twitter Direct Messages in Angular

I am attempting to create a Twitter direct message with predetermined text already filled in. My current method involves using window.open() to prepare the message. window.open(https://twitter.com/messages/compose?text=${this.helloWorld}); helloWorld = ...

Issue with the transparency of a model in three.js

Exploring my journey with Three.js, I have been actively engaging in online communities like Stack Overflow to enhance my skills. This project serves as my inaugural experiment with loading OBJ and MTL files. However, an issue has surfaced - certain parts ...

Troubleshooting the "@material-ui/core" issue in React: Step-by-step guide

Issue with React: Unable to locate the '@material-ui/core' module in 'C:\Users\user\Downloads\inTech'22-Web Development (EDUGEN)\edu-gen\src\components' Resolution Command: To resolve, run npm in ...