modality-kit 0.8.9 → 0.8.10
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.
- package/dist/browser.js +263 -1
- package/dist/index.js +5856 -3
- package/package.json +4 -4
package/dist/browser.js
CHANGED
|
@@ -1 +1,263 @@
|
|
|
1
|
-
var
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __moduleCache = /* @__PURE__ */ new WeakMap;
|
|
6
|
+
var __toCommonJS = (from) => {
|
|
7
|
+
var entry = __moduleCache.get(from), desc;
|
|
8
|
+
if (entry)
|
|
9
|
+
return entry;
|
|
10
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function")
|
|
12
|
+
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
|
|
13
|
+
get: () => from[key],
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
}));
|
|
16
|
+
__moduleCache.set(from, entry);
|
|
17
|
+
return entry;
|
|
18
|
+
};
|
|
19
|
+
var __export = (target, all) => {
|
|
20
|
+
for (var name in all)
|
|
21
|
+
__defProp(target, name, {
|
|
22
|
+
get: all[name],
|
|
23
|
+
enumerable: true,
|
|
24
|
+
configurable: true,
|
|
25
|
+
set: (newValue) => all[name] = () => newValue
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// src/browser.ts
|
|
30
|
+
var exports_browser = {};
|
|
31
|
+
__export(exports_browser, {
|
|
32
|
+
render: () => render,
|
|
33
|
+
registerStore: () => registerStore,
|
|
34
|
+
ReactiveComponent: () => ReactiveComponent
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(exports_browser);
|
|
37
|
+
|
|
38
|
+
// src/ReactiveComponent.ts
|
|
39
|
+
class ReactiveComponent extends HTMLElement {
|
|
40
|
+
#state;
|
|
41
|
+
#isRendering = false;
|
|
42
|
+
#pendingUpdate = false;
|
|
43
|
+
#hasRendered = false;
|
|
44
|
+
#shadow;
|
|
45
|
+
#options;
|
|
46
|
+
#delegatedEventListeners = {};
|
|
47
|
+
#eventTypes = [
|
|
48
|
+
"click",
|
|
49
|
+
"dblclick",
|
|
50
|
+
"mousedown",
|
|
51
|
+
"mouseup",
|
|
52
|
+
"mousemove",
|
|
53
|
+
"mouseover",
|
|
54
|
+
"mouseout",
|
|
55
|
+
"focus",
|
|
56
|
+
"blur",
|
|
57
|
+
"change",
|
|
58
|
+
"input",
|
|
59
|
+
"submit",
|
|
60
|
+
"keydown",
|
|
61
|
+
"keyup",
|
|
62
|
+
"keypress"
|
|
63
|
+
];
|
|
64
|
+
_stores;
|
|
65
|
+
_storeListeners;
|
|
66
|
+
constructor(options = {}) {
|
|
67
|
+
super();
|
|
68
|
+
this.#options = options;
|
|
69
|
+
this.#state = options.initialState || {};
|
|
70
|
+
}
|
|
71
|
+
get state() {
|
|
72
|
+
return Object.freeze({ ...this.#state });
|
|
73
|
+
}
|
|
74
|
+
setState(updator, _action, prevState) {
|
|
75
|
+
const oldState = prevState || { ...this.#state };
|
|
76
|
+
let newUpdates;
|
|
77
|
+
if (typeof updator === "function") {
|
|
78
|
+
newUpdates = updator(this.#state);
|
|
79
|
+
} else {
|
|
80
|
+
newUpdates = updator;
|
|
81
|
+
}
|
|
82
|
+
const newState = { ...this.#state, ...newUpdates };
|
|
83
|
+
if (this.#options.shouldUpdate && !this.#options.shouldUpdate(newState, oldState)) {
|
|
84
|
+
return this.#state;
|
|
85
|
+
} else {
|
|
86
|
+
this.#state = newState;
|
|
87
|
+
this.#scheduleUpdate(oldState);
|
|
88
|
+
return newState;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
forceUpdate() {
|
|
92
|
+
this.#scheduleUpdate({ ...this.#state });
|
|
93
|
+
}
|
|
94
|
+
#scheduleUpdate(previousState) {
|
|
95
|
+
if (this.#isRendering || this.#pendingUpdate) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
this.#pendingUpdate = true;
|
|
99
|
+
queueMicrotask(() => {
|
|
100
|
+
if (this.#pendingUpdate && this.isConnected) {
|
|
101
|
+
this.#performUpdate(previousState);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
#performUpdate(previousState) {
|
|
106
|
+
this.#isRendering = true;
|
|
107
|
+
this.#pendingUpdate = false;
|
|
108
|
+
const wasFirstRender = !this.#hasRendered;
|
|
109
|
+
const prevState = previousState || { ...this.#state };
|
|
110
|
+
try {
|
|
111
|
+
this.#updateDOM();
|
|
112
|
+
this.#hasRendered = true;
|
|
113
|
+
if (!wasFirstRender && typeof this.componentDidUpdate === "function") {
|
|
114
|
+
this.componentDidUpdate(this.#state, prevState);
|
|
115
|
+
}
|
|
116
|
+
} catch (error) {
|
|
117
|
+
console.error("Error during component update:", error);
|
|
118
|
+
} finally {
|
|
119
|
+
this.#isRendering = false;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
static #trustedTypesPolicy = (() => {
|
|
123
|
+
if (typeof window !== "undefined" && window.trustedTypes && window.trustedTypes.createPolicy) {
|
|
124
|
+
try {
|
|
125
|
+
return window.trustedTypes.createPolicy("reactive-component", {
|
|
126
|
+
createHTML: (string) => string
|
|
127
|
+
});
|
|
128
|
+
} catch (e) {
|
|
129
|
+
console.warn("Failed to create trusted types policy:", e);
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return null;
|
|
134
|
+
})();
|
|
135
|
+
#safeSetInnerHTML(element, html) {
|
|
136
|
+
if (ReactiveComponent.#trustedTypesPolicy) {
|
|
137
|
+
element.innerHTML = ReactiveComponent.#trustedTypesPolicy.createHTML(html);
|
|
138
|
+
} else {
|
|
139
|
+
element.innerHTML = html;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
#updateDOM() {
|
|
143
|
+
if (!this.#shadow) {
|
|
144
|
+
this.#shadow = this.shadowRoot || this.attachShadow({ mode: "open" });
|
|
145
|
+
this.#setupEventDelegation();
|
|
146
|
+
}
|
|
147
|
+
if (typeof window !== "undefined" && window.trustedTypes && window.trustedTypes.emptyHTML) {
|
|
148
|
+
this.#shadow.innerHTML = window.trustedTypes.emptyHTML;
|
|
149
|
+
} else {
|
|
150
|
+
while (this.#shadow.firstChild) {
|
|
151
|
+
this.#shadow.removeChild(this.#shadow.firstChild);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
const renderResult = this.render();
|
|
155
|
+
if (typeof renderResult === "string") {
|
|
156
|
+
const template = document.createElement("template");
|
|
157
|
+
this.#safeSetInnerHTML(template, renderResult);
|
|
158
|
+
this.#shadow.appendChild(template.content.cloneNode(true));
|
|
159
|
+
} else if (renderResult instanceof DocumentFragment || renderResult instanceof Element) {
|
|
160
|
+
this.#shadow.appendChild(renderResult);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
#setupEventDelegation() {
|
|
164
|
+
this.#eventTypes.forEach((eventType) => {
|
|
165
|
+
const listener = (e) => {
|
|
166
|
+
this.#handleDelegatedEvent(e);
|
|
167
|
+
};
|
|
168
|
+
this.#delegatedEventListeners[eventType] = listener;
|
|
169
|
+
const usePassive = !["mousedown", "keydown", "submit"].includes(eventType);
|
|
170
|
+
this.#shadow.addEventListener(eventType, listener, {
|
|
171
|
+
passive: usePassive
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
#handleDelegatedEvent(e) {
|
|
176
|
+
const eventType = e.type;
|
|
177
|
+
const target = e.target;
|
|
178
|
+
if (!target)
|
|
179
|
+
return;
|
|
180
|
+
const handlerAttribute = `data-${eventType}`;
|
|
181
|
+
const elementsWithHandlers = this.#shadow.querySelectorAll(`[${handlerAttribute}]`);
|
|
182
|
+
Array.from(elementsWithHandlers).forEach((element) => {
|
|
183
|
+
if (target.isSameNode(element) || element.contains(target)) {
|
|
184
|
+
const handlerName = element.getAttribute(handlerAttribute);
|
|
185
|
+
if (handlerName && typeof this[handlerName] === "function") {
|
|
186
|
+
this[handlerName](e);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
connectedCallback() {
|
|
192
|
+
if (this._stores && this._stores.length > 0) {
|
|
193
|
+
this._storeListeners = [];
|
|
194
|
+
this._stores.forEach((store) => {
|
|
195
|
+
const boundSetState = this.setState.bind(this);
|
|
196
|
+
store.addListener(boundSetState);
|
|
197
|
+
this._storeListeners.push({ store, listener: boundSetState });
|
|
198
|
+
const initialState = store.getState();
|
|
199
|
+
this.setState(initialState);
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
this.#performUpdate();
|
|
203
|
+
if (typeof this.componentDidMount === "function") {
|
|
204
|
+
this.componentDidMount();
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
disconnectedCallback() {
|
|
208
|
+
if (this.#shadow && Object.keys(this.#delegatedEventListeners).length > 0) {
|
|
209
|
+
this.#eventTypes.forEach((eventType) => {
|
|
210
|
+
const listener = this.#delegatedEventListeners[eventType];
|
|
211
|
+
if (listener) {
|
|
212
|
+
this.#shadow.removeEventListener(eventType, listener);
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
this.#delegatedEventListeners = {};
|
|
216
|
+
}
|
|
217
|
+
if (this._storeListeners && this._storeListeners.length > 0) {
|
|
218
|
+
this._storeListeners.forEach(({ store, listener }) => {
|
|
219
|
+
store.removeListener(listener);
|
|
220
|
+
});
|
|
221
|
+
this._storeListeners = [];
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function render(componentName, props = {}, stores) {
|
|
226
|
+
const element = document.createElement(componentName);
|
|
227
|
+
const { appendTo = document.body, ...restProps } = props;
|
|
228
|
+
Object.keys(restProps).forEach((key) => {
|
|
229
|
+
const value = restProps[key];
|
|
230
|
+
if (value != null) {
|
|
231
|
+
const stringValue = typeof value === "object" ? JSON.stringify(value) : String(value);
|
|
232
|
+
element.setAttribute(key, stringValue);
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
if (stores && stores.length > 0) {
|
|
236
|
+
element._stores = stores;
|
|
237
|
+
}
|
|
238
|
+
if (appendTo instanceof HTMLElement) {
|
|
239
|
+
appendTo.appendChild(element);
|
|
240
|
+
}
|
|
241
|
+
return element;
|
|
242
|
+
}
|
|
243
|
+
var lazyStores = { current: {} };
|
|
244
|
+
function registerStore(componentName, stores) {
|
|
245
|
+
const connectionSectionElement = document.querySelector("connection-section");
|
|
246
|
+
if (connectionSectionElement) {
|
|
247
|
+
connectionSectionElement._stores = stores;
|
|
248
|
+
} else {
|
|
249
|
+
lazyStores.current[componentName] = stores;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (typeof document !== "undefined") {
|
|
253
|
+
document.addEventListener("DOMContentLoaded", () => {
|
|
254
|
+
const promise = Object.keys(lazyStores.current).map(async (componentName) => {
|
|
255
|
+
const connectionSectionElement = document.querySelector(componentName);
|
|
256
|
+
if (connectionSectionElement) {
|
|
257
|
+
connectionSectionElement._stores = lazyStores.current[componentName];
|
|
258
|
+
delete lazyStores.current[componentName];
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
Promise.all(promise);
|
|
262
|
+
});
|
|
263
|
+
}
|