Created
February 6, 2024 20:56
-
-
Save sevos/6b4544f9f0b66b87efc0c27cfc36190b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="categories" data-controller="persist-scroll-position" data-action="scroll->persist-scroll-position#persist:passive" class="flex-auto flex flex-col gap-4 overflow-y-auto"> | |
... | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Controller } from "@hotwired/stimulus" | |
export default class extends Controller { | |
connect() { | |
this.element.scrollTop = this.position || 0 | |
} | |
persist() { | |
this.position = this.element.scrollTop | |
} | |
get storeId() { | |
return `persisted-scroll-position-${this.element.id}` | |
} | |
set position(currentPosition) { | |
localStorage.setItem(this.storeId, currentPosition); | |
} | |
get position() { | |
return localStorage.getItem(this.storeId); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment