-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ : add a react-spring group tracking helper
- Loading branch information
Showing
9 changed files
with
138 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useActiveSectionSpring } from '@react-scrollytelling/react-spring'; | ||
import { animated } from '@react-spring/web'; | ||
|
||
const TEXT_COLOR = { | ||
RED: 'text-rose-500', | ||
ORANGE: 'text-orange-500', | ||
YELLOW: 'text-yellow-500', | ||
GREEN: 'text-green-500', | ||
BLUE: 'text-blue-500', | ||
PURPLE: 'text-purple-500', | ||
}; | ||
|
||
export const ActiveSectionInfoSpring = () => { | ||
const { trackingId, scrolledRatioSpring } = useActiveSectionSpring(); | ||
|
||
return ( | ||
<div className="flex h-screen items-center justify-center"> | ||
<ul className="list-disc text-xl leading-9 marker:text-slate-400"> | ||
<small> | ||
Active Section = <b>{trackingId}</b>: | ||
</small> | ||
<li> | ||
You are viewing{' '} | ||
<span className="rounded-md bg-slate-100 px-2 py-1 dark:bg-slate-800"> | ||
<b className={TEXT_COLOR[trackingId]}>{trackingId}</b> | ||
</span>{' '} | ||
section | ||
<br /> | ||
</li> | ||
<li> | ||
Reading ratio: | ||
<animated.b> | ||
{scrolledRatioSpring.to((val) => { | ||
return `${Math.round(val * 10000) / 10000}`; | ||
})} | ||
</animated.b> | ||
</li> | ||
</ul> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './useSectionScrollSpring'; | ||
export * from './useActiveSectionSpring'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useCallback, useRef, useState } from 'react'; | ||
|
||
import { | ||
ActiveSectionScrollInfo, | ||
useActiveSection, | ||
ActiveSectionObserver, | ||
} from '@react-scrollytelling/grouped'; | ||
import { SpringValue } from '@react-spring/web'; | ||
|
||
/** | ||
* Watches for all tracked sections to find the section closet to the bottom of the viewport | ||
* through ScrollytellingProvider. | ||
* It invokes the onActiveSectionChange when the active section changes. | ||
* Returns the Spring value (react-spring) of the section scroll. | ||
* @param onActiveSectionChange - The callback needs to be memoized | ||
* @returns The trackingId of the active section and the Spring value of the section scroll | ||
*/ | ||
export function useActiveSectionSpring(onActiveSectionChange?: ActiveSectionObserver) { | ||
const scrolledRatioSpringRef = useRef(new SpringValue(0)); | ||
const [trackingId, setTrackingId] = useState<string | null>(null); | ||
|
||
const onSectionScroll = useCallback( | ||
(scrollInfo: ActiveSectionScrollInfo) => { | ||
const { trackingId: id, scrolledRatio } = scrollInfo; | ||
setTrackingId(id); | ||
scrolledRatioSpringRef.current.set(scrolledRatio); | ||
|
||
if (onActiveSectionChange) { | ||
onActiveSectionChange(scrollInfo); | ||
} | ||
}, | ||
[onActiveSectionChange] | ||
); | ||
|
||
useActiveSection(onSectionScroll); | ||
|
||
return { trackingId, scrolledRatioSpring: scrolledRatioSpringRef.current }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters