Created
October 29, 2020 08:43
-
-
Save fouad-j/0e19a0d9b94f50b53c87dbceca51b26c to your computer and use it in GitHub Desktop.
Create Component (custom-element & slot) with lit-element
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
import {LitElement} from 'lit-element'; | |
class LitElementLight extends LitElement { | |
slotMap: object; | |
connectedCallback() { | |
this.slotMap = Array | |
.from(this.renderRoot.querySelectorAll('[slot]')) | |
.reduce((map, obj) => ({ | |
...map, | |
[obj.getAttribute('slot')]: obj | |
}), {}); | |
super.connectedCallback(); | |
} | |
protected getSlot(slotName: string): ChildNode { | |
return this.slotMap && this.slotMap[slotName]; | |
} | |
createRenderRoot() { | |
return this; | |
} | |
} | |
export { | |
LitElementLight | |
}; |
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
<test-light> | |
<div slot="actions"> | |
<button type="button" class="btn btn-primary" (click)="toggleImage()">Angular Action</button> | |
<button type="button" class="btn btn-secondary" v-on:click="counter += 1">VueJS Action</button> | |
</div> | |
</test-light> |
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
@customElement('test-light') | |
class TestLight extends LitElementLight { | |
render() { | |
return html` | |
<div>LitElement content bla...</div> | |
<div>${this.getSlot('actions')}</div> | |
<div>Styling is global</div> | |
`; | |
} | |
} | |
declare global { | |
interface HTMLElementTagNameMap { | |
'test-light': TestLight; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment