useClickAway should support one or more elements
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- Medio día
- Aptitud para principiantes
- 48/100
- Tipo de issue
- Nueva funcionalidad
- Claridad
- Bien especificado
- Estado de actividad
- Estancado
- Stack tecnológico
- react, typescript
- Área
- frontend
Línea de trabajo
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.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- TypeScript
- Estrellas
- 44k
- Forks
- 3.3k
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de streamich/react-use
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
-
Add Generic Fn for useDebouncePosiblemente ocupada @itsmejay80 la tomó hace 189 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 64/100
-
useVideo.story.tsx - sampleVideo can not be loadedQuizá libre de nuevo Un pull request para esta issue se cerró sin fusionarse. Abierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 62/100
-
useSlider: event callbacks are not updatedPosiblemente ocupada @erykpiast la tomó hace 1836 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
Todos los issues de streamich/react-use
Issues similares
-
documentation
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
components-web-app/docs#260 ·
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 65/100
521xueweihan/HelloGitHub#4025 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
ctrf-io/playwright-ctrf-json-reporter#117 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
voidzero-dev/void#478 ·
Los mantenedores suelen responder en 1 día