create modal language selector

This commit is contained in:
zahravaziri
2025-06-30 13:45:33 +03:30
parent 864d5c51dc
commit 1d5b25354d
2 changed files with 76 additions and 0 deletions
+2
View File
@@ -1,4 +1,5 @@
import Header from '@/components/layout/header'; import Header from '@/components/layout/header';
import ModalLanguageSelector from '@/components/layout/languageModal';
import AboutTopInfo from '@/components/pages/about/TopInfo'; import AboutTopInfo from '@/components/pages/about/TopInfo';
import HomePageBlog from '@/components/pages/home/HomePageBlog'; import HomePageBlog from '@/components/pages/home/HomePageBlog';
import HomeStorySection from '@/components/pages/home/HomeStorySection'; import HomeStorySection from '@/components/pages/home/HomeStorySection';
@@ -24,6 +25,7 @@ export default async function Home({ params }: { params: IPageParams }) {
const t = await getTranslations({ locale }); const t = await getTranslations({ locale });
return ( return (
<main className="flex flex-col items-center justify-between"> <main className="flex flex-col items-center justify-between">
<ModalLanguageSelector />
<Header /> <Header />
<AboutTopInfo /> <AboutTopInfo />
<HomePageProducts locale={locale} /> <HomePageProducts locale={locale} />
@@ -0,0 +1,74 @@
'use client';
import { useEffect, useState } from 'react';
import { TLocales } from '@/models/layout';
import Button, { IconButton } from '@/components/uikit/button';
import images from '@/assets/images/images';
import Image from 'next/image';
import { Icon } from '@/components/uikit/icons';
const languages = [
{
title: 'EN',
image: images.langEn,
locale: 'en',
},
{
title: 'FA',
image: images.langFa,
locale: 'fa',
},
];
export default function ModalLanguageSelector() {
const [showModal, setShowModal] = useState(false);
useEffect(() => {
const alreadySelected = localStorage.getItem('selectedLocale');
if (!alreadySelected) {
setShowModal(true);
}
}, []);
const handleCancel = () => {
setShowModal(false);
};
const changeLocale = (locale: TLocales) => {
localStorage.setItem('selectedLocale', locale);
const currentPath = window.location.pathname;
const newPath = currentPath.replace(/^\/(en|fa)/, '');
window.location.pathname = `/${locale}${newPath}`;
};
if (!showModal) return null;
return (
<div className="bg-opacity-60 fixed inset-0 z-50 flex items-center justify-center bg-black">
<div className="relative w-80 rounded-2xl bg-white p-6 text-center shadow-xl">
<IconButton
size="small"
onClick={handleCancel}
className="absolute end-3 top-0 mb-3"
>
<Icon name="close" className="h-5 w-5" />
</IconButton>
<h2 className="mb-4 text-lg font-semibold text-gray-800">
Select Language
</h2>
<div className="flex justify-around">
{languages.map((lang) => (
<Button
key={lang.locale}
onClick={() => changeLocale(lang.locale as TLocales)}
className="rounded-xl bg-blue-500 px-4 py-2 text-white transition hover:bg-blue-700"
>
<Image src={lang.image} alt={lang.title} />
</Button>
))}
</div>
</div>
</div>
);
}