Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions static/js/base/global-nav.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,63 @@
import { createNav } from "@canonical/global-nav";

/**
* Add custom listeners to control animation of dropdowns and be able to remove the
* display of some hidden elements from global-nav that block clicks for underneath
* elements (see WD-26684).
*
* We must set these listeners before the global-nav sets its own listeners for them
* to work properly.
*/

const DROPDOWN_ANIMATION_DURATION = 333;
const toggles = [
...document.querySelectorAll(
".p-navigation__nav .p-navigation__link[aria-controls]:not(.js-back-button)",
),
].filter((element) => element.id !== "all-canonical-link");

const toggleAnimationPlaying = (element: Element) => {
const endAnimation = () => {
element.classList.toggle("js-animation-playing", false);
};

element.classList.toggle("js-animation-playing", true);
setTimeout(endAnimation, DROPDOWN_ANIMATION_DURATION);
};

const setAnimationPlaying = () => {
// get all open toggles to add the animation playing to them
toggles
.filter(
(toggle: Element): toggle is Element =>
toggle.parentElement != null &&
toggle.parentElement.classList.contains("is-active"),
)
.forEach((toggle: Element) => {
toggleAnimationPlaying(toggle.parentElement!);
});
};

const handleToggle = (e: Event, toggle: Element) => {
e.preventDefault();

const toggleParent = toggle.parentElement;
if (toggleParent) {
toggleAnimationPlaying(toggleParent);
}

e.stopPropagation();
};

toggles.forEach((toggle: Element) => {
const handler = (e: Event) => handleToggle(e, toggle);
toggle.addEventListener("click", handler);
});

// when clicking outside navigation, set js-animation-playing on all open dropdowns
document.addEventListener("click", setAnimationPlaying);

// Once our listeners are added then we run global-nav, which will add others
window.addEventListener("DOMContentLoaded", function () {
createNav({ isSliding: true, closeMenuAnimationDuration: 200 });
});
7 changes: 6 additions & 1 deletion static/sass/_snapcraft_p-navigation.scss
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
.p-navigation__dropdown--container {
// a bit extra % to be able to show the shadow of the box if present
clip-path: rect(0 105% 105% 0);
display: block;
display: none;
min-width: 100%;
position: absolute;

Expand All @@ -91,6 +91,11 @@
}
}
}

.js-animation-playing .p-navigation__dropdown--container,
.is-active .p-navigation__dropdown--container {
display: block;
}
}
}
}