mirror of
https://github.com/raphaelsalaja/userinterface-wiki.git
synced 2026-09-14 20:06:29 +08:00
0181755d12
- 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.
42 lines
1.1 KiB
TypeScript
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;
|
|
});
|
|
}
|