Skip to content

Commit a8bf174

Browse files
committed
Reapply active class when slot content changes
1 parent 0411935 commit a8bf174

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<slot onclick={handleClick}>
2+
<slot onclick={handleClick} onslotchange={handleSlotChange}>
33
<lightning-button variant={variant} label={label} title={title} class="slds-m-left_x-small"></lightning-button>
44
</slot>
55
</template>

force-app/main/default/lwc/link/link.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { LightningElement, api, track } from 'lwc';
22
import { REGISTER_ROUTER_EVENT_NAME, dispatchEvent, getRouteMatch } from 'c/lwcRouterUtil';
3+
34
export default class Link extends LightningElement {
45
@api label;
56
@api to = '*';
@@ -43,9 +44,31 @@ export default class Link extends LightningElement {
4344
this.active = this.currentPath === this.to;
4445
})
4546
}
47+
renderedCallback() {
48+
this.captureSlottedElement();
49+
}
50+
disconnectedCallback() {
51+
if (this.unsubscribe) {
52+
this.unsubscribe.unsubscribe();
53+
}
54+
}
55+
handleClick(e) {
56+
e.stopPropagation();
57+
if (this.to) {
58+
this.routerInstance.currentPath = this.to;
59+
this.currentPath = this.to;
60+
}
61+
}
62+
handleSlotChange() {
63+
this.captureSlottedElement();
64+
this.toggleActiveAttributes();
65+
}
4666
setCurrentPath() {
4767
this.currentPath = this.routerInstance.currentPath;
4868
}
69+
captureSlottedElement() {
70+
this._slottedElement = this.querySelector('*');
71+
}
4972
toggleActiveAttributes() {
5073
if (this.active) {
5174
this.setAttribute("active", "");
@@ -63,19 +86,4 @@ export default class Link extends LightningElement {
6386
if (!element) return;
6487
this.activeClass.forEach(activeClass => element.classList.remove(activeClass));
6588
}
66-
handleClick(e) {
67-
e.stopPropagation();
68-
if (this.to) {
69-
this.routerInstance.currentPath = this.to;
70-
this.currentPath = this.to;
71-
}
72-
}
73-
renderedCallback() {
74-
this._slottedElement = this.querySelector('*');
75-
}
76-
disconnectedCallback() {
77-
if (this.unsubscribe) {
78-
this.unsubscribe.unsubscribe();
79-
}
80-
}
8189
}

0 commit comments

Comments
 (0)