Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/react-web-components/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
3 changes: 3 additions & 0 deletions examples/react-web-components/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.cache
public
12 changes: 12 additions & 0 deletions examples/react-web-components/gatsby-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
plugins: [
{
resolve: 'gatsby-plugin-mdx',
options: {
defaultLayouts: {
default: require.resolve('./src/components/Layout')
}
}
}
]
}
17 changes: 17 additions & 0 deletions examples/react-web-components/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "react-web-components-example",
"version": "1.5.9",
"private": true,
"scripts": {
"build": "gatsby build",
"start": "gatsby develop"
},
"dependencies": {
"@mdx-js/mdx": "^1.5.9",
"@mdx-js/react": "^1.5.9",
"gatsby": "^2.20.27",
"gatsby-plugin-mdx": "^1.1.9",
"react": "^16.13.1",
"react-dom": "^16.13.1"
}
}
8 changes: 8 additions & 0 deletions examples/react-web-components/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Web components + React + MDX

```sh
yarn
yarn start
```

> [Try it on CodeSandbox](https://codesandbox.io/s/github/mdx-js/mdx/tree/master/examples/react-web-components)
7 changes: 7 additions & 0 deletions examples/react-web-components/src/components/Layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react'

export default ({children}) => (
<>
<main>{children}</main>
</>
)
39 changes: 39 additions & 0 deletions examples/react-web-components/src/components/WebComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React, {useRef} from 'react'

class ImperativeCounter extends HTMLElement {
constructor() {
super()
this.shadow = this.attachShadow({mode: 'open'})
this.currentCount = 0
this.update()
}

update() {
this.shadow.innerHTML = `<div><h2>Count: ${this.currentCount}</h2></div>`
}

increment() {
this.currentCount++
this.update()
}
}

window.customElements.define('i-counter', ImperativeCounter)

export const RenderCounter = () => {
const counterElement = useRef(null)
return (
<div>
<i-counter ref={counterElement}></i-counter>
<button onClick={() => counterElement.current.increment()}>
Increment
</button>
</div>
)
}

export default () => (
<>
<RenderCounter />
</>
)
5 changes: 5 additions & 0 deletions examples/react-web-components/src/pages/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import WebComponent from '../components/WebComponent'

# Hello, world!

<WebComponent />