forked from stenciljs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap-custom-element.ts
More file actions
168 lines (158 loc) · 5.38 KB
/
Copy pathbootstrap-custom-element.ts
File metadata and controls
168 lines (158 loc) · 5.38 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { BUILD } from '@app-data';
import {
addHostEventListeners,
consoleError,
forceUpdate,
getHostRef,
registerHost,
styles,
supportsShadow,
transformTag,
} from '@platform';
import type * as d from '../declarations';
import { CMP_FLAGS } from '../utils/constants';
import { createShadowRoot } from '../utils/shadow-root';
import { connectedCallback } from './connected-callback';
import { disconnectedCallback } from './disconnected-callback';
import {
patchChildSlotNodes,
patchCloneNode,
patchPseudoShadowDom,
patchSlotAppendChild,
patchTextContent,
} from './dom-extras';
import { computeMode } from './mode';
import { proxyComponent } from './proxy-component';
import { PROXY_FLAGS } from './runtime-constants';
import { attachStyles, getScopeId, hydrateScopedToShadow, registerStyle } from './styles';
export const defineCustomElement = (Cstr: any, compactMeta: d.ComponentRuntimeMetaCompact) => {
customElements.define(
transformTag(compactMeta[1]),
proxyCustomElement(Cstr, compactMeta) as CustomElementConstructor,
);
};
export const proxyCustomElement = (Cstr: any, compactMeta: d.ComponentRuntimeMetaCompact) => {
const cmpMeta: d.ComponentRuntimeMeta = {
$flags$: compactMeta[0],
$tagName$: compactMeta[1],
};
try {
if (BUILD.member) {
cmpMeta.$members$ = compactMeta[2];
}
if (BUILD.hostListener) {
cmpMeta.$listeners$ = compactMeta[3];
}
if (BUILD.propChangeCallback) {
cmpMeta.$watchers$ = Cstr.$watchers$;
cmpMeta.$deserializers$ = Cstr.$deserializers$;
cmpMeta.$serializers$ = Cstr.$serializers$;
}
if (BUILD.reflect) {
cmpMeta.$attrsToReflect$ = [];
}
if (BUILD.shadowDom && !supportsShadow && cmpMeta.$flags$ & CMP_FLAGS.shadowDomEncapsulation) {
// TODO(STENCIL-854): Remove code related to legacy shadowDomShim field
cmpMeta.$flags$ |= CMP_FLAGS.needsShadowDomShim;
}
if (!(cmpMeta.$flags$ & CMP_FLAGS.shadowDomEncapsulation) && cmpMeta.$flags$ & CMP_FLAGS.hasSlot) {
if (BUILD.experimentalSlotFixes) {
patchPseudoShadowDom(Cstr.prototype);
} else {
if (BUILD.slotChildNodesFix) {
patchChildSlotNodes(Cstr.prototype);
}
if (BUILD.cloneNodeFix) {
patchCloneNode(Cstr.prototype);
}
if (BUILD.appendChildSlotFix) {
patchSlotAppendChild(Cstr.prototype);
}
if (BUILD.scopedSlotTextContentFix && cmpMeta.$flags$ & CMP_FLAGS.scopedCssEncapsulation) {
patchTextContent(Cstr.prototype);
}
}
} else if (BUILD.cloneNodeFix) {
patchCloneNode(Cstr.prototype);
}
if (BUILD.hydrateClientSide && BUILD.shadowDom) {
hydrateScopedToShadow();
}
const originalConnectedCallback = Cstr.prototype.connectedCallback;
const originalDisconnectedCallback = Cstr.prototype.disconnectedCallback;
Object.assign(Cstr.prototype, {
__hasHostListenerAttached: false,
__registerHost() {
registerHost(this, cmpMeta);
},
connectedCallback() {
if (!this.__hasHostListenerAttached) {
const hostRef = getHostRef(this);
if (!hostRef) {
return;
}
addHostEventListeners(this, hostRef, cmpMeta.$listeners$, false);
this.__hasHostListenerAttached = true;
}
connectedCallback(this);
if (originalConnectedCallback) {
originalConnectedCallback.call(this);
}
},
disconnectedCallback() {
disconnectedCallback(this);
if (originalDisconnectedCallback) {
originalDisconnectedCallback.call(this);
}
},
__attachShadow() {
if (supportsShadow) {
if (!this.shadowRoot) {
createShadowRoot.call(this, cmpMeta);
} else {
// we want to check to make sure that the mode for the shadow
// root already attached to the element (i.e. created via DSD)
// is set to 'open' since that's the only mode we support
if (this.shadowRoot.mode !== 'open') {
throw new Error(
`Unable to re-use existing shadow root for ${cmpMeta.$tagName$}! Mode is set to ${this.shadowRoot.mode} but Stencil only supports open shadow roots.`,
);
}
}
} else {
(this as any).shadowRoot = this;
}
},
});
Object.defineProperty(Cstr, 'is', {
value: cmpMeta.$tagName$,
configurable: true,
});
return proxyComponent(Cstr, cmpMeta, PROXY_FLAGS.isElementConstructor | PROXY_FLAGS.proxyState);
} catch (e) {
consoleError(e);
return Cstr;
}
};
export const forceModeUpdate = (elm: d.RenderNode) => {
if (BUILD.style && BUILD.mode && !BUILD.lazyLoad) {
const mode = computeMode(elm);
const hostRef = getHostRef(elm);
if (hostRef && hostRef.$modeName$ !== mode) {
const cmpMeta = hostRef.$cmpMeta$;
const oldScopeId = elm['s-sc'];
const scopeId = getScopeId(cmpMeta, mode);
const style = (elm.constructor as any).style[mode];
const flags = cmpMeta.$flags$;
if (style) {
if (!styles.has(scopeId)) {
registerStyle(scopeId, style, !!(flags & CMP_FLAGS.shadowDomEncapsulation));
}
hostRef.$modeName$ = mode;
elm.classList.remove(oldScopeId + '-h', oldScopeId + '-s');
attachStyles(hostRef);
forceUpdate(elm);
}
}
}
};