{"id":27045034,"url":"https://github.com/mefechoel/interserver","last_synced_at":"2025-04-05T05:32:22.415Z","repository":{"id":57275317,"uuid":"261778613","full_name":"mefechoel/interserver","owner":"mefechoel","description":"IntersectionObserver simplified","archived":false,"fork":false,"pushed_at":"2020-05-14T12:52:58.000Z","size":57,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-22T05:16:18.639Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/mefechoel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-06T14:09:16.000Z","updated_at":"2020-05-14T12:53:01.000Z","dependencies_parsed_at":"2022-09-15T19:12:37.498Z","dependency_job_id":null,"html_url":"https://github.com/mefechoel/interserver","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mefechoel%2Finterserver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mefechoel%2Finterserver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mefechoel%2Finterserver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mefechoel%2Finterserver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mefechoel","download_url":"https://codeload.github.com/mefechoel/interserver/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247294195,"owners_count":20915332,"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":[],"created_at":"2025-04-05T05:31:24.994Z","updated_at":"2025-04-05T05:32:22.403Z","avatar_url":"https://github.com/mefechoel.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[npm]: https://img.shields.io/npm/v/interserver.svg?style=flat-square\n[npm-url]: https://npmjs.com/package/interserver\n\n# Interserver\n\n[![npm package][npm]][npm-url]\n![npm bundle size](https://img.shields.io/bundlephobia/min/interserver?style=flat-square)\n![NPM](https://img.shields.io/npm/l/interserver?style=flat-square)\n![GitHub last commit](https://img.shields.io/github/last-commit/mefechoel/interserver?style=flat-square)\n\n\u003e IntersectionObserver simplified\n\n`Interserver` is an easy way to check if a dom element is intersecting the viewport.\n\n## Features\n\n- Tiny (~1kb minified)\n- TypeScript ready\n- Framework agnostic (easily integrate `Interserver` with your favourite framework)\n- React companion package ([`interserver-react`](https://www.npmjs.com/package/interserver-react))\n- Svelte companion package ([`interserver-svelte`](https://www.npmjs.com/package/interserver-svelte))\n\n## Installation\n\nWith `yarn`:\n```bash\nyarn add interserver\n```\n\nWith `npm`:\n```bash\nnpm install --save interserver\n```\n\n## Usage\n\n```js\nimport interserver from \"interserver\";\n\nconst container = document.querySelector(\"#container\");\n\n// The handler is fired whenever `isIntersecting` changes\nfunction handleChange(isIntersecting) {\n  if (isIntersecting) {\n    console.log(\"Container is visible!\")\n  }\n}\n\nconst unobserve = interserver(container, handleChange);\n\n// Cancel observation after five seconds\nsetTimeout(unobserve, 5000);\n```\n\nIf you want to run cancel the observation after the first time, the container is visible, you can pass the `once` option to `interserver`:\n\n```js\nimport interserver from \"interserver\";\n\nconst container = document.querySelector(\"#container\");\n\nfunction handleChange(isIntersecting) {\n  if (isIntersecting) {\n    console.log(\"I will run only once.\")\n  }\n}\n\ninterserver(container, handleChange, { once: true });\n```\n\nYou can also specify margins around the element (`top`, `right`, `bottom`, `left`), so that the handler will fire when the container is the specified margin away from the viewport:\n\n```js\nimport interserver from \"interserver\";\n\nconst container = document.querySelector(\"#container\");\n\nfunction handleChange(isIntersecting) {\n  if (isIntersecting) {\n    console.log(\"I will run when I am 20px away from the viewport.\")\n  }\n}\n\ninterserver(\n  container,\n  handleChange,\n  { top: 20, right: 20, bottom: 20, left: 20 },\n);\n```\n\n## API\n\n```ts\n/**\n * Observe an element and invoke a callback, when it is intersecting the viewport.\n *\n * @param container The DOM element that is being observed.\n * @param onChange The callback handler,\n * that will be called when the `isIntersecting` state changes.\n * @param options The observer options,\n * consisting of offset margins for the container (`top`, `right`, `bottom`, `left`)\n * and `once`. With `once` set to `true`,\n * observing stops after the element is first intersecting.\n *\n * @returns The `unobserve` function. Observation is canceled, when it is called.\n */\nexport type Interserver = (\n  container: Element,\n  onChange: InterserverOnChange,\n  options?: InterserverOptions,\n) =\u003e InterserverUnsubscribe;\n\n/**\n * The callback handler, that will be called when the `isIntersecting` state changes.\n */\nexport type InterserverOnChange = (isIntersecting: boolean) =\u003e void;\n\n/**\n * The observer options,\n * consisting of offset margins for the container (`top`, `right`, `bottom`, `left`)\n * and `once`.\n * With `once` set to `true`, observing stops after the element is first intersecting.\n */\nexport interface InterserverOptions {\n  top?: number;\n  right?: number;\n  bottom?: number;\n  left?: number;\n  once?: boolean;\n}\n\n/**\n * The `unsubscribe` function returned from a call to `interserver`.\n * It should be called, when the observation is not needed any more,\n * to prevent memory leaks.\n * If `InterserverOptions.once` is set to true, the `unsubscribe`\n * function will be called internally.\n */\nexport type InterserverUnsubscribe = () =\u003e void;\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmefechoel%2Finterserver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmefechoel%2Finterserver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmefechoel%2Finterserver/lists"}