useClickAway should support one or more elements
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- Mezza giornata
- Idoneità per principianti
- 48/100
- Tipo di issue
- Funzionalità
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Ferma
- Stack tecnologico
- react, typescript
- Ambito
- frontend
Direzione di ricerca
Read src/useClickAway.ts and compare its current single-ref API with the multiple-ref implementation described in the issue. Verify that a click invokes the callback only when it is outside every observed element, while the existing single-element behavior and toggle case remain intact.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
I have a case where a popover is supposed to be closed when I click anything outside the popover. The button which triggers the popover also works as a toggle, i. e. clicking it again closes the popover. This behavior is quite common and intuitive and should be preserved. Now, using useClickAway I can close the popover but when I try to do the same via toggling said button, the events interfere with each other and result in the popover being displayed again (the click outside closing it and the click on the button showing it again).
The apparent fix would be to allow useClickAway to receive one element or a list of elements and in the latter case only invoke the callback function if the click happened outside all observed elements.
I've adapted the useClickAway implementation to do this as follows and it works for my specific case:
import type { MutableRefObject } from "react";
import { useEffect, useRef } from "react";
import { off, on } from "react-use/lib/misc/util";
const defaultEvents = ["mousedown", "touchstart"];
// This is based on react-use's clickAway hook but made to work with multiple refs in addition to a single ref,
// meaning the click needs to happen outside all registered refs.
// See https://github.com/streamich/react-use/blob/master/src/useClickAway.ts for the original hook
export function useClickAway<
T extends Element | null = Element,
E extends Event = Event
>(
refs: Array<MutableRefObject<T>>,
onClickAway: (event: E) => void,
events: string[] = defaultEvents
): void {
const savedCallback = useRef(onClickAway);
const savedRefs = useRef(refs);
useEffect(() => {
savedCallback.current = onClickAway;
savedRefs.current = refs;
}, [onClickAway, refs]);
useEffect(() => {
const handler = (event: E) => {
const clickedOutside = savedRefs.current.every(ref => {
const { current: el } = ref;
return el && !el.contains(event.target as Node);
});
if (clickedOutside) {
savedCallback.current(event);
}
};
for (const eventName of events) {
on(document, eventName, handler);
}
return () => {
for (const eventName of events) {
off(document, eventName, handler);
}
};
}, [events]);
}
I'd appreciate it if something like that became available upstream so I could get rid of my custom implementation. I could also make a PR if that's increases the chance of it being made available upstream.
- Lingua principale
- TypeScript
- Stelle
- 44k
- Fork
- 3.3k
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di streamich/react-use
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
Add Generic Fn for useDebounceForse già presa @itsmejay80 l’ha presa 189 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 64/100
-
useVideo.story.tsx - sampleVideo can not be loadedForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 62/100
-
useSlider: event callbacks are not updatedForse già presa @erykpiast l’ha presa 1836 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
Tutte le issue di streamich/react-use
Issue simili
-
documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
components-web-app/docs#260 ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 65/100
521xueweihan/HelloGitHub#4025 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
ctrf-io/playwright-ctrf-json-reporter#117 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
voidzero-dev/void#478 ·
I maintainer di solito rispondono entro 1 giorno