Skip to content

Commit 275b9f1

Browse files
bolt-botremydenton
authored andcommitted
fix: add a custom event to the nav-priority component to let others know when the component is ready to go (like the nav-indicator); update nav indicator logic to listen for this even before trying to animate if the nav-priority is a nested component AND isn't yet ready to go
1 parent 434d255 commit 275b9f1

File tree

2 files changed

+49
-35
lines changed

2 files changed

+49
-35
lines changed

packages/components/bolt-nav-indicator/nav-indicator.js

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -84,55 +84,60 @@ let gumshoeStateModule = (function () {
8484
return;
8585
}
8686

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) {
8889

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;
9293

93-
if (!priorityNav.isReady){
94-
return;
94+
if (originalTarget) {
95+
originalTargetHref = originalTarget.getAttribute('href');
96+
} else {
97+
originalTargetHref = nav.nav.getAttribute('href');
9598
}
96-
}
9799

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}"]`);
101102

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+
}
107110

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;
110114

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;
113118

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+
}
117123
}
118124

119-
// Prefer visible links over hidden links
120-
if (isVisible(linkInstance)) {
121-
normalizedTarget = linkInstance;
125+
const normalizedParent = normalizedTarget.parentNode;
122126

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+
}
126129

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();
130137
}
138+
} else {
139+
activateGumshoeLink();
131140
}
132-
133-
const normalizedParent = normalizedTarget.parentNode;
134-
135-
normalizedParent.activate();
136141
},
137142
});
138143
}

packages/components/bolt-nav-priority/nav-priority.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,15 @@ export class BoltNavPriority extends BoltComponent() {
205205
this.setAttribute('is-ready', '');
206206
this.classList.add('is-ready');
207207

208+
this.dispatchEvent(
209+
new CustomEvent('nav-priority:ready', {
210+
detail: {
211+
isReady: true,
212+
},
213+
bubbles: true,
214+
}),
215+
);
216+
208217
// make sure containerTabs exists first
209218
if (this.containerTabs) {
210219
this.containerTabs.classList.add('is-ready');

0 commit comments

Comments
 (0)