|
| 1 | +import { Suspense, useId, useLayoutEffect } from "react"; |
| 2 | +import { ErrorBoundary } from "react-error-boundary"; |
| 3 | +import { ClientOnly } from "./shared/client-only"; |
| 4 | +import { PolyfillLoader } from "./shared/scroll-polyfill"; |
| 5 | +import { DebugDelayLoader } from "./shared/debug-delay-loader"; |
| 6 | +import { escapeCssSpecifier } from "./shared/css-utils"; |
| 7 | + |
| 8 | +const componentAttributeId = `data-ws-animate-id`; |
| 9 | + |
| 10 | +const Animate = ({ id }: { id: string }) => { |
| 11 | + useLayoutEffect(() => { |
| 12 | + const elt = document.querySelector(`[${componentAttributeId}="${id}"]`); |
| 13 | + if (elt === null) { |
| 14 | + return; |
| 15 | + } |
| 16 | + |
| 17 | + if (elt instanceof HTMLElement) { |
| 18 | + const animation = elt.animate( |
| 19 | + [{ offset: 0, transform: "translate(0, 100px)" }], |
| 20 | + { |
| 21 | + fill: "backwards", |
| 22 | + duration: 400, |
| 23 | + easing: "linear", |
| 24 | + } |
| 25 | + ); |
| 26 | + |
| 27 | + return () => { |
| 28 | + animation.cancel(); |
| 29 | + }; |
| 30 | + } |
| 31 | + }, [id]); |
| 32 | + |
| 33 | + return undefined; |
| 34 | +}; |
| 35 | + |
| 36 | +const StylesBeforeAnimate = ({ id }: { id: string }) => { |
| 37 | + const styleContent = ` |
| 38 | + @keyframes ws-scroll-animation-${id} { |
| 39 | + from { |
| 40 | + transform: translate(0, 100px); |
| 41 | + } |
| 42 | + } |
| 43 | +
|
| 44 | + [${componentAttributeId}="${id}"] { |
| 45 | + animation-name: ws-scroll-animation-${id}; |
| 46 | + animation-fill-mode: backwards; |
| 47 | + animation-play-state: paused; |
| 48 | + animation-duration: 1ms; |
| 49 | +
|
| 50 | + } |
| 51 | + `; |
| 52 | + return <style dangerouslySetInnerHTML={{ __html: styleContent }} />; |
| 53 | +}; |
| 54 | + |
| 55 | +type ScrollProps = { debug?: boolean }; |
| 56 | + |
| 57 | +export const Scroll = ({ debug = false }: ScrollProps) => { |
| 58 | + const nakedId = useId(); |
| 59 | + const id = escapeCssSpecifier(nakedId); |
| 60 | + |
| 61 | + return ( |
| 62 | + <> |
| 63 | + <ClientOnly fallback={<StylesBeforeAnimate id={id} />}> |
| 64 | + <Suspense fallback={<StylesBeforeAnimate id={id} />}> |
| 65 | + <ErrorBoundary |
| 66 | + fallback={null} |
| 67 | + onError={(error) => { |
| 68 | + console.error(`Polyfill loading error`, error); |
| 69 | + }} |
| 70 | + > |
| 71 | + <PolyfillLoader /> |
| 72 | + {debug && <DebugDelayLoader />} |
| 73 | + </ErrorBoundary> |
| 74 | + <Animate id={id} /> |
| 75 | + </Suspense> |
| 76 | + </ClientOnly> |
| 77 | + <div |
| 78 | + {...{ [componentAttributeId]: id }} |
| 79 | + style={{ height: "200px", width: "200px", backgroundColor: "red" }} |
| 80 | + ></div> |
| 81 | + <div |
| 82 | + data-ws-id={id} |
| 83 | + style={{ |
| 84 | + height: "100px", |
| 85 | + width: "100px", |
| 86 | + backgroundColor: "yellow", |
| 87 | + position: "fixed", |
| 88 | + left: 0, |
| 89 | + top: 0, |
| 90 | + }} |
| 91 | + ></div> |
| 92 | + </> |
| 93 | + ); |
| 94 | +}; |
0 commit comments