Search code examples
cssreactjssemantic-ui

Toggle button / decrease width grid and div Semantic React


I have a menu that will have a size and when you click the button it will decrease the width to 50px

ie I will have a menu with button and icons and when clicking the button will appear only the icon

but i'm having a hard time how do i decrease the width of my div and how would it work on the semantic grid

code:

function Menu() {
  const [open, setOpen] = useState(true); // declare new state variable "open" with setter
  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:

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

code: https://codesandbox.io/s/cool-kepler-cxj4x


Solution

  • You can decrease the width of div when state of open being change on button click, please review 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); // declare new state variable "open" with setter
    
      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;