-
-
Notifications
You must be signed in to change notification settings - Fork 949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Directive function is not defined #569
Comments
Yeah there is two things going on. declare module "solid-js" {
namespace JSX {
interface Directives {
use:clickOutside: boolean;
}
}
} The second part, which I've only realized recently, is TS likes to strip out unused variables even before the bundler gets its hands on it. You can tell it to only strip out unused types with this option: { onlyRemoveTypeImports: true } With both of these you should be set. |
Hi @ryansolid , Thank you for your answer. Using your snippet with quoted property name:
I tried to add the module declaration in
The problem is that now I have errors everywhere : Maybe that is my vscode setup that is wrong. This I do not know where to add:
Searching on google it seems it is a babel setting, though I use Vite. I will try to make a minimal project to try with a clean start. |
Also if my directive is a .tsx and not a .ts then the passed accessor will be undefined. |
I used the tutorial code in a demo, if you want to take a look: https://github.com/lud/-click-outside |
Now using a snowpack boilerplate and it works without the console.log thanks to the config flag you gave me as it uses babel. I've also found a workaround for the type declarations:
Though it is not following the solid js docs. |
Yeah the babel transform in Vite probably needs to be handled by vite-plugin-solid which uses babel under the hood to transform Solid's .tsx files. Although not sure why Directives interface wouldn't work since HTMLAttributes inherit from them. I use that pattern in Solid's tests and I used it in our previous starter. But I am seeing what you are describing in the playground. So I suspect it isn't limited to just there. |
Oh sorry the docs are bad. Or at least misleading. It should be: declare module "solid-js" {
namespace JSX {
interface Directives {
clickOutside?: () => void;
}
}
} You don't need to put the namespace in. It automatically applies it. |
Right! That works.
Also the declaration works if it is right above the code:
"go to definition" shortcuts on code editors would then directly go to the right file. Thank you for your help 👍 |
Hi, I having the same issue. I couldn't configure vite. When I add the option
do I missing something? For now I figured out a workaround
|
I think it's actually on babel-preset-typescript which means we'd need to change the plugin to set that option on the TS conversion. Given how this breaks directives probably should just have it configured that way by default. Made an issue: solidjs/vite-plugin-solid#18 |
This is fixed in |
I couldn't test the directives. The change |
Yeah I had to go in and fix up the templates. But as far as we know that's the only way to get TS to not strip out unused imports. It really shouldn't be its responsibility but it does that. So far we haven't found another way to get TS to play nice and not pre-emptively strip it. |
Thank you Ryan for doing that. I published a new version this morning that that reverted the change and let you manually opt-in the option. Sorry for that one :/ |
Hi! I've also stumbled upon this same issue recently, but solved it with the help above. Thanks to all of you! However, I now get a different error with the click outside function above.
My directive looks like this: |
@codechips Not sure. Looking at the tutorial as well as the latest compiled output the example looks fine. Need to see what it compiles to. Can you reproduce on https://playground.solidjs.com or a codesandbox etc..? |
@ryansolid I tried this in playground and it works fine. I think it's because of how the code is compiled there. However, I've managed to reproduce it with the setup I have in my project. Here is the repo https://github.com/codechips/solidjs-clickoutside-bug Both versions, working and broken, compile down to this for the directive. clickOutside(_el$3, () => () => setShowMenu(false)); It might be that it's something very obvious, but I am totally code-blind atm 😅 |
I have fallen in the same trap before... when you export a default function from a |
@jfgodoy thanks. We need to work on that heuristic a bit. It's a bit tricky though since functions don't tend to have much identifying with them. I guess we could see if it only has one or no arguments. In this case these tend to have 2. But it wouldn't be fullproof as boolean directives might not use the second argument. Short of doing some sort of cross template analysis this might be tough. |
Thanks a ton @jfgodoy! Renaming to @ryansolid maybe the directive file should be renamed in the tutorial and examples as well in order to avoid the confusion. If not, maybe add a note in the docs on when to use |
It's specific to the hot reload plugin. So yeah we should look at the file times. By default the playground(tutorials) only offers up .tsx and .css. I think we should expand that. |
In my rather simple vite project, none of the proposed solutions seems to work. I have a simple
Currently I just "use" it as a sole statement before I use it as a directive: anchorMenu;
//...
<div use:anchorMenu> This works, but it is obviously rather clunky to use. Any tips? I can create a full demo if you want, sadly it does work in the playground. |
The option is here now; it should be Note that you have to both declare the directive and add the plugin option, not just one or the other. The first prevents typescript from complaining that the directive doesn't exist in props, and the second prevents typescript from removing the import during compilation as it assumes you didn't use it anywhere (but Solid's transform will, resulting in a runtime error). |
It should have a way to let typescript reference the function when using |
Hello,
I used the
vite-solid-tailwind-starter
package to start a project and integrate the app with Phoenix Framework.The starter features a dropdown so I decided to integrate the
clickOutside
directive from the tutorial.I added this bit to my div:
First it has this error:
But as the tutorial features a
//@ts-nocheck
comment I guess it is a known bug.Now, when I run the app, I have an error:
If I add a dummy
console.log(clickOutside)
anywhere in the file then the directive works as expected.It looks like the tree is shaken a bit too much.
I get the same behaviour with the default code in the starter (without configuring for Phoenix).
I am not sure if I am missing something, as it should work, since it works in the tutorial.
Thank you
The text was updated successfully, but these errors were encountered: