Skip to content
This repository was archived by the owner on May 7, 2025. It is now read-only.

Commit 467f625

Browse files
author
Emanuel Suriano
committed
fix: use object instead of array to keep sections
1 parent c9d4121 commit 467f625

File tree

4 files changed

+20
-28
lines changed

4 files changed

+20
-28
lines changed

packages/react-scroll-section/src/ScrollingProvider.tsx

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import React, {
1+
import {
22
ReactNode,
33
createRef,
44
useEffect,
55
useMemo,
66
useState,
7-
RefObject,
87
useCallback,
98
} from 'react';
109
import { debounce } from './utils';
11-
import { Provider } from './context';
10+
import { Provider, Section } from './context';
1211
import smoothscroll from 'smoothscroll-polyfill';
1312

1413
type Props = {
@@ -18,12 +17,6 @@ type Props = {
1817
children: ReactNode;
1918
};
2019

21-
type Section = {
22-
id: string;
23-
ref: RefObject<HTMLElement>;
24-
meta: unknown;
25-
};
26-
2720
if (typeof window !== 'undefined') {
2821
smoothscroll.polyfill();
2922
}
@@ -35,7 +28,7 @@ export const ScrollingProvider = ({
3528
children,
3629
}: Props) => {
3730
const [selected, setSelected] = useState('');
38-
const [sections, setSections] = useState<Section[]>([]);
31+
const [sections, setSections] = useState<Record<string, Section>>({});
3932

4033
useEffect(() => {
4134
document.addEventListener('scroll', debounceScroll, true);
@@ -46,12 +39,12 @@ export const ScrollingProvider = ({
4639
}, [sections]);
4740

4841
const handleScroll = useCallback(() => {
49-
const selectedSection = sections.reduce(
50-
(acc, curr) => {
51-
const sectionRef = curr.ref.current;
42+
const selectedSection = Object.keys(sections).reduce(
43+
(acc, id) => {
44+
const sectionRef = sections[id].ref.current;
5245
if (!sectionRef) {
5346
return {
54-
id: curr.id,
47+
id,
5548
differenceFromTop: 0,
5649
};
5750
}
@@ -62,13 +55,13 @@ export const ScrollingProvider = ({
6255
if (differenceFromTop >= acc.differenceFromTop) return acc;
6356

6457
return {
58+
id,
6559
differenceFromTop,
66-
id: curr.id,
6760
};
6861
},
6962
{
70-
differenceFromTop: 9999,
7163
id: '',
64+
differenceFromTop: 9999,
7265
},
7366
);
7467

@@ -77,19 +70,19 @@ export const ScrollingProvider = ({
7770

7871
const debounceScroll = debounce(handleScroll, debounceDelay);
7972

80-
const registerRef = ({ id, meta }: Omit<Section, 'ref'>) => {
73+
const registerRef = ({ id, meta }: { id: string; meta: unknown }) => {
8174
const ref = createRef<HTMLElement>();
82-
setSections((prev) => [...prev, { id, ref, meta }]);
75+
setSections((prev) => ({ ...prev, [id]: { ref, meta } }));
8376
return ref;
8477
};
8578

8679
const unregisterRef = (id: string) => {
87-
setSections((prev) => prev.filter((section) => section.id !== id));
80+
setSections(({ [id]: toRemove, ...rest }) => rest);
8881
};
8982

9083
const scrollTo = useCallback(
9184
(id: string) => {
92-
const section = sections.find((section) => section.id === id);
85+
const section = sections[id];
9386
if (!section) return console.warn('Section ID not recognized!'); // eslint-disable-line
9487

9588
setSelected(id);

packages/react-scroll-section/src/Section.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import React, { useMemo, useContext, useEffect } from 'react';
1+
import { useMemo, useContext, useEffect, ReactNode, HTMLProps } from 'react';
22
import { ScrollContext } from './context';
33

44
type Props = {
55
id: string;
66
meta?: unknown;
7-
children: React.ReactNode;
8-
} & React.HTMLProps<HTMLDetailsElement>;
7+
children: ReactNode;
8+
} & HTMLProps<HTMLDetailsElement>;
99

1010
export const Section = ({ id, children, meta, ...rest }: Props) => {
1111
const { registerRef, unregisterRef } = useContext(ScrollContext);

packages/react-scroll-section/src/context.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { createContext, RefObject } from 'react';
22

33
export type Section = {
4-
id: string;
54
ref: RefObject<HTMLElement>;
65
meta: unknown;
76
};
@@ -13,15 +12,15 @@ export type ScrollContextType = {
1312
}) => RefObject<HTMLElement> | null;
1413
unregisterRef: (id: string) => void;
1514
scrollTo: (section: string) => void;
16-
sections: Section[];
15+
sections: Record<string, Section>;
1716
selected: string;
1817
};
1918

2019
const DEFAULT_CONTEXT: ScrollContextType = {
2120
registerRef: () => null,
2221
unregisterRef: () => {},
2322
scrollTo: () => {},
24-
sections: [],
23+
sections: {},
2524
selected: '',
2625
};
2726

packages/react-scroll-section/src/hooks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ export const useScrollSections = () => {
1515
sections,
1616
} = useContext(ScrollContext);
1717

18-
return sections.map(({ id, meta }) => ({
18+
return Object.keys(sections).map((id) => ({
1919
id,
20-
meta,
20+
meta: sections[id].meta,
2121
onClick: () => scrollTo(id),
2222
selected: selectedSection === id,
2323
}));

0 commit comments

Comments
 (0)