-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5709d73
commit a80df78
Showing
15 changed files
with
297 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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 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 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 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 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,2 @@ | ||
export { default as Sidebar } from './sidebar.astro'; | ||
export { default as SidebarItem } from './sidebar-item.astro'; |
This file contains 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,93 @@ | ||
--- | ||
import type { HTMLAttributes } from 'astro/types'; | ||
import { Icon } from '@/components/icon'; | ||
import Tooltip from '@/web/components/tooltip.astro'; | ||
export interface Props extends HTMLAttributes<'main'> { | ||
icon: string; | ||
sectionId: string; | ||
} | ||
const { icon, sectionId, title } = Astro.props; | ||
--- | ||
|
||
<li> | ||
<Tooltip> | ||
<a | ||
slot="trigger" | ||
href={`#${sectionId}`} | ||
class="inline-flex size-10 items-center justify-center rounded-lg transition" | ||
data-sidebar-link | ||
aria-label={`${title} section`} | ||
> | ||
<Icon name={icon} class="size-5" /> | ||
</a> | ||
<slot /> | ||
</Tooltip> | ||
</li> | ||
|
||
<style> | ||
a[aria-current='page'] { | ||
@apply bg-color-button-bg text-color-button-bg-contrast; | ||
} | ||
|
||
a:not([aria-current='page']) { | ||
@apply text-color-icon-main hover:bg-color-bg-main; | ||
} | ||
</style> | ||
|
||
<script> | ||
import { throttle } from 'throttle-debounce'; | ||
|
||
// Works best when set to the half of the gap between sections. | ||
const DETECT_SECTION_OFFSET = 32; | ||
|
||
const sidebarLinks = [...document.querySelectorAll('[data-sidebar-link]')].flatMap((link) => | ||
link instanceof HTMLAnchorElement ? [link] : [], | ||
); | ||
const sections = [...document.querySelectorAll('section[id]')].flatMap((section) => | ||
section instanceof HTMLElement && sidebarLinks.some((link) => link.hash === `#${section.id}`) ? [section] : [], | ||
); | ||
|
||
function setCurrentNavlinkFromHash() { | ||
const currentHash = location.hash; | ||
|
||
sidebarLinks.forEach((link) => { | ||
if (link.getAttribute('href') === currentHash) { | ||
link.setAttribute('aria-current', 'page'); | ||
} else { | ||
link.removeAttribute('aria-current'); | ||
} | ||
}); | ||
} | ||
|
||
function setCurrentHashFromScroll() { | ||
const section = sections.find((section) => { | ||
const { top, bottom } = section.getBoundingClientRect(); | ||
return top <= DETECT_SECTION_OFFSET && bottom > -DETECT_SECTION_OFFSET; | ||
}); | ||
|
||
console.log(section?.id || null); | ||
|
||
if (!section && location.hash) { | ||
return setHash(null); | ||
} | ||
|
||
if (section && location.hash !== `#${section.id}`) { | ||
return setHash(section.id); | ||
} | ||
} | ||
|
||
function setHash(sectionId: string | null) { | ||
history.replaceState(null, '', sectionId ? `#${sectionId}` : location.pathname + location.search); | ||
window.dispatchEvent(new HashChangeEvent('hashchange')); | ||
} | ||
|
||
addEventListener('hashchange', setCurrentNavlinkFromHash); | ||
addEventListener('scroll', throttle(250, setCurrentHashFromScroll)); | ||
addEventListener('load', () => { | ||
setCurrentNavlinkFromHash(); | ||
setCurrentHashFromScroll(); | ||
}); | ||
</script> |
This file contains 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,17 @@ | ||
--- | ||
import type { HTMLAttributes } from 'astro/types'; | ||
import { cn } from '@/utils/cn'; | ||
export interface Props extends HTMLAttributes<'nav'> { | ||
className?: string; | ||
} | ||
const { class: className, ...props } = Astro.props; | ||
--- | ||
|
||
<nav data-sidebar class={cn(['absolute -right-18 hidden h-full w-max xl:block', className])} {...props}> | ||
<ul class={cn('sticky top-4 grid h-fit gap-2 rounded-lg bg-color-bg-card p-2 shadow-lg')}> | ||
<slot /> | ||
</ul> | ||
</nav> |
Oops, something went wrong.