Skip to content

Commit 748fb42

Browse files
committed
docs: migrate code samples to Sandpack
1 parent fff10ad commit 748fb42

File tree

1 file changed

+49
-9
lines changed
  • src/content/reference/react-dom/components

1 file changed

+49
-9
lines changed

src/content/reference/react-dom/components/index.md

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,21 @@ Non-string JavaScript values passed to custom elements will be serialized by def
188188

189189
React will, however, recognize an custom element's property as one that it may pass arbitrary values to if the property name shows up on the class during construction:
190190

191-
```javascript {4-6}
192-
class MyElement extends HTMLElement {
191+
<Sandpack>
192+
193+
```js src/index.js hidden
194+
import {MyElement} from './MyElement.js';
195+
import { createRoot } from 'react-dom/client';
196+
import {App} from "./App.js";
197+
198+
customElements.define('my-element', MyElement);
199+
200+
const root = createRoot(document.getElementById('root'))
201+
root.render(<App />);
202+
```
203+
204+
```js src/MyElement.js active
205+
export class MyElement extends HTMLElement {
193206
constructor() {
194207
super();
195208
// The value here will be overwritten by React
@@ -203,12 +216,33 @@ class MyElement extends HTMLElement {
203216
}
204217
```
205218

219+
```js src/App.js
220+
export function App() {
221+
return <my-element value={[1,2,3]}></my-element>
222+
}
223+
```
224+
225+
</Sandpack>
226+
206227
#### Listening for events on custom elements {/*custom-element-events*/}
207228

208-
A common pattern when using custom elements is that they may dispatch [`CustomEvent`s](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent) rather than accept a function to call when an event occur:
229+
A common pattern when using custom elements is that they may dispatch [`CustomEvent`s](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent) rather than accept a function to call when an event occur. You can listen for these events using an `on` prefix when binding to the event via JSX.
209230

210-
```javascript {8-15}
211-
class MyElement extends HTMLElement {
231+
<Sandpack>
232+
233+
```js src/index.js hidden
234+
import {MyElement} from './MyElement.js';
235+
import { createRoot } from 'react-dom/client';
236+
import {App} from "./App.js";
237+
238+
customElements.define('my-element', MyElement);
239+
240+
const root = createRoot(document.getElementById('root'))
241+
root.render(<App />);
242+
```
243+
244+
```javascript src/MyElement.js
245+
export class MyElement extends HTMLElement {
212246
constructor() {
213247
super();
214248
this.test = undefined;
@@ -237,12 +271,18 @@ class MyElement extends HTMLElement {
237271
}
238272
```
239273

240-
You can listen for these events using an `on` prefix when binding to the event via JSX:
241-
242-
```jsx
243-
<my-element onspeak={e => console.log(e.detail.message)}></my-element>
274+
```jsx src/App.js active
275+
export function App() {
276+
return (
277+
<my-element
278+
onspeak={e => console.log(e.detail.message)}
279+
></my-element>
280+
)
281+
}
244282
```
245283

284+
</Sandpack>
285+
246286
<Note>
247287

248288
Events are case-sensative and support dashes (`-`). Preserve the casing of the event and include all dashes when listening for custom element's events:

0 commit comments

Comments
 (0)