{"_id":"@byndyusoft-ui/use-intersection-observer","name":"@byndyusoft-ui/use-intersection-observer","dist-tags":{"latest":"0.0.1"},"versions":{"0.0.1":{"name":"@byndyusoft-ui/use-intersection-observer","version":"0.0.1","description":"Byndyusoft UI React Hook","keywords":["byndyusoft","byndyusoft-ui","react","hook","intersection-observer"],"author":{"name":"Gleb Fomin","email":"gleb.fom28@gmail.com"},"homepage":"https://github.com/Byndyusoft/ui/tree/master/hooks/use-intersection-observer#readme","license":"Apache-2.0","main":"dist/index.js","types":"dist/index.d.ts","repository":{"type":"git","url":"git+https://github.com/Byndyusoft/ui.git"},"scripts":{"build":"tsc --project tsconfig.build.json","clean":"rimraf dist && rimraf .turbo && rimraf node_modules && rimraf package-lock.json","lint":"eslint src --config ../../eslint.config.js","test":"jest --config ../../jest.config.js --roots hooks/use-intersection-observer/src"},"bugs":{"url":"https://github.com/Byndyusoft/ui/issues"},"publishConfig":{"access":"public"},"_id":"@byndyusoft-ui/use-intersection-observer@0.0.1","gitHead":"932aff4f5ee3978e9f371b56bdfe3fd8e3d3d481","_nodeVersion":"20.19.3","_npmVersion":"10.8.2","dist":{"integrity":"sha512-wbuSzKSb5iRIsXYByUeAnEo9Hj4gT6nZIeVb4+cAg8Gr8KgPKzrmvtTZoOeDd6r3vjTAhJMi//ZzEShgIxv4LQ==","shasum":"6baf8d8e38648840920db874947bab0f70967071","tarball":"https://registry.npmjs.org/@byndyusoft-ui/use-intersection-observer/-/use-intersection-observer-0.0.1.tgz","fileCount":13,"unpackedSize":18783,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQDWUWYt7pbGJ/k9M9y0ri7xlGry5weM5lwpzRE9OWdq5gIhANyI8v5Me2FsD5Idw0X7VOIrnqrzKYrQnwGfhwLZykEY"}]},"_npmUser":{"name":"sadcitizen","email":"sadcitizen@yandex.ru"},"directories":{},"maintainers":[{"name":"sadcitizen","email":"sadcitizen@yandex.ru"},{"name":"alexanderbyndyu","email":"alexander.byndyu@yandex.ru"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/use-intersection-observer_0.0.1_1771869722326_0.4677149728329226"},"_hasShrinkwrap":false}},"time":{"created":"2026-02-23T18:02:02.196Z","0.0.1":"2026-02-23T18:02:02.493Z","modified":"2026-02-23T18:02:03.113Z"},"maintainers":[{"name":"sadcitizen","email":"sadcitizen@yandex.ru"},{"name":"alexanderbyndyu","email":"alexander.byndyu@yandex.ru"}],"description":"Byndyusoft UI React Hook","homepage":"https://github.com/Byndyusoft/ui/tree/master/hooks/use-intersection-observer#readme","keywords":["byndyusoft","byndyusoft-ui","react","hook","intersection-observer"],"repository":{"type":"git","url":"git+https://github.com/Byndyusoft/ui.git"},"author":{"name":"Gleb Fomin","email":"gleb.fom28@gmail.com"},"bugs":{"url":"https://github.com/Byndyusoft/ui/issues"},"license":"Apache-2.0","readme":"# `@byndyusoft-ui/use-intersection-observer`\n---\n> A React hook that provides a simple and flexible way to use the Intersection Observer API, allowing you to track the visibility of DOM elements and react to their intersection with a specified root element or the viewport.\n\n### Installation\n\n```\nnpm i @byndyusoft-ui/use-intersection-observer\n```\n\n### Usage `useIntersectionObserver` hook\n```js\n// Use object destructuring, so you don't need to remember the exact order\nconst { isIntersecting, entry } = useIntersectionObserver(ref, options);\n\n// Or array destructuring, making it easy to customize the field names\nconst [isIntersecting, entry] = useIntersectionObserver(ref, options);\n```\n\n#### Default\n\n```jsx\nimport React, { useRef } from \"react\";\nimport {useIntersectionObserver} from \"@byndyusoft-ui/use-intersection-observer\";\n\nconst Component = () => {\n  const ref = useRef<HTMLDivElement | null>(null);\n  const { isIntersecting, entry } = useIntersectionObserver(ref);\n\n  return (\n    <div class=\"scroll-container\">\n      <div ref={ref}>\n        {`isIntersecting: ${isIntersecting}`}\n      </div>\n    </div>\n  );\n};\n```\n\n### Usage of `useIntersectionObserver` for Unmounted or Lazy-Loaded Components\n\n\n\n```jsx\nimport React, { useState } from \"react\";\nimport { useIntersectionObserver } from \"@byndyusoft-ui/use-intersection-observer\";\n\nconst Component = () => {\n  const [ref, setRef] = useState<HTMLDivElement | null>(null);\n  const [isVisible, setIsVisible] = useState<boolean>(false);\n  \n  const { isIntersecting, entry } = useIntersectionObserver({ current: ref });\n\n  return (\n    <div class=\"scroll-container\">\n      <button onClick={() => setIsVisible(p => !p)}>Toggle visible</button>\n      {isVisible && (\n          <div ref={setRef}>\n            {`isIntersecting: ${isIntersecting}`}\n          </div>\n      )}\n    </div>\n  );\n};\n```\n\n> The example below demonstrates a non-standard approach to using `useIntersectionObserver` with components that can be\n> unmounted or lazy-loaded. This method involves using `useState` to manage the reference to the target element, \n> which ensures that the intersection observer works correctly even when the target element is dynamically mounted or unmounted.\n\n\n### Usage `useIntersectionObserver` with options\n\n```jsx\nimport React from \"react\";\nimport { useIntersectionObserver } from \"@byndyusoft-ui/use-intersection-observer\";\n\nconst Component = () => {\n  const tergetRef = useRef<HTMLDivElement | null>(null);\n  const containerRef = useRef<HTMLDivElement | null>(null);\n\n  const { isIntersecting, entry } = useIntersectionObserver(tergetRef, {\n    root: scrollContainerRef.current,\n    rootMargin: \"10px\",\n    threshold: 0.5,\n    triggerOnce: false,\n    skip: false,\n    isIntersectingInitial: false,\n    isIntersectingFallback: false,\n    trackVisibility: false, // experimental\n    delay: 1500, // experimental\n    onChange: (isIntersecting, entry) => console.log(isIntersecting, entry),\n  });\n\n  return (\n    <div ref={containerRef} class=\"scroll-container\">\n      <div ref={tergetRef}>\n        {`isIntersecting: ${isIntersecting}`}\n      </div>\n    </div>\n  );\n};\n```\n### Options\n\nProvide these as the options argument in the `useIntersectionObserver ` hook.\n\n| Name                               | Type                              | Default     | Description                                                                                                                                                                                                                                                                                             |\n|------------------------------------|-----------------------------------| ----------- |---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| **root**                           | `Element`                         | `document`  | The Intersection Observer interface's read-only root property identifies the Element or Document whose bounds are treated as the bounding box of the viewport for the element which is the observer's target. If the root is `null`, then the bounds of the actual document viewport are used.          |\n| **rootMargin**                     | `string`                          | `'0px'`     | Margin around the root. Can have values similar to the CSS margin property, e.g. `\"10px 20px 30px 40px\"` (top, right, bottom, left). Also supports percentages, to check if an element intersects with the center of the viewport for example `\"-50% 0% -50% 0%\"`.                                      |\n| **threshold**                      | `number` or `number[]`            | `0`         | Number between `0` and `1` indicating the percentage that should be visible before triggering. Can also be an array of numbers, to create multiple trigger points.                                                                                                                                      |\n| **onChange**                       | `(isIntersecting, entry) => void` | `undefined` | Call this function whenever the `isIntersecting` state changes. It will receive the `isIntersecting` boolean, alongside the current `IntersectionObserverEntry`.                                                                                                                                |\n| **trackVisibility** (experimental) | `boolean`                         | `false`     | A boolean indicating whether this Intersection Observer will track visibility changes on the target.                                                                                                                                                                                                    |\n| **delay** (experimental)           | `number`                          | `undefined` | A number indicating the minimum delay in milliseconds between notifications from this observer for a given target. This must be set to at least `100` if `trackVisibility` is `true`.                                                                                                                   |\n| **skip**                           | `boolean`                         | `false`     | Skip creating the IntersectionObserver. You can use this to enable and disable the observer as needed. If `skip` is set while `isIntersecting`, the current state will still be kept.                                                                                                                   |\n| **triggerOnce**                    | `boolean`                         | `false`     | Only trigger the observer once.                                                                                                                                                                                                                                                                         |\n| **isIntersectingInitial**          | `boolean`                         | `false`     | Set the initial value of the `isIntersecting` boolean. This can be used if you expect the element to be in the viewport to start with, and you want to trigger something when it leaves.                                                                                                                |\n| **isIntersectingFallback**         | `boolean`                         | `undefined` | If the `IntersectionObserver` API isn't available in the client, the default behavior is to throw an Error. You can set a specific fallback behavior, and the `isIntersecting` value will be set to this instead of failing. To set a global default, you can set it with the `defaultFallbackInView()` |\n","readmeFilename":"README.md","_rev":"1-913b9902b11bbc5038f18a59da9108e8"}