React Semantic UI Grid and Div components can be toggled by using a button to decrease

My goal is to create a menu that initially displays with a certain width, but when a button is clicked, it will decrease the width to 50px and only show the icon.

I'm struggling to figure out how to adjust the width of my div and integrate this functionality into a semantic grid layout.

Here is the code snippet:

function Menu() {
  const [open, setOpen] = useState(true); 
  const handleClick = e => {
    e.preventDefault();
    setOpen(!open);
  };
  return (
    <Grid style={{background: '#eee'}}>

    <Grid.Column computer={2} tablet={4} mobile={5} style={{background: '#000', padding:'0', height:'100vh'}}>
    
    <div style={{background:'#000', width:'100%', height:'100%'}}>
        
    </div>
    </Grid.Column>
    <Grid.Column width={14} style={{background: '#eee', padding:'0'}}>
          <Button icon onClick={handleClick}>
            <Icon name="align justify" />
          </Button>
    </Grid.Column>
   </Grid>
  );
}

CSS Styles:

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

#root,
.App,
.ui.grid{
  height: 100vh !important;
  margin: 0 !important;
  padding:0 !important;
}

View the live code example here.

Answer №1

To adjust the width of a div based on the state change triggered by button click, check out this demo link

App.js

import React, { useState } from "react";
import { Grid, Button, Icon } from "semantic-ui-react";
import "./style.css";

function Menu() {
  const [open, setOpen] = useState(true); // setting up a new state variable "open"

  const handleClick = e => {
    e.preventDefault();
    setOpen(!open);
  };

  return (
    <Grid style={{ background: "#eee" }}>
      <Grid.Column
        computer={2}
        tablet={4}
        mobile={5}
        style={{
          background: "#000",
          padding: "0",
          height: "100vh"
        }}
      >
        <div
          style={{
            background: "red",
            width: open ? "100%" : "calc(100% - 50px)",
            height: "100vh"
          }}
        />
      </Grid.Column>
      <Grid.Column
        computer={14}
        tablet={12}
        mobile={11}
        style={{ background: "#eee", padding: "0" }}
      >
        <Button icon onClick={handleClick}>
          <Icon name="align justify" />
        </Button>
      </Grid.Column>
    </Grid>
  );
}
export default Menu;

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

When navigating back to the Homepage from another page, React displays the Homepage

Recently, I started learning React and following the Traversy crash course along with an additional video on React router 6+. My route setup looks like this: import { BrowserRouter as Router, Route, Routes } from 'react-router-dom' return ( &l ...

The layout of webpages on Internet explorer 11 or IE 8 may appear differently than on Chrome or Mozilla

While coding a webpage using cshtml, I encountered an issue with a link pointing to a doc or pdf file in a Windows server location. In Windows Explorer, the file could be opened by clicking the link, but Chrome or Mozilla did not allow me to open the file ...

The Material-UI TreeView component fails to update when new children are added

I've hit a roadblock with this issue that has lingered for quite some time. I am utilizing Material-UI's TreeView component to showcase a list of directories with nested documents. The problem arises when I attempt to add a new directory or docum ...

Footer rises with bootstrap zooming

Hey everyone! I'm a beginner in the world of web development and currently working on creating my own website. I've run into an issue with my footer - I want it to stay fixed at the bottom even when zoomed in, but for some reason, when I zoom in, ...

Ensuring my form adjusts effortlessly to the sprites

I need assistance in making my contact form responsive to match the eagle sprite on this specific webpage: The goal is to ensure that as the screen size changes, the eagle's mouth aligns perfectly with the form. While the sprites and form are both re ...

`Border radius for the items in Apex Chart's donut chart`

I need help rounding the corners of my donut chart items. Can someone please provide guidance? Here is what I want it to look like And here is what I have created so far const Chart = dynamic(() => import('react-apexcharts'), { ssr: false }) ...

Displaying a list of images in a single line using React's Material UI

Is it possible to avoid the scroll bar in React Material UI below the image list? I am looking for a way to display multiple images in a single line without overflowing (overflow x). https://i.stack.imgur.com/mTMzZ.png const useStyles = makeStyles((theme) ...

Is there a way to restrict access to my website to only be opened in the Chrome browser?

Is there a way to prevent my web application from loading when the link is opened in browsers other than Chrome? Can this be achieved using Javascript or Java? I want to restrict the usage of my web application to only Chrome. Any assistance would be appre ...

Switch up the Background using .css and jCarousel

I am attempting to create a dynamic gallery using jCarousel and Ajax. The thumbnails are being loaded from a '.txt' file. I am trying to achieve the following effect: When a thumbnail is clicked, the background of the body changes. This action sh ...

String iteration with Stylus

Is there a way to remove the brackets and the quotation marks when putting media queries into an object and looping over them? The code works well, but the stylus is causing the output to have unwanted characters. Here's my code: $maxBreakpoints = { ...

What is the best way to shift my content to the next line once it reaches a specific width in my paragraph

When a string is pulled from a database and inserted into a paragraph, it breaks perfectly if the paragraph has spaces. However, if a word is longer than the line, it goes off the page. How can I make it wrap to the next line instead of overflowing off the ...

What is the best way to use multiple for loops in different columns within a flask template?

In my first column, the last link in my initial for loop leads to the header in the second column where another for loop is present. Oddly enough, the link for Broccoli also creates a hyperlink in the text Previous Ads, which is not intended. https://i.ss ...

Error encountered while attempting to install material-ui v3.0.3 due to an unexpected termination of the JSON input

I'm currently in the process of installing the most recent stable version of material-ui(v3.03) by running: npm install @material-ui/core. However, I encountered an issue with npm ERR! Unexpected end of JSON input while parsing near '...-/brcast- ...

The mobile screen size shortcuts are malfunctioning, while the regular links are functioning properly

ISSUE: Links in alias buttons are not working on mobile screen size, while normal links to other webpages like social media work fine. Description I created a project using an HTML, CSS, JS building tool from The project was exported and placed into a ...

Identifying shifts in offsetTop

In my design, I have a page where scroll bars are not allowed, meaning all content must be visible at once. The image below shows the basic layout of the screen to give you an idea. The layout consists of two blocks - Block 1 and Block 2. Block 1 is expan ...

Hide the button when you're not actively moving the mouse, and show it when you start moving it

How can I make my fixed positioned button only visible when the mouse is moved, and otherwise hidden? ...

Struggling to decide on the perfect CSS selector for my puppeteer script

I am trying to use puppeteer page.type with a CSS selector. <div class="preloader"> <div class="cssload-speeding-wheel"></div> </div> <section id="wrapper" class="login-register"> <div class="login-box"> <d ...

Having trouble getting the height to work properly on your Blogger Blogspot template?

Issue with embed height=100% not working <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr"> <head> & ...

I'm attempting to install the "firebase" package using npm, but I keep encountering a python-related error message during the installation process

I am experiencing difficulties while attempting to install the firebase package in a local expo-managed project. Unfortunately, I keep receiving the following error message... Here is the error message that I am encountering I have already tried using "e ...

Customize your Bootstrap 4 navbar to align on the right with a button that remains visible on mobile devices

I'm working on setting up a navbar with three elements. I want to have a left-aligned brand, a group of right-aligned links that will collapse on smaller screens, and an additional button that stays visible at all times. To achieve the alignment of t ...