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

feat: save and restore grouped tabs selection #78

Merged
merged 13 commits into from
Mar 4, 2025
25 changes: 24 additions & 1 deletion src/runtime/TabsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ export class TabsController {
this.selectTab(tabs[newIndex]);
nodes[newIndex].focus();
});

this._document.addEventListener('DOMContentLoaded', () => {
this.restoreTabsPreferred();
});
}

onSelectTab(handler: Handler) {
Expand Down Expand Up @@ -163,6 +167,8 @@ export class TabsController {
return;
}

this.saveTabPreferred({group, key, variant});

const scrollableParent = targetTab && getClosestScrollableParent(targetTab);
const previousTargetOffset =
scrollableParent && getOffsetByScrollableParent(targetTab, scrollableParent);
Expand Down Expand Up @@ -193,8 +199,25 @@ export class TabsController {
return this.updateHTMLDropdown(tab);
}
}
}

return 0;
private saveTabPreferred(tab: Required<Tab>) {
const tabsHistory = JSON.parse(localStorage.getItem('tabsHistory') || '{}');
tabsHistory[tab.group] = {key: tab.key, variant: tab.variant};
localStorage.setItem('tabsHistory', JSON.stringify(tabsHistory));
}

private restoreTabsPreferred() {
const tabsHistory = JSON.parse(localStorage.getItem('tabsHistory') || '{}') as Record<
string,
{key: string; variant: TabsVariants}
>;
for (const [group, fields] of Object.entries(tabsHistory)) {
if (group) {
const tab = {group, ...fields};
this.selectTab(tab);
}
}
}

private updateHTMLRadio(tab: Required<Tab>, target: HTMLElement | undefined) {
Expand Down