-
Notifications
You must be signed in to change notification settings - Fork 116
WD-30045 - Implement sliding navigation in snapcraft.io #5437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
11ab25b
WD-30045 - Move changes from global-nav to snapcraft.io
alvaromateo 9e548c0
WD-30045 - Navigation mobile view fixed
alvaromateo c54343d
WD-30045 - Navigation desktop view fixed
alvaromateo 00f4618
WD-30045 - Pretty print dev logs
alvaromateo a25589c
WD-30045 - Fix SCSS linting
alvaromateo 4588fb9
WD-30045 - Fix Copilot comments
alvaromateo f5ce448
WD-30045 - Fix bug in adding "back" buttons to global-nav
alvaromateo 985c60a
WD-30045 - Fix margin on /docs page and other PR comments
alvaromateo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| import "./navigation"; | ||
| import "./ga"; | ||
| import "./contactForm"; | ||
| import "./sentry"; |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| export const setActiveDropdown = ( | ||
| dropdownToggleButton: HTMLElement, | ||
| isActive = true, | ||
| ) => { | ||
| // set active state of the dropdown toggle (to slide the panel into view) | ||
| const dropdownToggleEl = dropdownToggleButton.closest( | ||
| ".p-navigation__item--dropdown-toggle", | ||
| ); | ||
| if (dropdownToggleEl) { | ||
| dropdownToggleEl.classList.toggle("is-active", isActive); | ||
| dropdownToggleEl.classList.toggle("is-selected", isActive); | ||
| const globalNavButton = dropdownToggleEl.querySelector( | ||
| ":scope > .p-navigation__link", | ||
| ); | ||
| // fix some states from global-nav elements in mobile | ||
| if (globalNavButton) { | ||
| globalNavButton.setAttribute("aria-expanded", isActive.toString()); | ||
| globalNavButton.classList.toggle("is-selected", isActive); | ||
| } | ||
| } | ||
|
|
||
| // set active state of the parent dropdown panel (to fade it out of view) | ||
| const parentLevelDropdown = dropdownToggleEl?.closest( | ||
| ".p-navigation__dropdown", | ||
| ); | ||
| if (parentLevelDropdown) { | ||
| parentLevelDropdown.classList.toggle("is-active", isActive); | ||
| } | ||
|
|
||
| // set active state of the top navigation list under p-navigation__nav | ||
| // to set the position of the sliding panel properly | ||
| const topLevelNavigation = dropdownToggleButton.closest(".p-navigation__nav"); | ||
| if (topLevelNavigation) { | ||
| const topLevelItems = topLevelNavigation.querySelectorAll( | ||
| ":scope > .p-navigation__items", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
| ); | ||
|
|
||
| for (const item of topLevelItems) { | ||
| // in case there are more than one top level navigation lists, we need to | ||
| // mark as active the one that contains the clicked button and hide the rest | ||
| if (item.contains(dropdownToggleButton)) { | ||
| item.classList.toggle("is-active", isActive); | ||
| } else { | ||
| item.classList.toggle("u-hide", isActive); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| export const setListFocusable = (list: Element) => { | ||
| // turn on focusability for all direct children in the target dropdown | ||
| if (list) { | ||
| for (const item of list.children) { | ||
| item.children[0].setAttribute("tabindex", "0"); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| export const setFocusable = (target: Element) => { | ||
| // if target dropdown is not a list, find the list in it | ||
| const isList = | ||
| target.classList.contains("p-navigation__dropdown") || | ||
| target.classList.contains("p-navigation__items"); | ||
|
|
||
| if (!isList) { | ||
| // find all lists in the target dropdown and make them focusable | ||
| target.querySelectorAll(".p-navigation__dropdown").forEach((element) => { | ||
| setListFocusable(element); | ||
| }); | ||
| } else { | ||
| setListFocusable(target); | ||
| } | ||
| }; | ||
|
|
||
| export const collapseDropdown = ( | ||
| dropdownToggleButton: HTMLElement, | ||
| targetDropdown: HTMLElement, | ||
| ) => { | ||
| targetDropdown.setAttribute("aria-hidden", "true"); | ||
| setActiveDropdown(dropdownToggleButton, false); | ||
| }; | ||
|
|
||
| export const expandDropdown = ( | ||
| dropdownToggleButton: HTMLElement, | ||
| targetDropdown: HTMLElement, | ||
| ) => { | ||
| setActiveDropdown(dropdownToggleButton); | ||
| targetDropdown.setAttribute("aria-hidden", "false"); | ||
| setFocusable(targetDropdown); | ||
| }; | ||
|
|
||
| const toggleAnimationPlaying = ( | ||
| element: Element, | ||
| animationDuration?: number, | ||
| ) => { | ||
| const endAnimation = () => { | ||
| element.classList.toggle("js-animation-playing", false); | ||
| }; | ||
| element.classList.toggle("js-animation-playing", true); | ||
| // force browser to flush all pending style and layout calculations immediately | ||
| void (element as HTMLElement).offsetWidth; | ||
| setTimeout(endAnimation, animationDuration); | ||
| }; | ||
|
|
||
| export const setupAnimationStart = ( | ||
| elements: Element[], | ||
| animationDuration?: number, | ||
| ) => { | ||
| elements.forEach((toggle: Element) => { | ||
| toggleAnimationPlaying(toggle.parentElement!, animationDuration); | ||
| }); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| export function patchAllCanonicalMobileMarkup() { | ||
|
bartaz marked this conversation as resolved.
|
||
| const allCanonicalMobile = document.getElementById("all-canonical-mobile"); | ||
| const topMobileSections = allCanonicalMobile?.querySelectorAll( | ||
| ".global-nav__dropdown-toggle", | ||
| ); | ||
|
|
||
| topMobileSections?.forEach((section: Element) => { | ||
| const sectionLink = section.querySelector("button.p-navigation__link"); | ||
| const sectionHref = sectionLink?.getAttribute("href"); | ||
|
|
||
| const sectionLinksList = section.querySelector("ul"); | ||
| sectionLinksList?.setAttribute("aria-hidden", "true"); | ||
|
|
||
| // add the back button as the first item of the section | ||
| if (sectionHref && sectionLinksList) { | ||
| sectionLinksList.prepend(createBackButtonItem(sectionHref)); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| function createFromHTML(html: string) { | ||
| const div = window.document.createElement("div"); | ||
| div.innerHTML = html; | ||
| return div.childNodes[0]; | ||
| } | ||
|
|
||
| function createBackButtonItem(href: string) { | ||
| // remove the # from the href | ||
| const ariaControls = href.slice(1); | ||
| return createFromHTML(`<li class="p-navigation__item--dropdown-close"> | ||
| <a href=${href} aria-controls=${ariaControls} class="p-navigation__link js-back-button"> | ||
| Back | ||
| </a> | ||
| </li>`); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import "./login"; | ||
|
|
||
| import { createNav as createAllCanonicalNav } from "@canonical/global-nav"; | ||
| import { initNavigationListeners } from "./listeners"; | ||
| import { patchAllCanonicalMobileMarkup } from "./globalNav"; | ||
|
|
||
| // initialize global-nav ("All Canonical" link) and the rest of the navigation | ||
| window.addEventListener("DOMContentLoaded", function () { | ||
| createAllCanonicalNav(); | ||
| patchAllCanonicalMobileMarkup(); | ||
| initNavigationListeners(); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, why is this change needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's optional, but given that you are already in the latest flask-base version, with this change you will get the pretty printed logs in DEV mode, which is a nice addition 😄