-
Notifications
You must be signed in to change notification settings - Fork 32
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
feat: Introduced Right Subsections for Seamless Topic Access #99
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,4 +75,5 @@ export function insertCodeSnippetCopyButtons() { | |
}, 2000); | ||
}); | ||
}); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,73 @@ export function enableScrollToTop() { | |
}); | ||
} | ||
|
||
export function setupSidebarItemEventListeners() { | ||
const sidebarItems = document.querySelectorAll('.sidebar-item-h2'); | ||
|
||
sidebarItems.forEach(function(item) { | ||
item.addEventListener('mouseenter', function() { | ||
item.style.color = '#34d399'; | ||
}); | ||
item.addEventListener('mouseleave', function() { | ||
item.style.color = 'gray'; | ||
}); | ||
item.addEventListener('click', function() { | ||
const currentVersion = item.textContent; | ||
const headings = document.querySelectorAll('h2'); | ||
let targetElement = null; | ||
headings.forEach(function(heading) { | ||
if (heading.textContent.trim() === currentVersion.trim()) { | ||
targetElement = heading; | ||
} | ||
}); | ||
if (targetElement) { | ||
window.scrollTo(0, targetElement.offsetTop - 100); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
export function setupSidebar() { | ||
const h1Elements = document.querySelectorAll('h1'); | ||
const h2Elements = document.querySelectorAll('h2'); | ||
const sidebar = document.querySelector('.sidebar'); | ||
|
||
sidebar.style.position = 'fixed'; | ||
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. these styles should all be expressed as tailwind classes in the HTML |
||
sidebar.style.top = '100px'; | ||
sidebar.style.right = '50px'; | ||
sidebar.style.width = 'fit-content'; | ||
sidebar.style.maxWidth = '300px'; | ||
sidebar.style.height = '100%'; | ||
|
||
function appendSidebarItem(textContent, tagName) { | ||
const newItem = document.createElement('p'); | ||
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. these should be anchor tags so that they are accessible. That would have the added advantage that you then don't need to apply custom colors on mouseenter and mouseleave. |
||
newItem.textContent = textContent; | ||
if (tagName === 'h1') { | ||
newItem.classList.add('sidebar-item'); | ||
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.
|
||
newItem.classList.add('text-green-500'); | ||
} | ||
if (tagName === 'h2') { | ||
newItem.classList.add('sidebar-item-h2'); | ||
newItem.classList.add('text-gray-500'); | ||
newItem.style.marginLeft = '20px'; | ||
newItem.style.cursor = 'pointer'; | ||
} | ||
sidebar.appendChild(newItem); | ||
} | ||
|
||
h1Elements.forEach(function (element) { | ||
appendSidebarItem(element.textContent, 'h1'); | ||
}); | ||
|
||
let categories = 0; | ||
h2Elements.forEach(function (element) { | ||
if (categories > 4) { | ||
appendSidebarItem(element.textContent, 'h2'); | ||
} | ||
categories++; | ||
}); | ||
} | ||
|
||
export function saveAndRestoreNavigationPosition() { | ||
var scrollPosition = sessionStorage.getItem('scrollPosition'); | ||
if (scrollPosition !== undefined) { | ||
|
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.
changing this line to
document.querySelectorAll('article h2')
would mean you can get rid of the counting of categories down below. That also future-proofs your changes in case the number of h2 tags in the navigation panel changes. you can't trust that the number of elements on the page will remain fixed. and if you have to for some reason you have to leave a comment explaining your decision