@salesforcedevs/docs-components
Advanced tools
Comparing version 1.3.327-do-dont-2 to 1.3.327-rnbtab-alpha
{ | ||
"name": "@salesforcedevs/docs-components", | ||
"version": "1.3.327-do-dont-2", | ||
"version": "1.3.327-rnbtab-alpha", | ||
"description": "Docs Lightning web components for DSC", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -14,2 +14,3 @@ /* eslint-disable @lwc/lwc/no-document-query */ | ||
const TOC_HEADER_TAG = "doc-heading"; | ||
const RNB_BY_TAB = "docs-tab"; | ||
const HIGHLIGHTABLE_SELECTOR = [ | ||
@@ -105,2 +106,58 @@ "p", | ||
get showTabBasedRNB() { | ||
const tabPanelListItem: any = this.getTabPanelList(); | ||
return tabPanelListItem?.id === RNB_BY_TAB ? true : false; | ||
} | ||
onTabChanged = () => { | ||
this.updateRNB(); | ||
}; | ||
private getTabPanelList() { | ||
return document.querySelector("dx-tab-panel-list"); | ||
} | ||
updateRNB = () => { | ||
const headingElements = this.getHeadingElements(); | ||
headingElements.forEach((headingElement: any) => { | ||
// Sometimes elements hash and header is not being set when slot content is wrapped with div | ||
headingElement.hash = headingElement.attributes.hash?.nodeValue; | ||
headingElement.header = headingElement.attributes.header?.nodeValue; | ||
}); | ||
this.updateTocItems(headingElements); | ||
}; | ||
private getHeadingElements() { | ||
let headingElements = document.querySelectorAll(TOC_HEADER_TAG); | ||
if (this.showTabBasedRNB) { | ||
const tabPanelListItem: any = this.getTabPanelList(); | ||
const tabPanels = | ||
tabPanelListItem?.querySelectorAll("dx-tab-panel"); | ||
for (const tabPanelItem of tabPanels) { | ||
if (tabPanelItem.active) { | ||
headingElements = | ||
tabPanelItem.querySelectorAll(TOC_HEADER_TAG); | ||
break; | ||
} | ||
} | ||
} | ||
return headingElements; | ||
} | ||
private updateURL() { | ||
const tabs = this.getAllTabs(); | ||
const selectedTabId = this.getSelectedTabId(); | ||
tabs.forEach((tab: any) => { | ||
if (tab.getAttribute("aria-selected") === "true") { | ||
const tabID = tab.getAttribute("aria-label"); | ||
const url = new URL(window.location.href); | ||
if (selectedTabId !== tabID) { | ||
url.searchParams.set("type", tabID); | ||
url.hash = ""; | ||
window.history.pushState({}, "", url.toString()); | ||
} | ||
} | ||
}); | ||
} | ||
private searchSyncer = new SearchSyncer({ | ||
@@ -153,4 +210,44 @@ callbacks: { | ||
} | ||
if (this.showTabBasedRNB) { | ||
window.addEventListener("tabchanged", this.onTabChanged); | ||
} | ||
} | ||
private getSelectedTabId() { | ||
const urlParams = new URLSearchParams(window.location.search); | ||
const selectedTabId = urlParams.get("type"); | ||
return selectedTabId; | ||
} | ||
private restoreTabSelection() { | ||
const selectedTabId = this.getSelectedTabId(); | ||
if (selectedTabId) { | ||
this.selectTabById(selectedTabId); | ||
} | ||
} | ||
private getAllTabs(): any[] { | ||
const tabPanelListItem: any = this.getTabPanelList(); | ||
if (tabPanelListItem?.shadowRoot) { | ||
return Array.from( | ||
tabPanelListItem.shadowRoot.querySelectorAll( | ||
"dx-tab-panel-item" | ||
) | ||
).map((tabPanelItem: any) => | ||
tabPanelItem.shadowRoot.querySelector("button") | ||
); | ||
} | ||
return []; | ||
} | ||
private selectTabById(tabId: string) { | ||
const tabs = this.getAllTabs(); | ||
tabs.forEach((tab: any) => { | ||
if (tab.getAttribute("aria-label") === tabId) { | ||
tab.click(); | ||
} | ||
}); | ||
} | ||
renderedCallback(): void { | ||
@@ -173,2 +270,5 @@ /** | ||
this.hasRendered = true; | ||
if (this.showTabBasedRNB) { | ||
this.restoreTabSelection(); | ||
} | ||
this.restoreScroll(); | ||
@@ -186,2 +286,3 @@ } | ||
window.removeEventListener("resize", this.adjustNavPosition); | ||
window.removeEventListener("tabchanged", this.onTabChanged); | ||
this.searchSyncer.dispose(); | ||
@@ -332,3 +433,9 @@ this.clearRenderObserverTimer(); | ||
// Note: We are doing document.querySelectorAll as a quick fix as we are not getting heading elements reference this.querySelectorAll | ||
const headingElements = document.querySelectorAll(TOC_HEADER_TAG); | ||
const headingElements = this.getHeadingElements(); | ||
// We only need to update URL in case of /docs and ignore if tabs are used anywhere else in DSC | ||
if (this.showTabBasedRNB) { | ||
this.updateURL(); | ||
} | ||
for (const headingElement of headingElements as any) { | ||
@@ -349,45 +456,30 @@ // Add headingElements to intersectionObserver for highlighting respective RNB item when user scroll | ||
onSlotChange(event: Event): void { | ||
const slotElements = ( | ||
event.target as HTMLSlotElement | ||
).assignedElements(); | ||
onSlotChange(): void { | ||
this.updateRNB(); | ||
} | ||
if (slotElements.length) { | ||
this.contentLoaded = true; | ||
const slotContentElement = slotElements[0]; | ||
const headingElements = | ||
slotContentElement.ownerDocument?.getElementsByTagName( | ||
TOC_HEADER_TAG | ||
); | ||
// eslint-disable-next-line no-undef | ||
private updateTocItems(headingElements: NodeListOf<Element>): void { | ||
const tocOptions = []; | ||
for (const headingElement of headingElements as any) { | ||
// Sometimes elements hash and header is not being set when slot content is wrapped with div | ||
headingElement.hash = headingElement.attributes.hash?.nodeValue; | ||
headingElement.header = | ||
headingElement.attributes.header?.nodeValue; | ||
} | ||
for (const headingElement of headingElements as any) { | ||
headingElement.id = headingElement.hash; | ||
const tocOptions = []; | ||
// Update tocOptions from anchorTags only for H2, consider default as 2 as per component | ||
const headingAriaLevel = | ||
headingElement.attributes["aria-level"]?.nodeValue || "2"; | ||
const isH2 = headingAriaLevel === "2"; | ||
for (const headingElement of headingElements as any) { | ||
headingElement.id = headingElement.hash; | ||
// Update tocOptions from anchorTags only for H2, consider default as 2 as per component | ||
const headingAriaLevel = | ||
headingElement.attributes["aria-level"]?.nodeValue || "2"; | ||
const isH2 = headingAriaLevel === "2"; | ||
if (isH2) { | ||
const tocItem = { | ||
anchor: `#${headingElement.hash}`, | ||
id: headingElement.id, | ||
label: headingElement.header | ||
}; | ||
tocOptions.push(tocItem); | ||
this.tocOptionIdsSet.add(headingElement.id); | ||
} | ||
if (isH2) { | ||
const tocItem = { | ||
anchor: `#${headingElement.hash}`, | ||
id: headingElement.id, | ||
label: headingElement.header | ||
}; | ||
tocOptions.push(tocItem); | ||
this.tocOptionIdsSet.add(headingElement.id); | ||
} | ||
} | ||
this._tocOptions = tocOptions; | ||
} | ||
this._tocOptions = tocOptions; | ||
} | ||
@@ -394,0 +486,0 @@ |
@@ -6,3 +6,3 @@ import { LightningElement, api } from "lwc"; | ||
@api caption: string = ""; | ||
@api imgSrc!: string; | ||
@api src!: string; | ||
_isDo: boolean = false; | ||
@@ -9,0 +9,0 @@ |
Sorry, the diff of this file is not supported yet
260250
6679