Skip to content

Commit

Permalink
Fix menu scroll position (#133)
Browse files Browse the repository at this point in the history
* Fix menu scroll position
* Move js to stimulus controller
  • Loading branch information
stephannv authored Nov 11, 2024
1 parent 0decfa4 commit e1bd150
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
3 changes: 3 additions & 0 deletions app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ application.register("color-theme", ColorThemeController)

import LottieController from "./lottie_controller"
application.register("lottie", LottieController)

import SidebarMenuController from "./sidebar_menu_controller"
application.register("sidebar-menu", SidebarMenuController)
14 changes: 14 additions & 0 deletions app/javascript/controllers/sidebar_menu_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="sidebar-menu"
export default class extends Controller {
connect() {
window.addEventListener("turbo:before-visit", () => {
localStorage.setItem("menuScrollPositon", this.element.scrollTop);
});

window.addEventListener("turbo:load", () => {
this.element.scrollTop = localStorage.getItem("menuScrollPositon") || 0;
});
}
}
2 changes: 1 addition & 1 deletion app/views/components/shared/sidebar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Shared::Sidebar < ApplicationComponent
def view_template
aside(class: "fixed top-14 z-30 -ml-2 hidden h-[calc(100vh-3.5rem)] w-full shrink-0 md:sticky md:block") do
div(class: "relative overflow-hidden h-full py-6 pl-8 pr-6 lg:py-8", style: "position:relative;--radix-scroll-area-corner-width:0px;--radix-scroll-area-corner-height:0px") do
div(class: "h-full w-full rounded-[inherit]", style: "overflow: hidden scroll;") do
div(class: "h-full w-full rounded-[inherit]", style: "overflow: hidden scroll;", data_controller: "sidebar-menu") do
render Shared::Menu.new
end
end
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": "true",
"dependencies": {
"@hotwired/stimulus": "3.2.2",
"@hotwired/turbo-rails": "8.0.5",
"@hotwired/turbo-rails": "8.0.12",
"autoprefixer": "10.4.19",
"chart.js": "4.4.3",
"class-variance-authority": "0.7.0",
Expand Down
12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@
resolved "https://registry.yarnpkg.com/@hotwired/stimulus/-/stimulus-3.2.2.tgz#071aab59c600fed95b97939e605ff261a4251608"
integrity sha512-eGeIqNOQpXoPAIP7tC1+1Yc1yl1xnwYqg+3mzqxyrbE5pg5YFBZcA6YoTiByJB6DKAEsiWtl6tjTJS4IYtbB7A==

"@hotwired/[email protected].5":
version "8.0.5"
resolved "https://registry.yarnpkg.com/@hotwired/turbo-rails/-/turbo-rails-8.0.5.tgz#18c2f0e4f7f952307650308590edf5eb9544b0d3"
integrity sha512-1A9G9u28IRAl0C57z8Ka3AhNPyJdwfOrbjr+ABZk2ZEUw2QO7cJ0pgs77asUj2E/tzn1PgrxrSVu24W+1Q5uBA==
"@hotwired/[email protected].12":
version "8.0.12"
resolved "https://registry.yarnpkg.com/@hotwired/turbo-rails/-/turbo-rails-8.0.12.tgz#6f1a2661122c0a2bf717f3bc68b5106638798c89"
integrity sha512-ZXwu9ez+Gd4RQNeHIitqOQgi/LyqY8J4JqsUN0nnYiZDBRq7IreeFdMbz29VdJpIsmYqwooE4cFzPU7QvJkQkA==
dependencies:
"@hotwired/turbo" "^8.0.5"
"@hotwired/turbo" "^8.0.12"
"@rails/actioncable" "^7.0"

"@hotwired/turbo@^8.0.5":
"@hotwired/turbo@^8.0.12":
version "8.0.12"
resolved "https://registry.yarnpkg.com/@hotwired/turbo/-/turbo-8.0.12.tgz#50aa8345d7f62402680c6d2d9814660761837001"
integrity sha512-l3BiQRkD7qrnQv6ms6sqPLczvwbQpXt5iAVwjDvX0iumrz6yEonQkNAzNjeDX25/OJMFDTxpHjkJZHGpM9ikWw==
Expand Down

0 comments on commit e1bd150

Please sign in to comment.