diff --git a/examples/react-web-components/.eslintignore b/examples/react-web-components/.eslintignore new file mode 100644 index 000000000..1521c8b76 --- /dev/null +++ b/examples/react-web-components/.eslintignore @@ -0,0 +1 @@ +dist diff --git a/examples/react-web-components/.gitignore b/examples/react-web-components/.gitignore new file mode 100644 index 000000000..74ad86279 --- /dev/null +++ b/examples/react-web-components/.gitignore @@ -0,0 +1,3 @@ +node_modules +.cache +public diff --git a/examples/react-web-components/gatsby-config.js b/examples/react-web-components/gatsby-config.js new file mode 100644 index 000000000..1808267b0 --- /dev/null +++ b/examples/react-web-components/gatsby-config.js @@ -0,0 +1,12 @@ +module.exports = { + plugins: [ + { + resolve: 'gatsby-plugin-mdx', + options: { + defaultLayouts: { + default: require.resolve('./src/components/Layout') + } + } + } + ] +} diff --git a/examples/react-web-components/package.json b/examples/react-web-components/package.json new file mode 100644 index 000000000..f52d36d49 --- /dev/null +++ b/examples/react-web-components/package.json @@ -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" + } +} diff --git a/examples/react-web-components/readme.md b/examples/react-web-components/readme.md new file mode 100644 index 000000000..e8e71bafa --- /dev/null +++ b/examples/react-web-components/readme.md @@ -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) diff --git a/examples/react-web-components/src/components/Layout.js b/examples/react-web-components/src/components/Layout.js new file mode 100644 index 000000000..ca022368c --- /dev/null +++ b/examples/react-web-components/src/components/Layout.js @@ -0,0 +1,7 @@ +import React from 'react' + +export default ({children}) => ( + <> +
{children}
+ +) diff --git a/examples/react-web-components/src/components/WebComponent.js b/examples/react-web-components/src/components/WebComponent.js new file mode 100644 index 000000000..7fb282d4c --- /dev/null +++ b/examples/react-web-components/src/components/WebComponent.js @@ -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 = `

Count: ${this.currentCount}

` + } + + increment() { + this.currentCount++ + this.update() + } +} + +window.customElements.define('i-counter', ImperativeCounter) + +export const RenderCounter = () => { + const counterElement = useRef(null) + return ( +
+ + +
+ ) +} + +export default () => ( + <> + + +) diff --git a/examples/react-web-components/src/pages/index.mdx b/examples/react-web-components/src/pages/index.mdx new file mode 100644 index 000000000..b544af347 --- /dev/null +++ b/examples/react-web-components/src/pages/index.mdx @@ -0,0 +1,5 @@ +import WebComponent from '../components/WebComponent' + +# Hello, world! + +