Skip to content

Commit 6296683

Browse files
committed
bug fixes: multiple instance
1 parent 42c4799 commit 6296683

File tree

11 files changed

+95
-42
lines changed

11 files changed

+95
-42
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ event whenever the component wrapped with `DetectSection` enters the viewport.
2727
This is a wrapper component for your scrollable component. Wrap your component with `ReactScrollDetect` and attach the `onChange` listener.
2828

2929
##### props
30-
| props | signature | required | description |
31-
|------ | -------------- | ---- | ----------- |
32-
| onChange | (index: number) => void | false | Function which is exectuted when a new section enters the viewport |
33-
| triggerPoint | 'center', 'top', 'bottom | false | Trigger point for DetectSection (default: center) |
34-
| index | number | false | This spectifies which section(index) the needs to be scrolled to the viewport. Default value: 0 (note: this is an experimental feature) |
30+
| props | signature | required | default value | description |
31+
|------ | -------------- | ---- | ----------- |
32+
| onChange | (index: number) => void | false | () => null | Function which is exectuted when a new section enters the viewport |
33+
| triggerPoint | 'center', 'top', 'bottom | false | | Trigger point for DetectSection (default: center) |
34+
| index | number | false | 0 | This spectifies which section(index) the needs to be scrolled to the viewport.(note: this is an experimental feature) |
3535

36+
| offset | number | false |0 |The offset from the top (of the screen) for the scroll to snap |
3637

3738

3839
#### Example

dist/index.es.js

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

dist/index.es.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.

dist/index.js

Lines changed: 29 additions & 9 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.

dist/lib/context.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ export declare type TriggerPoint = 'center' | 'top' | 'bottom';
33
export declare type TSectionEntry = {
44
height: number;
55
index: number;
6+
ref: HTMLDivElement;
67
};
78
export declare type ReactScrollDetectContextProviderValue = {
89
onChange: (index: number) => void;
9-
addSection: (section: TSectionEntry) => void;
10+
addSection: (section: Omit<TSectionEntry, 'index'>) => void;
1011
sections: TSectionEntry[];
1112
triggerPoint: TriggerPoint;
1213
index: number;

package-lock.json

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@
4646
"ts-jest": "^24.2.0",
4747
"typescript": "^3.7.4"
4848
}
49-
}
49+
}

src/lib/DetectSection.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ import React, { useRef, useContext, useEffect } from "react";
22
import { ReactScrollDetectContext } from "./context";
33

44

5-
let INDEX = 0;
65

76
export const DetectSection: React.FC = (props) => {
87
const ref = useRef<HTMLDivElement>(null)
98

109
const { addSection } = useContext(ReactScrollDetectContext);
11-
const index = INDEX++;
1210
useEffect(() => {
13-
const height = ref.current?.clientHeight || 0;
14-
addSection({ index, height })
15-
}, [])
11+
if (!ref.current) return;
12+
const height = ref.current.clientHeight || 0;
13+
addSection({ height, ref: ref.current })
14+
}, [ref])
1615

1716

1817

src/lib/ReactScrollDetect.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,29 @@ import { TSectionEntry, ReactScrollDetectContextProviderValue, ReactScrollDetect
33
export interface ReactScrollDetectProps {
44
onChange?: (index: number) => void
55
index?: number
6+
offset?: number
67
triggerPoint?: TriggerPoint
78
}
89

910

1011
const ReactScrollDetect: FC<ReactScrollDetectProps> = (props) => {
11-
const { onChange = () => { }, index = 0, triggerPoint = 'center' } = props;
12+
const { onChange = () => { }, index = 0, triggerPoint = 'center', offset = 0 } = props;
1213
const [sections, setSections] = useState<TSectionEntry[]>([])
14+
let numSections = 0;
15+
16+
const addSection = (section: Omit<TSectionEntry, 'index'>) => {
17+
setSections(sections => [...sections, { ...section, index: numSections++ }])
18+
}
19+
1320

14-
const addSection = (section: TSectionEntry) => setSections(sections => [...sections, section])
1521

1622
const providerValue: ReactScrollDetectContextProviderValue = {
1723
onChange,
1824
addSection,
1925
sections,
2026
index,
21-
triggerPoint
27+
triggerPoint,
28+
offset
2229
}
2330
return (
2431
<ReactScrollDetectContext.Provider value={providerValue}>
@@ -33,11 +40,12 @@ const WINDOW_HEIGHT = window.innerHeight;
3340

3441

3542
const _ScrollContainer: FC = (props) => {
36-
const { sections, onChange, index, triggerPoint } = useContext(ReactScrollDetectContext);
43+
const { sections, onChange, index, triggerPoint, offset = 0 } = useContext(ReactScrollDetectContext);
3744
const [sectionEntryPoints, setSectionEntryPoints] = useState<number[]>([])
3845
const [currentIndex, setCurrentIndex] = useState(0);
3946
const TRIGGER_CONST = triggerPoint === 'center' ? WINDOW_HEIGHT / 2 : triggerPoint === 'top' ? 0 : WINDOW_HEIGHT
4047

48+
4149
useEffect(() => {
4250
initializeEntryPoints()
4351
}, [sections])
@@ -48,14 +56,16 @@ const _ScrollContainer: FC = (props) => {
4856
setCurrentIndex(index)
4957
onChange(index)
5058
}
51-
window.scrollTo({ top: sectionEntryPoints[index] || 0, behavior: 'smooth' })
59+
window.scrollTo({ top: (sectionEntryPoints[index] || 0) + offset, behavior: 'smooth' })
5260
}, [index])
5361

5462

5563

5664
const initializeEntryPoints = () => {
5765
const _sectionEntryPoints: number[] = [];
58-
let prev = 0;
66+
if (sections.length === 0) return;
67+
// console.log("first section height", sections[0].ref.offsetTop, sections[0].ref)
68+
let prev = sections[0].ref.offsetTop;
5969
sections.forEach(section => {
6070
_sectionEntryPoints.push(prev);
6171
prev = prev + section.height;

0 commit comments

Comments
 (0)