-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JSX plus declarative shadow root example
- Loading branch information
1 parent
45b7fea
commit b108166
Showing
3 changed files
with
96 additions
and
15 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
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,54 @@ | ||
// JSX does not support inline style tags | ||
// https://stackoverflow.com/questions/27530462/tag-error-react-jsx-style-tag-error-on-render | ||
const styles = ` | ||
:host .card { | ||
width: 30%; | ||
margin: 0 auto; | ||
} | ||
:host h3 { | ||
text-align: center; | ||
} | ||
:host button { | ||
margin: 0 auto; | ||
display: block; | ||
} | ||
`; | ||
|
||
export default class CardJsx extends HTMLElement { | ||
|
||
selectItem() { | ||
alert(`selected item is => ${this.getAttribute('title')}!`); | ||
} | ||
|
||
connectedCallback() { | ||
if (!this.shadowRoot) { | ||
console.log('NO shadowRoot detected for card.jsx!'); | ||
this.thumbnail = this.getAttribute('thumbnail'); | ||
this.title = this.getAttribute('title'); | ||
|
||
this.attachShadow({ mode: 'open' }); | ||
this.render(); | ||
} else { | ||
console.log('SUCCESS, shadowRoot detected for card.jsx!'); | ||
} | ||
} | ||
|
||
render() { | ||
const { thumbnail, title } = this; | ||
|
||
return ( | ||
<div> | ||
<style> | ||
{styles} | ||
</style> | ||
<h3>{title}</h3> | ||
<img src={thumbnail} alt={title} loading="lazy" width="100%"/> | ||
<button onclick={this.selectItem}>View Item Details</button> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
customElements.define('sb-card-jsx', CardJsx); |
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