Skip to content

Commit 34e56bf

Browse files
committed
Conditionally load Spline on view
1 parent 9ad32ff commit 34e56bf

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"react": "^19.0.0",
2626
"react-dom": "^19.0.0",
2727
"react-icons": "^5.5.0",
28+
"react-intersection-observer": "^9.16.0",
2829
"react-router-dom": "^7.5.0",
2930
"tailwindcss": "^4.1.3"
3031
},

pnpm-lock.yaml

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/sections/updates.section.tsx

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import { Box, Flex, Text, Button, Heading, Link } from '@chakra-ui/react';
22
import Spline from '@splinetool/react-spline';
3-
import { useState } from 'react';
3+
import { useState, useEffect } from 'react';
4+
import { useInView } from 'react-intersection-observer';
45

56
export const UpdatesSection = () => {
67
const [isSplineLoaded, setIsSplineLoaded] = useState(false);
78
const [splineError, setSplineError] = useState(false);
9+
const { ref, inView } = useInView({
10+
threshold: 0.1,
11+
rootMargin: '500px 0px 500px 0px',
12+
});
13+
814

915
function onSplineLoad() {
1016
setIsSplineLoaded(true);
@@ -20,16 +26,18 @@ export const UpdatesSection = () => {
2026
'https://prod.spline.design/TmAYMNy2qJHyDE9m/scene.splinecode';
2127

2228
return (
23-
<Flex className="relative bg-black min-h-screen">
29+
<Flex ref={ref} className="relative bg-black min-h-screen">
2430
<Box w="50%" h="full" maxH="100%">
25-
<Spline
26-
scene={splineSceneUrl}
27-
onLoad={onSplineLoad}
28-
onError={onSplineError}
29-
className="w-1/2 max-h-screen absolute top-0 left-0 z-0"
30-
/>
31+
{inView && (
32+
<Spline
33+
scene={splineSceneUrl}
34+
onLoad={onSplineLoad}
35+
onError={onSplineError}
36+
className="w-1/2 max-h-screen absolute top-0 left-0 z-0"
37+
/>
38+
)}
3139

32-
{(!isSplineLoaded || splineError) && (
40+
{(!isSplineLoaded || splineError || !inView) && (
3341
<Box
3442
position="absolute"
3543
top="0"

0 commit comments

Comments
 (0)