Skip to content

Commit

Permalink
Updates intersection observer
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredLunde committed Oct 29, 2019
1 parent 5e5a093 commit 8ec5fc4
Show file tree
Hide file tree
Showing 17 changed files with 4,347 additions and 751 deletions.
39 changes: 18 additions & 21 deletions packages/intersection-observer/.babelrc
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
{
"presets": [
["@stellar-apps/react", {"removePropTypes": false}]
"@babel/preset-react",
[
"@lunde/es",
{
"env": {
"targets": {
"node": "8"
}
},
"typescript": false
}
]
],
"env": {
"cjs": {
"presets": [
[
"@stellar-apps/es",
{
"env": {
"useBuiltIns": false,
"targets": {"browsers": ">5% in US"}
},
"runtime": false,
"closureElimination": false
}
]
]
},
"es": {
"presets": [
[
"@stellar-apps/es",
"@lunde/es",
{
"env": {
"useBuiltIns": false,
"modules": false,
"targets": {"browsers": ">5% in US"}
"targets": {
"browsers": "> 2%"
}
},
"runtime": false,
"closureElimination": false
"objectAssign": false,
"typescript": false
}
]
]
Expand Down
3 changes: 3 additions & 0 deletions packages/intersection-observer/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
/coverage
/dist
51 changes: 51 additions & 0 deletions packages/intersection-observer/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true,
"experimentalObjectRestSpread": true
}
},
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:jest/recommended"
],
"plugins": ["jest", "react", "react-hooks"],
"rules": {
"no-console": "off",
"no-prototype-builtins": "off",
"react/no-children-prop": "off",
"react/display-name": "off",
"react/prop-types": [2, {"ignore": ["children"]}]
},
"settings": {
"react": {
"pragma": "React",
"version": "detect"
}
},
"env": {
"browser": true,
"node": true,
"es6": true,
"jest/globals": true
},
"globals": {
"__DEV__": true
},
"overrides": [
{
"files": ["**/test.js", "**/*.test.js"],
"settings": {
"import/resolver": {
"jest": {
"jestConfigFile": "./jest.config.js"
}
}
}
}
]
}
5 changes: 1 addition & 4 deletions packages/intersection-observer/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
.git
node_modules
.DS_Store
*.log
dist
.idea
coverage
4 changes: 0 additions & 4 deletions packages/intersection-observer/.npmignore

This file was deleted.

2 changes: 2 additions & 0 deletions packages/intersection-observer/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
/coverage
7 changes: 7 additions & 0 deletions packages/intersection-observer/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": false,
"singleQuote": true,
"bracketSpacing": false
}
2 changes: 1 addition & 1 deletion packages/intersection-observer/LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The MIT License (MIT)
MIT License

Copyright (c) 2019 Jared Lunde

Expand Down
104 changes: 42 additions & 62 deletions packages/intersection-observer/README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,77 +1,57 @@
[![Bundlephobia](https://img.shields.io/bundlephobia/minzip/@react-hook/intersection-observer?style=for-the-badge)](https://bundlephobia.com/result?p=@react-hook/intersection-observer)
[![License](https://img.shields.io/github/license/jaredLunde/react-hook?style=for-the-badge)](https://jaredlunde.mit-license.org/)

---

# @react-hook/intersection-observer
A React hook for the `IntersectionObserver` API that uses a polyfill when the native API is not available.

The Intersection Observer API provides a way to asynchronously observe changes in
the intersection of a target element with an ancestor element or with a top-level document's viewport. The ancestor
A React hook for the IntersectionObserver API that uses a polyfill when the native API is not available

The Intersection Observer API provides a way to asynchronously observe changes in
the intersection of a target element with an ancestor element or with a top-level document's viewport. The ancestor
element or viewport is referred to as the root.

When an IntersectionObserver is created, it's configured to watch for given ratios of visibility within the root. The
configuration cannot be changed once the IntersectionObserver is created, so a given observer object is only useful for
When an IntersectionObserver is created, it's configured to watch for given ratios of visibility within the root. The
configuration cannot be changed once the IntersectionObserver is created, so a given observer object is only useful for
watching for specific changes in degree of visibility.

## Installation
`yarn add @react-hook/intersection-observer`

#### `npm i @react-hook/intersection-observer`

#### `yarn add @react-hook/intersection-observer`

## Usage

```js
import useIntersectionObserver from '@react-hook/intersection-observer'

const F = props => {
const [observerRef, entry] = useIntersectionObserver()
return (
<div ref={observerRef}>
Is intersecting? {entry.isIntersecting}
</div>
)
const Component = () => {
const [entry, observerRef] = useIntersectionObserver()
return <div ref={observerRef}>Is intersecting? {entry.isIntersecting}</div>
}
```
### `useIntersectionObserver(options: object)`
### Options
- `root {DOM Element}`
- **default** `document`
- A specific ancestor of the target element being observed. If no value was passed to the constructor or this is
`null`, the top-level document's viewport is used
- `rootMargin {string}`
- **default** `0 0 0 0`
- Margin around the root. Can have values similar to the CSS margin property, e.g.
"10px 20px 30px 40px" (top, right, bottom, left). The values can be percentages.
This set of values serves to grow or shrink each side of the root element's bounding
box before computing intersections.
- `thresholds {number|Array}`
- **default** `0`
- Either a single number or an array of numbers which indicate at what percentage of the
target's visibility the observer's callback should be executed. If you only want to
detect when visibility passes the 50% mark, you can use a value of `0.5`. If you want the
callback to run every time visibility passes another 25%, you would specify the array
`[0, 0.25, 0.5, 0.75, 1]`. The default is 0 (meaning as soon as even one pixel is visible,
the callback will be run). A value of 1.0 means that the threshold isn't considered passed until
every pixel is visible.
- `pollInterval {number}`
- **default** `null`
- The frequency in which the polyfill polls for intersection changes
- Only relevant if you'd like to detect changes for any of the following:
- CSS changes on `:hover`, `:active`, or `:focus` states
- CSS changes due to transitions or animations with a long initial delay
- Resizable `<textarea>` elements that cause other elements to
move around
- Scrolling of non-document elements in browsers that don't support the event capture phase
- `useMutationObserver {bool}`
- **default** `true`
- You can also choose to not check for intersections in the polyfill
when the DOM changes by setting this to `false`
- `initialIsIntersecting {bool}`
- **default** `false`
- Changes the default value of `isIntersecting` for use in places like SSR
### Return value
**Array** `[domRef, entry]`
- `domRef`
- **React.ref**
- You provide this to the dom element you want to monitor via the
`ref` property
- `entry`
- This is the
[IntersectionObserverEntry](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry)
object returned by the `IntersectionObserver` callback.
### `useIntersectionObserver(options?: Object): [entry, ref]`
#### Options
| Property | Type | Default | Description |
| --------------------- | --------------------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| root | `DOMElement` | `document` | A specific ancestor of the target element being observed. If no value was passed to the constructor or this is `null`, the top-level document's viewport is used |
| rootMargin | `string` | `"0 0 0 0"` | Margin around the root. Can have values similar to the CSS margin property, e.g. "10px 20px 30px 40px" (top, right, bottom, left). The values can be percentages. This set of values serves to grow or shrink each side of the root element's bounding box before computing intersections. |
| thresholds | <code>Number&#124;Number[]</code> | `0` | Either a single number or an array of numbers which indicate at what percentage of the target's visibility the observer's callback should be executed. If you only want to detect when visibility passes the 50% mark, you can use a value of `0.5`. If you want the callback to run every time visibility passes another 25%, you would specify the array `[0, 0.25, 0.5, 0.75, 1]`. The default is 0 (meaning as soon as even one pixel is visible, the callback will be run). A value of 1.0 means that the threshold isn't considered passed until every pixel is visible. |
| pollInterval | `Number` | `null` | The frequency in which the polyfill polls for intersection changes. |
| useMutationObserver | `bool` | `true` | You can also choose to not check for intersections in the polyfill when the DOM changes by setting this to `false` |
| initialIsIntersecting | `bool` | `false` | Changes the default value of `isIntersecting` for use in places like SSR |
#### Returns `[entry, ref]`
| Variable | Type | Description |
| -------- | --------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| entry | `IntersectionObserverEntry` | This is the [IntersectionObserverEntry](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry) object returned by the `IntersectionObserver` callback. |
| ref | `React.ref` | Provide this to the React element you want to monitor via the `ref` property |
## LICENSE
MIT
25 changes: 25 additions & 0 deletions packages/intersection-observer/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const path = require('path')

module.exports = {
// testEnvironment: 'jest-environment-jsdom',
moduleDirectories: [
'node_modules',
path.join(__dirname, 'src'),
path.join(__dirname, 'test'),
],
// moduleNameMapper: {},
setupFilesAfterEnv: [require.resolve('./test/setup.js')],
snapshotResolver: require.resolve('./test/resolve-snapshot.js'),
collectCoverageFrom: ['**/src/**/*.{js,jsx}'],
// coverageThreshold: {
// global: {
// statements:17,
// branches: 4,
// lines: 17,
// functions: 20
// }
// },
globals: {
__DEV__: true,
},
}
83 changes: 58 additions & 25 deletions packages/intersection-observer/package.json
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
{
"name": "@react-hook/intersection-observer",
"version": "1.0.1",
"main": "dist/cjs/index.js",
"author": "Jared Lunde <[email protected]> (https://BeStellar.co)",
"version": "1.0.0",
"homepage": "https://github.com/jaredLunde/react-hook#readme",
"repository": "github:jaredLunde/react-hook",
"bugs": "https://github.com/jaredLunde/react-hook/issues",
"author": "Jared Lunde <[email protected]> (https://jaredLunde.com)",
"license": "MIT",
"sideEffects": false,
"module": "dist/es/index.js",
"repository": "https://github.com/jaredLunde/react-hook/tree/master/packages/intersection-observer",
"scripts": {
"build": "yarn run build:es && yarn run build:cjs",
"build:es": "rimraf dist/es && cross-env NODE_ENV=production BABEL_ENV=es babel src --out-dir dist/es && npm run prettier:es",
"build:cjs": "rimraf dist/cjs && cross-env NODE_ENV=production BABEL_ENV=cjs babel src --out-dir dist/cjs && npm run prettier:cjs",
"watch:es": "rimraf dist/es && cross-env NODE_ENV=production BABEL_ENV=es babel src -w --out-dir dist/es",
"prettier": "prettier --single-quote --no-semi --no-bracket-spacing --trailing-comma es5 --write",
"prettier:es": "yarn prettier \"dist/es/**/*.js\"",
"prettier:cjs": "yarn prettier \"dist/cjs/**/*.js\"",
"prepublishOnly": "yarn build"
},
"description": "",
"keywords": [
"react",
"react hook",
Expand All @@ -26,17 +16,60 @@
"intersection",
"intersection observer"
],
"main": "dist/cjs/index.js",
"module": "dist/es/index.js",
"files": [
"/dist"
],
"sideEffects": false,
"scripts": {
"build": "npm run build:cjs && npm run build:es",
"build:cjs": "babel src -d dist/cjs -x .js --ignore \"**/*.test.js\",\"**/test.js\" --delete-dir-on-start",
"build:es": "cross-env BABEL_ENV=es babel src -d dist/es -x .js --ignore \"**/*.test.js\",\"**/test.js\" --delete-dir-on-start",
"format": "prettier --write \"**/*.{js,jsx,md,yml,json,babelrc,eslintrc,prettierrc}\"",
"lint": "eslint .",
"prepublishOnly": "npm run lint && npm run test && npm run build && npm run format",
"test": "jest",
"validate": "npm run lint && npm run test -- --coverage"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"**/*.{js,jsx}": [
"eslint",
"prettier --write"
],
"**/*.{md,yml,json,babelrc,eslintrc,prettierrc}": [
"prettier --write"
]
},
"devDependencies": {
"@stellar-apps/babel-preset-es": "^1.0.4",
"@stellar-apps/babel-preset-react": "^1.0.1",
"prettier": "^1.16.4",
"@babel/preset-react": "latest",
"@lunde/babel-preset-es": "latest",
"@testing-library/jest-dom": "latest",
"@testing-library/react": "latest",
"@testing-library/react-hooks": "latest",
"babel-eslint": "latest",
"cross-env": "latest",
"eslint": "latest",
"eslint-import-resolver-jest": "latest",
"eslint-plugin-jest": "latest",
"eslint-plugin-react": "latest",
"eslint-plugin-react-hooks": "latest",
"husky": "latest",
"jest": "latest",
"lint-staged": "latest",
"prettier": "latest",
"react": "latest",
"react-dom": "latest",
"react-test-renderer": "latest",
"rimraf": "^2.6.3"
},
"peerDependencies": {
"prop-types": "^15.6.0",
"react": "^16.8.0"
},
"dependencies": {
"intersection-observer": "^0.5.1"
}
"@react-hook/passive-layout-effect": "^1.0.1"
},
"peerDependencies": {}
}
Loading

0 comments on commit 8ec5fc4

Please sign in to comment.