What is the best way to prevent scrolling when a modal is open in Next.js with Tailwind CSS?

Currently I have this code implemented, but it seems to be causing an issue as it throws a "document is not defined" error.


export default function Modal() {
  const [modal, setModal] = useState(false);

  const toggleModal = () => {
    setModal(!modal);
  };

//   THE ERROR OCCURS HERE

  if (modal) {
    document.body.classList.add('active-modal');
  } else {
    document.body.classList.remove('active-modal');
  }

Answer №1

While I haven't tested it, I suggest placing the if statement within the useEffect() hook. It seems that initially the document object is not recognized by nextJs, and the same applies to the global window object!

When dealing with side-effects, it's best practice to utilize the useEffect() hook. This hook will run after the component has been mounted on the DOM.

Answer №2

When using Nextjs, you have the ability to prevent scrolling by utilizing the following code snippet:

document.documentElement.style.overflow = "hidden"

Furthermore, you can restore the scrolling functionality by executing:

document.documentElement.style.overflow = ""

This will enable scrolling once again on the webpage.

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

Integrating dual Google Maps onto a single HTML page

I'm facing an issue with implementing two Google maps on a single page where the second map seems to be malfunctioning. Below is the code I am currently using: <style> #map-london { width: 500px; height: 400px; } #map-belgium { wi ...

What strategies can be employed to troubleshoot CSS design problems that exclusively occur in the production environment?

My Node.js based URL shortening project can be found on GitHub at this link. It's deployed on Vercel, but I've been experiencing some CSS design discrepancies on the Pixel 7 device while using the Brave browser. Specifically, I'm having issu ...

What method can I use to adjust the font style when it overlays an image?

Sorry if this is a bit unclear, but I'm trying to figure out how to change only a section of my font when it overlaps with an image while scrolling. If you visit this example website, you'll see what I'm referring to: For a visual represen ...

What is the reason for the presence of additional space below when using x-overflow:hidden?

When I place two spans inside each other and use overflow-x:hidden on the inner span, it results in extra space below the inner span. Why does this happen? <span style="" class="yavbc-color-tip"><span style="">Some text</span></span&g ...

When attempting to utilize the Next.js Head component in a location other than _document, the page experiences

Currently, I am in the process of integrating Next.js with Typescript and Material UI. Despite the abundance of tutorials available online for setting up Next.js with Material UI, I have encountered a commonality among them where they all provide identical ...

adjusting the space between lines for paragraphs with exceptionally large text

Utilizing Bootstrap allows for the adjustment of paragraph font sizes to appear quite large, mimicking the size and style of a heading without the need for an actual heading level HTML tag. I am hesitant to use a genuine <H2> tag to alter the visual ...

Tips for repositioning my sidebar using CSS and HTML

I'm a beginner in CSS and HTML and I'm struggling to make my sidebar move to the side on my website. I have divided my site into three div areas: one for the 'sidebar', one for the 'main content', and one for both called &apos ...

Checking non-mandatory fields in a React application with Zod

Currently utilizing react-hook-form and zod in a Next.js project. I am encountering an issue with the .optional() method when using it with a URL validation flag. When applying the .url() flag, the field no longer remains optional. It only accepts a valid ...

Adjusting table dimensions according to webpage dimensions

When looking at the image below, you will notice a div that contains a table with some content. However, when resizing the web browser's page, the table goes beyond the boundaries of the div. https://i.sstatic.net/SRlzM.png My goal is to reduce the ...

Applying CSS styles to my container through a button click event

Currently, I am attempting to apply a padding-top to my container when a button is clicked. Unfortunately, the padding-top is not being added as expected. $('#login').on('click', function(){ $('#loginform1').css('padd ...

Center-align the table element

I've been struggling to center align this table element, specifically the one containing the square, but nothing seems to be working. Can anyone lend a hand in fixing this issue? <table> <tbody> <tr> <td>TURKEY - SU ...

Tips for customizing tab pane color using HTML/CSS

I am looking to customize the color scheme of my tab-pane. My goal is to have each tab turn "orange" when clicked, while keeping the rest of the tabs in a "gray" color. Below is the code I am currently experimenting with: <!-- Nav tabs --> <ul ...

Prevent sibling divs from being affected by onClick events in React

Currently, I am facing an issue with changing the className of a specific div element when clicked. Whenever I click on one div, the classNames of all the div elements change. This problem arose from converting a vanilla JS concept into a React component. ...

PHP script that dynamically highlights text based on values retrieved from a database

I have multiple small HTML tables generated by PHP on a single page, each with a unique <table id="">. The data in these tables corresponds to entries in a MySQL table with matching IDs. What I am looking for is to automatically highlight the values ...

I am struggling to make my table adapt to different screen sizes and become

Here is an example of a table: <TABLE class=vysledky > <TR class=podtrzeni> <TD width="39" height="19">Order <br /> League</TD> <TD width="39" height="19">Order <br /> Team</TD> <TD width="3 ...

Inquiries about creating collapsible content using html, css, and javascript

I have been exploring collapsibles and noticed that many of them use extra code that seems unnecessary or confusing. I decided to create a simple one based on my logic, but it's throwing an error: app.js:4 Uncaught TypeError: Cannot read property &apo ...

Is there a way for me to update the button text and class to make it toggle-like

Is there a way to switch the button text and class when toggling? Currently, the default settings show a blue button with "Show" text, but upon click it should change to "Hide" with a white button background. <button class="btn exam-int-btn">Show< ...

Using two modal popups while passing an identifier

UPDATE: In my investigation, I discovered that a plain input tag without MVC RAZOR works as expected: <input type="text" class="hiddenid2" /> //WORKED However, when using the following code, it does not work: @Html.Editor("id", "", new { htmlAtt ...

the initial loading time of useGLTF seems to be on the longer side

Currently in my project, I am implementing useGLTF from @react-three/drei to load a 3D model. The model has been compressed using gltf-pipeline and the .glb file size is 1.7MB. However, when accessing the website for the first time, the loading time for th ...

Is it possible to eliminate a character from text that lacks html tags or CSS classes using CSS?

I need assistance in removing characters that are not associated with any HTML tag, CSS id or class. Specifically, I want to eliminate the "--" from the provided code snippet below. Can someone please guide me on how to achieve this? <span cl ...