Gathering detailed insights and metrics for svelte-inview
Gathering detailed insights and metrics for svelte-inview
Gathering detailed insights and metrics for svelte-inview
Gathering detailed insights and metrics for svelte-inview
svelte-inview-next
A _simple_, _small_ and _easy_ to `use` intersection observer library for Svelte 5.
@jhubbardsf/svelte-inview
A Svelte action that monitors an element enters or leaves the viewport or a parent element. Performant and efficient thanks to using Intersection Observer under the hood.
svelte-5-inview
A Svelte action that monitors an element enters or leaves the viewport or a parent element. Performant and efficient thanks to using Intersection Observer under the hood. Forked from maciekgrzybek to support Svelte 5 after no merge of fixes.
npm install svelte-inview
Typescript
Module System
Node Version
NPM Version
86.1
Supply Chain
98.6
Quality
79.8
Maintenance
100
Vulnerability
100
License
TypeScript (77.36%)
Svelte (10.42%)
JavaScript (8.22%)
HTML (4%)
Total Downloads
763,419
Last Day
796
Last Week
7,793
Last Month
34,091
Last Year
373,674
762 Stars
88 Commits
23 Forks
9 Watching
19 Branches
9 Contributors
Minified
Minified + Gzipped
Latest Version
4.0.4
Package Id
svelte-inview@4.0.4
Unpacked Size
45.15 kB
Size
9.71 kB
File Count
16
NPM Version
10.8.3
Node Version
22.9.0
Publised On
01 Oct 2024
Cumulative downloads
Total Downloads
Last day
-44.9%
796
Compared to previous day
Last week
-0.9%
7,793
Compared to previous week
Last month
-6.4%
34,091
Compared to previous month
Last year
46.6%
373,674
Compared to previous year
1
21
A Svelte action that monitors an element enters or leaves the viewport/parent element. Performant and efficient thanks to using Intersection Observer under the hood. Can be used in multiple projects including lazy loading images, infinite scrolling, playing/pausing the video when in the viewport, tracking user behaviour firing link pre-fetching and animations and many many more.
🔥 Check it out live here
The only thing you need is Svelte itself.
Svelte Inview is distributed via npm.
1$ yarn add svelte-inview 2# or 3$ npm install --save svelte-inview
⚠️ Modern browsers have the full support of Intersection Observer, but if you need to support ones like IE you can use this simple polyfill. Just install it and import it in your project.
This is the most basic use case for svelte-inview
. Just add the action to the element you want to track - use:inview
. You can also pass other configuration props. You can see if the element is visible by checking the inView
or from the inside of the callback method - on:inview_change
.
1 2<script lang="ts"> 3 import { inview } from 'svelte-inview'; 4 5 let isInView: boolean; 6 const options = {}; 7</script> 8 9 <div 10 use:inview={options} 11 on:inview_change={(event) => { 12 const { inView, entry, scrollDirection, observer, node} = event.detail; 13 isInView = inView; 14 }} 15 on:inview_enter={(event) => { 16 const { inView, entry, scrollDirection, observer, node} = event.detail; 17 isInView = inView; 18 }} 19 on:inview_leave={(event) => { 20 const { inView, entry, scrollDirection, observer, node} = event.detail; 21 isInView = inView; 22 }} 23 on:inview_init={(event) => { 24 const { observer, node } = event.detail; 25 }}>{isInView ? 'Hey I am in the viewport' : 'Bye, Bye'}</div>
Svelte Inview lets you easily lazy load images. For a better UX we can pass a rootMargin="50px"
props, so the image will be loaded when scroll is 50px before the viewport. After it appears in the viewport, you don't want to observe it anymore, hence the unobserveOnEnter
props set to true.
1<script lang="ts"> 2 import { inview } from 'svelte-inview'; 3 import type { ObserverEventDetails, Options } from 'svelte-inview'; 4 5 let isInView; 6 const options: Options = { 7 rootMargin: '50px', 8 unobserveOnEnter: true, 9 }; 10 11 const handleChange = ({ detail }: CustomEvent<ObserverEventDetails>) => 12 (isInView = detail.inView); 13</script> 14 15<div use:inview="{options}" on:inview_change="{handleChange}"> 16 {#if isInView} 17 <img src="path/to/image.jpg" /> 18 {:else} 19 <div class="placeholder" /> 20 {/if} 21</div>
You can play/pause a video when it's in/out of the viewport. Simply pass correct methods in on:inview_enter
and on:inview_leave
callbacks.
1<script lang="ts"> 2 import { inview } from 'svelte-inview'; 3 import type { ObserverEventDetails } from 'svelte-inview'; 4 5 let isInView: boolean; 6 let videoRef: HTMLElement; 7</script> 8 9 <div 10 use:inview 11 on:inview_enter={() => videoRef.play()} 12 on:inview_leave={() => videoRef.pause()} 13 > 14 <video width="500" controls bind:this={videoRef}> 15 <source src="path/to/video.mp4" type="video/mp4" /> 16 </video> 17 </div>
You can also add some cool animations when an element enters the viewport. To make sure the animation won't fire too soon you can pass a negative value to rootMargin
. When inView
is true, add an animation class to your target. Additionally, you can detect the scroll direction to make the animations even cooler!
1 2<script lang="ts"> 3 import { inview } from 'svelte-inview'; 4 import type { ObserverEventDetails, ScrollDirection, Options } from 'svelte-inview'; 5 6 let isInView: boolean; 7 let scrollDirection: ScrollDirection; 8 const options: Options = { 9 rootMargin: '-50px', 10 unobserveOnEnter: true, 11 }; 12 13 const handleChange = ({ detail }: CustomEvent<ObserverEventDetails>) => { 14 isInView = detail.inView; 15 scrollDirection = detail.scrollDirection.vertical; 16 }; 17</script> 18 19 <div use:inview={options} on:inview_change={handleChange}> 20 <div 21 class:animate={isInView} 22 class:animateFromBottom={scrollDirection === 'down'} 23 class:animateFromTop={scrollDirection === 'up'}> 24 Animate me! 25 </div> 26 </div>
'change' | 'leave' | 'enter' | 'init'
. In version 4 they're changed to 'inview_change' | 'inview_leave' | 'inview_enter' | 'inview_init'
.This change was needed to satisfy Typescript, as the package was messing up the default types coming from svelte typings. To ensure backward compatibility, the original events names will still work for some time, but they won't be properly recognized by Typescript. To sum up, this will still work, but TS won't be happy about it:
1on:change={(event) => { 2 const { inView, entry, scrollDirection, observer, node} = event.detail; 3 isInView = inView; 4}}
To make sure it works properly and satisfy TS you'll need to change it to this:
1on:inview_change={(event) => { 2 const { inView, entry, scrollDirection, observer, node} = event.detail; 3 isInView = inView; 4}}
observe
and unobserve
methods on the events. In version 3 they were removed, and the observer
and node
are being returned instead. So if you used those methods before like this:1event.detail.observe(node);
You'll need to change it to:
1event.detail.observer.observe(node);
Inview
component. In version 2 that was changed to action
- API is easier to consume, plus the obsolete wrapper is not longer needed. If you still want to use the component, check the documentation for version 1.Name | Type | Default | Description | Required |
---|---|---|---|---|
options.root | HTMLElement | window | The element that is used as the viewport for checking visibility of the target. Must be the ancestor of the target. | false |
options.rootMargin | string | 0px | Margin around the root element. Values similar to the CSS margin property, e.g. "10px 20px 30px 40px". Can also be a percentage. See more. | false |
options.threshold | number or number[] | 0 | Either a single number or an array of numbers which indicate at what percentage of the target's visibility the observer's callback should be executed. If you only want to detect when visibility passes the 50% mark, you can use a value of 0.5. If you want the callback to run every time visibility passes another 25%, you would specify the array [0, 0.25, 0.5, 0.75, 1]. The default is 0 (meaning as soon as even one pixel is visible, the callback will be run) | false |
options.unobserveOnEnter | boolean | false | If true, target element stops being observed after the first time it appears in the viewport. Can be used when you want to fire the callback only once. | false |
on:inview_change | function | - | Event fired every time the target element meets the specified threshold. Receives event object as an argument. Inside of event.detail you can find all the arguments specified here. | false |
on:inview_enter | function | - | Event fired every time the target element enters the viewport. Receives event object as an argument. Inside of event.detail you can find all the arguments specified here . | false |
on:inview_leave | function | - | Event fired every time the target element leaves the viewport. Receives event object as an argument. Inside of event.detail you can find all the arguments specified here . | false |
on:inview_init | function | - | Event fired on action initialization, before the observer starts observing the element. Receives lifecycle arguments specified here | false |
Name | Type | Description |
---|---|---|
inView | boolean | Visibility state of the target element. If it's true , target passed at least the value of the threshold props. |
entry | IntersectionObserverEntry | Intersection Observer entry object generated every time when IO callback is fired. |
scrollDirection.vertical | up or down | Vertical scrolling direction. |
scrollDirection.horizontal | left or right | Horizontal scrolling direction. |
node | HTMLElement | Element that is being observed |
observer | IntersectionObserver | Intersection Observer instance for the observed element. Among others, it allows to "turn off" the observer at the very beginning. |
Name | Type | Description |
---|---|---|
node | HTMLElement | Element that is being observed |
observer | IntersectionObserver | Intersection Observer instance for the observed element. Among others, it allows to "turn off" the observer at the very beginning. |
If you want to increase or decrease the area of the root, just pass the rootMargin
. On the image below you can see the blue area being the root
. It means that every time, the target element will enter or leave that area (or meet the specified threshold), a callback will be fired.
Distributed under the MIT License. See LICENSE for more information.
Maciek Grzybek - @grzybek_maciek - maciekgrzybek1@gmail.com - www.maciekgrzybek.dev
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
4 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 5
Reason
dependency not pinned by hash detected -- score normalized to 4
Details
Reason
Found 2/15 approved changesets -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
29 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More