Skip to content

Instantly share code, notes, and snippets.

@sjurgis
Created February 9, 2022 09:05
Show Gist options
  • Save sjurgis/b5f342d67dcbd62ff1b56854291b06eb to your computer and use it in GitHub Desktop.
Save sjurgis/b5f342d67dcbd62ff1b56854291b06eb to your computer and use it in GitHub Desktop.
lightning web component tabset with url navigation
<template>
<lightning-tabset variant="vertical" active-tab-value={activeTabValue}>
<lightning-tab label="Item One" value="one" onactive={handleActive}>
First
</lightning-tab>
<lightning-tab label="Item 2" value="two" onactive={handleActive}>
second
</lightning-tab>
<lightning-tab label="Item 3" value="three" onactive={handleActive}>
third
</lightning-tab>
</lightning-tabset>
</template>
import { LightningElement, wire } from 'lwc'
import { CurrentPageReference, NavigationMixin } from 'lightning/navigation'
export default class Main extends NavigationMixin(LightningElement) {
handleActive(event) {
event.preventDefault()
event.stopPropagation()
this[NavigationMixin.Navigate](
this.getUpdatedPageReference({
c__tab: event.target.value
}), true)
}
@wire(CurrentPageReference) currentPageReference
get activeTabValue() {
return this.currentPageReference?.state?.c__tab ?? 'two'
}
getUpdatedPageReference(stateChanges) {
return Object.assign({}, this.currentPageReference, {
state: Object.assign({}, this.currentPageReference.state, stateChanges)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment