Comments (6)
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
duringonClickOutside
- 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.
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.
A bit complicated, but hopefully it works 👍
from react-focus-on.
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.
@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.
Love it. Thanks!
from react-focus-on.
Related Issues (20)
- Allow `style` prop on <FocusOn /> HOT 1
- Use with react-transition-group doesn't clear aria-hidden attributes HOT 2
- Scroll lock prevents multi-touch zoom and back functionality HOT 6
- React-focus-lock dependency HOT 2
- Update aria-hidden to v. 1.1.3 to fix an IE11 incompatibility issue HOT 1
- Can't resolve tslib HOT 1
- Console error [Intervention] HOT 1
- Shadow DOM HOT 5
- How to preventDefault? HOT 4
- Heading, paragraph text are not selectable inside `FocusOn` HOT 3
- React@17 event delegation issue HOT 7
- Support passing `focusOptions` to `react-focus-lock`
- Upgrade react-focus-lock to 2.9.4 HOT 1
- `gapMode` prop/API is not actually used HOT 7
- "React does not recognize the `gapMode` prop on a DOM element" warning HOT 3
- Missing Prop `noFocusGuards` HOT 6
- iOS: keyboard popping up on input focus scrolls locked body HOT 3
- Bumping react-focus-lock to 2.9.4
- Adding support for `react-focus-lock` `crossFrame` prop HOT 2
- Prevent focus removal for external resources HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-focus-on.