Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Shared Popper requires two clicks to reopen

I’ve got buttons that represent sports. When a user clicks on a button a Popper is opened with the sport’s associated component/content. The same Popper is shared between all the buttons.

This works fine, but once the Popper is open it requires two clicks to open the Popper for another sport. One click to close the Popper and the second click to open it. So if I click the ‘Baseball’ button, I’ll have to click the ‘Basketball’ button twice before the Popper is reopened with the Basketball content.

Is there any way to accomplish this with just a single click? Link to Sandbox

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import React, { useEffect, useState } from 'react';
import { Popper, Button, Paper, Typography } from "@material-ui/core";

function CategoryMenuItemContent(props) {
  return (
    <>
    <p>{props.menu.label}</p>
    <p>{props.menu.content}</p>
    </>
  );
}

export default function App() {

  const baseballCategory = {
        label: 'Baseball',
        content: <p>some content</p>
    };

    const basketballCategory = {
        label: 'Basketball',
        content: <p>some content</p>
    };

    const footballCategory = {
        label: 'Football',
        content: <p>some content</p>
    };

    const hockeyCategory = {
        label: 'Hockey',
        content: <p>some content</p>
    };
  
  const [activeCategoryMenuContent, setActiveCategoryMenuContent] = React.useState('baseball');
  
  const categoryMenuItemContentComponents = {
        "baseball": <CategoryMenuItemContent menu={baseballCategory} />,
        "basketball": <CategoryMenuItemContent menu={basketballCategory} />,
        "football": <CategoryMenuItemContent menu={footballCategory} />,
        "hockey": <CategoryMenuItemContent menu={hockeyCategory} />,
    };

  const [anchorEl, setAnchorEl] = useState(null);

  const handleClick = (event) => {
    setAnchorEl(anchorEl ? null : event.currentTarget);
    setActiveCategoryMenuContent(event.currentTarget.textContent.toLowerCase());
  };

  const open = Boolean(anchorEl);
  const id = open ? 'simple-popper' : undefined;

  return (
    <>
      <Button onClick={(event) => {handleClick(event);}}>Baseball</Button>
      <Button onClick={(event) => {handleClick(event);}}>Basketball</Button>
      <Button onClick={(event) => {handleClick(event);}}>Football</Button>
      <Button onClick={(event) => {handleClick(event);}}>Hockey</Button>
      <Popper id={id} open={open} anchorEl={anchorEl}>
      {activeCategoryMenuContent === 'baseball' && categoryMenuItemContentComponents['baseball']}
      {activeCategoryMenuContent === 'basketball' && categoryMenuItemContentComponents['basketball']}
      {activeCategoryMenuContent === 'football' && categoryMenuItemContentComponents['football']}
      {activeCategoryMenuContent === 'hockey' && categoryMenuItemContentComponents['hockey']}
      </Popper>
    </>
  );
}

>Solution :

This would make more sense:

const handleClick = (event) => {
  if (anchorEl === event.currentTarget) {
    setAnchorEl(null)
    setActiveCategoryMenuContent('')
  } else {
    setAnchorEl(event.currentTarget); 
    setActiveCategoryMenuContent(event.currentTarget.textContent.toLowerCase())
  }
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading