Skip to content

Commit cb2ee1d

Browse files
authored
Restructure publishing (#222)
* v8.22.6 * Restructure how files are published, so we publish the "dist" directory This flattens the list of files published, creating cleaner imports As part of this, types have also been moved around * Sync with Prettier
1 parent 8396a0f commit cb2ee1d

File tree

11 files changed

+207
-95
lines changed

11 files changed

+207
-95
lines changed

package.json

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
{
22
"name": "react-intersection-observer",
3-
"version": "8.22.5",
3+
"version": "8.22.6",
44
"description": "Monitor if a component is inside the viewport, using IntersectionObserver API",
5-
"main": "dist/react-intersection-observer.cjs.js",
6-
"module": "dist/react-intersection-observer.esm.js",
7-
"unpkg": "dist/react-intersection-observer.umd.min.js",
8-
"typings": "dist/index.d.ts",
5+
"main": "react-intersection-observer.cjs.js",
6+
"module": "react-intersection-observer.esm.js",
7+
"unpkg": "react-intersection-observer.umd.min.js",
8+
"typings": "index.d.ts",
99
"author": "Daniel Schmidt",
10-
"files": [
11-
"dist/*",
12-
"test-utils.js",
13-
"test-utils.d.ts"
14-
],
10+
"private": true,
1511
"repository": {
1612
"type": "git",
1713
"url": "https://github.com/thebuilder/react-intersection-observer.git"
@@ -49,6 +45,7 @@
4945
"build": "run-s build:*",
5046
"build:lib": "rollup -c",
5147
"build:ts": "tsc -p tsconfig.build.json && tsc -p tsconfig.test.json",
48+
"build:copy": "copyfiles -f package.json README.md LICENSE dist && json -I -f dist/package.json -e \"this.private=false; this.devDependencies=undefined; this.optionalDependencies=undefined; this.scripts=undefined; this.husky=undefined; this.prettier=undefined; this.jest=undefined; this.eslintConfig=undefined; this.eslintIgnore=undefined; this.np=undefined;\"",
5249
"dev": "concurrently -k -r 'jest --watch' 'yarn run storybook'",
5350
"lint": "eslint . --ext js,ts,tsx",
5451
"preversion": "yarn build",
@@ -57,6 +54,9 @@
5754
"storybook:build": "build-storybook --output-dir example",
5855
"test": "jest"
5956
},
57+
"np": {
58+
"contents": "dist"
59+
},
6060
"husky": {
6161
"hooks": {
6262
"pre-commit": "tsc && lint-staged"
@@ -106,7 +106,7 @@
106106
},
107107
"dependencies": {
108108
"@babel/runtime": "^7.0.0",
109-
"invariant": "^2.0.0"
109+
"invariant": "^2.2.4"
110110
},
111111
"peerDependencies": {
112112
"react": "^15.0.0 || ^16.0.0 || ^17.0.0"
@@ -136,6 +136,7 @@
136136
"babel-jest": "^24.5.0",
137137
"babel-loader": "^8.0.5",
138138
"concurrently": "^4.1.0",
139+
"copyfiles": "^2.1.0",
139140
"coveralls": "^3.0.3",
140141
"eslint": "^5.15.2",
141142
"eslint-config-react-app": "^4.0.0",
@@ -148,6 +149,7 @@
148149
"intersection-observer": "^0.6.0",
149150
"jest": "^24.5.0",
150151
"jest-dom": "^3.1.3",
152+
"json": "^9.0.6",
151153
"lint-staged": "^8.1.5",
152154
"npm-run-all": "^4.1.5",
153155
"prettier": "^1.16.2",

rollup.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const getBabelOptions = ({ useESModules }) => ({
2626
export default [
2727
{
2828
input: './src/index.tsx',
29-
output: { file: pkg.module, format: 'esm', exports: 'named' },
29+
output: { file: 'dist/' + pkg.module, format: 'esm', exports: 'named' },
3030
external,
3131
plugins: [
3232
resolve({ extensions }),
@@ -35,7 +35,7 @@ export default [
3535
},
3636
{
3737
input: './src/index.tsx',
38-
output: { file: pkg.main, format: 'cjs', exports: 'named' },
38+
output: { file: 'dist/' + pkg.main, format: 'cjs', exports: 'named' },
3939
external,
4040
plugins: [
4141
resolve({ extensions }),
@@ -45,7 +45,7 @@ export default [
4545
{
4646
input: './src/index.tsx',
4747
output: {
48-
file: pkg.unpkg,
48+
file: 'dist/' + pkg.unpkg,
4949
format: 'umd',
5050
name: 'ReactIntersectionObserver',
5151
globals,

src/InView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react'
22
import invariant from 'invariant'
33
import { observe, unobserve } from './intersection'
4-
import { IntersectionObserverProps, PlainChildrenProps } from './typings/types'
4+
import { IntersectionObserverProps, PlainChildrenProps } from './index'
55

66
type State = {
77
inView: boolean

src/index.tsx

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,73 @@
1+
import * as React from 'react'
12
import { InView } from './InView'
3+
24
export { InView } from './InView'
35
export { useInView } from './useInView'
4-
56
export default InView
7+
8+
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
9+
10+
export type ObserverInstanceCallback = (
11+
inView: boolean,
12+
intersection: IntersectionObserverEntry,
13+
) => void
14+
15+
export type ObserverInstance = {
16+
callback: ObserverInstanceCallback
17+
element: Element
18+
inView: boolean
19+
observerId: string
20+
observer: IntersectionObserver
21+
thresholds: number[]
22+
}
23+
24+
interface RenderProps {
25+
inView: boolean
26+
entry: IntersectionObserverEntry | undefined
27+
ref: React.RefObject<any> | ((node?: Element | null) => void)
28+
}
29+
30+
export interface IntersectionOptions extends IntersectionObserverInit {
31+
/** Only trigger the inView callback once */
32+
triggerOnce?: boolean
33+
}
34+
35+
export interface IntersectionObserverProps extends IntersectionOptions {
36+
/**
37+
* Children expects a function that receives an object
38+
* contain an `inView` boolean and `ref` that should be
39+
* assigned to the element root.
40+
*/
41+
children: (fields: RenderProps) => React.ReactNode
42+
43+
/** Call this function whenever the in view state changes */
44+
onChange?: (inView: boolean, entry: IntersectionObserverEntry) => void
45+
}
46+
47+
/**
48+
* Types specific to the PlainChildren rendering of InView
49+
* */
50+
export type PlainChildrenProps = IntersectionOptions & {
51+
children: React.ReactNode
52+
53+
/**
54+
* Render the wrapping element as this element.
55+
* @default `'div'`
56+
*/
57+
as?: React.ReactType<any>
58+
59+
/**
60+
* Element tag to use for the wrapping component
61+
* @deprecated Replace with the 'as' prop
62+
*/
63+
tag?: React.ReactType<any>
64+
65+
/** Call this function whenever the in view state changes */
66+
onChange?: (inView: boolean, entry: IntersectionObserverEntry) => void
67+
} & Omit<React.HTMLProps<HTMLElement>, 'onChange'>
68+
69+
export type InViewHookResponse = [
70+
((node?: Element | null) => void),
71+
boolean,
72+
IntersectionObserverEntry | undefined
73+
]

src/intersection.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
import invariant from 'invariant'
2-
3-
type Callback = (
4-
inView: boolean,
5-
intersection: IntersectionObserverEntry,
6-
) => void
7-
8-
export type ObserverInstance = {
9-
callback: Callback
10-
element: Element
11-
inView: boolean
12-
observerId: string
13-
observer: IntersectionObserver
14-
thresholds: number[]
15-
}
2+
import { ObserverInstance, ObserverInstanceCallback } from './index'
163

174
const INSTANCE_MAP: Map<Element, ObserverInstance> = new Map()
185
const OBSERVER_MAP: Map<string, IntersectionObserver> = new Map()
@@ -43,7 +30,7 @@ function getRootId(root?: Element | null) {
4330
*/
4431
export function observe(
4532
element: Element,
46-
callback: Callback,
33+
callback: ObserverInstanceCallback,
4734
options: IntersectionObserverInit = {},
4835
) {
4936
// IntersectionObserver needs a threshold to trigger, so set it to 0 if it's not defined.

src/typings/types.ts

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/useInView.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
/* eslint-disable react-hooks/exhaustive-deps */
22
import * as React from 'react'
33
import { observe, unobserve } from './intersection'
4-
import { HookResponse, IntersectionOptions } from './typings/types'
4+
import { InViewHookResponse, IntersectionOptions } from './index'
55

66
type State = {
77
inView: boolean
88
entry?: IntersectionObserverEntry
99
}
1010

11-
export function useInView(options: IntersectionOptions = {}): HookResponse {
11+
export function useInView(
12+
options: IntersectionOptions = {},
13+
): InViewHookResponse {
1214
const [ref, setRef] = React.useState<Element | null | undefined>(null)
1315
const [state, setState] = React.useState<State>({
1416
inView: false,

stories/Hooks.story.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import * as React from 'react'
22
import { storiesOf } from '@storybook/react'
33
import { action } from '@storybook/addon-actions'
4-
import { useInView } from '../src/index'
4+
import { IntersectionOptions, useInView } from '../src/index'
55
import ScrollWrapper from './ScrollWrapper/index'
66
import { CSSProperties } from 'react'
7-
import { IntersectionOptions } from '../src/typings/types'
87
import { withKnobs, number, boolean } from '@storybook/addon-knobs'
98
import Status from './Status'
109

tsconfig.test.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"module": "commonjs",
55
"target": "es6",
66
"declaration": true,
7-
"emitDeclarationOnly": true,
8-
"outDir": ".",
7+
"emitDeclarationOnly": false,
8+
"outDir": "dist",
99
"noEmit": false
1010
},
1111
"files": ["src/test-utils.ts"]

0 commit comments

Comments
 (0)