Compare commits

..

3 Commits

Author SHA1 Message Date
Evan Chen 6b63b9f8c6 update 2022-07-18 11:10:42 +08:00
Evan Chen f1698b4cb2 update 2022-07-15 23:04:59 +08:00
Evan Chen cc27b07ce6 example ready 2022-07-15 14:35:33 +08:00
3 changed files with 42 additions and 19 deletions

View File

@ -9,11 +9,11 @@
content="Web site created using create-react-app"
/>
<title>React App</title>
<link rel="stylesheet" href="dist/index.css">
<link rel="stylesheet" href="index.css">
</head>
<body>
<wc-test></wc-test>
<wc-test id='test' name='Evan'></wc-test>
</body>
<script src="dist/index.js"></script>
<script src="index.js" type="module"></script>
</html>

View File

@ -4,35 +4,58 @@ import ReactDOM from 'react-dom/client';
import logo from './logo.svg';
const WcTestApp = () => {
const WcTestApp = (props) => {
const [ctr,incr] = useState(0)
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<p>Hello! {props.name}.</p>
<Button label={`clicked ${ctr} times`} icon="pi pi-check" onClick={()=>{incr(ctr+1)}}/>
</header>
</div>
);
}
WcTestApp.defaultProps = {
name: 'World'
};
// a wrapper class for react component
class WcTest extends HTMLElement {
connectedCallback() {
const mountPoint = this;
// to get attributes from outside
// const name = this.getAttribute('name');
root;
constructor(){
super()
let mountPoint = this;
// ShadowRoot (global styles will not apply, but safer with a complete isolation layer)
// this.attachShadow({ mode: 'open' }).appendChild(mountPoint);
// mountPoint = this.attachShadow({ mode: 'open' });
const root = ReactDOM.createRoot(mountPoint);
// render
root.render(<WcTestApp />);
this.root = ReactDOM.createRoot(mountPoint);
this.render()
}
// declare attributes we want to keep track of
static get observedAttributes() {
return ['name', 'onclick'];
}
// connectedCallback() {
// }
render(){
const name = this.hasAttribute('name')
? this.getAttribute('name')
: undefined;
this.root.render(<WcTestApp name={name} />);
}
attributeChangedCallback(){
this.render()
}
}
customElements.define('wc-test', WcTest);

View File

@ -1,5 +1,5 @@
{
"name": "wc-test",
"name": "prime-react-webcomponent",
"version": "0.1.0",
"private": true,
"dependencies": {
@ -10,8 +10,8 @@
"react-dom": "^18.2.0"
},
"scripts": {
"start": "yarn parcel index.js index.css",
"build": "yarn parcel build index.js index.css"
"start": "yarn parcel index.html",
"build": "(rm -rf dist||true) && yarn parcel build index.html"
},
"browserslist": {
"production": [