@madgex/design-system-ce 5.5.6 → 5.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,341 +0,0 @@
1
- /* eslint-disable no-cond-assign */
2
- /* eslint-disable no-param-reassign */
3
- /* eslint-disable guard-for-in */
4
- /* eslint-disable no-nested-ternary */
5
- /* eslint-disable no-restricted-syntax */
6
- /* eslint-disable no-undef */
7
- /* eslint-disable no-underscore-dangle */
8
- /* eslint-disable max-classes-per-file */
9
-
10
- /**
11
- * We're using a modified `defineCustomElement` where we can choose not to use shadowDom
12
- * shadowDom prevents access to external CSS, which is how design-system css works!
13
- * https://github.com/vuejs/core/issues/4314#issuecomment-1021393430
14
- */
15
- import { createVNode, defineComponent, nextTick, warn } from '@vue/runtime-core';
16
- import { camelize, extend, hyphenate, isArray, toNumber } from '@vue/shared';
17
- import HTMLParsedElement from 'html-parsed-element';
18
- import { hydrate, render } from '@vue/runtime-dom';
19
-
20
- const BaseClass = typeof HTMLElement !== 'undefined' ? HTMLParsedElement : class {};
21
-
22
- const __DEV__ = process.env.NODE_ENV !== 'production';
23
-
24
- export class VueElement extends BaseClass {
25
- constructor(_def, _props = {}, _config = {}, _hydrate) {
26
- super();
27
- this._def = _def;
28
- this._props = _props;
29
- this._config = _config;
30
- /**
31
- * @internal
32
- */
33
- this._instance = null;
34
- this._connected = false;
35
- this._resolved = false;
36
- this._numberProps = null;
37
- this._config = extend(
38
- {
39
- shadowRoot: true,
40
- },
41
- this._config
42
- );
43
- if (this._config.shadowRoot) {
44
- if (this.shadowRoot && _hydrate) {
45
- _hydrate(this._createVNode(), this._root);
46
- } else {
47
- if (__DEV__ && this.shadowRoot) {
48
- warn(
49
- `Custom element has pre-rendered declarative shadow root but is not ` +
50
- `defined as hydratable. Use \`defineSSRCustomElement\`.`
51
- );
52
- }
53
-
54
- this.attachShadow({ mode: 'open' });
55
- }
56
- } else if (_hydrate) {
57
- _hydrate(this._createVNode(), this._root);
58
- }
59
- }
60
-
61
- get _root() {
62
- return this._config.shadowRoot ? this.shadowRoot : this;
63
- }
64
-
65
- connectedCallback() {
66
- if (this._config.shadowRoot) {
67
- this._connect();
68
- } else {
69
- super.connectedCallback();
70
- }
71
- }
72
-
73
- // use of parsedCallback when shadowRoot is disabled
74
- // to wait for slots to be parsed
75
- // see https://stackoverflow.com/a/52884370
76
- parsedCallback() {
77
- if (!this._config.shadowRoot) {
78
- this._connect();
79
- }
80
- }
81
-
82
- _connect() {
83
- this._connected = true;
84
- if (!this._instance) {
85
- this._resolveDef();
86
- }
87
- }
88
-
89
- disconnectedCallback() {
90
- this._connected = false;
91
- nextTick(() => {
92
- if (!this._connected) {
93
- render(null, this._root);
94
- this._instance = null;
95
- }
96
- });
97
- }
98
-
99
- /**
100
- * resolve inner component definition (handle possible async component)
101
- */
102
- _resolveDef() {
103
- if (this._resolved) {
104
- return;
105
- }
106
-
107
- this._resolved = true;
108
- // set initial attrs
109
- for (let i = 0; i < this.attributes.length; i++) {
110
- this._setAttr(this.attributes[i].name);
111
- }
112
-
113
- // watch future attr changes
114
- new MutationObserver((mutations) => {
115
- for (const m of mutations) {
116
- this._setAttr(m.attributeName);
117
- }
118
- }).observe(this, { attributes: true });
119
- const resolve = (def) => {
120
- const { props, styles } = def;
121
- const hasOptions = !isArray(props);
122
- const rawKeys = props ? (hasOptions ? Object.keys(props) : props) : [];
123
- // cast Number-type props set before resolve
124
- let numberProps;
125
-
126
- // add props check to fix https://github.com/vuejs/core/issues/5326
127
- if (hasOptions && props) {
128
- for (const key in this._props) {
129
- const opt = props[key];
130
-
131
- if (opt === Number || (opt && opt.type === Number)) {
132
- this._props[key] = toNumber(this._props[key]);
133
- (numberProps || (numberProps = Object.create(null)))[key] = true;
134
- }
135
- }
136
- }
137
-
138
- this._numberProps = numberProps;
139
- // check if there are props set pre-upgrade or connect
140
- for (const key of Object.keys(this)) {
141
- if (key[0] !== '_') {
142
- this._setProp(key, this[key], true, false);
143
- }
144
- }
145
-
146
- // defining getter/setters on prototype
147
- for (const key of rawKeys.map(camelize)) {
148
- Object.defineProperty(this, key, {
149
- get() {
150
- return this._getProp(key);
151
- },
152
- set(val) {
153
- this._setProp(key, val);
154
- },
155
- });
156
- }
157
-
158
- // replace slot
159
- if (!this._config.shadowRoot) {
160
- this._slots = Array.from(this.children).map((n) => n.cloneNode(true));
161
-
162
- if(Element.prototype.replaceChildren) {
163
- this.replaceChildren();
164
- } else {
165
- this.textContent = '';
166
- }
167
- }
168
-
169
- // apply CSS
170
- this._applyStyles(styles);
171
- // initial render
172
- this._update();
173
- };
174
- const asyncDef = this._def.__asyncLoader;
175
-
176
- if (asyncDef) {
177
- asyncDef().then(resolve);
178
- } else {
179
- resolve(this._def);
180
- }
181
- }
182
-
183
- _setAttr(key) {
184
- let value = this.getAttribute(key);
185
-
186
- if (this._numberProps && this._numberProps[key]) {
187
- value = toNumber(value);
188
- }
189
-
190
- this._setProp(camelize(key), value, false);
191
- }
192
-
193
- /**
194
- * @internal
195
- */
196
- _getProp(key) {
197
- return this._props[key];
198
- }
199
-
200
- /**
201
- * @internal
202
- */
203
- _setProp(key, val, shouldReflect = true, shouldUpdate = true) {
204
- if (val !== this._props[key]) {
205
- this._props[key] = val;
206
- if (shouldUpdate && this._instance) {
207
- this._update();
208
- }
209
-
210
- // reflect
211
- if (shouldReflect) {
212
- if (val === true) {
213
- this.setAttribute(hyphenate(key), '');
214
- } else if (typeof val === 'string' || typeof val === 'number') {
215
- this.setAttribute(hyphenate(key), `${val}`);
216
- } else if (!val) {
217
- this.removeAttribute(hyphenate(key));
218
- }
219
- }
220
- }
221
- }
222
-
223
- _update() {
224
- render(this._createVNode(), this._root);
225
- }
226
-
227
- _createVNode() {
228
- let childs = null;
229
-
230
- // web components without shadow DOM do not supports native slot
231
- // so, we create a VNode based on the content of child nodes.
232
- // NB: named slots are currently not supported
233
- if (!this._config.shadowRoot) {
234
- childs = () => {
235
- const toObj = (a) => {
236
- const res = {};
237
-
238
- for (let i = 0, l = a.length; i < l; i++) {
239
- const attr = a[i];
240
-
241
- res[attr.nodeName] = attr.nodeValue;
242
- }
243
-
244
- return res;
245
- };
246
-
247
- return this._slots.map((n) => {
248
- const attrs = n.attributes ? toObj(n.attributes) : {};
249
-
250
- attrs.innerHTML = n.innerHTML;
251
- return createVNode(n.tagName, attrs, null);
252
- });
253
- };
254
- }
255
-
256
- const vnode = createVNode(this._def, extend({}, this._props), childs);
257
-
258
- if (!this._instance) {
259
- vnode.ce = (instance) => {
260
- this._instance = instance;
261
- if (this._config.shadowRoot) {
262
- instance.isCE = true;
263
- }
264
-
265
- // HMR
266
- if (__DEV__) {
267
- instance.ceReload = (newStyles) => {
268
- // always reset styles
269
- if (this._styles) {
270
- this._styles.forEach((s) => this._root.removeChild(s));
271
- this._styles.length = 0;
272
- }
273
-
274
- this._applyStyles(newStyles);
275
- // if this is an async component, ceReload is called from the inner
276
- // component so no need to reload the async wrapper
277
- if (!this._def.__asyncLoader) {
278
- // reload
279
- this._instance = null;
280
- this._update();
281
- }
282
- };
283
- }
284
-
285
- // intercept emit
286
- instance.emit = (event, ...args) => {
287
- this.dispatchEvent(
288
- new CustomEvent(event, {
289
- detail: args,
290
- })
291
- );
292
- };
293
-
294
- // locate nearest Vue custom element parent for provide/inject
295
- let parent = this;
296
-
297
- while ((parent = parent && (parent.parentNode || parent.host))) {
298
- if (parent instanceof VueElement) {
299
- instance.parent = parent._instance;
300
- break;
301
- }
302
- }
303
- };
304
- }
305
-
306
- return vnode;
307
- }
308
-
309
- _applyStyles(styles) {
310
- if (styles) {
311
- styles.forEach((css) => {
312
- const s = document.createElement('style');
313
-
314
- s.textContent = css;
315
- this._root.appendChild(s);
316
- // record for HMR
317
- if (__DEV__) {
318
- (this._styles || (this._styles = [])).push(s);
319
- }
320
- });
321
- }
322
- }
323
- }
324
-
325
- export function defineCustomElement(options, config, hydrate_) {
326
- const Comp = defineComponent(options);
327
-
328
- class VueCustomElement extends VueElement {
329
- constructor(initialProps) {
330
- super(Comp, initialProps, config, hydrate_);
331
- }
332
- }
333
-
334
- VueCustomElement.def = Comp;
335
- return VueCustomElement;
336
- }
337
-
338
- export const defineSSRCustomElement = (options, config) => {
339
- // @ts-expect-error
340
- return defineCustomElement(options, config, hydrate);
341
- };