Skip to content

Commit b7f44ca

Browse files
committed
2.1.2
- IMPROVED: perfromance - FIXED: in some cases, the context would get reverted when it shouldn’t. - FIXED: dependencies switched to peer dependencies
1 parent b00905d commit b7f44ca

File tree

5 files changed

+29
-22
lines changed

5 files changed

+29
-22
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import { useRef } from "react";
3030
import gsap from "gsap";
3131
import { useGSAP } from "@gsap/react";
3232

33+
gsap.registerPlugin(useGSAP); // register any plugins, including the useGSAP hook
34+
3335
const container = useRef();
3436
useGSAP(() => {
3537
// gsap code here...
@@ -84,9 +86,14 @@ useGSAP(() => {
8486
npm install @gsap/react
8587
```
8688

89+
At the top of your code right below your imports, it's usually a good idea to register `useGSAP` as a plugin:
90+
```javascript
91+
gsap.registerPlugin(useGSAP);
92+
```
93+
8794
## Using callbacks or event listeners? Use `contextSafe()` and clean up!
8895

89-
A function is considered "context-safe" if it is properly scoped to a <a href="https://gsap.com/docs/v3/GSAP/gsap.context()">`gsap.context()`</a> so that any GSAP-related objects created **while that function executes** are recorded by that `Context` and use its `scope` for selector text. When that `Context` gets reverted (like when the hook gets torn down or re-synchronizes), so do all of those GSAP-related objects. Cleanup is important in React and `Context` makes it simple. Otherwise, you'd need to manually keep track of all your animations and `revert()` them when necessary, like when the entire component gets unmounted/remounted. `Context` does that work for you.
96+
A function is considered "context-safe" if it is properly scoped to a <a href="https://gsap.com/docs/v3/GSAP/gsap.context()">`gsap.context()`</a> so that any GSAP-related objects created **while that function executes** are recorded by that `Context` and use its `scope` for selector text. When that `Context` gets reverted (like when the hook gets torn down or re-synchronizes), so will all of those GSAP-related objects. Cleanup is important in React and `Context` makes it simple. Otherwise, you'd need to manually keep track of all your animations and `revert()` them when necessary, like when the entire component gets unmounted/remounted. `Context` does that work for you.
9097

9198
The main `useGSAP(() => {...})` function is automatically context-safe of course. But if you're creating functions that get called **AFTER** the main `useGSAP()` function executes (like click event handlers, something in a `setTimeout()`, or anything delayed), you need a way to make those functions context-safe. Think of it like telling the `Context` when to hit the "record" button for any GSAP-related objects.
9299

@@ -208,4 +215,4 @@ Ask in the friendly <a href="https://gsap.com/community/">GSAP forums</a>. Or sh
208215
### License
209216
GreenSock's standard "no charge" license can be viewed at <a href="https://gsap.com/standard-license">https://gsap.com/standard-license</a>. <a href="https://gsap.com/pricing/">Club GSAP</a> members are granted additional rights. See <a href="https://gsap.com/licensing/">https://gsap.com/licensing/</a> for details. Why doesn't GSAP use an MIT (or similar) open source license, and why is that a **good** thing? This article explains it all: <a href="https://gsap.com/why-license/" target="_blank">https://gsap.com/why-license/</a>
210217

211-
Copyright (c) 2008-2024, GreenSock. All rights reserved.
218+
Copyright (c) 2008-2025, GreenSock. All rights reserved.

dist/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*!
2-
* @gsap/react 2.1.1
2+
* @gsap/react 2.1.2
33
* https://gsap.com
44
*
5-
* @license Copyright 2024, GreenSock. All rights reserved.
5+
* @license Copyright 2025, GreenSock. All rights reserved.
66
* Subject to the terms at https://gsap.com/standard-license or for Club GSAP members, the agreement issued with that membership.
77
* @author: Jack Doyle, jack@greensock.com
88
*/
@@ -15,7 +15,7 @@
1515

1616
gsap = gsap && Object.prototype.hasOwnProperty.call(gsap, 'default') ? gsap['default'] : gsap;
1717

18-
let useIsomorphicLayoutEffect = typeof window !== "undefined" ? react.useLayoutEffect : react.useEffect,
18+
let useIsomorphicLayoutEffect = typeof document !== "undefined" ? react.useLayoutEffect : react.useEffect,
1919
isConfig = value => value && !Array.isArray(value) && typeof value === "object",
2020
emptyArray = [],
2121
defaultConfig = {},
@@ -39,16 +39,16 @@
3939
context = react.useRef(_gsap.context(() => {}, scope)),
4040
contextSafe = react.useRef(func => context.current.add(null, func)),
4141
deferCleanup = dependencies && dependencies.length && !revertOnUpdate;
42+
deferCleanup && useIsomorphicLayoutEffect(() => {
43+
mounted.current = true;
44+
return () => context.current.revert();
45+
}, emptyArray);
4246
useIsomorphicLayoutEffect(() => {
4347
callback && context.current.add(callback, scope);
4448
if (!deferCleanup || !mounted.current) {
4549
return () => context.current.revert();
4650
}
4751
}, dependencies);
48-
deferCleanup && useIsomorphicLayoutEffect(() => {
49-
mounted.current = true;
50-
return () => context.current.revert();
51-
}, emptyArray);
5252
return {
5353
context: context.current,
5454
contextSafe: contextSafe.current

dist/index.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@gsap/react",
3-
"version": "2.1.1",
3+
"version": "2.1.2",
44
"description": "Tools for using GSAP in React, like useGSAP() which is a drop-in replacement for useLayoutEffect()/useEffect()",
55
"main": "./dist/index.js",
66
"module": "./src/index.js",
@@ -23,9 +23,9 @@
2323
],
2424
"author": "Jack Doyle (jack@greensock.com)",
2525
"license": "SEE LICENSE AT https://gsap.com/standard-license",
26-
"dependencies": {
26+
"peerDependencies": {
2727
"gsap": "^3.12.5",
28-
"react": ">=16"
28+
"react": ">=17"
2929
},
3030
"bugs": {
3131
"url": "https://gsap.com/community/"

src/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*!
2-
* @gsap/react 2.1.1
2+
* @gsap/react 2.1.2
33
* https://gsap.com
44
*
5-
* Copyright 2008-2024, GreenSock. All rights reserved.
5+
* Copyright 2008-2025, GreenSock. All rights reserved.
66
* Subject to the terms at https://gsap.com/standard-license or for
77
* Club GSAP members, the agreement issued with that membership.
88
* @author: Jack Doyle, jack@greensock.com
@@ -11,7 +11,7 @@
1111
import { useEffect, useLayoutEffect, useRef } from "react";
1212
import gsap from "gsap";
1313

14-
let useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect,
14+
let useIsomorphicLayoutEffect = typeof document !== "undefined" ? useLayoutEffect : useEffect,
1515
isConfig = value => value && !Array.isArray(value) && typeof(value) === "object",
1616
emptyArray = [],
1717
defaultConfig = {},
@@ -33,16 +33,16 @@ export const useGSAP = (callback, dependencies = emptyArray) => {
3333
context = useRef(_gsap.context(() => { }, scope)),
3434
contextSafe = useRef((func) => context.current.add(null, func)),
3535
deferCleanup = dependencies && dependencies.length && !revertOnUpdate;
36+
deferCleanup && useIsomorphicLayoutEffect(() => {
37+
mounted.current = true;
38+
return () => context.current.revert();
39+
}, emptyArray);
3640
useIsomorphicLayoutEffect(() => {
3741
callback && context.current.add(callback, scope);
3842
if (!deferCleanup || !mounted.current) { // React renders bottom-up, thus there could be hooks with dependencies that run BEFORE the component mounts, thus cleanup wouldn't occur since a hook with an empty dependency Array would only run once the component mounts.
3943
return () => context.current.revert();
4044
}
4145
}, dependencies);
42-
deferCleanup && useIsomorphicLayoutEffect(() => {
43-
mounted.current = true;
44-
return () => context.current.revert();
45-
}, emptyArray);
4646
return { context: context.current, contextSafe: contextSafe.current };
4747
};
4848
useGSAP.register = core => { _gsap = core; };

0 commit comments

Comments
 (0)