Skip to content

Commit 8fceeeb

Browse files
authored
Refine custom directives documentation in use.mdx
1 parent e284f54 commit 8fceeeb

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

  • src/routes/reference/jsx-attributes

src/routes/reference/jsx-attributes/use.mdx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,35 @@ title: use:*
33
order: 5
44
---
55

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 resizing, which are cumbersome to repeat in JSX.
7+
8+
A directive is a function with the following signature
79

810
```ts
9-
function directive(element: Element, accessor: () => any): void
11+
function directive(element: HTMLElement, accessor: Signal<any>): void
1012
```
1113

1214
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.
1315

1416
```tsx
1517
const [name, setName] = createSignal("");
1618
17-
function model(el, value) {
18-
const [field, setField] = value();
19-
createRenderEffect(() => (el.value = field()));
20-
el.addEventListener("input", (e) => setField(e.target.value));
19+
function model(element: HTMLInputElement, accessor: Accessor<Signal<string>>) {
20+
const [field, setField] = accessor();
21+
createRenderEffect(() => (element.value = field()));
22+
element.addEventListener("input", ({ target }) => setField(target.value));
2123
}
2224
2325
<input type="text" use:model={[name, setName]} />
2426
```
2527

26-
To register with TypeScript extend the JSX namespace.
28+
To register with TypeScript extend the JSX namespace
2729

2830
```ts
2931
declare module "solid-js" {
3032
namespace JSX {
31-
interface Directives {
32-
model: [() => any, (v: any) => any]
33+
interface DirectiveFunctions {
34+
model: typeof model;
3335
}
3436
}
3537
}

0 commit comments

Comments
 (0)