Skip to content

Commit b1372bf

Browse files
Fix up api.md documentation around event dispatching (#217)
* Fix up api.md documentation around event dispatching * fix typo in api.md
1 parent 3055089 commit b1372bf

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

docs/api.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
convert to a Web Component.
77
- `options` - An set of parameters.
88

9-
- `options.shadow` - Use shadow DOM rather than light DOM.
10-
- `options.dispatchEvents` - Will cause dispatchEvent to be called for functions when attribute is not passed (this object should be same type as passed to [Event constructor options](https://developer.mozilla.org/en-US/docs/Web/API/Event/Event#options))
9+
- `options.shadow` - ("open", "closed", or undefined) Use the specified shadow DOM mode rather than light DOM.
10+
- `options.events` - Array of camelCasedProps to dispatch as custom events or a Record of event names to their associated [Event constructor options](https://developer.mozilla.org/en-US/docs/Web/API/Event/Event#options).
11+
- When dispatching events from named properties, "on" is stripped from the beginning of the property name if present, and the result is lowercased: the property `onMyCustomEvent` dispatches as "mycustomevent".
1112
- `options.props` - Array of camelCasedProps to watch as String values or { [camelCasedProps]: "string" | "number" | "boolean" | "function" | "json" }
1213

1314
- When specifying Array or Object as the type, the string passed into the attribute must pass `JSON.parse()` requirements.
@@ -165,7 +166,7 @@ document.body.innerHTML = `
165166

166167
When `Function` is specified as the type, attribute values on the web component will be converted into function references when passed into the underlying React component. The string value of the attribute must be a valid reference to a function on `window` (or on `global`).
167168

168-
Note: If you want to avoid global functions, instead of passing attribute you can pass `dispatchEvents` object in options and simply listen on events using `addEventListener` on the custom element. See below.
169+
Note: If you want to avoid global functions, instead of passing an attribute you can pass an `events` object in options, and listen on events using `addEventListener` on the custom element. See below.
169170

170171
```js
171172
function ThemeSelect({ handleClick }) {
@@ -204,7 +205,7 @@ setTimeout(
204205

205206
### Event dispatching
206207

207-
When `Function` is specified as the type, instead of passing attribute values referencing global methods, you can simply listen on the DOM event.
208+
As an alternative to using function props, the `events` object insructs r2wc to dispatch a corresponding DOM event that can be listened to on the custom element itself, on ancestor elements using `bubbles`, and outside of any containing shadow DOM using `composed`.
208209

209210
```js
210211
function ThemeSelect({ onSelect }) {
@@ -218,8 +219,7 @@ function ThemeSelect({ onSelect }) {
218219
}
219220

220221
const WebThemeSelect = reactToWebComponent(ThemeSelect, {
221-
props: { onSelect: "function" },
222-
dispatchEvents: { bubbles: true }
222+
events: { onSelect: { bubbles: true } } // dispatches as "select", will bubble to ancestor elements but not escape a shadow DOM
223223
})
224224

225225
customElements.define("theme-select", WebThemeSelect)
@@ -236,4 +236,6 @@ setTimeout(() => {
236236
document.querySelector("theme-select button:last-child").click()
237237
}, 0)
238238
// ^ calls event listener, logs: true, "Jane"
239-
```
239+
```
240+
241+
> Note: `events` and `props` entries should not be used for the same named property. During initial setup, the event handler will overwrite the function property handler, and if the attribute changes after construction, the new function property handler will overwrite the event handler.

0 commit comments

Comments
 (0)