Skip to content

Instantly share code, notes, and snippets.

@fouad-j
Created October 29, 2020 08:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fouad-j/0e19a0d9b94f50b53c87dbceca51b26c to your computer and use it in GitHub Desktop.
Save fouad-j/0e19a0d9b94f50b53c87dbceca51b26c to your computer and use it in GitHub Desktop.
Create Component (custom-element & slot) with lit-element
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
};
<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>
@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