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

Added only render YouTubeVideoEmbed in the view port #127

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"env-cmd": "^10.1.0",
"luxon": "^3.3.0",
"redis": "^4.6.6",
"svelte-inview": "^4.0.0",
"zod": "^3.21.4"
},
"license": "MIT",
Expand Down
66 changes: 41 additions & 25 deletions src/lib/YouTubeVideoEmbed.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,57 @@
import { page } from '$app/stores';
import { formatNumberCompact, formatDuration, formatRelativeDate } from '$lib/utils';
import ViewCount from '$lib/ViewCount.svelte';
import { inview } from 'svelte-inview';
import type { ObserverEventDetails, Options } from 'svelte-inview';
import type { YouTubeVideoAPIResponse } from './server/YouTubeAPI';

export let locale: string;
export let active: boolean;
export let video: YouTubeVideoAPIResponse;

let isInView: boolean;
const options: Options = {
rootMargin: '50px',
unobserveOnEnter: true,
};

const handleChange = ({ detail }: CustomEvent<ObserverEventDetails>) => {
isInView = detail.inView;
};
</script>

<a
use:inview={options}
on:inview_change={handleChange}
href={`/list/${$page.params.id}/watch/${video.videoId}`}
class:variant-filled-primary={active}
class="card card-hover block cursor-pointer rounded-lg">
<div class="relative p-1">
<img
loading="lazy"
class="aspect-video w-full rounded-lg"
src={video.thumbnails.low}
alt={video.title} />
<!-- TODO: use icon library -->
<p class="absolute bottom-1 left-1 rounded-md bg-black bg-opacity-60 px-1.5 py-0.5 text-xs">
{formatNumberCompact(video.likes, locale)} 👍
</p>
<p class="absolute bottom-1 right-1 rounded-md bg-black bg-opacity-60 px-1.5 py-0.5">
{formatDuration(video.duration)}
</p>
</div>
<div class="m-2">
<p class="font-bold">{video.channelTitle}</p>
<div
class:dark:text-gray-400={!active}
class:light:text-gray-200={!active}
class="flex justify-between">
<ViewCount {locale} viewCount={video.viewCount} />
<span>{formatRelativeDate(video.publishedAt, locale)}</span>
{#if isInView}
<div class="relative p-1">
<img
loading="lazy"
class="aspect-video w-full rounded-lg"
src={video.thumbnails.low}
alt={video.title} />
<!-- TODO: use icon library -->
<p class="absolute bottom-1 left-1 rounded-md bg-black bg-opacity-60 px-1.5 py-0.5 text-xs">
{formatNumberCompact(video.likes, locale)} 👍
</p>
<p class="absolute bottom-1 right-1 rounded-md bg-black bg-opacity-60 px-1.5 py-0.5">
{formatDuration(video.duration)}
</p>
</div>
<div class="m-2">
<p class="font-bold">{video.channelTitle}</p>
<div
class:dark:text-gray-400={!active}
class:light:text-gray-200={!active}
class="flex justify-between">
<ViewCount {locale} viewCount={video.viewCount} />
<span>{formatRelativeDate(video.publishedAt, locale)}</span>
</div>
<!-- TODO: something better than ellipses... -->
<p class="my-2 line-clamp-2 break-words">{video.title}</p>
</div>
<!-- TODO: something better than ellipses... -->
<p class="my-2 line-clamp-2 break-words">{video.title}</p>
</div>
{/if}
</a>