Tips on keeping a Material UI menu item open when clicked

My menu consists of a single item, which is a text field along with a button. The idea is to input some information, save it by clicking the button, and then close the menu either by hitting "close" or the original button again. How can I stop the MenuItem from closing the menu when clicked on? Also, how do I enable the functionality to close the MenuItem using a button? Thank you!

function AppWindow() {

  const [settingsState, setSettingsState] = useState(false);
  const [anchorEl, setAnchorEl] = useState(null);
  const open = Boolean(anchorEl);

  const handleClick = (event) => {
    setAnchorEl(event.currentTarget);
    event.preventDefault();

    if(settingsState === true){
      console.log('true to false');
      setSettingsState(false);
    }
    else if(settingsState === false){
      console.log('false to true');
      setSettingsState(true);
    }
  };

  const handleClose = () => {
    setAnchorEl(null);
  };


return (
    <Box className="App">

      <Box className='AppContainer'>
      
          <Paper className='Application' elevation={24} sx={{backgroundColor: '#023453'}}>
              <Box sx={{margin: '1%'}}>
                  <Grid container>
                      <Grid item xs={11} sx={{color: '#d4e1ed'}}>
                          Name
                      </Grid>

                      <Grid item xs={1}>
                          <SettingsIcon onClick={handleClick}></SettingsIcon>
                              <Menu
                                id="basic-menu"
                                anchorEl={anchorEl}
                                open={open}
                                onClose={handleClose}
                                getContentAnchorEl={null}
                                anchorOrigin={{vertical: 'bottom', horizontal: ''}}
                                transformOrigin={{vertical: 'top', horizontal: 'right'}}
                                sx={{marginTop: '0.5%'}}

                              >
                                <MenuItem onClick={handleClose} sx={{}}><Settings /></MenuItem>
                          </Menu>
                      </Grid>
                  </Grid>

https://i.sstatic.net/s3vwx.png

Answer №1

To achieve this, make sure to exclude the following line:

const isOpen = Boolean(anchorEl);

Instead, implement a different state to manage the 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

Replacing the yellow autofill background

After much effort, I have finally discovered the ultimate method to remove autofill styling across all browsers: $('input').each(function() { var $this = $(this); $this.after($this.clone()).remove(); }); However, executing t ...

The web typography appears fuzzy or has incorrect thickness, lacks consistency across pages and even within a single page

The font (ProggyCleanSZBP.ttf from 1.1.5 release) appears to be intermittently thick and blurry. If you compare with , you'll notice that the latter displays as desired in most cases, but there are instances where the issue occurs, such as the first ...

Mapping through multiple items in a loop using Javascript

Typescript also functions Consider an array structured like this const elementList = ['one', 'two', 'three', 'four', 'five'] Now, suppose I want to generate components that appear as follows <div&g ...

What is the best way to ensure a table is responsive while maintaining a fixed header? This would involve the table scrolling when it reaches the maximum viewpoint, while also keeping

Can anyone help me create a responsive table with a fixed header that scrolls when it reaches the maximum viewpoint without scrolling the entire page? I've tried using box-sizing: border-box; and overflow-x:scroll; but it didn't work. Any suggest ...

Learn how to dynamically add comments to HTML elements in AngularJS

Here is the meta tag that I have: <meta title='raja' identifier> I would like to know how to manipulate the DOM so it looks like this: <!-- reference <meta title='raja'> --> Could you recommend a way to manipulat ...

React exclusively deletes the final element from an array upon re-rendering

When an item is removed from an array in React, it seems to only remove the last item rendered in the array instead of the actual element that was deleted. Check out the code snippet below: <View style={[styles.container, this.props.style]}> ...

How can I switch between columns in a dual-column layout?

In my latest project, I utilized CSS Flexbox to create a sophisticated two-column layout that allows toggling each column independently. While this functionality is exactly what I need, I can't help but feel that my current method is somewhat cumberso ...

Error Encountered in React: Review the render method within the TabPanel

I am interested in integrating a material design tab using the code snippet provided below: import React, { PureComponent } from 'react' import { Field, reduxForm,change } from 'redux-form' import PropTypes from 'prop-types' ...

I am attempting to transfer information from one page to another in Next.js, but unfortunately, I am experiencing difficulties in

Encountering the following error message: "Error: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead." Any assistance will be greatly appreciated. export default func ...

Beginner in CSS wanting to set background image for input field

I am working on incorporating icons into my project. The image I have contains an array of 16x16 icons at this URL: "http://www.freepbx.org/v3/browser/trunk/assets/css/jquery/vader/images/ui-icons_cd0a0a_256x240.png?rev=1" Does anyone know how I can selec ...

Minimize or conceal iframe

This iframe contains a Google form that cannot be edited. I am looking for a way to close or hide this iframe, either through a button, a popup window button, or without any button at all. The $gLink variable holds the Google form link through a PHP sessio ...

Using JQuery to apply a CSS class or check for a CSS class on multiple button elements that

Being new to Jquery and Javascript, I am facing challenges in understanding how to implement the following. Throughout my site, I use the same button class, and I am attempting to make Jquery apply a "clicked" class only to the button that was clicked, no ...

Updating row color according to values obtained from the map function in ReactJs

I have been experimenting with various methods to change the color of table rows based on specific values within a map function. Despite trying solutions like the UseRef hook and browsing through stack overflow, I have yet to achieve success. {dat ...

Is there a way for me to create a sidebar that is scrollable, like the one on www.youtube

Hey there! I'm looking to incorporate a feature on my website that resembles the layout of https://www.youtube.com/. More precisely, I want to have a sidebar on the left side displaying playlists, subscriptions, and other options. However, I don' ...

Implement automatic data replacement using PHP and MySQL triggers

The code provided below pertains to postar.php file include "conexao.php"; $consulta = mysqli_query($conexao, "SELECT titus.titus, titus.des, titus.link from titus"); while($postagem = mysqli_fetch_object($consulta)); echo "<d ...

Using media queries within specific CSS classes

I am attempting to create the following CSS code: .myClass { @media all (max-width: 1024px) { left: 33%; } @media all (min-width:1025px) { left: 25%; } } Unfortunately, it doesn't seem to be working as expected. Is t ...

You cannot dynamically add items to a React chips component from material-ui using an object array

I'm currently facing an issue with the Material-UI chips component. I have a select input containing a list of users. Upon selecting a user, a chip displaying their name appears below the form. However, I am encountering a problem where the chips are ...

React does not accept objects as valid children

Currently, I am working on developing a stopwatch using reactive js. Here is the code snippet that I have been working on: <div id="root"></div> <script src="https://unpkg.com/library/react.development.js"></script> <script sr ...

Is there a way to make a sass file universally accessible without having to import it into every file and causing an increase in bundle size?

Question How can I efficiently import variables.scss globally without the need to duplicate them in every file and reference them in my build instead of importing? Setup I am using Vue2 with laravel-mix, where I have imported index.scss in my main.js ...

Loading a Div Element on the Fly

I have a variety of checkboxes that are generated through iteration from a collection using the foreach loop of the core tags in JSP with parameters ID (which represents the ID of the checkbox) and key. Both the ID and key are dynamically populated based o ...