Skip to content

Commit

Permalink
add viewport checking to createChild, much more smooth experience now
Browse files Browse the repository at this point in the history
  • Loading branch information
github-tijlxyz committed Sep 3, 2023
1 parent fab6836 commit f344a14
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/components/Search.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import type { NDKEvent } from '@nostr-dev-kit/ndk';
import { onMount } from 'svelte';
import type { TabType } from '$lib/types';
import { stringify } from 'postcss';
export let query: string;
export let replaceSelf: (newType: TabType, newData: string) => void;
Expand Down
4 changes: 3 additions & 1 deletion src/components/Tab.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
};
const newTabs = $tabs.slice(0, index + 1).concat(newChild);
tabs.set(newTabs);
scrollTabIntoView(String(newChild.id), true);
if (isElementInViewport(String(newChild.id))) {
scrollTabIntoView(String(newChild.id), true);
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ export function scrollTabIntoView(el: string | HTMLElement, wait: boolean) {
}
}

export function isElementInViewport(el: HTMLElement) {
const rect = el.getBoundingClientRect();
export function isElementInViewport(el: string | HTMLElement) {
const element = typeof el === 'string' ? document.querySelector(`[id^="wikitab-v0-${el}"]`) : el;
if (!element) return;

const rect = element.getBoundingClientRect();

return (
rect.top >= 0 &&
Expand Down

0 comments on commit f344a14

Please sign in to comment.