|
| 1 | +--- |
| 2 | +import EnglishFlag from '@components/icons/flags/english.astro'; |
| 3 | +import SpanishFlag from '@components/icons/flags/spanish.astro'; |
| 4 | +
|
| 5 | +const LANGUAGES_SUPPORTED = [ |
| 6 | + { |
| 7 | + id: 'es', |
| 8 | + name: 'Español', |
| 9 | + flag: SpanishFlag, |
| 10 | + }, |
| 11 | + { |
| 12 | + id: 'en', |
| 13 | + name: 'English', |
| 14 | + flag: EnglishFlag, |
| 15 | + }, |
| 16 | +]; |
| 17 | +
|
| 18 | +const path = Astro.url.pathname; |
| 19 | +const currentLocale = LANGUAGES_SUPPORTED.find(({ id }) => path.startsWith(`/${id}`)) |
| 20 | + || LANGUAGES_SUPPORTED[0]; |
| 21 | +const LANGUAGES_FILTERED = LANGUAGES_SUPPORTED.filter( |
| 22 | + ({ id }) => id !== currentLocale.id, |
| 23 | +); |
| 24 | +--- |
| 25 | + |
| 26 | +<div class="custom-select"> |
| 27 | + <div class="select-selected"> |
| 28 | + <currentLocale.flag height={30} width={30} /> |
| 29 | + </div> |
| 30 | + <div class="select-items"> |
| 31 | + { |
| 32 | + LANGUAGES_FILTERED.map((lang) => ( |
| 33 | + <div class="language-option" data-lang-id={lang.id}> |
| 34 | + <lang.flag height={20} width={20} /> |
| 35 | + {lang.name} |
| 36 | + </div> |
| 37 | + )) |
| 38 | + } |
| 39 | + </div> |
| 40 | +</div> |
| 41 | + |
| 42 | +<style> |
| 43 | + .custom-select { |
| 44 | + position: relative; |
| 45 | + display: inline-block; |
| 46 | + width: fit-content; |
| 47 | + } |
| 48 | + |
| 49 | + .select-selected { |
| 50 | + padding: 10px; |
| 51 | + cursor: pointer; |
| 52 | + display: flex; |
| 53 | + align-items: center; |
| 54 | + transition: 0.3s ease; |
| 55 | + border-radius: 1rem; |
| 56 | + gap: 0.5rem; |
| 57 | + } |
| 58 | + |
| 59 | + .select-selected:hover { |
| 60 | + background-color: var(--bg-color-button-hover); |
| 61 | + } |
| 62 | + |
| 63 | + .select-items { |
| 64 | + display: none; |
| 65 | + position: absolute; |
| 66 | + z-index: 1; |
| 67 | + width: 100%; |
| 68 | + min-width: 10rem; |
| 69 | + right: 0; |
| 70 | + } |
| 71 | + |
| 72 | + .select-items div { |
| 73 | + padding: 10px; |
| 74 | + cursor: pointer; |
| 75 | + display: flex; |
| 76 | + align-items: center; |
| 77 | + gap: 0.5rem; |
| 78 | + } |
| 79 | + |
| 80 | + .select-items div:nth-child(1) { |
| 81 | + border-radius: 1rem 1rem 0 0; |
| 82 | + } |
| 83 | + |
| 84 | + .select-items div:last-child { |
| 85 | + border-radius: 0 0 1rem 1rem; |
| 86 | + } |
| 87 | + .select-items div:hover { |
| 88 | + background-color: var(--bg-color-button-hover); |
| 89 | + } |
| 90 | + |
| 91 | + .select-items:has(div:only-child) div { |
| 92 | + border-radius: 1rem; |
| 93 | + } |
| 94 | + |
| 95 | + .custom-select.active .select-items { |
| 96 | + display: block; |
| 97 | + border-radius: 1rem; |
| 98 | + border: 1px solid var(--border-color-nav); |
| 99 | + margin-top: 5px; |
| 100 | + } |
| 101 | +</style> |
| 102 | + |
| 103 | +<script> |
| 104 | + function changeLanguage(langId: string) { |
| 105 | + const currentPath = window.location.pathname; |
| 106 | + const hasLangPrefix = /^\/[a-z]{2}(\/|$)/.test(currentPath); |
| 107 | + |
| 108 | + let newPath; |
| 109 | + if (hasLangPrefix) { |
| 110 | + newPath = currentPath.replace(/^\/[a-z]{2}/, `/${langId}`); |
| 111 | + } else { |
| 112 | + newPath = `/${langId}${currentPath}`; |
| 113 | + } |
| 114 | + |
| 115 | + window.location.pathname = newPath; |
| 116 | + } |
| 117 | + |
| 118 | + document.addEventListener('DOMContentLoaded', () => { |
| 119 | + const selectSelected = document.querySelector('.select-selected'); |
| 120 | + const languageOptions = document.querySelectorAll('.language-option'); |
| 121 | + |
| 122 | + languageOptions.forEach((option) => { |
| 123 | + option.addEventListener('click', () => { |
| 124 | + const langId = option.getAttribute('data-lang-id'); |
| 125 | + langId && changeLanguage(langId); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + if (selectSelected) { |
| 130 | + selectSelected.addEventListener('click', () => { |
| 131 | + selectSelected.parentElement?.classList.toggle('active'); |
| 132 | + }); |
| 133 | + } |
| 134 | + }); |
| 135 | +</script> |
0 commit comments