Giter Site home page Giter Site logo

Comments (6)

theKashey avatar theKashey commented on May 28, 2024

Yeah, I hate this. Clicking external buttons, expecting this modal to be closed, is reopening it.
It's working quite simple - you click, the click got caught on capture phase, DOM got unblocked, and the initial click lands on the element you just clicked.

Known solutions:

  • delay lock deactivation, when "click" is ended. Just by one useEffect - would be enough.
  • prevent during onClickOutside - see #32. However, it does not always work, because that's not "React Event"
  • (my favourite) use shards to include "Profile" button to the lock. As a result, when you will click on it - it would not be "outside", and 👍

from react-focus-on.

zoontek avatar zoontek commented on May 28, 2024

prevent during onClickOutside did not worked for me.

What I finally found (using portals):

<div id="app-root">
  <!-- … -->
</div>

<div id="modals-root">
  <FocusOn>
    <!-- a div that cover the page entirelly -->
    <div style={{ position: "absolute", top: 0, left: 0, right: 0, bottom: 0 }} onClick={handleClose} />

    <div id="the-modal">
      <!-- … -->
    </div>
  </FocusOn>
</div>

from react-focus-on.

theKashey avatar theKashey commented on May 28, 2024

A bit complicated, but hopefully it works 👍

from react-focus-on.

pablen avatar pablen commented on May 28, 2024

I'm trying something like this. Basically I check if the click was inside the trigger element before closing the modal. If that's the case I do nothing and let the trigger element handler toggle the modal visibility.

const [referenceElement, setReferenceElement] = React.useState(null)
const [isVisible, setIsVisible] = React.useState(false)

<button
  aria-expanded={isVisible}
  onClick={() => setIsVisible(s => !s)}
  ref={setReferenceElement}
>
  Click here
</button>


<FocusOn
  onClickOutside={event => {
    if (
      !referenceElement.contains(event.target) &&
      referenceElement !== event.target
    ) {
      setIsVisible(false)
    }
  }}
>
  <Modal />
</FocusOn>

Seems to work fine, but I would appreciate any heads up.

from react-focus-on.

theKashey avatar theKashey commented on May 28, 2024

@pablen your code can be simplified a little:

const referenceElement = React.useRef(null);
const [isVisible, setIsVisible] = React.useState(false)

<button
  aria-expanded={isVisible}
  onClick={() => setIsVisible(s => !s)}
  ref={setReferenceElement}
>
  Click here
</button>


<FocusOn
  shards={[referenceElement]} // 👈 now that button is considered as a part of FocusOn
  onClickOutside={() =>  setIsVisible(false)}
>
  <Modal />
</FocusOn>

from react-focus-on.

pablen avatar pablen commented on May 28, 2024

Love it. Thanks!

from react-focus-on.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.