Skip to content

Commit 39f5db7

Browse files
Add section about custom events (#391)
Add section about custom events
2 parents b7c0b10 + fd664b9 commit 39f5db7

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

content/en/guide/v10/web-components.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,18 @@ function Foo() {
4242
return <x-foo ref={myRef} />;
4343
}
4444
```
45+
46+
## Triggering custom events
47+
48+
Preact normalizes the casing of standard built-in DOM Events, which is how we can pass an `onChange` prop to `<div>`, when the event listener actually requires lowercase `"change"`. However, Custom Elements often fire custom events as part of their public API, and there's no way to know what custom events might be fired. In order to ensure Custom Elements are seamlessly supported in Preact, any unrecognized event handler props passed to a DOM Element will have their casing preserved.
49+
50+
```jsx
51+
// native DOM event -> listens for a "click" event
52+
<div onClick={() => console.log('click')} />
53+
54+
// Custom Element
55+
// Add handler for "IonChange" event
56+
<my-foo onIonChange={() => console.log('IonChange')} />
57+
// Add handler for "ionChange" event (note the casing)
58+
<my-foo onionChange={() => console.log('ionChange')} />
59+
```

0 commit comments

Comments
 (0)