mirror of
https://github.com/Unleash/unleash.git
synced 2025-06-09 01:17:06 +02:00
fix: prevent blur when selecting text (#4762)
This commit is contained in:
parent
a50a483d0c
commit
4484615321
@ -114,7 +114,6 @@ export const Search = ({
|
|||||||
useKeyboardShortcut({ key: 'Escape' }, () => {
|
useKeyboardShortcut({ key: 'Escape' }, () => {
|
||||||
if (searchContainerRef.current?.contains(document.activeElement)) {
|
if (searchContainerRef.current?.contains(document.activeElement)) {
|
||||||
searchInputRef.current?.blur();
|
searchInputRef.current?.blur();
|
||||||
hideSuggestions();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
const placeholder = `${customPlaceholder ?? 'Search'} (${hotkey})`;
|
const placeholder = `${customPlaceholder ?? 'Search'} (${hotkey})`;
|
||||||
|
@ -5,24 +5,36 @@ export const useOnBlur = (
|
|||||||
callback: () => void
|
callback: () => void
|
||||||
): void => {
|
): void => {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
let mouseDownInside = false;
|
||||||
|
|
||||||
|
const handleMouseDown = (event: MouseEvent) => {
|
||||||
|
mouseDownInside =
|
||||||
|
containerRef.current?.contains(event.target as Node) || false;
|
||||||
|
};
|
||||||
|
|
||||||
const handleBlur = (event: FocusEvent) => {
|
const handleBlur = (event: FocusEvent) => {
|
||||||
// setTimeout is used because activeElement might not immediately be the new focused element after a blur event
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (
|
if (
|
||||||
|
!mouseDownInside &&
|
||||||
containerRef.current &&
|
containerRef.current &&
|
||||||
!containerRef.current.contains(document.activeElement)
|
!containerRef.current.contains(document.activeElement)
|
||||||
) {
|
) {
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
|
// Reset the flag for the next sequence of events.
|
||||||
|
mouseDownInside = false;
|
||||||
}, 0);
|
}, 0);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
document.addEventListener('mousedown', handleMouseDown);
|
||||||
|
|
||||||
const containerElement = containerRef.current;
|
const containerElement = containerRef.current;
|
||||||
if (containerElement) {
|
if (containerElement) {
|
||||||
containerElement.addEventListener('blur', handleBlur, true);
|
containerElement.addEventListener('blur', handleBlur, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
|
document.removeEventListener('mousedown', handleMouseDown);
|
||||||
if (containerElement) {
|
if (containerElement) {
|
||||||
containerElement.removeEventListener('blur', handleBlur, true);
|
containerElement.removeEventListener('blur', handleBlur, true);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user