From f6f4da032ae453c3c75e2ea78ccdd6b8b0968012 Mon Sep 17 00:00:00 2001 From: binarygit <87677429+binarygit@users.noreply.github.com> Date: Mon, 29 Jul 2024 23:57:57 +0545 Subject: [PATCH] Fix: Side menu always displays (#3064) * Set correct cookie value after sidebar is clicked * Hide sidebar if cookie is not set * Fix: wrong cookie set if cookie is nil at init * Lint * Donot take cookies into account in mobile view * Open sidebar by default in desktop view * Hide sidebar by default in mobile view * Force reflow * fix toggleSidebar --------- Co-authored-by: Paul Bob --- app/components/avo/sidebar_component.html.erb | 2 +- app/controllers/avo/application_controller.rb | 13 ++++++++---- .../js/controllers/sidebar_controller.js | 20 ++++++++++++++++++- app/views/avo/partials/_navbar.html.erb | 3 ++- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/app/components/avo/sidebar_component.html.erb b/app/components/avo/sidebar_component.html.erb index 65a2c52bd9..22c568cf7c 100644 --- a/app/components/avo/sidebar_component.html.erb +++ b/app/components/avo/sidebar_component.html.erb @@ -1,5 +1,5 @@
diff --git a/app/controllers/avo/application_controller.rb b/app/controllers/avo/application_controller.rb index 8d5b52ba7d..ff330462a6 100644 --- a/app/controllers/avo/application_controller.rb +++ b/app/controllers/avo/application_controller.rb @@ -37,7 +37,7 @@ def exception_logger(exception) format.html { raise exception } format.json { render json: { - errors: exception.respond_to?(:record) && exception.record.present? ? exception.record.errors : [], + errors: (exception.respond_to?(:record) && exception.record.present?) ? exception.record.errors : [], message: exception.message, traces: exception.backtrace }, status: ActionDispatch::ExceptionWrapper.status_code_for_exception(exception.class.name) @@ -62,7 +62,7 @@ def resource_name begin request.path - .match(/\/?#{Avo.root_path.delete('/')}\/resources\/([a-z1-9\-_]*)\/?/mi) + .match(/\/?#{Avo.root_path.delete("/")}\/resources\/([a-z1-9\-_]*)\/?/mi) .captures .first rescue @@ -292,8 +292,13 @@ def set_force_locale(&action) end def set_sidebar_open - value = cookies["#{Avo::COOKIES_KEY}.sidebar.open"] - @sidebar_open = value.blank? || value == "1" + value = if cookies["#{Avo::COOKIES_KEY}.sidebar.open"].nil? + cookies["#{Avo::COOKIES_KEY}.sidebar.open"] = "1" + else + cookies["#{Avo::COOKIES_KEY}.sidebar.open"] + end + + @sidebar_open = value == "1" end # Set the current host for ActiveStorage diff --git a/app/javascript/js/controllers/sidebar_controller.js b/app/javascript/js/controllers/sidebar_controller.js index 0b8b1b929b..fe77aa2e7f 100644 --- a/app/javascript/js/controllers/sidebar_controller.js +++ b/app/javascript/js/controllers/sidebar_controller.js @@ -83,7 +83,25 @@ export default class extends Controller { } toggleSidebar() { + if (this.sidebarTarget.classList.contains('hidden')) { + this.sidebarTarget.classList.remove('hidden') + } + this.mainAreaTarget.classList.toggle('sidebar-open') + const value = Cookies.get(this.cookieKey) + Cookies.set(this.cookieKey, value === '1' ? '0' : '1') + } + + toggleSidebarOnMobile() { + if (this.mobileSidebarTarget.classList.contains('hidden')) { + this.mainAreaTarget.classList.remove('sidebar-open') + this.mobileSidebarTarget.classList.remove('hidden') + + // we force a reflow here because we remove then + // immediately add the sidebar-open class + // which doesn't give the browser enough time to apply the + // transistion. + this.mainAreaTarget.offsetHeight; + } this.mainAreaTarget.classList.toggle('sidebar-open') - Cookies.set(this.cookieKey, this.cookieKey === '1' ? '0' : '1') } } diff --git a/app/views/avo/partials/_navbar.html.erb b/app/views/avo/partials/_navbar.html.erb index 91fef3aa60..e8b18f4100 100644 --- a/app/views/avo/partials/_navbar.html.erb +++ b/app/views/avo/partials/_navbar.html.erb @@ -1,7 +1,8 @@ <%= content_tag :div, class: class_names("fixed bg-white p-2 w-full flex flex-shrink-0 items-center z-[100] px-4 lg:px-4 border-b space-x-4 lg:space-x-0 h-16", {"print:hidden": Avo.configuration.hide_layout_when_printing}) do %>
- <%= a_button icon: 'avo/menu', size: :xs, compact: true, style: :text, data: { action: 'click->sidebar#toggleSidebar' }, aria: {label: "Toggle sidebar"} %> + <%= a_button class: 'lg:hidden', icon: 'avo/menu', size: :xs, compact: true, style: :text, data: { action: 'click->sidebar#toggleSidebarOnMobile' }, aria: {label: "Toggle sidebar"} %> + <%= a_button class: 'hidden lg:block', icon: 'avo/menu', size: :xs, compact: true, style: :text, data: { action: 'click->sidebar#toggleSidebar' }, aria: {label: "Toggle sidebar"} %>
<%= render partial: "avo/partials/logo" %>