@1hook/use-in-view 0.0.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +21 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/index.min.js.gz +0 -0
- package/package.json +39 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { UseIntersectionObserverOptions } from '@1hook/use-intersection-observer';
|
|
3
|
+
|
|
4
|
+
type UseInViewOptions = UseIntersectionObserverOptions & {
|
|
5
|
+
/**
|
|
6
|
+
* Set to `false` to avoid tracking inView state for better performance
|
|
7
|
+
*
|
|
8
|
+
* default: `true`
|
|
9
|
+
*/
|
|
10
|
+
trackState?: boolean;
|
|
11
|
+
onChange?: (inView: boolean) => void;
|
|
12
|
+
};
|
|
13
|
+
declare const useInView: (options?: UseInViewOptions) => {
|
|
14
|
+
inView: boolean | undefined;
|
|
15
|
+
observe: () => void;
|
|
16
|
+
unobserve: () => void;
|
|
17
|
+
ref: React.Dispatch<React.SetStateAction<Element | null>>;
|
|
18
|
+
target: Element | null;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export { type UseInViewOptions, useInView };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { usePrevious } from '@1hook/use-previous';
|
|
3
|
+
import { useIntersectionObserver } from '@1hook/use-intersection-observer';
|
|
4
|
+
import { useEventHandler } from '@1hook/use-event-handler';
|
|
5
|
+
|
|
6
|
+
// src/index.ts
|
|
7
|
+
var useInView = (options = {}) => {
|
|
8
|
+
const [inView, setInView] = React.useState();
|
|
9
|
+
const onChange = useEventHandler(options.onChange);
|
|
10
|
+
let prevIntersecting = React.useRef(inView);
|
|
11
|
+
const observer = useIntersectionObserver((entry) => {
|
|
12
|
+
(options.trackState ?? true) && setInView(entry.isIntersecting);
|
|
13
|
+
if (entry.isIntersecting !== prevIntersecting.current) {
|
|
14
|
+
onChange(entry.isIntersecting);
|
|
15
|
+
}
|
|
16
|
+
prevIntersecting.current = entry.isIntersecting;
|
|
17
|
+
}, options);
|
|
18
|
+
const prevTarget = usePrevious(observer.target);
|
|
19
|
+
return {
|
|
20
|
+
...observer,
|
|
21
|
+
// set to false if the target unmounts
|
|
22
|
+
inView: prevTarget && !observer.target ? false : inView
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export { useInView };
|
|
27
|
+
//# sourceMappingURL=index.js.map
|
|
28
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAkBO,IAAM,SAAY,GAAA,CAAC,OAA4B,GAAA,EAAO,KAAA;AAC3D,EAAA,MAAM,CAAC,MAAA,EAAQ,SAAS,CAAA,GAAI,MAAM,QAAkB,EAAA;AACpD,EAAM,MAAA,QAAA,GAAW,eAAgB,CAAA,OAAA,CAAQ,QAAQ,CAAA;AACjD,EAAI,IAAA,gBAAA,GAAmB,KAAM,CAAA,MAAA,CAAO,MAAM,CAAA;AAE1C,EAAM,MAAA,QAAA,GAAW,uBAAwB,CAAA,CAAC,KAAU,KAAA;AACjD,IAAA,CAAC,OAAQ,CAAA,UAAA,IAAc,IAAS,KAAA,SAAA,CAAU,MAAM,cAAc,CAAA;AAC/D,IAAI,IAAA,KAAA,CAAM,cAAmB,KAAA,gBAAA,CAAiB,OAAS,EAAA;AACrD,MAAA,QAAA,CAAS,MAAM,cAAc,CAAA;AAAA;AAE/B,IAAA,gBAAA,CAAiB,UAAU,KAAM,CAAA,cAAA;AAAA,KAChC,OAAO,CAAA;AAEV,EAAM,MAAA,UAAA,GAAa,WAAY,CAAA,QAAA,CAAS,MAAM,CAAA;AAE9C,EAAO,OAAA;AAAA,IACL,GAAG,QAAA;AAAA;AAAA,IAEH,MAAQ,EAAA,UAAA,IAAc,CAAC,QAAA,CAAS,SAAS,KAAQ,GAAA;AAAA,GACnD;AACF","file":"index.js","sourcesContent":["import React from 'react'\nimport { usePrevious } from '@1hook/use-previous'\nimport {\n useIntersectionObserver,\n type UseIntersectionObserverOptions,\n} from '@1hook/use-intersection-observer'\nimport { useEventHandler } from '@1hook/use-event-handler'\n\nexport type UseInViewOptions = UseIntersectionObserverOptions & {\n /**\n * Set to `false` to avoid tracking inView state for better performance\n *\n * default: `true`\n */\n trackState?: boolean\n onChange?: (inView: boolean) => void\n}\n\nexport const useInView = (options: UseInViewOptions = {}) => {\n const [inView, setInView] = React.useState<boolean>()\n const onChange = useEventHandler(options.onChange)\n let prevIntersecting = React.useRef(inView)\n\n const observer = useIntersectionObserver((entry) => {\n ;(options.trackState ?? true) && setInView(entry.isIntersecting)\n if (entry.isIntersecting !== prevIntersecting.current) {\n onChange(entry.isIntersecting)\n }\n prevIntersecting.current = entry.isIntersecting\n }, options)\n\n const prevTarget = usePrevious(observer.target)\n\n return {\n ...observer,\n // set to false if the target unmounts\n inView: prevTarget && !observer.target ? false : inView,\n }\n}\n"]}
|
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@1hook/use-in-view",
|
|
3
|
+
"version": "0.0.1-beta.1",
|
|
4
|
+
"files": [
|
|
5
|
+
"dist",
|
|
6
|
+
"README.md"
|
|
7
|
+
],
|
|
8
|
+
"type": "module",
|
|
9
|
+
"exports": {
|
|
10
|
+
"./package.json": "./package.json",
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"@types/react": ">=18.0.0 || >=19.0.0",
|
|
18
|
+
"@types/react-dom": ">=18.0.0 || >=19.0.0",
|
|
19
|
+
"react": ">=18.0.0 || >=19.0.0",
|
|
20
|
+
"react-dom": ">=18.0.0 || >=19.0.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@1hook/use-merge-refs": "0.0.1-beta.1"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@1hook/use-event-handler": "0.0.1-beta.1",
|
|
27
|
+
"@1hook/use-intersection-observer": "0.0.1-beta.1",
|
|
28
|
+
"@1hook/use-previous": "0.0.1-beta.1"
|
|
29
|
+
},
|
|
30
|
+
"tag": "beta",
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "NODE_ENV=production tsup",
|
|
33
|
+
"dev": "concurrently \"tsup --watch\" \"vitest\"",
|
|
34
|
+
"test": "vitest run && tsc --noEmit",
|
|
35
|
+
"lint": "eslint . && tsc --noEmit",
|
|
36
|
+
"lint:eslint": "eslint .",
|
|
37
|
+
"lint:typescript": "tsc --noEmit"
|
|
38
|
+
}
|
|
39
|
+
}
|