From 5041a105338f0d4f7a8da99ea69f5a2ff140d978 Mon Sep 17 00:00:00 2001 From: Ramesh Mane <101566080+rameshmane7218@users.noreply.github.com> Date: Fri, 5 Jul 2024 16:43:13 +0530 Subject: [PATCH] Fix: Persist the left sidebar width and use pixels instead of percentage to configure the sidebar width (#8931) * fix(nc-gui): use px to configure left sidebar width instead of percentage * fix(nc-gui): ai review changes * fix(nc-gui): use 256px sidebar default width in pw testing * fix: use constant sidebar width * fix(test): airtable import test fail issue * chore(test): remove console * chore(nc-gui): update comment --- packages/nc-gui/components/dashboard/View.vue | 63 ++++++++++--------- .../nc-gui/composables/useGlobal/actions.ts | 5 ++ .../nc-gui/composables/useGlobal/state.ts | 2 + .../nc-gui/composables/useGlobal/types.ts | 2 + packages/nc-gui/lib/constants.ts | 2 + packages/nc-gui/store/sidebar.ts | 29 ++++++--- .../pages/Dashboard/Grid/Column/index.ts | 5 +- tests/playwright/quickTests/commonTest.ts | 8 ++- 8 files changed, 75 insertions(+), 41 deletions(-) diff --git a/packages/nc-gui/components/dashboard/View.vue b/packages/nc-gui/components/dashboard/View.vue index 429db70c3a..49478ad30b 100644 --- a/packages/nc-gui/components/dashboard/View.vue +++ b/packages/nc-gui/components/dashboard/View.vue @@ -5,6 +5,8 @@ import 'splitpanes/dist/splitpanes.css' const router = useRouter() const route = router.currentRoute +const { setLeftSidebarSize } = useGlobal() + const { isMobileMode } = storeToRefs(useConfigStore()) const { @@ -30,22 +32,32 @@ const currentSidebarSize = computed({ const { handleSidebarOpenOnMobileForNonViews } = useConfigStore() -const contentSize = computed(() => 100 - sideBarSize.value.current) - const mobileNormalizedContentSize = computed(() => { if (isMobileMode.value) { return isLeftSidebarOpen.value ? 0 : 100 } - return contentSize.value + return 100 - leftSidebarWidthPercent.value }) -const sidebarWidth = computed(() => - isMobileMode.value ? viewportWidth.value : (sideBarSize.value.old * viewportWidth.value) / 100, -) - watch(currentSidebarSize, () => { - leftSidebarWidthPercent.value = currentSidebarSize.value + leftSidebarWidthPercent.value = (currentSidebarSize.value / viewportWidth.value) * 100 + setLeftSidebarSize(currentSidebarSize.value) +}) + +const sidebarWidth = computed(() => (isMobileMode.value ? viewportWidth.value : sideBarSize.value.old)) + +const normalizedWidth = computed(() => { + const maxSize = remToPx(viewportWidth.value <= 1560 ? 20 : 35) + const minSize = remToPx(16) + + if (sidebarWidth.value > maxSize) { + return maxSize + } else if (sidebarWidth.value < minSize) { + return minSize + } else { + return sidebarWidth.value + } }) watch(isLeftSidebarOpen, () => { @@ -87,10 +99,15 @@ function handleMouseMove(e: MouseEvent) { } } -function onWindowResize() { +function onWindowResize(e?: any): void { viewportWidth.value = window.innerWidth - onResize(currentSidebarSize.value) + leftSidebarWidthPercent.value = (currentSidebarSize.value / viewportWidth.value) * 100 + + // if sidebar width is greater than normalized width and this function is called from window resize event (not from template) update left sidebar width + if (e && normalizedWidth.value < sidebarWidth.value) { + onResize(leftSidebarWidthPercent.value) + } } onMounted(() => { @@ -138,10 +155,9 @@ function onResize(widthPercent: any) { const fontSize = parseFloat(getComputedStyle(document.documentElement).fontSize) // If the viewport width is less than 1560px, the max sidebar width should be 20rem - if (viewportWidth.value <= 1560) { if (width > remToPx(20)) { - sideBarSize.value.old = ((20 * fontSize) / viewportWidth.value) * 100 + sideBarSize.value.old = 20 * fontSize if (isLeftSidebarOpen.value) sideBarSize.value.current = sideBarSize.value.old return } @@ -150,31 +166,19 @@ function onResize(widthPercent: any) { const widthRem = width / fontSize if (widthRem < 16) { - sideBarSize.value.old = ((16 * fontSize) / viewportWidth.value) * 100 + sideBarSize.value.old = 16 * fontSize if (isLeftSidebarOpen.value) sideBarSize.value.current = sideBarSize.value.old return } else if (widthRem > 35) { - sideBarSize.value.old = ((35 * fontSize) / viewportWidth.value) * 100 + sideBarSize.value.old = 35 * fontSize if (isLeftSidebarOpen.value) sideBarSize.value.current = sideBarSize.value.old return } - sideBarSize.value.old = widthPercent + sideBarSize.value.old = width sideBarSize.value.current = sideBarSize.value.old } - -const normalizedWidth = computed(() => { - const maxSize = remToPx(35) - const minSize = remToPx(16) - if (sidebarWidth.value > maxSize) { - return maxSize - } else if (sidebarWidth.value < minSize) { - return minSize - } else { - return sidebarWidth.value - } -})