{"id":21966416,"url":"https://github.com/svelte-plugins/viewable","last_synced_at":"2025-04-24T05:08:30.179Z","repository":{"id":52802243,"uuid":"352424920","full_name":"svelte-plugins/viewable","owner":"svelte-plugins","description":"A rule-based approach to tracking element visibility.","archived":false,"fork":false,"pushed_at":"2024-02-13T04:57:35.000Z","size":359,"stargazers_count":37,"open_issues_count":0,"forks_count":6,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-31T17:58:27.650Z","etag":null,"topics":["advertising-and-tracking","beacons","events","iab","impressions","instrumentation","intersectionobserver","javascript","lazyload","measurement","observability","observer","svelte","tracking","viewability","viewable"],"latest_commit_sha":null,"homepage":"https://svelte-plugins-viewable.vercel.app","language":"Svelte","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/svelte-plugins.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2021-03-28T19:54:37.000Z","updated_at":"2024-10-16T09:34:26.000Z","dependencies_parsed_at":"2024-02-12T17:58:07.658Z","dependency_job_id":null,"html_url":"https://github.com/svelte-plugins/viewable","commit_stats":null,"previous_names":["svelte-plugins/svelte-viewable"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svelte-plugins%2Fviewable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svelte-plugins%2Fviewable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svelte-plugins%2Fviewable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svelte-plugins%2Fviewable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/svelte-plugins","download_url":"https://codeload.github.com/svelte-plugins/viewable/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227130256,"owners_count":17735120,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["advertising-and-tracking","beacons","events","iab","impressions","instrumentation","intersectionobserver","javascript","lazyload","measurement","observability","observer","svelte","tracking","viewability","viewable"],"created_at":"2024-11-29T13:16:00.875Z","updated_at":"2024-11-29T13:16:01.616Z","avatar_url":"https://github.com/svelte-plugins.png","language":"Svelte","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Unit Tests](https://github.com/svelte-plugins/viewable/actions/workflows/unit.yml/badge.svg)](https://github.com/svelte-plugins/viewable/actions/workflows/unit.yml)\n[![Integration Tests](https://github.com/svelte-plugins/viewable/actions/workflows/integration.yml/badge.svg?branch=main)](https://github.com/svelte-plugins/viewable/actions/workflows/integration.yml)\n[![codecov](https://codecov.io/gh/svelte-plugins/viewable/branch/main/graph/badge.svg?token=M4JCW0TXOV)](https://codecov.io/gh/svelte-plugins/viewable)\n\n# @svelte-plugins/viewable\n\nA simple rule-based approach to tracking element viewability.\n\nThis 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.\n\nIf you're simply looking for a Svelte flavor of IntersectionObserver visit [svelte-intersection-observer](https://github.com/metonym/svelte-intersection-observer).\n\nTry it in the [Svelte REPL](https://svelte.dev/repl/47fe04909fd14ee5ad5d02390cc147b1).\n\n## Install\n\n```bash\n# npm\n\u003e npm install @svelte-plugins/viewable\n\n# pnpm\n\u003e pnpm install @svelte-plugins/viewable\n\n# yarn\n\u003e yarn add @svelte-plugins/viewable\n```\n\n## Usage\n```svelte\n\u003cscript\u003e\n  import { Viewable } from \"@svelte-plugins/viewable\";\n\n  const immediately = (definition) =\u003e {\n    console.log('element has crossed the viewport');\n  };\n\n  const dwell = ({ percentage, duration }) =\u003e {\n    console.log(`${percentage}% of the element was visible for at least ${duration} consecutive seconds.`);\n  };\n\n  const rules = {\n    // do something when the element enters the viewport\n    immediately: { duration: 0, percentage: 0, fn: immediately },\n    // do something when 50% of the element is visible for 4.5 seconds (consecutively)\n    dwell: { duration: 4.5, percentage: 50, fn: dwell },\n  };\n\n  let element;\n\u003c/script\u003e\n\n\u003cViewable {rules} {element}\u003e\n  \u003cdiv bind:this={element}\u003eHello World\u003c/div\u003e\n\u003c/Viewable\u003e\n```\n\nTry the basic example in [Svelte REPL](https://svelte.dev/repl/c97c9abb9c944647a7dfed8f90d01da6?version=3.37.0).\n\n## API\n\n### Props\n| Prop name    | Description                                                       | Value                                                                                           |\n| :----------- | :---------------------------------------------------------------- | :---------------------------------------------------------------------------------------------- |\n| element      | Element to observe                                                | `HTMLElement`                                                                                   |\n| [rules](#rules)        | Viewability rules                                                 | `object` (default: `null`)                                                                      |\n| intervalRate | Rate to check measurement while intersecting (ms)                 | `number` (default: `200`)                                                                       |\n| gridSize     | Size of the obstruction grid                                      | `number` (default: `20`)                                                                        |\n| detectObstructions | If `true`, obstructions impacting the observed elements view will be a factor during measurement | `boolean` (default: `false`)                                                          |\n| root         | Containing element                                                | `null` or `HTMLElement` (default: `null`)                                                       |\n| rootMargin   | Margin offset of the containing element                           | `string` (default: `\"0px\"`)                                                                     |\n| intersecting | `true` if the observed element is intersecting                    | `boolean` (default: `false`)                                                                    |\n| observer     | IntersectionObserver instance                                     | [`IntersectionObserver`](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver) |\n| entry        | IntersectionObserver Entry                                           | [`IntersectionObserverEntry`](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry) |\n| debug        | If `true`, debug ouput will be logged to console                  | `boolean` (default: `false`)                                                                    |\n\n#### rules\n| Prop name    | Description                                                         | Value                               |\n| :----------- | :------------------------------------------------------------------ | :---------------------------------- |\n| duration     | Consecutive time (seconds) that the element must be in view         | `number` (default: `0`)             |\n| percentage   | Percentage of the element that must be viewable                     | `number` (default: `0`)             |\n| repeat       | If `true`, the rule will be applied indefinitely v once             | `function` (default: `null`)        |\n| fn           | Callback function to execute when rule has been met                 | `function` (default: `null`)        |\n\n\n```js\nconst rules = {\n  dwell: {\n    duration: 1,\n    percentage: 50,\n    fn: () =\u003e {\n      console.log('50% of the element was visible for at least 1 consecutive second.');\n    }\n  }\n}\n```\n\n### Debug props\n\nThe properties below can be used to assist with debugging any issues you might have (ex: `bind:duration`, `bind:percent`, etc.)\n\n| Prop name    | Description                                                       | Value                   |\n| :----------- | :---------------------------------------------------------------- | :---------------------- |\n| duration     | Viewable duration of the tracked element                          | `number` (default: `0`) |\n| percent      | Percentage of total viewable area (X+Y)                           | `number` (default: `0`) |\n| percentX     | Percentage of horizontal viewable area                            | `number` (default: `0`) |\n| percentY     | Percentage of vertical viewable area                              | `number` (default: `0`) |\n| entry        | IntersectionObserver Entry                                        | `object` (default: `null`) |\n| observer     | IntersectionObserver                                              | `object` (default: `null`) |\n\n### Events\n\n- **on:observe**: Fired when an intersection change occurs (type `IntersectionObserverEntry`)\n- **on:intersect**: Fired when an intersection change occurs and the element is intersecting (type `IntersectionObserverEntry`)\n- **on:complete**: Fired when all rules have been executed\n\n## Development\n\nRead the [Contributions](CONTRIBUTING.md) for instructions on how to setup your development environment.\n\n## Contributing\n\nContributions are always welcome and appreciated. Before contributing, please read the [Code of Conduct](COC.md).\n\n## Changelog\n\n[Changelog](CHANGELOG.md)\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsvelte-plugins%2Fviewable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsvelte-plugins%2Fviewable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsvelte-plugins%2Fviewable/lists"}