Skip to content

Commit 54ce5fa

Browse files
Merge pull request #8611 from christianbeeznest/ofaj-23519
Display: Close sidebar menu on mobile navigation - refs BT#23519
2 parents 4ebcf1a + c60ef3e commit 54ce5fa

1 file changed

Lines changed: 58 additions & 5 deletions

File tree

assets/vue/components/layout/Sidebar.vue

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@
8484
</template>
8585

8686
<script setup>
87-
import { onMounted, ref, watch, computed } from "vue"
87+
import { computed, onBeforeUnmount, onMounted, ref, watch } from "vue"
8888
import ToggleButton from "primevue/togglebutton"
8989
import { useI18n } from "vue-i18n"
90+
import { useRoute } from "vue-router"
9091
import { useSecurityStore } from "../../store/securityStore"
9192
import { useSidebarMenu } from "../../composables/sidebarMenu"
9293
import { usePlatformConfig } from "../../store/platformConfig"
@@ -97,18 +98,22 @@ import BaseSidebarPanelMenu from "../basecomponents/BaseSidebarPanelMenu.vue"
9798
import CategoryLinks from "../page/CategoryLinks.vue"
9899
99100
const { t } = useI18n()
101+
const route = useRoute()
100102
const securityStore = useSecurityStore()
101103
const enrolledStore = useEnrolledStore()
102104
const platformConfigStore = usePlatformConfig()
103105
104106
const { menuItemsBeforeMyCourse, menuItemMyCourse, menuItemsAfterMyCourse, hasOnlyOneItem, initialize } =
105107
useSidebarMenu()
106108
107-
const isMobile = () => window.innerWidth < 640
109+
const MOBILE_BREAKPOINT = 640
110+
111+
const isMobile = () => window.innerWidth < MOBILE_BREAKPOINT
108112
109113
const storedSidebarState = window.localStorage.getItem("sidebarIsOpen")
110114
111115
const sidebarIsOpen = ref(isMobile() ? false : storedSidebarState === null ? true : storedSidebarState === "true")
116+
const wasMobileViewport = ref(isMobile())
112117
113118
if (!isMobile() && storedSidebarState === null) {
114119
window.localStorage.setItem("sidebarIsOpen", "true")
@@ -150,7 +155,10 @@ watch(
150155
if (!isMobile()) {
151156
window.localStorage.setItem("sidebarIsOpen", newValue.toString())
152157
}
153-
appEl.classList.toggle("app--sidebar-inactive", !newValue)
158+
159+
if (appEl) {
160+
appEl.classList.toggle("app--sidebar-inactive", !newValue)
161+
}
154162
155163
if (!newValue) {
156164
if (!expandingDueToPanelClick.value) {
@@ -222,6 +230,38 @@ function handleLogoutClick(event) {
222230
}
223231
}
224232
233+
function closeSidebarOnMobile() {
234+
if (isMobile()) {
235+
sidebarIsOpen.value = false
236+
}
237+
}
238+
239+
function getStoredDesktopSidebarState() {
240+
const storedState = window.localStorage.getItem("sidebarIsOpen")
241+
242+
if (storedState === null) {
243+
window.localStorage.setItem("sidebarIsOpen", "true")
244+
245+
return true
246+
}
247+
248+
return storedState === "true"
249+
}
250+
251+
function handleViewportResize() {
252+
const mobile = isMobile()
253+
254+
if (mobile && !wasMobileViewport.value) {
255+
sidebarIsOpen.value = false
256+
}
257+
258+
if (!mobile && wasMobileViewport.value) {
259+
sidebarIsOpen.value = getStoredDesktopSidebarState()
260+
}
261+
262+
wasMobileViewport.value = mobile
263+
}
264+
225265
const handlePanelHeaderClick = (event) => {
226266
const header = event.target.closest(".p-panelmenu-header")
227267
@@ -239,15 +279,28 @@ const handlePanelHeaderClick = (event) => {
239279
}
240280
}
241281
242-
if (isMobile() && event.target.closest("a[href]")) {
243-
sidebarIsOpen.value = false
282+
if (event.target.closest("a[href]")) {
283+
closeSidebarOnMobile()
244284
}
245285
}
246286
287+
watch(
288+
() => route.fullPath,
289+
() => {
290+
closeSidebarOnMobile()
291+
},
292+
)
293+
247294
onMounted(async () => {
295+
window.addEventListener("resize", handleViewportResize)
296+
248297
if (securityStore.isAuthenticated && !isAnonymous.value) {
249298
await initialize()
250299
externalLogoutBehaviour.value = await fetchExternalLogoutBehaviour()
251300
}
252301
})
302+
303+
onBeforeUnmount(() => {
304+
window.removeEventListener("resize", handleViewportResize)
305+
})
253306
</script>

0 commit comments

Comments
 (0)