Files
raphaelsalaja__userinterfac…/components/narration/hooks/use-scroll-direction.ts
Raphael S. 0181755d12 feat(narration): implement audio player with scroll and word highlighting
- Added a new audio player component that supports playback controls, volume adjustment, and playback rate settings.
- Integrated scroll direction detection to show/hide the player based on user scroll behavior.
- Implemented word highlighting functionality that synchronizes with audio playback.
- Created a reading progress component to visually indicate the user's scroll progress on the page.
- Established a global state management system using Zustand for managing audio state and player visibility.

This feature enhances the user experience by providing interactive audio playback and visual feedback during narration.
2026-01-05 11:49:11 +00:00

42 lines
1.1 KiB
TypeScript

"use client";
import { useMotionValueEvent, useScroll } from "motion/react";
import { useEffect, useRef } from "react";
import { useNarrationStore } from "../store";
export function useScrollDirection() {
const lastScrollYRef = useRef(0);
const hasBeenShownRef = useRef(false);
const { scrollY } = useScroll();
const isPlayerVisible = useNarrationStore((state) => state.isPlayerVisible);
const setIsPlayerVisible = useNarrationStore(
(state) => state.setIsPlayerVisible,
);
useEffect(() => {
if (isPlayerVisible) {
hasBeenShownRef.current = true;
}
}, [isPlayerVisible]);
useMotionValueEvent(scrollY, "change", (current) => {
if (!hasBeenShownRef.current) return;
const delta = current - lastScrollYRef.current;
if (Math.abs(delta) < 10) {
lastScrollYRef.current = current;
return;
}
if (delta > 0 && current > 100 && isPlayerVisible) {
setIsPlayerVisible(false);
} else if (delta < 0 && !isPlayerVisible) {
setIsPlayerVisible(true);
}
lastScrollYRef.current = current;
});
}