diff --git a/packages/demo/src/apps/Home/components/DemoSelector.tsx b/packages/demo/src/apps/Home/components/DemoSelector.tsx index 9ab29c7..d181a77 100644 --- a/packages/demo/src/apps/Home/components/DemoSelector.tsx +++ b/packages/demo/src/apps/Home/components/DemoSelector.tsx @@ -15,7 +15,7 @@ export function DemoSelector() { ...Typography.heading2, }} > - Choose a demo + Select a demo
diff --git a/packages/demo/src/apps/Home/index.tsx b/packages/demo/src/apps/Home/index.tsx index 16b5c9e..3dae97e 100644 --- a/packages/demo/src/apps/Home/index.tsx +++ b/packages/demo/src/apps/Home/index.tsx @@ -4,8 +4,11 @@ import { Hint } from "../../components/Hint.js" import { Hero } from "./components/Hero.js" import { ScrollHint } from "../../components/ScrollHint.js" import { DemoSelector } from "./components/DemoSelector.js" +import { AnimatePresence, motion } from "framer-motion" export function Home() { + const hideScrollHintText = useScrollThreshold({ threshold: 10 }) + return ( <>
@@ -19,7 +22,18 @@ export function Home() { > - + + {!hideScrollHintText && ( + + + + )} +
Navigation - {"↑"}, {"↓"}, {"<-"} and {"->"} @@ -35,3 +49,24 @@ export function Home() { ) } + +function useScrollThreshold({ threshold }: { threshold: number }) { + const [passed, dispatch] = React.useReducer((_prevPassed: boolean, passed: boolean) => { + return passed + }, false) + + React.useEffect(() => { + function handleScroll() { + if (window.scrollY > threshold) { + return dispatch(true) + } + + dispatch(false) + } + window.addEventListener("scroll", handleScroll) + + return () => window.removeEventListener("scroll", handleScroll) + }, [threshold, dispatch]) + + return passed +} diff --git a/packages/demo/src/components/PageSlide.tsx b/packages/demo/src/components/PageSlide.tsx index 50fe237..cf42663 100644 --- a/packages/demo/src/components/PageSlide.tsx +++ b/packages/demo/src/components/PageSlide.tsx @@ -1,7 +1,7 @@ import * as React from "react" -import { RefObject } from "react" import { Branch, useFocusable, useFocusManager } from "react-sunbeam" import { useMergedRef } from "../apps/utils/useMergedRef.js" +import { useIntersectionObserver } from "./useIntersectionObserver.js" export const PageSlide = React.forwardRef(function PageSlide( { @@ -70,25 +70,3 @@ export const PageSlide = React.forwardRef(function PageSlide( ) }) - -function useIntersectionObserver( - ref: RefObject, - percentage: number, - callback: (isIntersecting: boolean) => void -) { - React.useEffect(() => { - if (!ref.current) return - - const intersectionObserver = new IntersectionObserver( - (entries) => { - const entry = entries[0] - callback(entry.isIntersecting) - }, - { threshold: percentage } - ) - - intersectionObserver.observe(ref.current) - - return () => intersectionObserver.disconnect() - }, [ref, callback, percentage]) -} diff --git a/packages/demo/src/components/ScrollHint.tsx b/packages/demo/src/components/ScrollHint.tsx index 9619727..9baf3d2 100644 --- a/packages/demo/src/components/ScrollHint.tsx +++ b/packages/demo/src/components/ScrollHint.tsx @@ -6,8 +6,6 @@ export function ScrollHint() { return (
( + ref: RefObject, + percentage: number, + callback: (isIntersecting: boolean) => void +) { + React.useEffect(() => { + if (!ref.current) return + + const intersectionObserver = new IntersectionObserver( + (entries) => { + const entry = entries[0] + callback(entry.isIntersecting) + }, + { threshold: percentage } + ) + + intersectionObserver.observe(ref.current) + + return () => intersectionObserver.disconnect() + }, [ref, callback, percentage]) +}