Create a layered panel underneath a button that covers a separate element

I am working on a layout that consists of an editor and multiple buttons positioned above it on the right side. My goal is to add a panel just below "Button2" that will overlay the editor. This panel should expand and collapse when "Button2" is clicked, which should be simple to implement.

I have already written some code for this project, which can be found here: https://codesandbox.io/s/fervent-mclaren-3mrtyj?file=/src/App.js. However, currently, the panel is not appearing under "Button2" as intended and does not overlay the editor.

If anyone has suggestions on how to adjust the CSS to achieve the desired layout, I would greatly appreciate the assistance.

import React from "react";
import { Stack } from "@fluentui/react";

export default class App extends React.Component {
  render() {
    return (
      <div>
        <Stack horizontal horizontalAlign="space-between">
          <div>Title</div>
          <div>Button1 Button2 Button3</div>
        </Stack>
        <div
          style={{
            backgroundColor: "yellow",
            width: "350px",
            height: "50px"
          }}
        >
          A floating panel which is supposed to be always under "Button2" and
          overlays the editor.
        </div>
        <div
          style={{
            backgroundColor: "gray",
            width: "100%",
            height: "300px"
          }}
        >
          An editor
        </div>
      </div>
    );
  }
}

Answer №1

To make the floating pane work properly, you must include position:absolute and place it inside the editor div with position:relative. The button 2 functionality will toggle the visibility of the floating panel.

Following these steps will ensure success:

var btn=document.querySelector('.drop_btn');
    btn.onclick=function()
    {
      document.querySelector('.dropdown').classList.toggle('block');
    }
*
        {
            font-family: 'arial';
            margin: 0px;
            padding: 0px;
        }
        .menu_pane
        {
            display: flex;
            background: #151515;
            color: white;
            padding:5px 10px;
            align-items: center;
            border-bottom: 1px solid rgba(255,255,255,0.2);
        }

        .menu_pane h3
        {
            font-weight: normal;
            font-size: 18px;
            flex-grow: 1;
        }

        .menu_pane .btn button
        {
            position: relative;
            background: #0971F1;
            color: white;
            border-radius: 5px;
            border:none;
            cursor: pointer;
            padding: 8px 20px;
            margin-right: 10px;
        }

        .dropdown
        {
            display: none;
            height: 100%;
            width: 100%;
            top: 0;
            left: 0;
            color: white !important;
            position: absolute;
            background: #242424;
            border-radius: 0 0 10px 10px;
        }

        .menu_pane .btn .dropdown p
        {
            font-size: 14px;
        }
        .editor_pane
        {
            position: relative;
          background:#151515;
          color: white;
          min-height: 50vh;
          border-radius: 0 0 10px 10px;
          padding: 10px;
          color: #512DA8;
        }
        
        .block
        {
            display: block;
        }
<div class="container">
    <div class="menu_pane">
        <h3>Title</h3>
        
        <div class="btn">
        <button>Button-1</button>
        </div>

        <div class="btn">
        <button class="drop_btn">Button-2</button>

        </div>

        <div class="btn">
        <button>Button-3</button>
        </div>
    </div>

    <div class="editor_pane">
        <p>An editor</p>
        <div class="dropdown">
            <p>A floating panel which is supposed to be always under "Button2" and overlays the editor.</p>
        </div>
    </div>
</div>

Answer №2

What do you think of this design?

https://codesandbox.io/s/hidden-platform-q3v4kc?file=/src/App.js

I made some updates to the layout - I moved the floating pane inside the button, changed the button's position to relative, and set the floating pane to position absolute.

Please note: I deliberately did not include the top property for the floating pane. An interesting point about using position: absolute is that if you don't specify top, left, bottom, or right properties, the element will be positioned where it would naturally be without any positioning applied.

Answer №3

Update

Upon reviewing the setup, I realized that the overlay should only cover the "editor" area. The example has been revised to reflect this adjustment.

Check out the updated live example: codesandbox

import React from "react";
import { Stack } from "@fluentui/react";

export default class App extends React.Component {
  state = { showOverlay: true };
  handleToggle = () =>
    this.setState((prev) => ({ showOverlay: !prev.showOverlay }));

  render() {
    return (
      <div>
        <Stack
          horizontal
          horizontalAlign="space-between"
          style={{ padding: "12px" }}
        >
          <div>Title</div>
          <Stack horizontal>
            <button>Button1</button>
            <button onClick={this.handleToggle}>
              {`Button2 (${this.state.showOverlay ? "Hide" : "Show"} Overlay)`}
            </button>
            <button>Button3</button>
          </Stack>
        </Stack>
        <div
          style={{
            backgroundColor: "gray",
            width: "100%",
            height: "300px",
            position: "relative"
          }}
        >
          An editor
          <div
            style={{
              position: "absolute",
              inset: "0",
              backgroundColor: "rgba(0, 0, 0, 0.70)",
              display: this.state.showOverlay ? "flex" : "none",
              justifyContent: "center",
              alignItems: "center",
              color: "#fff"
            }}
          >
            A floating panel meant to always be positioned under "Button2" and overlay the editor.
          </div>
        </div>
      </div>
    );
  }
}

For reference, the original example featured an overlay covering the entire component except for "Button 2."

Original

I may not have fully grasped the desired outcome, but here's the implementation showcasing a toggle overlay controlled by Button2.

The current overlay sits atop and obstructs all child elements besides Button2, functioning as a "start editing" button. However, it can be further customized to specify the element it covers, aligning better with the specific use case.

Take a quick look at the demo: codesandbox

import React from "react";
import { Stack } from "@fluentui/react";

export default class App extends React.Component {
  state = { showOverlay: true };
  handleToggle = () =>
    this.setState((prev) => ({ showOverlay: !prev.showOverlay }));

  render() {
    return (
      <div style={{ position: "relative", zIndex: "1" }}>
        <Stack
          horizontal
          horizontalAlign="space-between"
          style={{ padding: "12px" }}
        >
          <div>Title</div>
          <Stack horizontal>
            <button>Button1</button>
            <button style={{ zIndex: "75" }} onClick={this.handleToggle}>
              {`Button2 (${this.state.showOverlay ? "Hide" : "Show"} Overlay)`}
            </button>
            <button>Button3</button>
          </Stack>
        </Stack>
        <div
          style={{
            position: "absolute",
            inset: "0",
            backgroundColor: "rgba(0, 0, 0, 0.70)",
            zIndex: "50",
            display: this.state.showOverlay ? "flex" : "none",
            justifyContent: "center",
            alignItems: "center",
            color: "#fff",
          }}
        >
          A floating panel meant to always be positioned under "Button2" and overlay the editor.
        </div>
        <div
          style={{
            backgroundColor: "gray",
            width: "100%",
            height: "300px",
          }}
        >
          An editor
        </div>
      </div>
    );
  }
}

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 other option can be used in place of white-space:nowrap?

When using a textarea with the CSS property white-space:nowrap, it successfully removes spaces but adds a horizontal scrollbar to the text. I want to avoid the horizontal scrollbar while also preventing scrolling using arrow keys when using overflow-x:hi ...

What is the best way to align my content alongside my sidebar?

I am facing some challenges with my website layout because I used Bootstrap to integrate a sidebar. The sidebar has caused issues with the positioning of my content, and I'm struggling to align it properly next to the sidebar on the left side. Please ...

What is the best method for applying a gradient color to the parent class in CSS using pseudo classes (before and after)?

"Is there a way to overlay gradient colors on pseudo-classes (before and after) while working with parent classes in CSS? I am attempting to create arrow shapes using div elements and the 'after' class. The div's background color consis ...

What is the method for initiating a radial gradient from the middle of the pie chart?

In my pie chart with grid lines, I have successfully implemented a radial gradient. However, the issue is that there is a separate gradient for each pie slice. What I really want is a single gradient originating from the center of the entire pie chart. I ...

Is it possible to utilize LESS to dynamically read the width?

My LESS code looks like this: &.settings-prompt .panel { margin-left: -190px; width: 380px; height: 200px; } &.create-playlist-prompt .panel { margin-left: -165px; width: 330px; height: 180px; } I want the .panel to have ...

What is the purpose of the bold line down the left side of every HTML calendar?

Welcome to a calendar layout designed with flexbox: Check it out here Do you notice the thicker lines below the heading rows and want to remove them? Here is how you can modify the HTML & CSS code: #calendars-container { text-align: center; font-f ...

I can't get Toggleclass to switch classes properly, am I missing something?

I have been experimenting with creating a button that toggles a class and reveals another div below it. Feel free to check out the example on jsFiddle that I created for you. While the first function of showing and hiding the div is working perfectly, I ...

Elevate your website design by adding interactive Ripple Buttons using a combination of Javascript and

Recently, I came across a script that allows for an animation to occur when a button is clicked. However, I have encountered an issue where the script does not redirect to a link. (function() { var cleanUp, debounce, i, len, ripple, rippleContainer, rip ...

The layout of the element

Throughout the tutorial, functions are called in the following order: constructor() static getDerivedStateFromProps() render() componentDidMount() Why is fetching and assigning a value to state done in componentDidMount() instead of earlier in render() ...

Incorporating a component specified in a .jsx file into a TypeScript file

We recently acquired a react theme for our web application, but ran into issues transpiling the components. After resolving that problem, we are now facing type-related challenges. It seems that TypeScript is struggling because the props do not have a def ...

I am experiencing an issue with the initial for-loop in my code as it is not

Upon clicking the start button, the score button, as well as the try again or correct button, are not being updated. Can someone help me figure out what's wrong? I believe the issue lies within this section of the code: for (i=0; i<5; i++) { do ...

What is the best way to locate an item reference for a specific DOM element?

Imagine a vast DOM structure with approximately 10,000 HTML elements, including 1,000 span tags. None of the span elements have any identifiers and are buried deep within other objects with long, complex Xpath paths that are not user-friendly for selectio ...

Removing Delivery Address and Method From Checkout Page in OpenCart 2

Is there a way to remove "Step 2, Step 3: Billing Details" from the Checkout Page in OpenCart 2.0? I have searched for solutions online but haven't found one specifically for OpenCart 2.0. This is what I tried: <div class="panel panel-default" s ...

ReactJS - When a child component's onClick event triggers a parent method, the scope of the parent method may not behave as anticipated

In my current setup, I have a parent component and a child component. The parent component contains a method within its class that needs to be triggered when the child component is clicked. This particular method is responsible for updating the store. Howe ...

Encountered a discrepancy with npm dependencies during the update or installation process on a legacy project

I am encountering issues while attempting to run an older project. Every time I try to npm install or start the project, it throws various dependency errors, particularly related to the outdated npm version. My current node version is 16.x, whereas the pro ...

My dynamic route with Hooks and route parameters seems to be malfunctioning - what could be causing this

I have been delving into React in my free time, and as a project, I am developing a project management portal. I currently have a dashboard route set up at "/" and a projects route set up at "/projects". My aim is to create a dynamic ro ...

Building a personalized dropdown menu in React Native involves creating a customized component that includes

Is there a way to create a drop-down menu similar to the one shown in react native? Looking for suggestions on how to achieve this. ...

Updating the color scheme of the selected nav-item and showcasing the corresponding page in relation to the navbar selection

I have been trying various methods, but I am unable to change the background color of an active navigation item and also display the corresponding page. Important: The jQuery code below successfully changes the background color of the navbar list item. Ho ...

Display React parent input when the user clicks outside the textbox area

Whenever I update or write a letter in the Child Textbox component, the entire Parent component re-renders. How can I make it so that the component only re-renders when onBlur event occurs, or when the user leaves the textbox? This is being done using MUI ...

Retrieve data from HTML in order to apply styling to an element

I am attempting to set the width of my .bar-1 outer span element based on the value of my inner span element. <span class="bar bar-1"> <span name="PERCENTAGE" id="PERCENTAGE" disabled="" title="Total Percentage" maxlength="255" value="66" tabinde ...