Skip to content

Commit b249a8c

Browse files
committed
📚 DOC: add CHANGELOG, update docs
1 parent b51210b commit b249a8c

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Change Log
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
## [2.1.0] - 2021-11-13
6+
7+
### Added
8+
9+
- Set `useBoxMethod` as default way to check if `Element` is in `viewport`.
10+
- Can be overridden by setting `useBoxMethod` to `false`.
11+
12+
### Fixed
13+
14+
- Problems when div to Spy is not `100vh`.
15+
16+
## [2.0.1] (Beta) - 2021-11-9
17+
18+
- Beta version to test `2.1.0` release.

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,11 @@ import ScrollSpy from "react-ui-scrollspy";
110110

111111
### 🔧 Customize Attributes
112112

113-
| Attributes | Type | Description | Default | Required |
114-
| :----------------- | :------- | :-------------------------------------------------------- | :-------------------- | :------- |
115-
| `useDataAttribute` | `string` | To customize the string after `data-` | `"to-scrollspy-id"` | no |
116-
| `activeClass` | `string` | To customize the `class` added when the `Element` in view | `"active-scroll-spy"` | no |
113+
| Attributes | Type | Description | Default | Required |
114+
| :----------------- | :-------- | :------------------------------------------------------------------------------- | :-------------------- | :------- |
115+
| `useDataAttribute` | `string` | To customize the string after `data-` | `"to-scrollspy-id"` | no |
116+
| `activeClass` | `string` | To customize the `class` added when the `Element` in view | `"active-scroll-spy"` | no |
117+
| `useBoxMethod` | `boolean` | Set to `false` if you want your spy to be active if 50% of that `div` is in view | `true` | no |
117118

118119
## 📝 Authors
119120

package-lock.json

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

src/ScrollSpy/ScrollSpy.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const ScrollSpy = ({
5353
useDataAttribute = "to-scrollspy-id",
5454
activeClass = "active-scroll-spy",
5555

56-
useBoxMethod = false,
56+
useBoxMethod = true,
5757
}: ScrollSpyProps) => {
5858
const scrollContainerRef = useRef<HTMLDivElement | null>(null);
5959
const [navContainerItems, setNavContainerItems] = useState<NodeListOf<Element> | undefined>(); // prettier-ignore
@@ -85,17 +85,15 @@ const ScrollSpy = ({
8585
}, [navContainerItems]);
8686

8787
const isVisible = (el: HTMLElement) => {
88-
if (useBoxMethod) {
89-
var rectInView = el.getBoundingClientRect();
88+
const rectInView = el.getBoundingClientRect();
9089

90+
if (useBoxMethod) {
9191
const hitbox_top = window.innerHeight;
9292
const element_top = rectInView.top;
9393
const element_bottom = rectInView.top + window.innerHeight;
9494

9595
return hitbox_top < element_bottom && hitbox_top > element_top;
9696
} else {
97-
const rectInView = el.getBoundingClientRect();
98-
9997
// this decides how much of the element should be visible
10098
const leniency = parentScrollContainerRef?.current
10199
? parentScrollContainerRef?.current.offsetHeight * 0.5

0 commit comments

Comments
 (0)