Skip to content

Commit ad142b5

Browse files
committed
✨ NEW: add optional useBoxMethod prop (beta)
1 parent bf614b0 commit ad142b5

File tree

11 files changed

+71
-66
lines changed

11 files changed

+71
-66
lines changed

demo-app/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@
2121
npm-debug.log*
2222
yarn-debug.log*
2323
yarn-error.log*
24+
25+
# ScrollSpy test component
26+
src/components/src
27+
28+
# packages
29+
*.tgz

demo-app/package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo-app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"react": "^17.0.2",
1515
"react-dom": "^17.0.2",
1616
"react-scripts": "4.0.3",
17-
"react-ui-scrollspy": "^2.0.0",
17+
"react-ui-scrollspy": "file:react-ui-scrollspy-2.0.0.tgz",
1818
"typescript": "^4.4.4",
1919
"web-vitals": "^1.1.2"
2020
},

demo-app/src/App.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Center from "./components/Center/Center";
22
import Navigation from "./components/Navigation/Navigation";
3-
import ScrollSpy from "react-ui-scrollspy";
3+
// import ScrollSpy from "react-ui-scrollspy";
4+
import ScrollSpy from "./components/src/index";
45
import Buttons from "./components/Buttons/Buttons";
56

67
function App() {

demo-app/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
"noEmit": true,
1717
"jsx": "react-jsx"
1818
},
19-
"include": ["src"]
19+
"include": ["src", "../src"]
2020
}

dev.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ npm link react-ui-scrollspy
1212
creates a `.tgz`
1313

1414
```bash
15+
npm run build
1516
npm pack
17+
mv react-ui-scrollspy-2.0.0.tgz ./demo-app
18+
cd demo-app
19+
npm uninstall react-ui-scrollspy
20+
npm install react-ui-scrollspy-2.0.0.tgz --production
1621
```
1722

1823
## Commit Messages

dist/index.js

Lines changed: 14 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
{
2-
"name": "@pettiboy/react-ui-scrollspy",
2+
"name": "react-ui-scrollspy",
33
"version": "2.0.0",
44
"description": "Simple, Easy To Use and Customisable ScrollSpy component for react with callback, typescript and throttle support among others",
55
"main": "dist/index.js",
66
"scripts": {
77
"build": "rollup -c",
88
"start": "rollup -c -w"
99
},
10-
"publishConfig": {
11-
"registry": "https://npm.pkg.github.com/"
12-
},
1310
"repository": {
1411
"type": "git",
15-
"url": "git+https://github.com/pettiboy/react-ui-scrollspy.git",
16-
"directory": "react-ui-scrollspy"
12+
"url": "git+https://github.com/pettiboy/react-ui-scrollspy.git"
1713
},
1814
"bugs": {
1915
"url": "https://github.com/pettiboy/react-ui-scrollspy/issues"

src/ScrollSpy/ScrollSpy.tsx

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
useRef,
77
useState,
88
} from "react";
9-
import { isVisible } from "../utils/isVisible";
109
import { throttle } from "../utils/throttle";
1110

1211
interface ScrollSpyProps {
@@ -29,6 +28,8 @@ interface ScrollSpyProps {
2928
// customize attributes
3029
useDataAttribute?: string;
3130
activeClass?: string;
31+
32+
useBoxMethod?: boolean;
3233
}
3334

3435
const ScrollSpy = ({
@@ -51,6 +52,8 @@ const ScrollSpy = ({
5152
// customize attributes
5253
useDataAttribute = "to-scrollspy-id",
5354
activeClass = "active-scroll-spy",
55+
56+
useBoxMethod = false,
5457
}: ScrollSpyProps) => {
5558
const scrollContainerRef = useRef<HTMLDivElement | null>(null);
5659
const [navContainerItems, setNavContainerItems] = useState<NodeListOf<Element> | undefined>(); // prettier-ignore
@@ -81,6 +84,34 @@ const ScrollSpy = ({
8184
// eslint-disable-next-line react-hooks/exhaustive-deps
8285
}, [navContainerItems]);
8386

87+
const isVisible = (el: HTMLElement) => {
88+
if (useBoxMethod) {
89+
var rectInView = el.getBoundingClientRect();
90+
91+
const hitbox_top = window.innerHeight;
92+
const element_top = rectInView.top;
93+
const element_bottom = rectInView.top + window.innerHeight;
94+
95+
return hitbox_top < element_bottom && hitbox_top > element_top;
96+
} else {
97+
const rectInView = el.getBoundingClientRect();
98+
99+
// this decides how much of the element should be visible
100+
const leniency = parentScrollContainerRef?.current
101+
? parentScrollContainerRef?.current.offsetHeight * 0.5
102+
: window.innerHeight * 0.5;
103+
104+
const useHeight = parentScrollContainerRef?.current
105+
? parentScrollContainerRef?.current.offsetHeight
106+
: window.innerHeight;
107+
108+
return (
109+
rectInView.top + leniency + offsetTop >= 0 &&
110+
rectInView.bottom - leniency - offsetBottom <= useHeight
111+
);
112+
}
113+
};
114+
84115
const checkAndUpdateActiveScrollSpy = () => {
85116
const scrollParentContainer = scrollContainerRef.current;
86117

@@ -92,14 +123,7 @@ const ScrollSpy = ({
92123
// get child element
93124
const useChild = scrollParentContainer.children.item(i) as HTMLDivElement;
94125

95-
const elementIsVisible = parentScrollContainerRef
96-
? isVisible(
97-
useChild,
98-
offsetTop,
99-
offsetBottom,
100-
parentScrollContainerRef?.current
101-
)
102-
: isVisible(useChild, offsetTop, offsetBottom);
126+
const elementIsVisible = isVisible(useChild);
103127

104128
// check if the element is in the viewport
105129
if (elementIsVisible) {

0 commit comments

Comments
 (0)