gsap-nuxt-module 1.1.7 → 1.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -18,7 +18,8 @@ Find and replace all on all files (CMD+SHIFT+F):
18
18
 
19
19
  **Enhance your Nuxt application with powerful animations and transitions using GSAP!**
20
20
 
21
- - [🏀 Online playground](https://stackblitz.com/github/LucaArgentieri/gsap-nuxt-module?file=playground%2Fapp.vue)
21
+ - [📖 Documentation](https://lucaargentieri.github.io/gsap-nuxt-module/)
22
+ - [🏀 Online playground](https://stackblitz.com/edit/gsap-nuxt-module?file=README.md)
22
23
  - [📖 GSAP](https://gsap.com/)
23
24
 
24
25
  ## Features
@@ -156,8 +157,8 @@ export default defineNuxtConfig({
156
157
  GSAP animations are not automatically cleaned up when a component unmounts.
157
158
  Use `onUnmounted` to prevent memory leaks.
158
159
 
159
- Do not call `gsap.unregisterPlugin(...)` inside page/components: plugin registration is app-wide.
160
- In components, clean up only the instances created by that component.
160
+ Plugin registration is handled once by the module at app startup — never call `gsap.registerPlugin()` manually.
161
+ In components, clean up only the instances your component created (tweens, timelines, ScrollTrigger instances, Draggable instances, etc.).
161
162
 
162
163
  **Simple plugin instance cleanup (`Draggable`):**
163
164
 
package/dist/module.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "compatibility": {
5
5
  "nuxt": ">=3.0.0"
6
6
  },
7
- "version": "1.1.7",
7
+ "version": "1.1.8",
8
8
  "builder": {
9
9
  "@nuxt/module-builder": "1.0.2",
10
10
  "unbuild": "3.6.1"
@@ -1,15 +1,37 @@
1
+ import { gsap } from 'gsap';
2
+ import type { ComputedRef, Ref, WatchSource } from 'vue';
3
+ type ContextSafeFn = <F extends (...args: unknown[]) => unknown>(fn: F) => F;
4
+ export interface UseGsapOptions {
5
+ scope?: Ref<HTMLElement | null> | ComputedRef<HTMLElement | null>;
6
+ dependencies?: WatchSource | WatchSource[];
7
+ revertOnUpdate?: boolean;
8
+ }
1
9
  /**
2
- * Returns the GSAP instance for direct use.
10
+ * Zero-argument overload — returns the raw GSAP instance.
3
11
  *
4
12
  * Use it to call `gsap.timeline()`, `gsap.to()`, `gsap.set()`,
5
13
  * utility methods (`gsap.utils`, `gsap.ticker`), and more.
6
14
  *
7
15
  * **Cleanup is the caller's responsibility.**
8
- * Use `gsap.context()` + `ctx.revert()` in `onUnmounted` to avoid memory leaks.
9
16
  *
10
17
  * @see https://gsap.com/docs/v3/GSAP/
11
18
  */
12
- export declare function useGsap(): typeof globalThis.gsap;
19
+ export declare function useGsap(): typeof gsap;
20
+ /**
21
+ * Setup-function overload — wraps `gsap.context()` with automatic revert.
22
+ *
23
+ * Animations declared inside `setup` are scoped to the optional `scope` element
24
+ * and are reverted automatically when the component unmounts (or the effect scope
25
+ * is disposed). Pass `dependencies` to re-run `setup` reactively.
26
+ *
27
+ * Returns `{ contextSafe }` — a wrapper for event handlers that need to add
28
+ * animations to the existing context after mount.
29
+ *
30
+ * @see https://gsap.com/docs/v3/GSAP/gsap.context()
31
+ */
32
+ export declare function useGsap(setup: (ctx: gsap.Context) => void, options?: UseGsapOptions): {
33
+ contextSafe: ContextSafeFn;
34
+ };
13
35
  export declare const useScrollTrigger: () => {
14
36
  new (vars: ScrollTrigger.StaticVars, animation?: gsap.core.Animation): {
15
37
  readonly animation?: gsap.core.Animation | undefined;
@@ -258,3 +280,4 @@ export declare const useCustomBounce: () => {
258
280
  create(id: string, vars?: CustomBounceVars): EaseFunction;
259
281
  register(core: object): void;
260
282
  } | null;
283
+ export {};
@@ -1,7 +1,40 @@
1
1
  import { gsap } from "gsap";
2
+ import { onMounted, onScopeDispose, watch } from "vue";
2
3
  import { createGsapComposable } from "../create-gsap-composable.js";
3
- export function useGsap() {
4
- return gsap;
4
+ export function useGsap(setup, options) {
5
+ if (!setup) return gsap;
6
+ if (import.meta.server) {
7
+ return { contextSafe: (fn) => fn };
8
+ }
9
+ let ctx = null;
10
+ const runSetup = () => {
11
+ const scope = options?.scope?.value ?? void 0;
12
+ ctx = gsap.context(setup, scope);
13
+ };
14
+ onMounted(() => {
15
+ runSetup();
16
+ });
17
+ if (options?.dependencies !== void 0) {
18
+ const deps = Array.isArray(options.dependencies) ? options.dependencies : [options.dependencies];
19
+ watch(deps, () => {
20
+ if (options.revertOnUpdate === false) return;
21
+ ctx?.revert();
22
+ runSetup();
23
+ }, { flush: "post" });
24
+ }
25
+ onScopeDispose(() => {
26
+ ctx?.revert();
27
+ ctx = null;
28
+ });
29
+ const contextSafe = (fn) => {
30
+ return ((...args) => {
31
+ if (ctx) {
32
+ return ctx.add(() => fn(...args));
33
+ }
34
+ return fn(...args);
35
+ });
36
+ };
37
+ return { contextSafe };
5
38
  }
6
39
  export const useScrollTrigger = createGsapComposable("ScrollTrigger");
7
40
  export const useScrollSmoother = createGsapComposable("ScrollSmoother");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gsap-nuxt-module",
3
- "version": "1.1.7",
3
+ "version": "1.1.8",
4
4
  "description": "GSAP integration for Nuxt.",
5
5
  "repository": "LucaArgentieri/gsap-nuxt-module",
6
6
  "author": "Luca Argentieri",
@@ -29,6 +29,7 @@
29
29
  "dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
30
30
  "release": "npm run lint && npm run prepack && changelogen --release --patch && npm publish && git push --follow-tags",
31
31
  "lint": "eslint .",
32
+ "lint:fix": "eslint . --fix",
32
33
  "test": "vitest run",
33
34
  "test:watch": "vitest watch",
34
35
  "test:types": "vue-tsc --noEmit && nuxi prepare playground && cd playground && vue-tsc --noEmit"