maciekgrzybek / svelte-inview

A Svelte action that monitors an element enters or leaves the viewport.🔥
MIT License
746 stars 23 forks source link

Svelte 5 support #49

Open J4gQBqqR opened 2 months ago

J4gQBqqR commented 2 months ago

Event syntax like on:inview_change is not supported in svelte 5 anymore.

maciekgrzybek commented 2 months ago

Is svelte 5 even released yet? Seems like it's not 🤔

J4gQBqqR commented 2 months ago

I guess it wouldn't hurt to bring the umbrella before it rains? 😉

maciekgrzybek commented 2 months ago

Yeah sure thing :) I was just afraid that I missed it :) Not sure if I'll find the time soon... Are you interested in doing this? :)

J4gQBqqR commented 2 months ago
<script lang="ts">
    import {
        inview,
        type Options,
        type ObserverEventDetails,
    } from "svelte-inview";

    let isInView: boolean = $state(false);
    const options: Options = { rootMargin: "-100px" };
</script>

<div
    use:inview={options}
    oninview_change={(event: CustomEvent<ObserverEventDetails>) => {
        const { inView, entry, scrollDirection, observer, node } = event.detail;
        isInView = inView;
    }}
    oninview_enter={(event: CustomEvent<ObserverEventDetails>) => {
        const { inView, entry, scrollDirection, observer, node } = event.detail;
        isInView = inView;
    }}
    oninview_leave={(event: CustomEvent<ObserverEventDetails>) => {
        const { inView, entry, scrollDirection, observer, node } = event.detail;
        isInView = inView;
    }}
    oninview_init={(event: CustomEvent<LifecycleEventDetails>) => {
        const { observer, node } = event.detail;
    }}
>
    {isInView ? "Hey I am in the viewport" : "Bye, Bye"}
</div>

I tested and verified that the above code works in svelte 5. However, there is one caveat: 图片 Not sure this VS Code prompt is coming from svelte 5's incomplete language tool or this library's incompatible type definition between svelte 5.

jhubbardsf commented 1 month ago

I believe this should fix the issue with Svelte5 @maciekgrzybek @J4gQBqqR

https://github.com/maciekgrzybek/svelte-inview/pull/50

Docs: https://svelte.dev/docs/svelte-action#types-actionreturn

travisdmathis commented 2 weeks ago

can confirm this works after audit fixes for vulnerabilities..

https://github.com/travisdmathis/svelte-5-inview -- deployed and tested because I couldn't wait around for a merge.

npm install svelte-5-inview

will kill this package once you merge and update.

maciekgrzybek commented 2 weeks ago

Hey folks, sorry for lack of response, I had busy period.... I'll try to merge the update this week 👌

matteocontrini commented 1 week ago

Is there a particular reason why the naming convention used here is a mix of styles?

I would expect the handler methods to be called something like onInviewChange, instead of the (IMO weird) oninview_change.

Btw, thanks everyone for the efforts :)

jhubbardsf commented 4 days ago

@matteocontrini

This is just the new Svelte 5 event handler naming convention (link)