@@ -84,55 +84,60 @@ let gumshoeStateModule = (function () {
84
84
return ;
85
85
}
86
86
87
- // if this there's a <bolt-nav-priority> instance, make sure that component's ready to go before proceeding trying to animate anything.
87
+ // logic once we know we should try to animate in a gumshoe-activated link
88
+ function activateGumshoeLink ( waitedToAnimate = false ) {
88
89
89
- // @todo : iterate on a more refined solution so we animate in ASAP
90
- if ( nav . nav . closest ( 'bolt-nav-priority' ) ) {
91
- const priorityNav = nav . nav . closest ( 'bolt-nav-priority' ) ;
90
+ const originalTarget = nav . nav ;
91
+ let originalTargetHref ;
92
+ let normalizedTarget ;
92
93
93
- if ( ! priorityNav . isReady ) {
94
- return ;
94
+ if ( originalTarget ) {
95
+ originalTargetHref = originalTarget . getAttribute ( 'href' ) ;
96
+ } else {
97
+ originalTargetHref = nav . nav . getAttribute ( 'href' ) ;
95
98
}
96
- }
97
99
98
- const originalTarget = nav . nav ;
99
- let originalTargetHref ;
100
- let normalizedTarget ;
100
+ // Need to target via document vs this custom element reference since only one gumshoe instance is shared across every component instance to better optimize for performance
101
+ const matchedTargetLinks = document . querySelectorAll ( `bolt-navlink > [href*="${ originalTargetHref } "]` ) ;
101
102
102
- if ( originalTarget ) {
103
- originalTargetHref = originalTarget . getAttribute ( 'href' ) ;
104
- } else {
105
- originalTargetHref = nav . nav . getAttribute ( 'href' ) ;
106
- }
103
+ for ( var i = 0 , len = matchedTargetLinks . length ; i < len ; i ++ ) {
104
+ const linkInstance = matchedTargetLinks [ i ] ;
105
+
106
+ // Stop if normalizedTarget already set.
107
+ if ( normalizedTarget ) {
108
+ break ;
109
+ }
107
110
108
- // Need to target via document vs this custom element reference since only one gumshoe instance is shared across every component instance to better optimize for performance
109
- const matchedTargetLinks = document . querySelectorAll ( `bolt-navlink > [href*="${ originalTargetHref } "]` ) ;
111
+ // Prefer visible links over hidden links
112
+ if ( isVisible ( linkInstance ) ) {
113
+ normalizedTarget = linkInstance ;
110
114
111
- for ( var i = 0 , len = matchedTargetLinks . length ; i < len ; i ++ ) {
112
- const linkInstance = matchedTargetLinks [ i ] ;
115
+ // Prefer dropdown links over non-dropdown links if the link is hidden
116
+ } else if ( linkInstance . parentNode . isDropdownLink ) {
117
+ normalizedTarget = linkInstance ;
113
118
114
- // Stop if normalizedTarget already set.
115
- if ( normalizedTarget ) {
116
- break ;
119
+ // otherwise default to what was originally selected.
120
+ } else if ( i === len - 1 ) {
121
+ normalizedTarget = originalTarget ;
122
+ }
117
123
}
118
124
119
- // Prefer visible links over hidden links
120
- if ( isVisible ( linkInstance ) ) {
121
- normalizedTarget = linkInstance ;
125
+ const normalizedParent = normalizedTarget . parentNode ;
122
126
123
- // Prefer dropdown links over non-dropdown links if the link is hidden
124
- } else if ( linkInstance . parentNode . isDropdownLink ) {
125
- normalizedTarget = linkInstance ;
127
+ normalizedParent . activate ( ) ;
128
+ }
126
129
127
- // otherwise default to what was originally selected.
128
- } else if ( i === len - 1 ) {
129
- normalizedTarget = originalTarget ;
130
+ // if this there's a <bolt-nav-priority> instance, make sure that component's ready to go before proceeding trying to animate anything.
131
+ if ( nav . nav . closest ( 'bolt-nav-priority' ) ) {
132
+ const priorityNav = nav . nav . closest ( 'bolt-nav-priority' ) ;
133
+ if ( ! priorityNav . isReady ) {
134
+ document . addEventListener ( 'nav-priority:ready' , activateGumshoeLink ( true ) ) ;
135
+ } else {
136
+ activateGumshoeLink ( ) ;
130
137
}
138
+ } else {
139
+ activateGumshoeLink ( ) ;
131
140
}
132
-
133
- const normalizedParent = normalizedTarget . parentNode ;
134
-
135
- normalizedParent . activate ( ) ;
136
141
} ,
137
142
} ) ;
138
143
}
0 commit comments