Files
blog-nextjs/components/scroll-reveal.tsx
gbanyan 1b495d2d2d Remove next-view-transitions and use native View Transition API
- Remove external next-view-transitions dependency
- Use Next.js 16 native navigation and Safari 18+ native View Transition API
- Add ViewTransitionProvider for minimal wrapping with Safari 18+ detection
- Updated all Link imports from external package to next/link
- Removed link-wrapper.tsx and view-transitions-wrapper.tsx

This resolves Safari compatibility issues while maintaining transitions on modern browsers.
2026-03-14 23:00:21 +08:00

73 lines
1.6 KiB
TypeScript

'use client';
import { useEffect, useRef, useCallback } from 'react';
import clsx from 'clsx';
interface ScrollRevealProps {
children: React.ReactNode;
className?: string;
once?: boolean;
}
export function ScrollReveal({
children,
className,
once = true
}: ScrollRevealProps) {
const ref = useRef<HTMLDivElement | null>(null);
const observerRef = useRef<IntersectionObserver | null>(null);
const handleObserver = useCallback((entries: IntersectionObserverEntry[]) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.classList.add('is-visible');
if (once && observerRef.current) {
observerRef.current.unobserve(entry.target);
}
} else if (!once) {
entry.target.classList.remove('is-visible');
}
});
}, [once]);
useEffect(() => {
const el = ref.current;
if (!el) return;
// Fallback for browsers without IntersectionObserver
if (!('IntersectionObserver' in window)) {
el.classList.add('is-visible');
return;
}
observerRef.current = new IntersectionObserver(
handleObserver,
{
threshold: 0.05,
rootMargin: '0px 0px -20% 0px'
}
);
observerRef.current.observe(el);
// Fallback timeout for slow connections - reduce to 300ms
const fallback = setTimeout(() => {
el.classList.add('is-visible');
}, 300);
return () => {
observerRef.current?.disconnect();
clearTimeout(fallback);
};
}, [handleObserver, once]);
return (
<div
ref={ref}
className={clsx('scroll-reveal', className)}
>
{children}
</div>
);
}