Avoid Scroll Below Stuck Navigation

I've been searching for a solution to my issue for some time now, and while I've come across many articles discussing similar problems, none of them seem to address my specific problem.

In my React app, I have a mobile version where users can tap on a hamburger menu to open a navigation menu. The navigation menu occupies 80vw and 100vh of the screen. However, users are still able to scroll through the background application by swiping the nav menu for some reason.

Below is the styling for my mobile navigation:

.mobileNav {
  height: 100vh;
  width: 0vw;
  position: fixed;
  top: 0;
  right: 0;
  background-color: #1d1d1d;
  z-index: 999;
  will-change: transform;

  -webkit-transition: 0.75s;
  -moz-transition: 0.75s;
  -ms-transition: 0.75s;
  -o-transition: 0.75s;
  transition: width 0.75s;

  &--open {
    width: 80vw;
  }
}

I've attempted setting position: fixed or overflow: hidden on the parent element when the navBar is opened to prevent scrolling. However, this action also causes the user to jump back to the top of the page, which is not ideal.

You can view my code on codesandbox here.

Answer №2

const App = () => {
  const [isNavOpen, setIsNavOpen] = useState(false);

  useEffect(() => {
    document.body.style.overflow = isNavOpen ? "hidden" : "auto";
  }, [isNavOpen]);

  return (
    <div className="App">
      <NavMenu isNavOpen={isNavOpen} setIsNavOpen={setIsNavOpen} />
      <div className={styles.red}>
        Howdy <button onClick={() => setIsNavOpen(true)}>Launch</button>
      </div>
      <div className={styles.blue}>Greetings</div>
      <div className={styles.green}>Hola</div>
    </div>
  );
};

export default App;

The useEffect function triggers each time the navigation modal switches between open and closed states. When it's closed, body scrolling is allowed. When it's open, body scrolling is disabled.

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

Automatically scraping Facebook API data with PHP

I'm completely new to php and I am looking for a way to automatically scrape metadata. I came across a solution, but I am unsure how to implement it in php. POST /?id={object-instance-id or object-url}&scrape=true Can anyone provide a sample php ...

Utilizing shared state in React components through props

Currently, I am utilizing a shared global state in the following manner: interface DashboardProps extends React.Props<Dashboard> { globalState? : GlobalState } export class Dashboard extends React.Component<DashboardProps, any>{ } Withi ...

Is there a way to differentiate between render() being triggered by an explicit setState() invocation or by the parent component passing props to it?

In my setup, there are two sibling components called RegionsDropdownComponent and CitiesDropdownComponent residing within a container named UpdateProfileFormComponent. Initially, the UpdateProfileFormComponent has profile data (fetched from the server) wi ...

Primeng - Concealed dropdown values within a scrollable table header

Recently, I integrated Primeng p-table with a filter and frozen column feature (with one column being fixed while the others are movable). However, I encountered an issue with the select dropdown in the header. When I try to open the dropdown, the values a ...

"Execution of the console.log statement occurs following the completion of the request handling

When I have a piece of middleware that responds if no token is found, why does the console.log line still run after the request is responded to? I always believed that the res.json call would "end" the middleware. Any insights on this behavior would be g ...

Having issues when dynamically adding options to a multiselect dropdown

I'm working on dynamically adding options to a multiselect using AJAX: <select size='2' name="CraftCode" id=@ccrf class="form-control js-select manualentrydd" ></select> $.ajax({ type: "GET", url: "GetCraftCodes", data: ...

Issues arise with React during the installation of styled-components

Every time I install other packages, it goes smoothly. However, when I try to install styled-components, I encounter the following error: npm ERR! Cannot read properties of null (reading 'edgesOut') npm ERR! A complete log of this run can be fo ...

The Angular build is unsuccessful due to the presence of components from a separate Angular project

Whenever I execute ng build project1 --prod, the build fails and displays this error message: ERROR in : Cannot determine the module for class MyComponent in .../project2/app/my.component.ts! Add MyComponent to the NgModule to fix it.. Although the sol ...

Assistance needed for adjusting margins and padding within the inner content box

I'm currently in the process of designing a new website layout using CSS, but I've encountered some challenges along the way. My main goal is to ensure that all elements stay within the boundaries of the browser window, with the scroll bar appear ...

"Explore the convenience of viewing two separate videos on one webpage, each in its own

After extensive research, I have been unable to find any information on the issue at hand. I am attempting to use the jquery plugin OkVideo to display different videos in two separate "section" tags. However, even after explicitly assigning IDs to each con ...

How can you set the listbox in Sumo Select to always be open?

https://i.sstatic.net/fkiHB.png Is there a way to ensure the listbox is always open by default, as if the user had clicked? ...

Troubles with Geocoding functionality on Google Maps integration within Wordpress

I have a challenge where I want to utilize the title of a Wordpress post (a specific location) as a visible marker on a Google map. The code provided by Google successfully displays the map without any markers: <script>function initialize() { va ...

I am looking to modify various attributes linked to Rails

My Desire for Reality I am looking to update multiple related values in Rails by sending an update request from JavaScript. While creating data was seamless, I encountered difficulties when attempting to update it. #Code JavaScript* export const actions ...

Using AngularJS date picker to set value in Spring model setter

When using AngularJS with Spring, I noticed a discrepancy in the date values between my view file and the POJO User object. In my view file, I have used an input type date to bind the "user.dateOfBirth" attribute. When I select a date, it is displayed cor ...

Group an object by its name using Java Script/Vue.Js

I am looking to group objects by partial name and assign them to variables data = { SCHOOL-ADMISSION_YEAR: "2021" SCHOOL-SCHOOL_NAME: "ABC SCHOOL" SCHOOL-SCHOOL_LOCATION: "NEWYORK" ENROLLMENT-ADMISSION_YEAR: " ...

Encountered a surprise hiccup while attempting to upload file to AWS S3 using browser (putObject

After successfully obtaining a presigned URL through my Node/Express backend for a putObject request on S3, I attempt to upload the relevant files via the browser. However, when making the put request through the browser (or Postman), I encounter a 400 or ...

Managing the AJAX response from a remote CGI script

I'm currently working on a project that involves handling the response of a CGI script located on a remote machine within a PHP generated HTML page on an apache server. The challenge I am facing relates to user authentication and account creation for ...

Oops! You can't switch to `multiple` mode on a select dropdown once it has already

Here's where the issue lies: This is the code I am referring to: <div fxFlex.gt-lg="100" fxFlex="100" *ngIf="requestAction == 'add'"> <div class="pb-1"> <md2-select plac ...

Achieving JSON element sorting in the most effective way

https://i.stack.imgur.com/NQbdN.png Each array contains the following information: {{ id: 39, treaty_number: "qwe", insurant_name: "222", belonging_to_the_holding_company: "test", date_start: "2016-04-15", etc }} Is there a way to sort each array in asc ...

What is the best way to incorporate plugins designed for various jQuery versions onto a single webpage?

I have integrated three jQuery scripts into an HTML page, including a Colorbox plugin for the gallery, a popup plugin for a reservation form, and a sliding jQuery script for the footer. However, I am facing issues where either the close button on the pop ...