-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
324 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
{ | ||
"presets": [ | ||
"@babel/preset-env", | ||
"@babel/preset-react" | ||
"react-app", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width" /> | ||
<title>Editor Web component</title> | ||
</head> | ||
<body> | ||
<editor-wc title="a web component!"></editor-wc> | ||
<script src="./bundle.js"></script> | ||
</body> | ||
|
||
<script> | ||
document.addEventListener('DOMContentLoaded', (e) => { | ||
const webComp = document.querySelector('editor-wc'); | ||
|
||
webComp.menuItems = ['one', 'two'] | ||
|
||
// subscribe to the 'custom' event which is pushed by the Foo component in a click handler | ||
webComp.addEventListener('custom', function(e) { | ||
console.log('listener in index html'); | ||
}); | ||
}); | ||
</script> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.foo { | ||
color: red; | ||
cursor: pointer; | ||
font-weight: 900; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React from 'react'; | ||
import Style from 'style-it'; | ||
import styles from './Foo.css'; | ||
|
||
const Foo = (props) => { | ||
console.log(props) | ||
const { title } = props; | ||
|
||
const clickHandler = () => { | ||
|
||
const customEvent = new CustomEvent("custom", { | ||
bubbles: true, | ||
cancelable: false, | ||
composed: true | ||
}); | ||
|
||
// This seems to be easiest way of firing an event on the web component | ||
const foo = document.querySelector('editor-wc') | ||
console.log('click event in react component') | ||
foo.dispatchEvent(customEvent) | ||
} | ||
|
||
return ( | ||
<Style> | ||
{styles.toString()} | ||
<div className="App"> | ||
<header className="App-header"> | ||
<p className='foo'> | ||
<span onClick={clickHandler}>Wow {title}!</span> | ||
</p> | ||
</header> | ||
</div> | ||
</Style> | ||
); | ||
} | ||
|
||
export default Foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import * as ReactDOMClient from 'react-dom/client'; | ||
import Foo from './components/Foo/Foo.js'; | ||
|
||
class WebComponent extends HTMLElement { | ||
mountPoint; | ||
componentAttributes = {}; | ||
componentProperties = {}; | ||
|
||
connectedCallback() { | ||
console.log('connected') | ||
this.mountReactApp(); | ||
} | ||
|
||
disconnectedCallback() { | ||
ReactDOM.unmountComponentAtNode(this.mountPoint); | ||
} | ||
|
||
static get observedAttributes() { | ||
return ['title']; | ||
} | ||
|
||
attributeChangedCallback(name, oldVal, newVal) { | ||
console.log('attr changed') | ||
console.log(name, newVal) | ||
this.componentAttributes[name] = newVal; | ||
|
||
// this.mountReactApp(); | ||
} | ||
|
||
get menuItems() { | ||
// console.log('get menuitems') | ||
return this.componentProperties.menuItems; | ||
} | ||
|
||
set menuItems(newValue) { | ||
console.log('menu items set') | ||
this.componentProperties.menuItems = newValue; | ||
|
||
this.mountReactApp(); | ||
} | ||
|
||
reactProps() { | ||
return { ...this.componentAttributes, ...this.componentProperties }; | ||
} | ||
|
||
mountReactApp() { | ||
// console.log('mount') | ||
let root; | ||
if (!this.mountPoint) { | ||
console.log('no mountpoint') | ||
this.mountPoint = document.createElement('div'); | ||
this.mountPoint.setAttribute("id", "root"); | ||
this.attachShadow({ mode: 'open' }).appendChild(this.mountPoint); | ||
root = ReactDOMClient.createRoot(this.mountPoint); | ||
|
||
root.render(<Foo { ...this.reactProps() }/>); | ||
} | ||
} | ||
} | ||
|
||
window.customElements.define('editor-wc', WebComponent); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const path = require('path'); | ||
|
||
module.exports = { | ||
entry: path.resolve(__dirname, './src/web-component.js'), | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.(js|jsx)$/, | ||
exclude: /node_modules/, | ||
use: ['babel-loader'] | ||
}, | ||
{ | ||
test: /\.css$/, | ||
use: [ | ||
'css-loader', | ||
] | ||
} | ||
] | ||
}, | ||
resolve: { | ||
extensions: ['*', '.js', '.jsx', '.css'] | ||
}, | ||
output: { | ||
path: path.resolve(__dirname, './public'), | ||
filename: 'bundle.js', | ||
}, | ||
devServer: { | ||
contentBase: path.resolve(__dirname, './public/web-component'), | ||
port: 9000 | ||
}, | ||
}; |
Oops, something went wrong.