128 lines
4.3 KiB
TypeScript
128 lines
4.3 KiB
TypeScript
import React, { useState, useEffect, useRef, useCallback } from 'react';
|
|
import { slides } from './slidesData.ts';
|
|
import SlideContent from './SlideContent.tsx';
|
|
|
|
const Presentation: React.FC = () => {
|
|
const [currentSlide, setCurrentSlide] = useState(0);
|
|
const [isPlaying, setIsPlaying] = useState(false);
|
|
const [pointerPos, setPointerPos] = useState({ x: -100, y: -100 });
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
const playIntervalRef = useRef<number | null>(null);
|
|
|
|
const nextSlide = useCallback(() => {
|
|
setCurrentSlide((prev) => (prev + 1) % slides.length);
|
|
}, []);
|
|
|
|
const prevSlide = () => {
|
|
setCurrentSlide((prev) => (prev - 1 + slides.length) % slides.length);
|
|
};
|
|
|
|
const togglePlay = () => {
|
|
setIsPlaying((prev) => !prev);
|
|
};
|
|
|
|
useEffect(() => {
|
|
if (isPlaying) {
|
|
playIntervalRef.current = window.setInterval(() => {
|
|
nextSlide();
|
|
}, 5000); // Change slide every 5 seconds
|
|
} else {
|
|
if (playIntervalRef.current) {
|
|
clearInterval(playIntervalRef.current);
|
|
}
|
|
}
|
|
return () => {
|
|
if (playIntervalRef.current) {
|
|
clearInterval(playIntervalRef.current);
|
|
}
|
|
};
|
|
}, [isPlaying, nextSlide]);
|
|
|
|
useEffect(() => {
|
|
const handleKeyDown = (e: KeyboardEvent) => {
|
|
if (e.key === 'ArrowRight' || e.key === ' ') {
|
|
nextSlide();
|
|
} else if (e.key === 'ArrowLeft') {
|
|
prevSlide();
|
|
}
|
|
};
|
|
window.addEventListener('keydown', handleKeyDown);
|
|
return () => {
|
|
window.removeEventListener('keydown', handleKeyDown);
|
|
};
|
|
}, [nextSlide]);
|
|
|
|
useEffect(() => {
|
|
const handleMouseMove = (e: MouseEvent) => {
|
|
if (containerRef.current) {
|
|
const rect = containerRef.current.getBoundingClientRect();
|
|
setPointerPos({ x: e.clientX - rect.left, y: e.clientY - rect.top });
|
|
}
|
|
};
|
|
|
|
const container = containerRef.current;
|
|
container?.addEventListener('mousemove', handleMouseMove);
|
|
|
|
return () => {
|
|
container?.removeEventListener('mousemove', handleMouseMove);
|
|
};
|
|
}, []);
|
|
|
|
return (
|
|
<div
|
|
ref={containerRef}
|
|
className="relative w-full h-full bg-white shadow-xl aspect-video overflow-hidden cursor-none select-none rounded-lg"
|
|
>
|
|
{/* Laser Pointer */}
|
|
<div
|
|
className="absolute w-6 h-6 bg-red-500/50 rounded-full border-2 border-white pointer-events-none transform -translate-x-1/2 -translate-y-1/2 transition-transform duration-75 ease-out shadow-lg"
|
|
style={{ left: pointerPos.x, top: pointerPos.y }}
|
|
aria-hidden="true"
|
|
/>
|
|
|
|
{/* Slides */}
|
|
<div className="w-full h-full">
|
|
{slides.map((slide, index) => (
|
|
<div
|
|
key={index}
|
|
className={`absolute inset-0 transition-opacity duration-700 ease-in-out ${index === currentSlide ? 'opacity-100 z-10' : 'opacity-0'}`}
|
|
aria-hidden={index !== currentSlide}
|
|
>
|
|
<SlideContent slide={slide} />
|
|
</div>
|
|
))}
|
|
</div>
|
|
|
|
{/* Controls */}
|
|
<div className="absolute bottom-4 left-1/2 -translate-x-1/2 flex items-center space-x-4 bg-white/50 backdrop-blur-sm p-2 rounded-full z-20 shadow-lg border border-gray-200">
|
|
<button
|
|
onClick={prevSlide}
|
|
className="w-10 h-10 flex items-center justify-center text-gray-600 hover:text-gray-900 hover:bg-gray-200/50 rounded-full transition"
|
|
aria-label="Previous Slide"
|
|
>
|
|
<i className="fa-solid fa-arrow-left"></i>
|
|
</button>
|
|
<button
|
|
onClick={togglePlay}
|
|
className="w-10 h-10 flex items-center justify-center text-gray-600 hover:text-gray-900 hover:bg-gray-200/50 rounded-full transition"
|
|
aria-label={isPlaying ? 'Pause Slideshow' : 'Play Slideshow'}
|
|
>
|
|
<i className={`fa-solid ${isPlaying ? 'fa-pause' : 'fa-play'}`}></i>
|
|
</button>
|
|
<span className="text-sm font-mono text-gray-600 w-12 text-center">
|
|
{currentSlide + 1} / {slides.length}
|
|
</span>
|
|
<button
|
|
onClick={nextSlide}
|
|
className="w-10 h-10 flex items-center justify-center text-gray-600 hover:text-gray-900 hover:bg-gray-200/50 rounded-full transition"
|
|
aria-label="Next Slide"
|
|
>
|
|
<i className="fa-solid fa-arrow-right"></i>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default Presentation;
|