<meta name="description" content="An example of a custom element to be SSRed with Declarative Shadow DOM">
<h3>An example of a custom element to be SSRed with Declarative Shadow DOM</h3>
<!-- This is just to polyfill declarative <shadow-root> -->
<script src="https://cdn.jsdelivr.net/npm/document-register-element@1.7.2/build/document-register-element.min.js"></script>
<script>/* This is just to polyfill declarative shadowroot*/
customElements.define('shadow-root', class extends HTMLTemplateElement{
//constructor(){ // we cannot use parentElement on constructor
connectedCallback(){
this.parentNode.attachShadow({mode:'open'});
this.parentNode.shadowRoot.appendChild(document.importNode(this.content, true));
}
},{extends: 'template'});
</script>
<p>Check the SSRed result at <a href="http://jsbin.com/gequvih/edit?html,output">http://jsbin.com/gequvih/edit?html,output</a></p>
<hr>
<script>
customElements.define('fancy-content', class extends HTMLElement{
constructor(){
super();
if(!this.shadowRoot){this.attachShadow({mode:'open'});}
this.shadowRoot.innerHTML = `
<!-- a huuuge div-soup full of nasty styles that should not leak -->
<style>
/* Yey! It's Shadow DOM, I can make all spans pink, an it will affect only my playground */
span{
color: pink;
}
::slotted(*){
color: green;
}
</style>
<span>I'm Pinky - the shady span</span>
<div>
<p>Hurray, I can scope element ids in shadow DOM, look at this beautiful input:
<input id="one-and-only" placehoder="shadowdom input"></p>
<slot></slot>
</div>
`;
}
});
</script>
<fancy-content>
<p>some content that goes inside the default slot and becomes green</p>
</fancy-content>
<label for="one-and-only">Label for light dom input</label>
<input id="one-and-only" placeholder="lightdom input">
<p><span>I'm a boring black span, not affected by any styles from someone else's shadow</span></p>
Output
300px
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |