-
-
Notifications
You must be signed in to change notification settings - Fork 91
First stab at fixing the primary nav on small viewports #693
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
Open
DmitrySharabin
wants to merge
1
commit into
main
Choose a base branch
from
responsive-nav
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+140
−15
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,34 @@ | ||
| <a href="{{ page | relative }}/get/">Get<span class="wide"> Color.js</span></a> | ||
| <div class="menu"> | ||
| <a href="{{ page | relative }}/get/" data-nav-item="1">Get<span class="wide"> Color.js</span></a> | ||
| <div class="menu" data-nav-item="2"> | ||
| <a href="{{ page | relative }}/docs/">Docs</a> | ||
| <ul> | ||
| {% include "./_docs-nav.njk" %} | ||
| </ul> | ||
| </div> | ||
| <a href="{{ page | relative }}/api/">API</a> | ||
| <a href="{{ page | relative }}/notebook/">Play!</a> | ||
| <a href="https://apps.colorjs.io/">Demos</a> | ||
| <a href="https://elements.colorjs.io/">Elements</a> | ||
| <a href="{{ page | relative }}/test/" class="footer">Tests</a> | ||
| <a href="https://github.com/LeaVerou/color.js">GitHub</a> | ||
| <a href="https://discord.gg/K64FJBznq4">Discord</a> | ||
| <a href="https://opencollective.com/color">♡ Sponsor</a> | ||
| <a href="https://github.com/LeaVerou/color.js/issues/new" class="footer">File bug</a> | ||
| <a href="{{ page | relative }}/api/" data-nav-item="3">API</a> | ||
| <a href="{{ page | relative }}/notebook/" data-nav-item="4">Play!</a> | ||
| <a href="https://apps.colorjs.io/" data-nav-item="5">Demos</a> | ||
| <a href="https://elements.colorjs.io/" data-nav-item="6">Elements</a> | ||
| <a href="{{ page | relative }}/test/" class="footer" data-nav-item="7">Tests</a> | ||
| <a href="https://github.com/LeaVerou/color.js" data-nav-item="8">GitHub</a> | ||
| <a href="https://discord.gg/K64FJBznq4" data-nav-item="9">Discord</a> | ||
| <a href="https://opencollective.com/color" data-nav-item="10">♡ Sponsor</a> | ||
| <a href="https://github.com/LeaVerou/color.js/issues/new" class="footer" data-nav-item="11">File bug</a> | ||
| <div class="hamburger-menu"> | ||
| <button class="hamburger-button" aria-label="Menu"> | ||
| <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 48 48"> | ||
| <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="4" d="M7.95 11.95h32m-32 12h32m-32 12h32"/> | ||
| </svg> | ||
| </button> | ||
| <ul class="hamburger-list"> | ||
| <li data-nav-item="1" hidden><a href="{{ page | relative }}/get/">Get Color.js</a></li> | ||
| <li data-nav-item="2" hidden><a href="{{ page | relative }}/docs/">Docs</a></li> | ||
| <li data-nav-item="3" hidden><a href="{{ page | relative }}/api/">API</a></li> | ||
| <li data-nav-item="4" hidden><a href="{{ page | relative }}/notebook/">Play!</a></li> | ||
| <li data-nav-item="5" hidden><a href="https://apps.colorjs.io/">Demos</a></li> | ||
| <li data-nav-item="6" hidden><a href="https://elements.colorjs.io/">Elements</a></li> | ||
| <li data-nav-item="8" hidden><a href="https://github.com/LeaVerou/color.js">GitHub</a></li> | ||
| <li data-nav-item="9" hidden><a href="https://discord.gg/K64FJBznq4">Discord</a></li> | ||
| <li data-nav-item="10" hidden><a href="https://opencollective.com/color">♡ Sponsor</a></li> | ||
| </ul> | ||
| </div> | ||
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 |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /** | ||
| * Responsive Navigation Script | ||
| * Dynamically hides navigation items that don't fit on screen in the main nav | ||
| * and shows them in the hamburger menu instead. Uses ResizeObserver to monitor | ||
| * the nav element and checks items from right to left to determine which ones | ||
| * overflow. Items are matched between nav and hamburger menu using data-nav-item | ||
| * attributes. | ||
| */ | ||
|
|
||
| const nav = document.querySelector("header nav"); | ||
| const menu = nav.querySelector(".hamburger-menu"); | ||
| const [menuButton, menuList] = menu.children; | ||
|
|
||
| // Map to track nav items and their hamburger menu counterparts | ||
| let itemMap = new Map(); | ||
|
|
||
| // Get all nav items (excluding the hamburger menu and the ones that are supposed to be shown in the footer) | ||
| let navItems = [...nav.children].filter( | ||
| child => !child.classList.contains("hamburger-menu") && !child.classList.contains("footer"), | ||
| ); | ||
|
|
||
| let hamburgerMenuItems = [...menuList.children]; | ||
| for (let navItem of navItems) { | ||
| let index = navItem.dataset.navItem; | ||
| let hamburgerItem = hamburgerMenuItems.find(item => item.dataset.navItem === index); | ||
| if (hamburgerItem) { | ||
| itemMap.set(navItem, hamburgerItem); | ||
| } | ||
| } | ||
|
|
||
| const resizeObserver = new ResizeObserver(checkFit); | ||
| resizeObserver.observe(nav); | ||
|
|
||
| function checkFit () { | ||
| // Reset: show all items in nav, hide all in hamburger | ||
| itemMap.forEach((hamburgerItem, navItem) => { | ||
| navItem.hidden = false; | ||
| hamburgerItem.hidden = true; | ||
| }); | ||
|
|
||
| // Temporarily show hamburger menu to measure its width | ||
| menu.style.setProperty("display", "block"); | ||
|
|
||
| let hamburgerButtonWidth = menuButton.offsetWidth ?? 50; | ||
| let navRect = nav.getBoundingClientRect(); | ||
| let navRight = navRect.right; | ||
| let availableRight = navRight - hamburgerButtonWidth; | ||
|
|
||
| let items = [...itemMap.keys()]; | ||
| let toHide = []; | ||
|
|
||
| // Check each item from right to left to see which ones overflow | ||
| // by comparing their right edge to available space | ||
| for (let i = items.length - 1; i >= 0; i--) { | ||
| const item = items[i]; | ||
| const itemRect = item.getBoundingClientRect(); | ||
| const itemRight = itemRect.right; | ||
| // If this item's right edge exceeds available space, hide it | ||
| if (itemRight > availableRight) { | ||
| toHide.push(item); | ||
| } | ||
| else { | ||
| // Once we find an item that fits, we can stop | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| menu.style.removeProperty("display"); | ||
|
|
||
| // Hide items in nav and show corresponding items in hamburger menu | ||
| for (let navItem of toHide) { | ||
| let hamburgerItem = itemMap.get(navItem); | ||
|
|
||
| navItem.hidden = true; | ||
| hamburgerItem.hidden = false; | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
As a TODO, I'd try to DRYify this part, but unfortunately, those items are not entirely identical to the nav items. Therefore, for now, I have decided to leave it as is and revisit it in the next iteration.