Skip to content

Commit da72c1f

Browse files
committed
fix hash links
1 parent 7a5e78d commit da72c1f

3 files changed

Lines changed: 38 additions & 21 deletions

File tree

src/Index.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@
288288
</h2>
289289
{#if frameworkIdsSelectedInitialized}
290290
<div
291-
class="grid grid-cols-1 2xl:grid-cols-2 gap-10 mt-4"
291+
class="grid grid-cols-1 2xl:grid-cols-2 gap-y-4 2xl:gap-y-8 gap-x-10 mt-2"
292292
>
293293
{#each frameworkIdsSelectedArr as frameworkId (frameworkId)}
294294
{@const framework = matchFrameworkId(frameworkId)}

src/components/Aside.svelte

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
function getLargestElement(elements: NodeListOf<Element>): Element | null {
99
let largestArea = 0;
1010
let largestElement: Element | null = null;
11+
let firstFullyVisibleElement: Element | null = null;
1112
1213
for (const element of elements) {
1314
const rect = element.getBoundingClientRect();
@@ -19,18 +20,42 @@
1920
if (visibleWidth > 0 && visibleHeight > 0) {
2021
const area = visibleWidth * visibleHeight;
2122
23+
// Check if element is fully visible
24+
const isFullyVisible =
25+
rect.top >= 0 &&
26+
rect.left >= 0 &&
27+
rect.bottom <= window.innerHeight &&
28+
rect.right <= window.innerWidth;
29+
30+
// Prioritize first fully visible element
31+
if (isFullyVisible && !firstFullyVisibleElement) {
32+
firstFullyVisibleElement = element;
33+
}
34+
35+
// Track largest element as fallback
2236
if (area > largestArea) {
2337
largestArea = area;
2438
largestElement = element;
2539
}
2640
}
2741
}
2842
29-
return largestElement;
43+
// Return first fully visible element if found, otherwise largest element
44+
return firstFullyVisibleElement || largestElement;
45+
}
46+
47+
function scrollToElement(elementId: string) {
48+
const target = document.getElementById(elementId);
49+
if (target) {
50+
// Update URL hash
51+
window.history.pushState(null, "", `#${elementId}`);
52+
// Scroll to target
53+
target.scrollIntoView({ block: "start" });
54+
}
3055
}
3156
32-
function listenLargestSnippetOnScroll(): () => void {
33-
function onScroll(): void {
57+
onMount(function listenLargestSnippetOnScroll() {
58+
function onScroll() {
3459
const largestSnippet = getLargestElement(
3560
document.querySelectorAll("[data-snippet-id]"),
3661
);
@@ -49,14 +74,6 @@
4974
return () => {
5075
document.removeEventListener("scroll", throttleOnScroll);
5176
};
52-
}
53-
54-
let unlistenLargestSnippetOnScroll: (() => void) | undefined;
55-
56-
onMount(() => {
57-
unlistenLargestSnippetOnScroll = listenLargestSnippetOnScroll();
58-
59-
return () => unlistenLargestSnippetOnScroll?.();
6077
});
6178
</script>
6279

@@ -67,35 +84,35 @@
6784
<ul class="space-y-6">
6885
{#each sections as section (section.sectionId)}
6986
<li>
70-
<a
71-
href={`#${section.sectionId}`}
87+
<button
7288
class={[
73-
"inline-block w-full py-1.5 px-4 text-white font-semibold opacity-90 hover:opacity-100 hover:bg-gray-800 rounded transition-opacity",
89+
"inline-block w-full py-1.5 px-4 text-white font-semibold opacity-90 hover:opacity-100 hover:bg-gray-800 rounded transition-opacity text-left",
7490
{
7591
"bg-gray-800":
7692
largestVisibleSnippetId &&
7793
largestVisibleSnippetId.startsWith(section.sectionId),
7894
},
7995
]}
96+
onclick={() => scrollToElement(section.sectionId)}
8097
>
8198
{section.title}
82-
</a>
99+
</button>
83100
<ul>
84101
{#each snippets.filter((s: any) => s.sectionId === section.sectionId) as snippet (snippet.snippetId)}
85102
{@const snippetPathId =
86103
section.sectionId + "." + snippet.snippetId}
87104
<li>
88-
<a
89-
href={`#${snippetPathId}`}
105+
<button
90106
class={[
91-
"inline-block w-full py-1.5 px-4 text-white font-medium hover:bg-gray-800 rounded hover:opacity-100 transition-opacity",
107+
"inline-block w-full py-1.5 px-4 text-white font-medium hover:bg-gray-800 rounded hover:opacity-100 transition-opacity text-left",
92108
snippetPathId === largestVisibleSnippetId
93109
? "bg-gray-800 opacity-70"
94110
: "opacity-50",
95111
]}
112+
onclick={() => scrollToElement(snippetPathId)}
96113
>
97114
{snippet.title}
98-
</a>
115+
</button>
99116
</li>
100117
{/each}
101118
</ul>

src/components/CodeEditor.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
<div class="relative group">
6060
<div
6161
bind:this={codeSnippetEl}
62-
class="bg-[#0d1117] px-4 py-3 text-sm overflow-auto"
62+
class="bg-[#0d1117] px-4 py-3 text-sm overflow-auto rounded-b rounded-tr"
6363
>
6464
{#if snippet}
6565
<!-- eslint-disable-next-line svelte/no-at-html-tags -->

0 commit comments

Comments
 (0)