Antdev Posted May 1, 2022 Share Posted May 1, 2022 Hello You peeps rocked with helping me previously with a typescript gsap related issue so reaching out again after hours of pulling my hair out. I am using typescript and vue and gsap in my app. I need to disable a draggable but struggling how to store a reference to my draggable so I can disable it. I tried using the following which resulted in "Property 'disable' does not exist on type '{}' when I called this.draggable.disable() data() { return { draggable: {}, proxy:new HTMLDivElement() } } And also the following which resulted in the following error: Type 'Draggable[]' is missing the following properties from type 'typeof _Draggable': prototype, version, zIndex, create, and 3 more data() { return { draggable: Draggable, proxy:new HTMLDivElement() } } Any help would be much appreciated so I can manage to disable my draggable. Thanks Ant See the Pen OJzEOYj by antdev (@antdev) on CodePen Link to comment Share on other sites More sharing options...
PointC Posted May 1, 2022 Share Posted May 1, 2022 I don't know much about Vue, but I think you'd want: this.draggable[0].disable(); Does that help? Happy tweening. 1 1 Link to comment Share on other sites More sharing options...
Antdev Posted May 1, 2022 Author Share Posted May 1, 2022 Thank you very much That does indeed disable the draggable in my test on codepen. See the Pen WNMNJvw by antdev (@antdev) on CodePen Though on my actual app I have the following: this.draggable = Draggable.create(this.proxy, { ... })[0]; So this.draggable.disable(); should have worked. Many thanks Ant Link to comment Share on other sites More sharing options...
Antdev Posted May 1, 2022 Author Share Posted May 1, 2022 I wonder if the issues I am having with setting the type as Draggable is related to this in draggable.d.ts readonly scrollProxy: any; // TODO: Create interface I am pretty sure I have the latest version of gsap Link to comment Share on other sites More sharing options...
OSUblake Posted May 2, 2022 Share Posted May 2, 2022 8 hours ago, Antdev said: I wonder if the issues I am having with setting the type as Draggable is related to this in draggable.d.ts readonly scrollProxy: any; // TODO: Create interface That is not going cause any errors, and you really shouldn't worry about that property as it's really not documented. Can you post your TypeScript code and why you think it's an error? You can use CodeSandbox if you really need to show the TypeScript error. Link to comment Share on other sites More sharing options...
Antdev Posted May 2, 2022 Author Share Posted May 2, 2022 Thanks Blake for confirming the TODO code is not the issue. I did as you suggested and created a CodeSandbox test that includes my code from my app https://codesandbox.io/s/competent-moon-6dykbv?file=/src/App.vue I am not experiencing the issue in this CodeSandbox test but still having the issue on my actual app where when I try to set the type of my draggable instance to Draggable I get the error: TS2739: Type 'Draggable' is missing the following properties from type 'typeof _Draggable': prototype, version, zIndex, create, get > 495 | this.myDraggable = Draggable.create("#work-wheel4", { this.myDraggable = Draggable.create("#work-wheel4", { trigger:".myTrigger", type: "rotation", inertia: true, resistance:0.7, dragResistance:0.7, onDrag: () => this.updateProgress(this.myDraggable), onThrowUpdate: () => this.updateProgress(this.myDraggable), onThrowComplete: () => this.endThrow() })[0]; I have installed the latest bonus file and ran npm install ./gsap-bonus.tgz My import statements are as follows: <script lang="ts"> import { defineComponent } from 'vue'; import { gsap } from 'gsap'; import { ScrollTrigger } from 'gsap/ScrollTrigger'; import { InertiaPlugin } from 'gsap/InertiaPlugin'; import { Draggable } from 'gsap/Draggable'; import Config from './config.json'; gsap.registerPlugin(Draggable, InertiaPlugin, ScrollTrigger); And this is where I set the data property export default defineComponent({ name: 'App', data(){ return { myDraggable: Draggable, Any idea why I am getting this error Link to comment Share on other sites More sharing options...
OSUblake Posted May 2, 2022 Share Posted May 2, 2022 Yeah, that's weird. I have no idea why it might be doing that for you on your setup but not on CodeSandbox. Just curious is using something like typeof Draggable works. But you really don't even need to have myDraggable and the proxy in the data because you really don't need those to be reactive, so something like this should be fine. Also note that if you're only creating a single Draggable, you can use new Draggable instead of Draggable.create to get the instance. Link to comment Share on other sites More sharing options...
Antdev Posted May 2, 2022 Author Share Posted May 2, 2022 Thanks Blake I am curious when you say that I don't need myDraggable and proxy in data. If I remove myDraggable from data I get an error TS2339: Property 'myDraggable' does not exist on type 'ComponentPublicInstance<{} | {}, {}, { I tried using the new Draggable syntax but when I set the type of myDraggable in data to Draggable I still get the error Issues checking in progress... ERROR in src/App.vue:241:7 TS2739: Type '_Draggable' is missing the following properties from type 'typeof _Draggable': prototype, version, zIndex, create, get 239 | 240 | > 241 | this.myDraggable = new Draggable(this.myProxy, { | ^^^^^^^^^^^^^^^^ 242 | trigger: ".scrollTarget", 243 | type: "x", 244 | inertia: true, With regards the bit about whether typeof Draggable works, can you clarify how I would test this - do I add console.log(typeof Draggable) in say the mounted function? Link to comment Share on other sites More sharing options...
OSUblake Posted May 2, 2022 Share Posted May 2, 2022 You shouldn't need to set the type if you exclude from the data as TypeScript should infer its type when you create the Draggable. https://codesandbox.io/s/interesting-euler-rhldbv?file=/src/App.vue The typeof thing I was suggesting was just for the type, but again, you really don't need to include myDraggable or the proxy in the data. return { myDraggable: typeof Draggable, proxy: document.createElement("div"), }; If you're still getting errors, there must something unique to your setup because I cannot reproduce on my end. Do you think you can send me a simplified version of your project that shows the error? A zip or simple repo is fine. Link to comment Share on other sites More sharing options...
Antdev Posted May 2, 2022 Author Share Posted May 2, 2022 Thank you very much Blake. I will try that now. If that doesn't work I will send you a zip. Thanks again Link to comment Share on other sites More sharing options...
Antdev Posted May 2, 2022 Author Share Posted May 2, 2022 No joy - with that so have attached a zip ERROR in src/App.vue:30:7 TS2322: Type 'Draggable' is not assignable to type '"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"'. Type 'Draggable' is not assignable to type '"function"'. 28 | methods: { 29 | init: function () { > 30 | this.myDraggable = Draggable.create(this.proxy, { | ^^^^^^^^^^^^^^^^ 31 | trigger: ".wrapper", 32 | type: "x" 33 | })[0]; ERROR in src/App.vue:37:24 TS2339: Property 'disable' does not exist on type '"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"'. Property 'disable' does not exist on type '"string"'. 35 | disableDraggable() { 36 | alert("disable draggable"); > 37 | this.myDraggable.disable(); | ^^^^^^^ 38 | } 39 | }); 40 | </script> debugApp.zip Link to comment Share on other sites More sharing options...
Antdev Posted May 2, 2022 Author Share Posted May 2, 2022 OK I have made some progress setting myDraggable to an Array of Draggable as I am aware the Draggable.create returns an array Then I needed to pass this.myDraggable[0] in my updateProgress function call Still confused why the other way didn't work that worked in CodeSandbox didn't work in my app instance but at least now I can happily disable my draggable Be still interested if you so look at the zip I uploaded if you discover anything but either way thank you very much for your timely responses. data() { return { myDraggable: Array<Draggable>(), proxy: document.createElement("div"), }; }, mounted() { this.init(); }, methods: { init: function () { this.myDraggable = Draggable.create(this.proxy, { trigger: ".wrapper", type: "x", inertia: true, resistance: 0.75, dragResistance: 0.75, onDrag: () => this.updateProgress(this.myDraggable[0]), }); }, updateProgress(myDraggable) { var debug = document.getElementById("debug"); debug.innerHTML = "draggable.x: " + myDraggable.x; }, disableDraggable() { this.myDraggable[0].disable(); }, }, Link to comment Share on other sites More sharing options...
OSUblake Posted May 2, 2022 Share Posted May 2, 2022 Thanks, I'll check it out. Link to comment Share on other sites More sharing options...
Antdev Posted May 2, 2022 Author Share Posted May 2, 2022 Thanks Blake - much appreciated. Link to comment Share on other sites More sharing options...
Solution OSUblake Posted May 3, 2022 Solution Share Posted May 3, 2022 I think you might need to use the data thing if you want full TS support without going through a bunch of hoops. This is the exact issue, but there is no good solution as the type will always be any and you have to use $options. https://github.com/vuejs/docs/issues/694 Unless you add some custom types... https://github.com/vuejs/docs/issues/721 But, I can't believe I didn't see what the initial problem was. This is not setting the type. It's setting myDraggable to the actual class. myDraggable: Draggable To get it to work, we actually need to define the type for the data object that is return by the function call. No errors here. export default defineComponent({ name: 'App', data(): { myDraggable?: Draggable } { return { myDraggable: undefined, } }, mounted() { this.init(); }, methods: { init() { let proxy = document.createElement("div"); this.myDraggable = new Draggable(proxy, { trigger: ".wrapper", type: "x" }); }, disableDraggable() { this.myDraggable?.disable(); } } }); If anyone knows a cleaner way to do this in Vue 3 without having to use data, please do share 🙏 1 Link to comment Share on other sites More sharing options...
Antdev Posted May 3, 2022 Author Share Posted May 3, 2022 Thank you Blake for your dedication to helping me solve this conundrum. I think I will stick with the solution setting it as an array as that feels cleaner to me. myDraggable: Array<Draggable>(), Hopefully there is no issue doing it this way? Many thanks Paul Link to comment Share on other sites More sharing options...
OSUblake Posted May 3, 2022 Share Posted May 3, 2022 3 minutes ago, Antdev said: myDraggable: Array<Draggable>(), Hopefully there is no issue doing it this way? I mean, it's fine as long you remember that you're always going to be dealing with an array. But I would still focus on my solution, because you may need to something similar to that in future. Like say you want to pause a Tween, how you would define that in the data? export default defineComponent({ data() { return { myTween: ??????????? }; }, mounted() { this.myTween = gsap.to(".foo", { x: 300 }); }, methods: { pauseTween() { this.myTween.pause(); } } }); If you define that data type, it should be no issue. export default defineComponent({ data(): { myTween?: gsap.core.Tween } { return { myTween: undefined }; }, mounted() { this.myTween = gsap.to(".foo", { x: 300 }); }, methods: { pauseTween() { this.myTween?.pause(); } } }); 1 Link to comment Share on other sites More sharing options...
OSUblake Posted May 3, 2022 Share Posted May 3, 2022 I guess something like this, but still a little sketchy as we are defining an object and then changing it in mounted. export default defineComponent({ data() { return { myTween: {} as gsap.core.Tween }; }, mounted() { this.myTween = gsap.to(".foo", { x: 300 }); }, methods: { pauseTween() { this.myTween?.pause(); } } }); 1 Link to comment Share on other sites More sharing options...
Antdev Posted May 3, 2022 Author Share Posted May 3, 2022 Ah OK thanks Blake tho interestingly I have no problem with the following in my app baseTl: gsap.timeline(), animation:gsap.timeline(), then in mounted I have this.baseTl = gsap.timeline({ paused: true }); this.animation = gsap .timeline({ repeat: -1, paused: true }) .add(this.baseTl.tweenFromTo(1, 2)); Link to comment Share on other sites More sharing options...
Antdev Posted May 3, 2022 Author Share Posted May 3, 2022 Anyway yeah I agree your solution is the best option and I will go with that as I have several apps with this kind of stuff in it and I don't want to get stuck again Link to comment Share on other sites More sharing options...
OSUblake Posted May 3, 2022 Share Posted May 3, 2022 Yeah, that's creating to 2 timelines. You creating one here. baseTl: gsap.timeline() And then overwriting the original one here. console.log("baseTl", this.baseTl); // logs out the original timeline this.baseTl = gsap.timeline({ paused: true }); That's why I explicitly asked about a Tween because I knew you would do that with a Timeline 😉 Link to comment Share on other sites More sharing options...
Antdev Posted May 3, 2022 Author Share Posted May 3, 2022 Thanks again Blake - I need to read up more on typescript as my lack of knowledge has really hampered me with this project. However I believe the advantages it has brought me in organising all the data that populates my apps was worth the pain I am suffering. It is just frustrating when you are up against the clock to get stuck for days on a line of code Link to comment Share on other sites More sharing options...
OSUblake Posted May 3, 2022 Share Posted May 3, 2022 Are you required to use TypeScript? Sometimes is more trouble than it's worth. Link to comment Share on other sites More sharing options...
Antdev Posted May 3, 2022 Author Share Posted May 3, 2022 Unfortunately yes it is a requirement and I am on the home run now fortunately. I will think twice next time about using typescript with gsap Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now