Skip to content
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

scroll into view when clicking on tab #4

Merged
merged 2 commits into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
scroll into view when clicking on tab.
  • Loading branch information
fiatjaf committed Sep 2, 2023
commit d3306da81f0d6fdb792a405fa761a5f647246802
2 changes: 1 addition & 1 deletion src/app.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!doctype html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
Expand Down
25 changes: 12 additions & 13 deletions src/components/Article.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<script lang="ts">
import { ndk } from "$lib/ndk";
import { afterUpdate, onMount } from "svelte";
import type { NDKEvent } from "@nostr-dev-kit/ndk";
import { formatDate } from "$lib/utils";
import { parse } from "$lib/articleParser.js";
import type { TabType } from "$lib/types";
import { ndk } from '$lib/ndk';
import { afterUpdate, onMount } from 'svelte';
import type { NDKEvent } from '@nostr-dev-kit/ndk';
import { formatDate } from '$lib/utils';
import { parse } from '$lib/articleParser.js';
import type { TabType } from '$lib/types';

export let eventid: string;
export let createChild: (type: TabType, data: string) => void;
Expand All @@ -14,10 +14,10 @@
const elements = document.querySelectorAll('[id^="wikilink-v0-"]');

elements.forEach((element) => {
element.addEventListener("click", () => {
element.addEventListener('click', () => {
//alert(`Clicked element with ID: ${element.id}`);
let a = element.id.slice(12);
createChild("articlefind", a);
createChild('articlefind', a);
});
});
}
Expand All @@ -35,19 +35,18 @@
<article class="prose font-sans mx-auto p-6 lg:max-w-4xl lg:pt-6 lg:pb-28">
{#if event !== null}
<h1 class="mb-0">
{#if event?.tags.find((e) => e[0] == "title")?.[0] && event?.tags.find((e) => e[0] == "title")?.[1]}
{event.tags.find((e) => e[0] == "title")?.[1]}
{#if event?.tags.find((e) => e[0] == 'title')?.[0] && event?.tags.find((e) => e[0] == 'title')?.[1]}
{event.tags.find((e) => e[0] == 'title')?.[1]}
{:else}
{event.tags.find((e) => e[0] == "d")?.[1]}
{event.tags.find((e) => e[0] == 'd')?.[1]}
{/if}
</h1>
<span>
{#await event.author?.fetchProfile()}
by <a href={`nostr:${event.author.npub}`}>...</a>,
{:then profile}
by <a href={`nostr:${event.author.npub}`}
>{profile !== null &&
JSON.parse(Array.from(profile)[0]?.content)?.name}</a
>{profile !== null && JSON.parse(Array.from(profile)[0]?.content)?.name}</a
>,
{/await}
{#if event.created_at}
Expand Down
10 changes: 4 additions & 6 deletions src/components/Search.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
</p>
</div>
{#each results as result}
<!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore a11y-no-static-element-interactions -->
<!-- svelte-ignore a11y-click-events-have-key-events a11y-no-static-element-interactions -->
<div
on:click={() => replaceSelf('article', result.id)}
class="cursor-pointer px-4 py-5 bg-white border border-gray-300 hover:bg-slate-50 rounded-lg mt-2 min-h-[48px]"
>
<h1>
{result.tags.find((e) => e[0] == 'title')?.[0] && result.tags.find((e) => e[0] == 'title')?.[1]
{result.tags.find((e) => e[0] == 'title')?.[0] &&
result.tags.find((e) => e[0] == 'title')?.[1]
? result.tags.find((e) => e[0] == 'title')?.[1]
: result.tags.find((e) => e[0] == 'd')?.[1]}
</h1>
Expand All @@ -67,9 +67,7 @@
192
)}{#if String(result.tags.find((e) => e[0] == 'summary')?.[1])?.length > 192}...{/if}
{:else}
{result.content.length <= 192
? result.content
: result.content.slice(0, 189) + '...'}
{result.content.length <= 192 ? result.content : result.content.slice(0, 189) + '...'}
{/if}
</p>
</div>
Expand Down
12 changes: 6 additions & 6 deletions src/components/Searchbar.svelte
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<script lang="ts">
import { tabs } from "$lib/state";
import { generateRandomNumber, scrollTabIntoView } from "$lib/utils";
import type { Tab } from "$lib/types";
import { tabs } from '$lib/state';
import { generateRandomNumber, scrollTabIntoView } from '$lib/utils';
import type { Tab } from '$lib/types';

let query = "";
let query = '';

function search() {
if (query) {
let newTabs = $tabs;
const newTab: Tab = {
id: generateRandomNumber(),
type: "articlefind",
data: query.toLowerCase().replaceAll(" ", "-"),
type: 'articlefind',
data: query.toLowerCase().replaceAll(' ', '-')
};
newTabs.push(newTab);
tabs.set(newTabs);
Expand Down
8 changes: 7 additions & 1 deletion src/components/Tab.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import { tabs } from '$lib/state';
import type { Tab, TabType } from '$lib/types';
import { generateRandomNumber, scrollTabIntoView } from '$lib/utils';
import { generateRandomNumber, scrollTabIntoView, isElementInViewport } from '$lib/utils';
import Article from './Article.svelte';
import Search from './Search.svelte';
export let tab: Tab;
Expand Down Expand Up @@ -61,8 +61,13 @@
} else if (tab.type == 'article') {
idtoload = tab.data;
}

function handleClick(ev: { currentTarget: HTMLElement }) {
if (!isElementInViewport(ev.currentTarget)) scrollTabIntoView(ev.currentTarget);
}
</script>

<!-- svelte-ignore a11y-no-static-element-interactions a11y-click-events-have-key-events -->
<div
id={`wikitab-v0-${tab.id}`}
class="
Expand All @@ -72,6 +77,7 @@
min-w-[365px] max-w-[365px] lg:min-w-[32rem] lg:max-w-[32rem]
rounded-lg border border-slate-500 bg-slate-50
h-[calc(100vh_-_64px_-_32px)]"
on:click={handleClick}
>
<button on:click={removeSelf}
><svg
Expand Down
21 changes: 17 additions & 4 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,26 @@ export function generateRandomNumber(): number {
return randomNumber;
}

export function scrollTabIntoView(id: string) {
export function scrollTabIntoView(el: string | HTMLElement) {
setTimeout(() => {
const elements = document.querySelectorAll(`[id^="wikitab-v0-${id}"]`);
if (!elements[0]) return;
elements[0].scrollIntoView({
const element =
typeof el === 'string' ? document.querySelector(`[id^="wikitab-v0-${el}"]`) : el;
if (!element) return;

element.scrollIntoView({
behavior: 'smooth',
inline: 'start'
});
}, 1);
}

export function isElementInViewport(el: HTMLElement) {
const rect = el.getBoundingClientRect();

return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
1 change: 0 additions & 1 deletion src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
/></svg
>
</button>
<!-- <Searchbar /> -->
</div>
<div class="ml-6 flex items-center">
<button
Expand Down
11 changes: 7 additions & 4 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
<script lang="ts">
import Tab from "../components/Tab.svelte";
import { tabs } from "$lib/state";
import Searchbar from "../components/Searchbar.svelte";
import Tab from '../components/Tab.svelte';
import { tabs } from '$lib/state';
import Searchbar from '../components/Searchbar.svelte';
import { scrollTabIntoView } from '$lib/utils';
</script>

<div class="flex overflow-x-scroll pb-2 scroll-smooth">
{#each $tabs as tab (tab.id)}
<Tab {tab} />
{/each}
<!-- svelte-ignore a11y-no-static-element-interactions a11y-click-events-have-key-events -->
<div
id={`wikitab-v0-special-search`}
class="
overflow-y-scroll
overflow-y-auto
overflow-x-hidden
mx-2 p-4 mt-2
min-w-[365px] max-w-[365px] lg:min-w-[32rem] lg:max-w-[32rem]
rounded-lg border border-slate-500 bg-slate-50
h-[calc(100vh_-_64px_-_32px)]"
on:click={(ev) => scrollTabIntoView(ev.currentTarget)}
>
<div class="p-6"><Searchbar /></div>
</div>
Expand Down