Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
ClickAwayListener not working with Collapse or Fade transitions

I'm trying to create a notifications area. I show a notification icon, and when the user clicks on it, I show the list of notifications.

Here's a codesandbox

The problem is that I can't mix it with ClickAwayListener.

When I use ClickAwayListener it's not shown at all.

How should I fix this?

HeaderAction.js

import Tooltip from "@material-ui/core/Tooltip";
import Fade from "@material-ui/core/Fade";
import Collapse from "@material-ui/core/Collapse";
import React, { useState } from "react";
import ClickAwayListener from "@material-ui/core/ClickAwayListener";
import Icon from "@material-ui/core/Icon";

const HeaderAction = ({ icon, title, component }) => {
  const Component = component || (() => <div>NA</div>);

  const [showComponent, setShowComponent] = useState(false);

  const handleClick = () => {
    setShowComponent(!showComponent);
  };

  return (
    <>
      <Tooltip title={title || ""}>
        <div onClick={() => handleClick()}>
          <Icon>{icon}</Icon>
        </div>
      </Tooltip>

      {/* This part is not working */}
      {/* <ClickAwayListener onClickAway={() => setShowComponent(false)}>
        <div>
          <Fade in={showComponent}>
            <div>
              <Component />
            </div>
          </Fade>
        </div>
      </ClickAwayListener> */}

      <Fade in={showComponent}>
        <div>
          <Component />
        </div>
      </Fade>
    </>
  );
};

export { HeaderAction };

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

When you click the icon button, handleClick is called and the showComponent state is set to true, but then onClickAway from ClickAwayListener is also called and set the showComponent state to false again. The fix is simple, don't let the onClickAway handler execute by stopping the propagation after clicking the button:

<div
  onClick={(e) => {
    e.stopPropagation();
    handleClick();
  }}
>

Codesandbox Demo

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!