• Stars
    star
    108
  • Rank 321,259 (Top 7 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

React Hooks for IntersectionObserver.

use-intersection

npm CircleCI

React Hooks for IntersectionObserver.

๐Ÿถ See Storybook.

Installation

$ yarn add use-intersection

Usage

Basic

This is the simplest example.

import React, { useRef } from 'react';
import { useIntersection } from 'use-intersection';

const Component: React.FC = () => {
  const target = useRef<HTMLDivElement>(null);
  const intersecting = useIntersection(target);

  return <div ref={target}>{intersecting ? 'visible' : 'invisible'}</div>;
};

Custom Root Element

This is an example of using scrollable elements other than Viewport.

import React, { useRef } from 'react';
import { useIntersection } from 'use-intersection';

const Component: React.FC = () => {
  const root = useRef<HTMLDivElement>(null);
  const target = useRef<HTMLDivElement>(null);
  const intersecting = useIntersection(target, {
    root,
    rootMargin: '100px',
  });

  return (
    <div style={{ overflow: 'hidden auto', height: 300 }}>
      {/* ... */}
      <div ref={target}>{intersecting ? 'visible' : 'invisible'}</div>
      {/* ... */}
    </div>
  );
};

Lazy Image

This is an example of an Image component that delays loading.

import React, { useRef } from 'react';
import { useIntersection } from 'use-intersection';

const LazyImage: React.FC<React.ComponentProps<'img'>> = (props) => {
  const target = useRef<HTMLSpanElement>(null);
  const intersected = useIntersection(target, {
    rootMargin: '250px',
    once: true,
  });

  return <span ref={target}>{intersected && <img {...props} />}</span>;
};

Browser support

Supports modern web browser.

If your browser does not support IntersectionObserver, we recommend using Polyfill.

npm package

$ yarn add intersection-observer

https://www.npmjs.com/package/intersection-observer

CDN

<script src="https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserverEntry%2CIntersectionObserver"></script>

https://polyfill.io/v3/

API

The following resources will help you.


useIntersection

useIntersection returns a flag whether the target intersects.

const useIntersection = (
  target: React.RefObject<Element> | Element | null,
  options: IntersectionOptions = {},
  callback?: IntersectionChangeHandler,
) => boolean;

options: IntersectionOptions

type IntersectionOptions = {
  root?: React.RefObject<Element>;
  rootMargin?: string;
  threshold?: number | number[];
  once?: boolean;
  defaultIntersecting?: boolean;
};

callback: IntersectionChangeHandler

type IntersectionChangeHandler = (entry: IntersectionObserverEntry) => void;

CHANGELOG

See CHANGELOG.md.

Contributing

We are always welcoming your contribution ๐Ÿ‘

  1. Fork (https://github.com/cats-oss/use-intersection) ๐ŸŽ‰
  2. Create a feature branch โ˜•
  3. Run test suite with the $ yarn test command and confirm that it passes โšก
  4. Commit your changes ๐Ÿ“
  5. Rebase your local changes against the master branch ๐Ÿ’ก
  6. Create new Pull Request ๐Ÿ’Œ

Bugs, feature requests and comments are more than welcome in the issues.

Development scripts

yarn storybook

Run Storybook.

$ yarn storybook

yarn test

Run Unit test with Jest.

$ yarn test

yarn lint

Run lint with ESLint.

$ yarn lint

yarn format

Run formatting with ESLint (--fix) and Prettier.

$ yarn format

License

MIT ยฉ Cyberagent, Inc

More Repositories

1

android-gpuimage

Android filters based on OpenGL (idea from GPUImage for iOS)
Java
8,952
star
2

Sica

๐ŸฆŒ Simple Interface Core Animation. Run type-safe animation sequencially or parallelly
Swift
1,056
star
3

VideoCast-Swift

A framework for broadcasting live video
Swift
190
star
4

Chausie

Chausie provides a customizable view containers that manages navigation between pages of content. ๐Ÿฑ
Swift
178
star
5

Unio

๐Ÿ”„ KeyPath based Unidirectional Input / Output framework with RxSwift.
Swift
159
star
6

android-tab-animation

Easily create TabLayout.Tab animations that sync with the scrolling progress of ViewPager
Kotlin
104
star
7

android-license-sample

Kotlin
86
star
8

AcknowledgementsPlist

AcknowledgementsPlist manages the licenses of libraries that depend on your iOS app.
Swift
76
star
9

Matcha

๐Ÿต Matcha is Framework that can get parameters from URL Path.
Swift
58
star
10

grpc-swift-client

๐Ÿ” Client-side library that depends on SwiftGRPC which is a library of gRPC written in Swift.
Swift
49
star
11

cujira

๐Ÿณ cujira is a command line tool that makes easy to show issue list from Jira.
Swift
48
star
12

fastlane-plugin-firebase_test_lab_android

Test your app with Firebase Test Lab with ease using fastlane for Android
Ruby
48
star
13

RxDucks

๐Ÿฆ† RxDucks is a Redux-like framework working on RxSwift.
Swift
40
star
14

Ship

๐Ÿšข Ship is a APIKit plugin that can inject common processing to requests on APIKit.
Swift
28
star
15

yarn-outdated-notifier

๐Ÿš€ Add link to CHANGELOG the result of $ yarn outdated, and notify to GitHub Issue.
JavaScript
25
star
16

Degu

๐Ÿญ Degu is debug utility for iOS, tvOS and macOS.
Swift
24
star
17

Mag

๐ŸงฒMag is Framework which wraps NSLayoutAnchor.
Swift
21
star
18

kite

A Kotlin DSL to bind Android UI components to your app state.
Kotlin
20
star
19

github-action-auto-assign

The GitHub Actions assigns reviewers & changes the status labels.
JavaScript
18
star
20

xcbt

โŒ›๏ธxcbt shows Xcode build time of a specified project.
Swift
14
star
21

intly

intly is Type-safe (TS friendly) i18n library.
TypeScript
12
star
22

ts-proto-optimize

It's CLI tool for optimize TypeScript protobuf type-definition file.
TypeScript
9
star
23

fastlane-plugin-accessibility_test

Java
8
star
24

ExtensionProperty

๐Ÿ—œThe utility interface for Associated Object
Swift
8
star
25

fastlane-plugin-snapshot_test

Ruby
7
star
26

docker-node-headless-chrome-ja

๐Ÿณ Docker image for headless Chrome.
Dockerfile
6
star
27

github-action-detect-unmergeable

The GitHub Action to detect an unmergeable pull request on changing its upstream.
Go
5
star
28

eslint-config-abema

This project is presets of eslint configurations which we used in our some internal projects.
JavaScript
4
star
29

docker-node-headless-chrome

Docker Image for Chrome headless.
Dockerfile
3
star
30

fastlane-plugin-screenshot_notifier

2
star
31

hugo-cats-studio

A simple and minimal Hugo theme.
CSS
1
star