1
1
import { LightningElement , api , track } from 'lwc' ;
2
2
import { REGISTER_ROUTER_EVENT_NAME , dispatchEvent , getRouteMatch } from 'c/lwcRouterUtil' ;
3
+
3
4
export default class Link extends LightningElement {
4
5
@api label ;
5
6
@api to = '*' ;
@@ -43,9 +44,31 @@ export default class Link extends LightningElement {
43
44
this . active = this . currentPath === this . to ;
44
45
} )
45
46
}
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
+ }
46
66
setCurrentPath ( ) {
47
67
this . currentPath = this . routerInstance . currentPath ;
48
68
}
69
+ captureSlottedElement ( ) {
70
+ this . _slottedElement = this . querySelector ( '*' ) ;
71
+ }
49
72
toggleActiveAttributes ( ) {
50
73
if ( this . active ) {
51
74
this . setAttribute ( "active" , "" ) ;
@@ -63,19 +86,4 @@ export default class Link extends LightningElement {
63
86
if ( ! element ) return ;
64
87
this . activeClass . forEach ( activeClass => element . classList . remove ( activeClass ) ) ;
65
88
}
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
- }
81
89
}
0 commit comments