-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
149 lines (138 loc) · 4.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
Usage - convert svelte app to web component
import component from "svelte-tag"
new component({component:App,tagname:"hello-world",href="/your/stylesheet.css",attributes:["name"]})
*/
// witchcraft from svelte issue - https://github.com/sveltejs/svelte/issues/2588
import { detach, insert, noop } from 'svelte/internal';
function createSlots(slots) {
const svelteSlots = {};
for (const slotName in slots) {
svelteSlots[slotName] = [createSlotFn(slots[slotName])];
}
function createSlotFn(element) {
return function() {
return {
c: noop,
m: function mount(target, anchor) {
insert(target, element.cloneNode(true), anchor);
},
d: function destroy(detaching) {
if (detaching && element.innerHTML){
detach(element);
}
},
l: noop,
};
}
}
return svelteSlots;
}
export default function(opts){
class Wrapper extends HTMLElement{
constructor() {
super();
this.slotcount = 0
let root = opts.shadow ? this.attachShadow({ mode: 'open' }) : this
// link generated style
if(opts.href && opts.shadow){
let link = document.createElement('link');
link.setAttribute("href",opts.href)
link.setAttribute("rel","stylesheet")
root.appendChild(link);
}
if(opts.shadow){
this._root = document.createElement('div')
root.appendChild(this._root)
}else{
this._root = root
}
}
static get observedAttributes() {
return opts.attributes || []
}
connectedCallback(){
let props = opts.defaults ? opts.defaults : {};
let slots
props.$$scope = {}
Array.from(this.attributes).forEach( attr => props[attr.name] = attr.value )
props.$$scope = {}
if(opts.shadow){
slots = this.getShadowSlots()
let props = opts.defaults ? opts.defaults : {};
props.$$scope = {}
this.observer = new MutationObserver(this.processMutations.bind(this,{root:this._root,props}))
this.observer.observe(this,{childList: true, subtree: true, attributes: false})
}else{
slots = this.getSlots()
}
this.slotcount = Object.keys(slots).length
props.$$slots = createSlots(slots)
this.elem = new opts.component({ target: this._root, props});
}
disconnectedCallback(){
if(this.observe){
this.observer.disconnect()
}
try{ this.elem.$destroy()}catch(err){} // detroy svelte element when removed from dom
}
unwrap(from){
let node = document.createDocumentFragment();
while (from.firstChild) {
node.appendChild(from.removeChild(from.firstChild));
}
return node
}
getSlots(){
const namedSlots = this.querySelectorAll('[slot]')
let slots = {}
namedSlots.forEach(n=>{
slots[n.slot] = this.unwrap(n)
this.removeChild(n)
})
if(this.innerHTML.length){
slots.default = this.unwrap(this)
this.innerHTML = ""
}
return slots
}
getShadowSlots(){
const namedSlots = this.querySelectorAll('[slot]')
let slots = {}
let htmlLength = this.innerHTML.length
namedSlots.forEach(n=>{
slots[n.slot] = document.createElement("slot")
slots[n.slot].setAttribute("name",n.slot)
htmlLength-=n.outerHTML.length
})
if(htmlLength>0){
slots.default = document.createElement("slot")
}
return slots
}
processMutations({root,props},mutations){
for(let mutation of mutations){
if(mutation.type=="childList"){
let slots = this.getShadowSlots()
if(Object.keys(slots).length){
props.$$slots = createSlots(slots)
this.elem.$set({"$$slots":createSlots(slots)})
// do full re-render on slot count change - needed for tabs component
if(this.slotcount != Object.keys(slots).length){
Array.from(this.attributes).forEach( attr => props[attr.name] = attr.value )
this.slotcount = Object.keys(slots).length
root.innerHTML = ""
this.elem = new opts.component({ target: root, props});
}
}
}
}
}
attributeChangedCallback(name, oldValue, newValue) {
if(this.elem && newValue!=oldValue){
this.elem.$set({[name]:newValue})
}
}
}
window.customElements.define(opts.tagname, Wrapper);
}