Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@svelte-plugins/viewable

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@svelte-plugins/viewable

A simple rule-based approach to tracking element viewability.

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
58
increased by61.11%
Maintainers
1
Weekly downloads
 
Created
Source

Unit Tests Integration Tests codecov

@svelte-plugins/viewable

A simple rule-based approach to tracking element viewability.

This provides the ability to define multiple viewability rules with callbacks for a single element. This comes in handy for instrumentation (specifically dwell time), ad tracking and beaconing, lazy-loading content or images, or doing fancy things at various trigger points.

If you're simply looking for a Svelte flavor of IntersectionObserver visit svelte-intersection-observer.

Try it in the Svelte REPL.

Install

# npm
> npm install @svelte-plugins/viewable

# pnpm
> pnpm install @svelte-plugins/viewable

# yarn
> yarn add @svelte-plugins/viewable

Usage

<script>
  import { Viewable } from "@svelte-plugins/viewable";

  const immediately = (definition) => {
    console.log('element has crossed the viewport');
  };

  const dwell = ({ percentage, duration }) => {
    console.log(`${percentage}% of the element was visible for at least ${duration} consecutive seconds.`);
  };

  const rules = {
    // do something when the element enters the viewport
    immediately: { duration: 0, percentage: 0, fn: immediately },
    // do something when 50% of the element is visible for 4.5 seconds (consecutively)
    dwell: { duration: 4.5, percentage: 50, fn: dwell },
  };

  let element;
</script>

<Viewable {rules} {element}>
  <div bind:this={element}>Hello World</div>
</Viewable>

Try the basic example in Svelte REPL.

API

Props

Prop nameDescriptionValue
elementElement to observeHTMLElement
rulesViewability rulesobject (default: null)
intervalRateRate to check measurement while intersecting (ms)number (default: 200)
gridSizeSize of the obstruction gridnumber (default: 20)
detectObstructionsIf true, obstructions impacting the observed elements view will be a factor during measurementboolean (default: false)
rootContaining elementnull or HTMLElement (default: null)
rootMarginMargin offset of the containing elementstring (default: "0px")
intersectingtrue if the observed element is intersectingboolean (default: false)
observerIntersectionObserver instanceIntersectionObserver
entryIntersectionObserver EntryIntersectionObserverEntry
debugIf true, debug ouput will be logged to consoleboolean (default: false)
rules
Prop nameDescriptionValue
durationConsecutive time (seconds) that the element must be in viewnumber (default: 0)
percentagePercentage of the element that must be viewablenumber (default: 0)
repeatIf true, the rule will be applied indefinitely v oncefunction (default: null)
fnCallback function to execute when rule has been metfunction (default: null)
const rules = {
  dwell: {
    duration: 1,
    percentage: 50,
    fn: () => {
      console.log('50% of the element was visible for at least 1 consecutive second.');
    }
  }
}

Debug props

The properties below can be used to assist with debugging any issues you might have (ex: bind:duration, bind:percent, etc.)

Prop nameDescriptionValue
durationViewable duration of the tracked elementnumber (default: 0)
percentPercentage of total viewable area (X+Y)number (default: 0)
percentXPercentage of horizontal viewable areanumber (default: 0)
percentYPercentage of vertical viewable areanumber (default: 0)
entryIntersectionObserver Entryobject (default: null)
observerIntersectionObserverobject (default: null)

Events

  • on:observe: Fired when an intersection change occurs (type IntersectionObserverEntry)
  • on:intersect: Fired when an intersection change occurs and the element is intersecting (type IntersectionObserverEntry)
  • on:complete: Fired when all rules have been executed

Development

Read the Contributions for instructions on how to setup your development environment.

Contributing

Contributions are always welcome and appreciated. Before contributing, please read the Code of Conduct.

Changelog

Changelog

License

MIT

Keywords

FAQs

Package last updated on 12 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc