Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix jumpiness on tab change #1841

Merged
merged 1 commit into from
Sep 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions static/js/tabpane-persist.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const _tdSupportsLocalStorage = () => typeof Storage !== 'undefined';
// Helpers
function tdPersistKey(key, value) {
// @requires: tdSupportsLocalStorage();
console.log("key", key, "value", value);
try {
if (value) {
localStorage.setItem(key, value);
Expand Down Expand Up @@ -36,10 +35,15 @@ function tdGetTabSelectEventCountAndInc() {

// Main functions

function tdActivateTabsWithKey(key) {
function tdActivateTabsWithKey(key, clicked_element, rectangle) {
if (!key) return;

document.querySelectorAll(`.nav-tabs > .nav-item > a[data-td-tp-persist='${key}']`).forEach((element) => {
document.querySelectorAll(`.nav-tabs > .nav-item > a[data-td-tp-persist='${key}']`).forEach((element) => {
if (element == clicked_element) {
// Ensures that if the dom changes above the element, the user's view
// doesn't jump
window.scrollBy(0, element.getBoundingClientRect().top - rectangle.top);
}
new bootstrap.Tab(element).show();
});
}
Expand All @@ -51,7 +55,6 @@ function tdPersistActiveTab(activeTabKey) {
_tdStoragePersistKey(activeTabKey),
tdGetTabSelectEventCountAndInc()
);
tdActivateTabsWithKey(activeTabKey);
}

// Handlers
Expand Down Expand Up @@ -86,10 +89,10 @@ function tdGetAndActivatePersistedTabs(tabs) {

function tdRegisterTabClickHandler(tabs) {
tabs.forEach((tab) => {
tab.addEventListener('click', () => {
tab.addEventListener('click', (event) => {
const activeTabKey = tab.getAttribute("data-td-tp-persist");
tdPersistActiveTab(activeTabKey);
tdActivateTabsWithKey(activeTabKey)
tdActivateTabsWithKey(activeTabKey, event.srcElement, event.srcElement.getBoundingClientRect())
});
});
}
Expand Down