Managing the vertical space within a nested accordion section

I've built a custom accordion component, but I'm encountering scrolling issues when trying to use nested levels within the accordion. I'd like to prevent scrolling inside the accordion section and instead have the page scroll below it.

Any suggestions on how to achieve this?

App.js

import Accordion from "./Accordion";

export default function App() {
  return (
    <div>
      <Accordion title="What is lorem ipsum?">
        <Accordion title="What is lorem ipsum?">
          Lorem Ipsum is simply dummy text of the printing and typesetting
          industry. Lorem Ipsum has been the industry's standard dummy text ever
          since the 1500s.
        </Accordion>
        <Accordion title="What is lorem ipsum?">
          <ul>
            <li>
              a) Lorem Ipsum is simply dummy text of the printing and
              typesetting industry.{" "}
            </li>
            <li>
              b) Lorem Ipsum is simply dummy text of the printing and
              typesetting industry.{" "}
            </li>
            <li>
              c) Lorem Ipsum is simply dummy text of the printing and
              typesetting industry.{" "}
            </li>
            <li>
              d) Lorem Ipsum is simply dummy text of the printing and
              typesetting industry.{" "}
            </li>
          </ul>
        </Accordion>
      </Accordion>
      <Accordion title="What is lorem ipsum?">
        Lorem Ipsum is simply dummy text of the printing and typesetting
        industry. Lorem Ipsum has been the industry's standard dummy text ever
        since the 1500s.
      </Accordion>
      <Accordion title="What is lorem ipsum?">
        <ul>
          <li>
            a) Lorem Ipsum is simply dummy text of the printing and typesetting
            industry.{" "}
          </li>
          <li>
            b) Lorem Ipsum is simply dummy text of the printing and typesetting
            industry.{" "}
          </li>
          <li>
            c) Lorem Ipsum is simply dummy text of the printing and typesetting
            industry.{" "}
          </li>
          <li>
            d) Lorem Ipsum is simply dummy text of the printing and typesetting
            industry.{" "}
          </li>
        </ul>
      </Accordion>
    </div>
  );
}

If you want to see the code in action, check out the codesandbox link

Any assistance would be greatly appreciated

Answer №1

Based on the code provided in codesandbox:

Accordian/index.js

several modifications have been successfully implemented. Feel free to review them.

import React, { useState, useRef } from "react";
import Chevron from "./Chevron";

import "./style.css";

function Accordion(props) {
  const [setActive, setActiveState] = useState("");
  const [setHeight, setHeightState] = useState("0px");
  const [setRotate, setRotateState] = useState("accordion__icon");
  const [setMargin, setMarginState] = useState("0px");

  const content = useRef(null);

  function toggleAccordion() {
    setActiveState(setActive === "" ? "active" : "");
    setHeightState(
      setActive === "active" ? "0px" : "500px" // adjust pixel based on your requirements 
    );
    setRotateState(
      setActive === "active" ? "accordion__icon" : "accordion__icon rotate"
    );
    setMarginState(setActive === "active" ? "0px" : "1rem");
  }

  return (
    <div className="accordion__section">
      <button className={`accordion ${setActive}`} onClick={toggleAccordion}>
        <p className="accordion__title">{props.title}</p>
        <Chevron className={`${setRotate}`} width={10} fill={"#777"} />
      </button>
      <div
        ref={content}
        style={{
          maxHeight: `${setHeight}`,
          marginBottom: `${setMargin}`,
          overflow: "hidden" // ensure overflow is hidden
        }}
        className="accordion__content"
      >
        <div className="accordion__text">{props.children}</div>
      </div>
    </div>
  );
}

export default Accordion;

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

Implementing script loading within the Angular scope

I'm attempting to load a custom script from the database based on client-side logic. I am having trouble figuring out how to make it function properly. Here is my controller code: 'use strict'; angular.module('youshareApp') . ...

Retrieve Data from Form Using PHP and Ajax

I currently have a form on my HTML page that looks like this: <form id="submission" action="testresponse.php" method="post"> <input id="URL" name="URL" type="text"> <button name="Submit" type="submit">Subm ...

Hydration Error arises when Next.js SSG Suspense is triggered

My static site is encountering an issue when trying to display a simple list of items retrieved from Supabase. Everything was working fine in React 17, but after upgrading to React 18, I am now getting sporadic errors and the fallback doesn't consiste ...

Display JSON data in a hierarchical tree structure using AngularJS

Looking to display three nodes of a JSON file as a tree using AngularJS. The nodes are data.key, data.parentItem, and data.title. Below is the JavaScript code: var phonecatApp = angular.module('myApp', []) phonecatApp.controller('myContr ...

Tips on incorporating animations into a class design?

I'm trying to figure out how to make an image disappear by adding a class However, when I try this, the element vanishes without any animation I'd like it to fade away slowly instead I've heard there are CSS3 properties that can help achi ...

Display the text above overlapping div elements

I need a design where one div overlaps another, but the text inside the overlapping div must still be visible. <div class='box1'> <div class='sendAbove'> This message should remain visible in this div </div> ...

The performance of the NextJs app API is unpredictable when deployed on Vercel

After deploying my NextJs app to Vercel, I encountered a strange issue. The static pages are functioning perfectly fine, but the API part (which pulls data from an Azure SQL database using async mssql) is behaving inconsistently. Sometimes it returns the e ...

Having trouble with Javascript/jQuery not functioning in IE8?

My current project involves a website that functions smoothly in Chrome, but encounters issues with running JavaScript/jQuery scripts in IE8. Even a simple alert on page load fails to execute. Admittedly, my approach is a bit messy as I have mixed CSS an ...

Prevent the need to keep deleting text when entering data into selectize.js <select>_dropdown

I am working with a single choice <select> element that has been enhanced using selectize.js. This <select> element already has an option pre-selected as the current value. To change this value, users must: Click on the drop-down control, Pr ...

The width of the unordered list is not what I defined

when I set this line: <ul style="width:100px; height:100px; background-color:green"> <li style="width:100px; height:100px; border:1px solid blue"> 1 </li> </ul> The ul ends up wider than my specified 100px width. Any thoughts ...

Troubleshooting: Font-Face Won't Load on Any Browser

I'm completely new to using @font-face and I'm encountering some issues. Despite going through numerous posts and tutorials, I can't seem to get my fonts to load properly. I've tried activating and deactivating the font on the server, b ...

Accessing external content within our webpage

Previously, I utilized iframes to display an external page within our asp.net page. However, I have now decided to explore alternative methods that do not involve iframes. My goal is to open an external page within our page using only a simple aspx page wi ...

Choose CSS Option

I need assistance with customizing my dropdown select option - see the example below <select> <option value="volvo" title="NEED THIS BIGGER AND FORMATED" >Volvo</option> <option value="saab">Saab</option> <option val ...

What could be causing my scene to fail to render?

I'm attempting to adapt this particular example into CoffeeScript. Below is a snippet of my code: class Example width: 640 height: 480 constructor: -> @camera = new THREE.PerspectiveCamera 45, @width/@height, 10000 @cam ...

When using Array.prototype.map(callback, thisArg), the second parameter is disregarded

Currently, I am developing a small game using Node.js and aiming to offer support for two different languages. To display the translated lists of various game modes along with their descriptions, I have implemented Array.prototype.map(callback, thisArg). ...

Show segments of video stream on page using node.js

I am currently exploring Node.js and trying to understand how streams work, so please bear with me if I seem a bit unclear. After reading some beginner notes on streams, I decided to test it out. In my project files, specifically in public/images/videos/S ...

How to eliminate Image notifications in NextJS

I keep receiving a warning about the local images on my website: Image with src "http://localhost:3000/_next/static/media/icon_home.0f967506.svg" has either width or height modified, but not the other. If you use CSS to change the size of your image, also ...

Here is a guide on how to specify function selection values for a total order. By default, the selection will have predetermined values, and upon clicking the sum button,

<tr id=""> <th> <select id="selection" onchange="myFunction()"> <option id="0" value="0">None</option> <option id="1" value="4.00">Women Suit</option> <option id="2" value="10.00">Dres ...

The information seems to not be getting transferred to the req.body variables from the HTML form

Within my server-side settings using knex and express, I have defined the following function: // POST: Create new users app.post('/add-user', (req, res) => { const {firstName, lastName, emailAdd, gender, dob, password} = req.body; cons ...

Error in Next.js using next-auth/react: "PrismaClient cannot be executed in the browser"

Currently, I am in the process of developing a Next.js application and implementing authentication using the next-auth/react package. One of the server-side functions I have created utilizes PrismaClient to retrieve information about the current user based ...