You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/routes/reference/jsx-attributes/use.mdx
+64-11Lines changed: 64 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,38 +3,91 @@ title: use:*
3
3
order: 5
4
4
---
5
5
6
-
These are custom directives. In a sense this is just syntax sugar over ref but allows us to easily attach multiple directives to a single element. A directive is a function with the following signature:
6
+
Custom directives attach reusable behavior to DOM elements, acting as syntactic sugar over `ref`. They’re ideal for complex DOM interactions like scrolling, tooltips, or form handling, which are cumbersome to repeat in JSX.
7
+
8
+
A directive is a function with the following signature
7
9
8
10
```ts
9
-
function directive(element:Element, accessor:() =>any):void
11
+
function directive(element:HTMLElement, accessor:Accessor<any>):void;
10
12
```
11
13
12
14
Directive functions are called at render time but before being added to the DOM. You can do whatever you'd like in them including create signals, effects, register clean-up etc.
To type custom directives, extend the `DirectiveFunctions` interface
27
37
28
38
```ts
39
+
declaremodule"solid-js" {
40
+
namespaceJSX {
41
+
interfaceDirectiveFunctions {
42
+
model:typeofmodel;
43
+
}
44
+
}
45
+
}
46
+
```
47
+
48
+
If you just want to constrain the second argument to the directive function, you can extend the older `Directives` interface
49
+
50
+
```tsx
29
51
declaremodule"solid-js" {
30
52
namespaceJSX {
31
53
interfaceDirectives {
32
-
model: [() => any, (v: any) => any]
54
+
model:Signal<string>;
33
55
}
34
56
}
35
57
}
36
58
```
37
59
60
+
## Avoiding Tree-Shaking
61
+
62
+
When importing a directive `d` from another module and using it only as `use:d`, TypeScript (via [babel-preset-typescript](https://babeljs.io/docs/babel-preset-typescript)) may remove the import, as it doesn’t recognize `use:d` as a reference to `d`.
63
+
To prevent this:
64
+
65
+
1. Use the `onlyRemoveTypeImports: true` option in `babel-preset-typescript`. For `vite-plugin-solid`, add this to `vite.config.ts`
66
+
67
+
```ts
68
+
importsolidPluginfrom"vite-plugin-solid";
69
+
70
+
exportdefault {
71
+
plugins: [
72
+
solidPlugin({
73
+
typescript: { onlyRemoveTypeImports: true }
74
+
})
75
+
],
76
+
};
77
+
```
78
+
79
+
Note: This requires consistent use of `export type` and `import type` in your codebase to avoid issues.
80
+
81
+
2. Add a fake access like `false && d;` in the module
82
+
83
+
```tsx
84
+
import { model } from"./directives";
85
+
false&&model; // Prevents tree-shaking
86
+
<inputtype="text"use:model={[name, setName]} />;
87
+
```
88
+
89
+
This is removed by bundlers like Terser, unlike a plain `model;` which may remain in the bundle.
90
+
38
91
:::caution[Limitations]
39
92
Directives only work with native HTML elements (HTML/SVG/MathML/Custom Elements).
40
93
Directives are not forwarded and **won't work in user defined components**, such as `<MyComponent use:myinput={[..]}/>`[see also](https://github.com/solidjs/solid/discussions/722)
0 commit comments