gsap-react
GSAP React patterns: useGSAP hook, refs, gsap.context(), cleanup, SSR compatibility. Trigger: When using GSAP in React components - animations, timelines, ScrollTrigger.
- License 许可
- Apache-2.0
- 最近更新
- 2026-08-26
- Stars
- ★ 1
- Forks
- 0
01 前言 Front Matter
- name
- gsap-react
- license
- Apache-2.0
- metadata
- { "author": "gentleman-programming", "version": "1.0" }
- description
- GSAP React patterns: useGSAP hook, refs, gsap.context(), cleanup, SSR compatibility. Trigger: When using GSAP in React components - animations, timelines, ScrollTrigger.
02 资源文件 5
- 参考文档.skill-meta.json475 B
- 资产文件assets/01-example.tsx26 B
- 参考文档README.md431 B
- 参考文档references/01-guide.md38 B
- 参考文档references/02-patterns.md29 B
03 SKILL.md 正文
When to Use
- GSAP animations in React
- useGSAP hook integration
- Component cleanup
- SSR projects
- Ref-based animations
Critical Patterns
Setup & Imports
import { useGSAP } from "@gsap/react";
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
gsap.registerPlugin(ScrollTrigger);
useGSAP (RECOMMENDED)
function MyComponent() {
const containerRef = useRef(null);
useGSAP(() => {
gsap.to(".box", { x: 100, duration: 0.5 });
}, { scope: containerRef }); // REQUIRED: scope prevents global selector leaks
return (
<div ref={containerRef}>
<div className="box" />
</div>
);
}
useGSAP with ScrollTrigger
function ScrollSection() {
const sectionRef = useRef(null);
useGSAP(() => {
gsap.to(".item", {
x: 200,
scrollTrigger: {
trigger: ".item",
start: "top center",
end: "bottom center",
scrub: 1
}
});
}, { scope: sectionRef });
return (
<section ref={sectionRef}>
<div className="item">Content</div>
</section>
);
}
useGSAP Options
useGSAP(callback, options);
// Available options:
{
scope: ref | ".selector" | document, // Required for scoped selectors
dependencies?: [], // Re-run when these change
revertOnKill?: boolean, // Auto-cleanup (default: true)
}
Without useGSAP (Legacy Pattern)
// Only if useGSAP unavailable
function LegacyComponent() {
const containerRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const ctx = gsap.context(() => {
gsap.to(".box", { x: 100 });
}, containerRef);
return () => ctx.revert(); // Cleanup REQUIRED
}, []);
return <div ref={containerRef}><div className="box" /></div>;
}
Multiple Animations
function MultiAnimation() {
const containerRef = useRef(null);
useGSAP(() => {
const tl = gsap.timeline();
tl.to(".a", { x: 100 })
.to(".b", { y: 50 }, "<")
.to(".c", { opacity: 0 });
return () => tl.kill(); // Or rely on revertOnKill
}, { scope: containerRef });
return (
<div ref={containerRef}>
<div className="a" />
<div className="b" />
<div className="c" />
</div>
);
}
Dynamic Values from Props
function DynamicAnimation({ delay = 0 }) {
const containerRef = useRef(null);
useGSAP(() => {
gsap.to(".box", {
x: 100,
delay,
duration: 0.5
});
}, { scope: containerRef, dependencies: [delay] }); // Re-run when delay changes
return (
<div ref={containerRef}>
<div className="box" />
</div>
);
}
SSR Compatibility
// useGSAP handles SSR automatically
// For manual handling:
function ServerComponent() {
const containerRef = useRef(null);
useGSAP(() => {
// Only runs on client
}, { scope: containerRef });
// Or check for window
useEffect(() => {
if (typeof window !== "undefined") {
gsap.to(".box", { x: 100 });
}
}, []);
return <div ref={containerRef}><div className="box" /></div>;
}
Cleanup (CRITICAL)
// useGSAP auto-cleans when component unmounts
// But explicit cleanup is recommended for complex cases:
useGSAP(() => {
const tl = gsap.timeline();
// ... animations
return () => {
tl.kill(); // Explicit cleanup
};
}, { scope: containerRef });
// Or disable auto-cleanup:
useGSAP(() => {
// animations
}, { scope: containerRef, revertOnKill: false });
Commands
npm install gsap @gsap/react
Resources
See references/ for detailed guides and code examples in assets/.
External Resources
- [Official Documentation]
04 同仓库更多 Skills
zustand-5
Zustand 5 state management patterns. Trigger: When managing React state with Zustand.
zod-4
Zod 4 schema validation patterns. Trigger: When using Zod for validation - breaking changes from v3.
x-bookmark-quote-posts
Workflow para revisar bookmarks de X/Twitter y convertirlos en drafts de quote posts. Accede a la sección de bookmarks vía browser headless, extrae posts recientes, y genera drafts con voz personal del usuario. No publica ni muta X — solo genera contenido para revisión. Trigger: X bookmarks, Twitter bookmarks, quote posts, bookmark automation, crear quote posts, revisar bookmarks, growth content X.
writing-skills
Use when creating new skills, editing existing skills, or verifying skills work before deployment
writing-plans
Use when you have a spec or requirements for a multi-step task, before touching code
work-unit-commits
Plan commits as reviewable work units. Trigger: implementation, commit splitting, chained PRs, or keeping tests and docs with code.