Skip to content

Commit a09fe26

Browse files
authored
WD-26684 - Fix bug with dropdown preventing clicks when closed (#5365)
* WD-26684 - Add listeners to fix issue with hidden elements * WD-26684 - Fix but with install button not clickable * WD-26684 - Fix linting
1 parent 0454456 commit a09fe26

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

static/js/base/global-nav.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,63 @@
11
import { createNav } from "@canonical/global-nav";
22

3+
/**
4+
* Add custom listeners to control animation of dropdowns and be able to remove the
5+
* display of some hidden elements from global-nav that block clicks for underneath
6+
* elements (see WD-26684).
7+
*
8+
* We must set these listeners before the global-nav sets its own listeners for them
9+
* to work properly.
10+
*/
11+
12+
const DROPDOWN_ANIMATION_DURATION = 333;
13+
const toggles = [
14+
...document.querySelectorAll(
15+
".p-navigation__nav .p-navigation__link[aria-controls]:not(.js-back-button)",
16+
),
17+
].filter((element) => element.id !== "all-canonical-link");
18+
19+
const toggleAnimationPlaying = (element: Element) => {
20+
const endAnimation = () => {
21+
element.classList.toggle("js-animation-playing", false);
22+
};
23+
24+
element.classList.toggle("js-animation-playing", true);
25+
setTimeout(endAnimation, DROPDOWN_ANIMATION_DURATION);
26+
};
27+
28+
const setAnimationPlaying = () => {
29+
// get all open toggles to add the animation playing to them
30+
toggles
31+
.filter(
32+
(toggle: Element): toggle is Element =>
33+
toggle.parentElement != null &&
34+
toggle.parentElement.classList.contains("is-active"),
35+
)
36+
.forEach((toggle: Element) => {
37+
toggleAnimationPlaying(toggle.parentElement!);
38+
});
39+
};
40+
41+
const handleToggle = (e: Event, toggle: Element) => {
42+
e.preventDefault();
43+
44+
const toggleParent = toggle.parentElement;
45+
if (toggleParent) {
46+
toggleAnimationPlaying(toggleParent);
47+
}
48+
49+
e.stopPropagation();
50+
};
51+
52+
toggles.forEach((toggle: Element) => {
53+
const handler = (e: Event) => handleToggle(e, toggle);
54+
toggle.addEventListener("click", handler);
55+
});
56+
57+
// when clicking outside navigation, set js-animation-playing on all open dropdowns
58+
document.addEventListener("click", setAnimationPlaying);
59+
60+
// Once our listeners are added then we run global-nav, which will add others
361
window.addEventListener("DOMContentLoaded", function () {
462
createNav({ isSliding: true, closeMenuAnimationDuration: 200 });
563
});

static/sass/_snapcraft_p-navigation.scss

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
.p-navigation__dropdown--container {
7070
// a bit extra % to be able to show the shadow of the box if present
7171
clip-path: rect(0 105% 105% 0);
72-
display: block;
72+
display: none;
7373
min-width: 100%;
7474
position: absolute;
7575

@@ -91,6 +91,11 @@
9191
}
9292
}
9393
}
94+
95+
.js-animation-playing .p-navigation__dropdown--container,
96+
.is-active .p-navigation__dropdown--container {
97+
display: block;
98+
}
9499
}
95100
}
96101
}

0 commit comments

Comments
 (0)