Detect click outside of the component (manage no language selection)

This commit is contained in:
Nicola Benaglia 2025-04-26 15:39:45 +02:00
parent 5ce06f403d
commit 53facb9f2c

View File

@ -1,4 +1,4 @@
import { useState } from 'react'; import { useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { supportedLanguages } from '../../../i18n'; import { supportedLanguages } from '../../../i18n';
import { Tooltip, useTheme } from '@mui/material'; import { Tooltip, useTheme } from '@mui/material';
@ -7,6 +7,7 @@ const LanguageSelector = () => {
const { i18n, t } = useTranslation(['core']); const { i18n, t } = useTranslation(['core']);
const [showSelect, setShowSelect] = useState(false); const [showSelect, setShowSelect] = useState(false);
const theme = useTheme(); const theme = useTheme();
const selectorRef = useRef(null);
const handleChange = (e) => { const handleChange = (e) => {
const newLang = e.target.value; const newLang = e.target.value;
@ -18,8 +19,23 @@ const LanguageSelector = () => {
const { name, flag } = const { name, flag } =
supportedLanguages[currentLang] || supportedLanguages['en']; supportedLanguages[currentLang] || supportedLanguages['en'];
// Detect clicks outside the component
useEffect(() => {
const handleClickOutside = (event) => {
if (selectorRef.current && !selectorRef.current.contains(event.target)) {
setShowSelect(false);
}
};
document.addEventListener('mousedown', handleClickOutside);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, []);
return ( return (
<div <div
ref={selectorRef}
style={{ style={{
bottom: '5%', bottom: '5%',
display: 'flex', display: 'flex',
@ -46,7 +62,7 @@ const LanguageSelector = () => {
}} }}
value={currentLang} value={currentLang}
onChange={handleChange} onChange={handleChange}
onBlur={() => setShowSelect(false)} autoFocus
> >
{Object.entries(supportedLanguages).map(([code, { name }]) => ( {Object.entries(supportedLanguages).map(([code, { name }]) => (
<option key={code} value={code}> <option key={code} value={code}>