Skip to content

Commit

Permalink
Ensuring language selector component is created anew when screen size…
Browse files Browse the repository at this point in the history
… change, to avoid css being wrong if language has been changed between changing screen sizes
  • Loading branch information
Kahera committed Jan 14, 2024
1 parent cb240e7 commit 8eeb709
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/components/NavbarComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ import { useDarkModeStore } from '@/stores/darkMode';
import Dropdown from '@/components/DropdownComponent.vue';
import Button from '@/components/ButtonComponent.vue';
import LanguageSelector from './LanguageSelector.vue';
import { onUnmounted, ref } from 'vue';
// Variables
const darkModeStore = useDarkModeStore();
// Screen size variable
const isSmallScreen = ref(false);
checkWindowSize();
function checkWindowSize() {
isSmallScreen.value = window.matchMedia('(max-width: 768px)').matches;
}
window.addEventListener('resize', () => checkWindowSize());
function getNavIcon(name: string | undefined) {
switch (name) {
Expand All @@ -26,6 +34,8 @@ function getNavIcon(name: string | undefined) {
}
}
// Destroy event listener on unmount
onUnmounted(() => window.removeEventListener('resize', () => checkWindowSize()));
</script>

<template>
Expand Down Expand Up @@ -56,7 +66,12 @@ function getNavIcon(name: string | undefined) {
</RouterLink>
</nav>

<LanguageSelector class="max-md:hidden w-28" />
<!-- Using if here to ensure new component is generated on resize,
in case of language change between size changes -->
<LanguageSelector
v-if="!isSmallScreen"
class="w-28"
/>

<!-- Dark/light mode for larger screens -->
<Button
Expand All @@ -67,8 +82,8 @@ function getNavIcon(name: string | undefined) {
class="max-md:hidden focus:outline-1 focus:outline-primary-light"
@click="darkModeStore.toggle()"
/>
<!-- Dropdown navigation for smaller screens -->
<nav class="md:hidden">
<!-- Dropdown navigation for smaller screens, see v-if explanation above LanguageSelector -->
<nav v-if="isSmallScreen">
<!-- Dropdown on small screens, link list on larger -->
<Dropdown
:position="'right'"
Expand Down

0 comments on commit 8eeb709

Please sign in to comment.