1- import { useState , useEffect } from "preact/hooks" ;
2-
3- const AnchorLang = ( ) => {
4- const [ lang , setLang ] = useState ( "" ) ;
5- const [ switchLang , setSwitchLang ] = useState ( {
6- lang : "es" ,
7- href : "/" ,
8- } ) ;
9- useEffect ( ( ) => {
10- const userLang = navigator . language || navigator . userLanguage ;
11- setLang ( userLang . split ( "-" ) . at ( 0 ) || "es" ) ;
12- const currentPath = window . location . pathname ;
13- const isEnglish = currentPath . startsWith ( "/en" ) ;
14- setSwitchLang ( {
15- lang : isEnglish ? "es" : "en" ,
16- href : isEnglish ? "/" : "/en" ,
17- } ) ;
18-
19- } , [ ] ) ;
20- return (
21- < a href = { switchLang ?. href } class = "fixed bottom-10 right-10 z-50" >
22- < span class = "bg-slate-300/50 text-pink-600 backdrop-blur-sm inline-flex text-xl p-3 font-bold rounded-full justify-center items-center aspect-square min-w-5 min-h-5 size-[48px]" >
23- { switchLang ?. lang }
1+ interface AnchorLangProps {
2+ lang : string ;
3+ languages : string [ ] ;
4+ }
5+ const AnchorLang = ( { lang : currentLang , languages } : AnchorLangProps ) => {
6+ return (
7+ < ul class = "fixed bottom-10 right-10 z-50 bg-slate-300/40 dark:bg-slate-700/40 backdrop-blur-sm rounded-lg px-2 py-1.5" >
8+ { languages . map ( ( lang : string , index : number ) => {
9+ const isActive = lang === ( currentLang ?? "es" ) ;
10+ < li key = { index } >
11+ < a
12+ href = { `/${ lang } ` }
13+ class = "flex flex-col justify-center items-center"
14+ >
15+ < span
16+ class = { `${
17+ isActive ? "bg-white/70 dark:bg-black/70" : ""
18+ } text-black/60 dark:text-white/60 inline-flex text-xl px-2 py-1.5 font-bold rounded-md justify-center items-center aspect-video max-w-min`}
19+ >
20+ { lang }
2421 </ span >
25- </ a >
26- )
22+ </ a >
23+ </ li > ;
24+ } ) }
25+ </ ul >
26+ ) ;
2727} ;
28- export default AnchorLang ;
28+ export default AnchorLang ;
0 commit comments