Skip to content

Commit 5ec89aa

Browse files
committed
feat: implement intersection observer for card visibility and enhance copy button text
1 parent 1bed381 commit 5ec89aa

1 file changed

Lines changed: 31 additions & 4 deletions

File tree

  • homepage/components/HomepageQuickStart

‎homepage/components/HomepageQuickStart/index.tsx‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { ReactElement, useState } from 'react';
1+
import React, { ReactElement, useState, useEffect, useRef } from 'react';
22

33
interface QuickStartStepProps {
44
title: string;
@@ -29,8 +29,26 @@ const QuickStartList: QuickStartStepProps[] = [
2929
},
3030
];
3131

32-
const QuickStartStep = ({ title, description, code }: QuickStartStepProps): ReactElement => {
32+
const QuickStartStep = ({ title, description, code, index }: QuickStartStepProps & { index: number }): ReactElement => {
3333
const [copied, setCopied] = useState(false);
34+
const ref = useRef<HTMLDivElement>(null);
35+
const [isVisible, setIsVisible] = useState(false);
36+
37+
useEffect(() => {
38+
const el = ref.current;
39+
if (!el) return;
40+
const observer = new IntersectionObserver(
41+
([entry]) => {
42+
if (entry.isIntersecting) {
43+
setIsVisible(true);
44+
observer.unobserve(el);
45+
}
46+
},
47+
{ threshold: 0.15 }
48+
);
49+
observer.observe(el);
50+
return () => observer.unobserve(el);
51+
}, []);
3452

3553
const copyToClipboard = () => {
3654
navigator.clipboard.writeText(code);
@@ -39,15 +57,23 @@ const QuickStartStep = ({ title, description, code }: QuickStartStepProps): Reac
3957
};
4058

4159
return (
42-
<div className="card">
60+
<div
61+
ref={ref}
62+
className="card"
63+
style={{
64+
opacity: isVisible ? 1 : 0,
65+
transform: isVisible ? 'translateY(0)' : 'translateY(24px)',
66+
transition: `opacity 0.5s ease ${index * 0.12}s, transform 0.5s ease ${index * 0.12}s`,
67+
}}
68+
>
4369
<div className="cardHeader">
4470
<h3>{title}</h3>
4571
<p>{description}</p>
4672
</div>
4773
<div className="cardBody">
4874
<div style={{ position: 'relative' }}>
4975
<button onClick={copyToClipboard} className="copyButton">
50-
{copied ? 'Copied!' : 'Copy'}
76+
{copied ? '✓ Copied!' : '📋 Copy'}
5177
</button>
5278
<pre className="codeBlock">
5379
<code>{code}</code>
@@ -75,6 +101,7 @@ const HomepageQuickStart = (): ReactElement => {
75101
title={step.title}
76102
description={step.description}
77103
code={step.code}
104+
index={idx}
78105
/>
79106
</div>
80107
))}

0 commit comments

Comments
 (0)