Skip to content

Commit f35b3b9

Browse files
authored
perf(tabs): scope tabpanel lookup to instance (#2358)
1 parent f2b5056 commit f35b3b9

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/Tabs/Tabs.svelte

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
const selectedContent = writable(undefined);
6868
6969
let refTabList = null;
70+
let refRoot = null;
7071
7172
/**
7273
* @type {(data: { id: string; label: string; disabled: boolean }) => void}
@@ -175,28 +176,30 @@
175176
176177
tabs.update((currentTabs) => {
177178
const tabsMap = new Map(currentTabs.map((tab) => [tab.id, tab]));
178-
179179
const reorderedTabs = domIds
180-
.map((id) => tabsMap.get(id))
181-
.filter((tab) => tab !== undefined)
182-
.map((tab, index) => ({ ...tab, index }));
180+
.map((id, index) => {
181+
const tab = tabsMap.get(id);
182+
return tab ? { ...tab, index } : undefined;
183+
})
184+
.filter(Boolean);
183185
184186
return reorderedTabs;
185187
});
186188
}
187189
188-
const contentElements = Array.from(
189-
document.querySelectorAll("[role='tabpanel']"),
190-
);
190+
const contentElements = refRoot?.parentElement
191+
? Array.from(refRoot.parentElement.querySelectorAll("[role='tabpanel']"))
192+
: [];
191193
const contentIds = contentElements.map((el) => el.id);
192194
193195
content.update((currentContent) => {
194196
const contentMap = new Map(currentContent.map((c) => [c.id, c]));
195-
196197
const reorderedContent = contentIds
197-
.map((id) => contentMap.get(id))
198-
.filter((c) => c !== undefined)
199-
.map((c, index) => ({ ...c, index }));
198+
.map((id, index) => {
199+
const c = contentMap.get(id);
200+
return c ? { ...c, index } : undefined;
201+
})
202+
.filter(Boolean);
200203
201204
return reorderedContent;
202205
});
@@ -233,6 +236,7 @@
233236
</script>
234237
235238
<div
239+
bind:this={refRoot}
236240
role="navigation"
237241
class:bx--tabs={true}
238242
class:bx--tabs--container={type === "container"}

0 commit comments

Comments
 (0)