diff --git a/package.json b/package.json index 78b3ca8..f09c81e 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/lib/YouTubeVideoEmbed.svelte b/src/lib/YouTubeVideoEmbed.svelte index 85b0d75..b017558 100644 --- a/src/lib/YouTubeVideoEmbed.svelte +++ b/src/lib/YouTubeVideoEmbed.svelte @@ -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) => { + isInView = detail.inView; + }; -
- {video.title} - -

- {formatNumberCompact(video.likes, locale)} 👍 -

-

- {formatDuration(video.duration)} -

-
-
-

{video.channelTitle}

-
- - {formatRelativeDate(video.publishedAt, locale)} + {#if isInView} +
+ {video.title} + +

+ {formatNumberCompact(video.likes, locale)} 👍 +

+

+ {formatDuration(video.duration)} +

+
+
+

{video.channelTitle}

+
+ + {formatRelativeDate(video.publishedAt, locale)} +
+ +

{video.title}

- -

{video.title}

-
+ {/if}