@codedia/react-web-component-bridge 0.1.0

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.
Files changed (63) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +16 -0
  3. package/dist/configuration/global-config.d.ts +4 -0
  4. package/dist/configuration/global-config.d.ts.map +1 -0
  5. package/dist/configuration/registry.d.ts +6 -0
  6. package/dist/configuration/registry.d.ts.map +1 -0
  7. package/dist/core/create-react-element.d.ts +4 -0
  8. package/dist/core/create-react-element.d.ts.map +1 -0
  9. package/dist/core/define-react-element.d.ts +5 -0
  10. package/dist/core/define-react-element.d.ts.map +1 -0
  11. package/dist/core/event-controller.d.ts +9 -0
  12. package/dist/core/event-controller.d.ts.map +1 -0
  13. package/dist/core/form-controller.d.ts +11 -0
  14. package/dist/core/form-controller.d.ts.map +1 -0
  15. package/dist/core/lifecycle.d.ts +11 -0
  16. package/dist/core/lifecycle.d.ts.map +1 -0
  17. package/dist/core/method-controller.d.ts +11 -0
  18. package/dist/core/method-controller.d.ts.map +1 -0
  19. package/dist/core/portal-controller.d.ts +12 -0
  20. package/dist/core/portal-controller.d.ts.map +1 -0
  21. package/dist/core/property-controller.d.ts +25 -0
  22. package/dist/core/property-controller.d.ts.map +1 -0
  23. package/dist/core/render-controller.d.ts +25 -0
  24. package/dist/core/render-controller.d.ts.map +1 -0
  25. package/dist/core/slot-controller.d.ts +9 -0
  26. package/dist/core/slot-controller.d.ts.map +1 -0
  27. package/dist/core/style-controller.d.ts +10 -0
  28. package/dist/core/style-controller.d.ts.map +1 -0
  29. package/dist/index.cjs +848 -0
  30. package/dist/index.cjs.map +1 -0
  31. package/dist/index.d.ts +9 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +831 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/metadata/create-metadata.d.ts +3 -0
  36. package/dist/metadata/create-metadata.d.ts.map +1 -0
  37. package/dist/metadata/index.cjs +84 -0
  38. package/dist/metadata/index.cjs.map +1 -0
  39. package/dist/metadata/index.d.ts +3 -0
  40. package/dist/metadata/index.d.ts.map +1 -0
  41. package/dist/metadata/index.js +57 -0
  42. package/dist/metadata/index.js.map +1 -0
  43. package/dist/metadata/types.d.ts +2 -0
  44. package/dist/metadata/types.d.ts.map +1 -0
  45. package/dist/react.cjs +912 -0
  46. package/dist/react.cjs.map +1 -0
  47. package/dist/react.d.ts +20 -0
  48. package/dist/react.d.ts.map +1 -0
  49. package/dist/react.js +866 -0
  50. package/dist/react.js.map +1 -0
  51. package/dist/types/public.d.ts +125 -0
  52. package/dist/types/public.d.ts.map +1 -0
  53. package/dist/utilities/casing.d.ts +4 -0
  54. package/dist/utilities/casing.d.ts.map +1 -0
  55. package/dist/utilities/errors.d.ts +3 -0
  56. package/dist/utilities/errors.d.ts.map +1 -0
  57. package/dist/utilities/parsing.d.ts +3 -0
  58. package/dist/utilities/parsing.d.ts.map +1 -0
  59. package/dist/utilities/scheduling.d.ts +3 -0
  60. package/dist/utilities/scheduling.d.ts.map +1 -0
  61. package/dist/utilities/serialization.d.ts +3 -0
  62. package/dist/utilities/serialization.d.ts.map +1 -0
  63. package/package.json +57 -0
package/dist/react.cjs ADDED
@@ -0,0 +1,912 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/react.ts
31
+ var react_exports = {};
32
+ __export(react_exports, {
33
+ Children: () => import_react3.Children,
34
+ Component: () => import_react3.Component,
35
+ Fragment: () => import_react3.Fragment,
36
+ Profiler: () => import_react3.Profiler,
37
+ PureComponent: () => import_react3.PureComponent,
38
+ StrictMode: () => import_react3.StrictMode,
39
+ Suspense: () => import_react3.Suspense,
40
+ cloneElement: () => import_react3.cloneElement,
41
+ configureReactApi: () => configureReactApi,
42
+ configureReactElements: () => configureReactElements,
43
+ createComponentTag: () => createComponentTag,
44
+ createContext: () => import_react3.createContext,
45
+ createElement: () => import_react3.createElement,
46
+ createReactElement: () => createReactElement,
47
+ createRef: () => import_react3.createRef,
48
+ createWebComponent: () => createWebComponent,
49
+ default: () => react_default,
50
+ defineComponentTag: () => defineComponentTag,
51
+ defineReactElement: () => defineReactElement,
52
+ defineReactElements: () => defineReactElements,
53
+ defineWebComponent: () => defineWebComponent,
54
+ forwardRef: () => import_react3.forwardRef,
55
+ getReactElementDefinition: () => getReactElementDefinition,
56
+ getReactElementGlobalConfig: () => getReactElementGlobalConfig,
57
+ isReactElementDefined: () => isReactElementDefined,
58
+ isValidElement: () => import_react3.isValidElement,
59
+ lazy: () => import_react3.lazy,
60
+ listReactElementDefinitions: () => listReactElementDefinitions,
61
+ memo: () => import_react3.memo,
62
+ startTransition: () => import_react3.startTransition,
63
+ useCallback: () => import_react3.useCallback,
64
+ useContext: () => import_react3.useContext,
65
+ useDebugValue: () => import_react3.useDebugValue,
66
+ useDeferredValue: () => import_react3.useDeferredValue,
67
+ useEffect: () => import_react3.useEffect,
68
+ useId: () => import_react3.useId,
69
+ useImperativeHandle: () => import_react3.useImperativeHandle,
70
+ useInsertionEffect: () => import_react3.useInsertionEffect,
71
+ useLayoutEffect: () => import_react3.useLayoutEffect,
72
+ useMemo: () => import_react3.useMemo,
73
+ useReducer: () => import_react3.useReducer,
74
+ useRef: () => import_react3.useRef,
75
+ useState: () => import_react3.useState,
76
+ useSyncExternalStore: () => import_react3.useSyncExternalStore,
77
+ useTransition: () => import_react3.useTransition,
78
+ version: () => import_react3.version
79
+ });
80
+ module.exports = __toCommonJS(react_exports);
81
+ var import_react3 = __toESM(require("react"), 1);
82
+
83
+ // src/configuration/global-config.ts
84
+ var globalConfig = {
85
+ development: readNodeEnv() !== "production"
86
+ };
87
+ function configureReactElements(config) {
88
+ globalConfig = { ...globalConfig, ...config };
89
+ return getReactElementGlobalConfig();
90
+ }
91
+ function getReactElementGlobalConfig() {
92
+ return { ...globalConfig };
93
+ }
94
+ function readNodeEnv() {
95
+ const global = globalThis;
96
+ return global.process?.env?.NODE_ENV;
97
+ }
98
+
99
+ // src/configuration/registry.ts
100
+ var definitions = /* @__PURE__ */ new Map();
101
+ function registerDefinition(definition) {
102
+ definitions.set(definition.tagName, definition);
103
+ }
104
+ function getReactElementDefinition(tagName) {
105
+ return definitions.get(tagName.toLowerCase());
106
+ }
107
+ function isReactElementDefined(tagName) {
108
+ const normalized = tagName.toLowerCase();
109
+ if (typeof customElements !== "undefined" && customElements.get(normalized)) {
110
+ return true;
111
+ }
112
+ return definitions.has(normalized);
113
+ }
114
+ function listReactElementDefinitions() {
115
+ return [...definitions.values()];
116
+ }
117
+
118
+ // src/utilities/casing.ts
119
+ function camelToKebab(value) {
120
+ return value.replace(/([a-z0-9])([A-Z])/g, "$1-$2").replace(/[_\s]+/g, "-").toLowerCase();
121
+ }
122
+ function normalizeCustomElementName(tagName) {
123
+ const normalized = tagName.trim().toLowerCase();
124
+ if (!/^[a-z][.0-9_a-z]*-[-.0-9_a-z]*$/.test(normalized)) {
125
+ throw new Error(`Custom element names must be valid kebab-case names: "${tagName}".`);
126
+ }
127
+ return normalized;
128
+ }
129
+
130
+ // src/metadata/create-metadata.ts
131
+ function createMetadata(tagName, options) {
132
+ const props = options.props ?? {};
133
+ const events = options.events ?? {};
134
+ const slots = options.slots ?? {};
135
+ const methods = options.methods ?? {};
136
+ return {
137
+ tagName,
138
+ properties: Object.entries(props).map(
139
+ ([name, rawDefinition]) => {
140
+ const definition = rawDefinition;
141
+ const property = { name };
142
+ if (definition?.attribute !== false)
143
+ property.attribute = definition?.attribute ?? camelToKebab(name);
144
+ if (definition?.type) property.type = definition.type;
145
+ if (definition?.required !== void 0) property.required = definition.required;
146
+ return property;
147
+ }
148
+ ),
149
+ attributes: Object.entries(props).filter(
150
+ ([, definition]) => definition?.attribute !== false
151
+ ).map(([name, rawDefinition]) => {
152
+ const definition = rawDefinition;
153
+ const attribute = {
154
+ name: definition?.attribute ?? camelToKebab(name),
155
+ property: name
156
+ };
157
+ if (definition?.reflect !== void 0) attribute.reflect = definition.reflect;
158
+ return attribute;
159
+ }),
160
+ events: Object.entries(events).map(([prop, rawDefinition]) => {
161
+ const definition = rawDefinition;
162
+ return {
163
+ name: definition?.name ?? camelToKebab(prop.replace(/^on/, "")),
164
+ prop,
165
+ bubbles: definition?.bubbles ?? true,
166
+ composed: definition?.composed ?? true
167
+ };
168
+ }),
169
+ slots: Object.entries(slots).map(([prop, definition]) => ({
170
+ prop,
171
+ name: definition === true ? null : typeof definition === "string" ? definition : definition?.name ?? null
172
+ })),
173
+ methods: Object.keys(methods).map((name) => ({ name })),
174
+ formAssociated: Boolean(options.form),
175
+ shadow: options.shadow !== false
176
+ };
177
+ }
178
+
179
+ // src/utilities/scheduling.ts
180
+ function scheduleMicrotask(task) {
181
+ if (typeof queueMicrotask === "function") {
182
+ queueMicrotask(task);
183
+ return;
184
+ }
185
+ Promise.resolve().then(task);
186
+ }
187
+
188
+ // src/core/event-controller.ts
189
+ var EventController = class {
190
+ constructor(host, options) {
191
+ this.host = host;
192
+ this.options = options;
193
+ }
194
+ host;
195
+ options;
196
+ createEventProps() {
197
+ const eventProps = {};
198
+ for (const [propName, definition] of Object.entries(this.options.events ?? {})) {
199
+ eventProps[propName] = (...args) => this.dispatch(propName, definition, args);
200
+ }
201
+ return eventProps;
202
+ }
203
+ dispatch(propName, definition, args) {
204
+ const eventName = definition?.name ?? camelToKebab(propName.replace(/^on/, ""));
205
+ const detail = definition?.detail ? definition.detail(...args) : args.length <= 1 ? args[0] : [...args];
206
+ const event = new CustomEvent(eventName, {
207
+ detail,
208
+ bubbles: definition?.bubbles ?? true,
209
+ composed: definition?.composed ?? true,
210
+ cancelable: definition?.cancelable ?? false
211
+ });
212
+ const accepted = this.host.dispatchEvent(event);
213
+ return definition?.cancelable ? accepted : void 0;
214
+ }
215
+ };
216
+
217
+ // src/core/form-controller.ts
218
+ var FormController = class {
219
+ constructor(host, options) {
220
+ this.host = host;
221
+ this.options = options;
222
+ }
223
+ host;
224
+ options;
225
+ internals;
226
+ connect() {
227
+ if (!this.options.form || this.internals) {
228
+ return;
229
+ }
230
+ const attach = this.host.attachInternals;
231
+ if (attach) {
232
+ this.internals = attach.call(this.host);
233
+ }
234
+ }
235
+ update(name, value) {
236
+ if (!this.options.form || name !== this.options.form.valueProp) {
237
+ return;
238
+ }
239
+ const serialized = this.options.form.serializeValue ? this.options.form.serializeValue(value) : defaultSerializeValue(value);
240
+ this.internals?.setFormValue(serialized);
241
+ const validation = this.options.form.validate?.(value, this.host);
242
+ if (validation && validation !== true) {
243
+ this.internals?.setValidity({ customError: true }, validation);
244
+ } else {
245
+ this.internals?.setValidity({});
246
+ }
247
+ }
248
+ emitStandardChangeEvents() {
249
+ if (!this.options.form) {
250
+ return;
251
+ }
252
+ this.host.dispatchEvent(new Event("input", { bubbles: true, composed: true }));
253
+ this.host.dispatchEvent(new Event("change", { bubbles: true, composed: true }));
254
+ }
255
+ };
256
+ function defaultSerializeValue(value) {
257
+ if (value === void 0 || value === null) return null;
258
+ if (value instanceof File) return value;
259
+ if (typeof value === "object") return JSON.stringify(value);
260
+ return String(value);
261
+ }
262
+
263
+ // src/core/lifecycle.ts
264
+ var LifecycleController = class {
265
+ constructor(render, requestRender, cleanup) {
266
+ this.render = render;
267
+ this.requestRender = requestRender;
268
+ this.cleanup = cleanup;
269
+ }
270
+ render;
271
+ requestRender;
272
+ cleanup;
273
+ disconnectToken = 0;
274
+ connected() {
275
+ this.disconnectToken++;
276
+ this.render.reconnect();
277
+ this.requestRender();
278
+ }
279
+ disconnected(host) {
280
+ const token = ++this.disconnectToken;
281
+ queueMicrotask(() => {
282
+ if (token !== this.disconnectToken || host.isConnected) {
283
+ return;
284
+ }
285
+ this.cleanup();
286
+ this.render.unmount();
287
+ });
288
+ }
289
+ };
290
+
291
+ // src/core/method-controller.ts
292
+ var MethodController = class {
293
+ constructor(options) {
294
+ this.options = options;
295
+ }
296
+ options;
297
+ ref;
298
+ queue = /* @__PURE__ */ new Map();
299
+ setRef(value) {
300
+ this.ref = value;
301
+ for (const [name, calls] of this.queue) {
302
+ for (const args of calls) {
303
+ this.call(name, args);
304
+ }
305
+ }
306
+ this.queue.clear();
307
+ }
308
+ createRefSetter() {
309
+ return (value) => this.setRef(value);
310
+ }
311
+ call(name, args, host) {
312
+ const definition = this.options.methods?.[name];
313
+ if (!definition) {
314
+ throw new Error(`Unknown public method "${name}".`);
315
+ }
316
+ if (!this.ref) {
317
+ if (definition.queue) {
318
+ const calls = this.queue.get(name) ?? [];
319
+ calls.push([...args]);
320
+ this.queue.set(name, calls);
321
+ return void 0;
322
+ }
323
+ throw new Error(`Cannot call "${name}" before the React component ref is mounted.`);
324
+ }
325
+ return definition.call(this.ref, host ?? void 0, ...args);
326
+ }
327
+ };
328
+
329
+ // src/core/portal-controller.ts
330
+ var PortalController = class {
331
+ constructor(host, renderRoot, options) {
332
+ this.host = host;
333
+ this.renderRoot = renderRoot;
334
+ this.options = options;
335
+ }
336
+ host;
337
+ renderRoot;
338
+ options;
339
+ container;
340
+ getPortalProps() {
341
+ const portal = this.options.portal;
342
+ if (!portal?.enabled || !portal.prop) {
343
+ return {};
344
+ }
345
+ return { [portal.prop]: this.getContainer() };
346
+ }
347
+ cleanup() {
348
+ this.container?.remove();
349
+ this.container = void 0;
350
+ }
351
+ getContainer() {
352
+ if (this.container) {
353
+ return this.container;
354
+ }
355
+ const portal = this.options.portal;
356
+ const target = portal?.target;
357
+ if (target instanceof HTMLElement) {
358
+ this.container = target;
359
+ return target;
360
+ }
361
+ if (typeof target === "function") {
362
+ this.container = target(this.host);
363
+ return this.container;
364
+ }
365
+ const container = document.createElement("div");
366
+ container.setAttribute("part", portal?.part ?? "overlay-root");
367
+ container.setAttribute("data-rwcb-portal", "");
368
+ if (target === "body") {
369
+ document.body.append(container);
370
+ } else if (target === "host") {
371
+ this.host.append(container);
372
+ } else {
373
+ this.renderRoot.append(container);
374
+ }
375
+ this.container = container;
376
+ return container;
377
+ }
378
+ };
379
+
380
+ // src/utilities/errors.ts
381
+ function warnDevelopment(enabled, message) {
382
+ if (enabled && typeof console !== "undefined") {
383
+ console.warn(`[react-web-component-bridge] ${message}`);
384
+ }
385
+ }
386
+ function assertBrowserApi(apiName, value) {
387
+ if (!value) {
388
+ throw new Error(
389
+ `${apiName} is unavailable. Custom element registration must run in a browser.`
390
+ );
391
+ }
392
+ }
393
+
394
+ // src/utilities/parsing.ts
395
+ function parseAttributeValue(value, definition, host) {
396
+ if (definition.parseAttribute) {
397
+ return definition.parseAttribute(value, host);
398
+ }
399
+ switch (definition.type ?? "string") {
400
+ case "boolean":
401
+ return value !== null;
402
+ case "number": {
403
+ if (value === null || value.trim() === "") return void 0;
404
+ const parsed = Number(value);
405
+ return Number.isFinite(parsed) ? parsed : void 0;
406
+ }
407
+ case "json": {
408
+ if (value === null || value.trim() === "") return void 0;
409
+ try {
410
+ return JSON.parse(value);
411
+ } catch {
412
+ return void 0;
413
+ }
414
+ }
415
+ case "date": {
416
+ if (value === null || value.trim() === "") return void 0;
417
+ const date = new Date(value);
418
+ return Number.isNaN(date.getTime()) ? void 0 : date;
419
+ }
420
+ case "string":
421
+ return value;
422
+ default:
423
+ return value;
424
+ }
425
+ }
426
+
427
+ // src/utilities/serialization.ts
428
+ function serializeAttributeValue(value, definition, host) {
429
+ if (definition.serializeAttribute) {
430
+ return definition.serializeAttribute(value, host);
431
+ }
432
+ if (value === void 0 || value === null || value === false) {
433
+ return null;
434
+ }
435
+ switch (definition.type ?? "string") {
436
+ case "boolean":
437
+ return value ? "" : null;
438
+ case "json":
439
+ try {
440
+ return JSON.stringify(value);
441
+ } catch {
442
+ return null;
443
+ }
444
+ case "date":
445
+ return value instanceof Date && !Number.isNaN(value.getTime()) ? value.toISOString() : null;
446
+ case "number":
447
+ return typeof value === "number" && Number.isFinite(value) ? String(value) : null;
448
+ case "string":
449
+ return String(value);
450
+ default:
451
+ return String(value);
452
+ }
453
+ }
454
+
455
+ // src/core/property-controller.ts
456
+ var PropertyController = class {
457
+ constructor(context) {
458
+ this.context = context;
459
+ for (const [prop, definition] of Object.entries(this.context.options.props ?? {})) {
460
+ const attribute = this.attributeName(prop, definition);
461
+ if (attribute) {
462
+ this.attributeToProp.set(attribute, prop);
463
+ }
464
+ if (definition?.default !== void 0) {
465
+ this.values.set(prop, definition.default);
466
+ }
467
+ }
468
+ }
469
+ context;
470
+ values = /* @__PURE__ */ new Map();
471
+ reflectingAttributes = /* @__PURE__ */ new Set();
472
+ attributeToProp = /* @__PURE__ */ new Map();
473
+ static observedAttributes(options) {
474
+ return Object.entries(options.props ?? {}).map(
475
+ ([prop, definition]) => definition?.attribute === false ? void 0 : definition?.attribute ?? camelToKebab(prop)
476
+ ).filter((value) => Boolean(value));
477
+ }
478
+ initializePreUpgradeProperties() {
479
+ for (const prop of Object.keys(this.context.options.props ?? {})) {
480
+ if (Object.prototype.hasOwnProperty.call(this.context.host, prop)) {
481
+ const value = this.context.host[prop];
482
+ delete this.context.host[prop];
483
+ this.setProperty(prop, value);
484
+ }
485
+ }
486
+ }
487
+ getProperty(name) {
488
+ return this.values.get(name);
489
+ }
490
+ getProps() {
491
+ return Object.fromEntries(this.values);
492
+ }
493
+ setProperty(name, rawValue) {
494
+ const definition = this.context.options.props?.[name];
495
+ const nextValue = this.normalizeValue(name, rawValue, definition);
496
+ if (Object.is(this.values.get(name), nextValue)) {
497
+ return;
498
+ }
499
+ this.values.set(name, nextValue);
500
+ this.reflectProperty(name, nextValue, definition);
501
+ this.context.formValueChanged(name, nextValue);
502
+ this.context.requestRender();
503
+ }
504
+ attributeChanged(attributeName, value) {
505
+ if (this.reflectingAttributes.has(attributeName)) {
506
+ return;
507
+ }
508
+ const prop = this.attributeToProp.get(attributeName);
509
+ if (!prop) {
510
+ return;
511
+ }
512
+ const definition = this.context.options.props?.[prop];
513
+ const parsed = parseAttributeValue(value, definition ?? {}, this.context.host);
514
+ this.setProperty(prop, parsed);
515
+ }
516
+ normalizeValue(name, rawValue, definition) {
517
+ let value = definition?.transform ? definition.transform(rawValue, this.context.host) : rawValue;
518
+ if (definition?.validate) {
519
+ const result = definition.validate(value, this.context.host);
520
+ if (result !== true) {
521
+ warnDevelopment(
522
+ this.context.development,
523
+ typeof result === "string" ? result : `Invalid value for property "${name}".`
524
+ );
525
+ }
526
+ }
527
+ if (definition?.required && (value === void 0 || value === null || value === "")) {
528
+ warnDevelopment(this.context.development, `Required property "${name}" is missing.`);
529
+ }
530
+ return value;
531
+ }
532
+ reflectProperty(name, value, definition) {
533
+ if (!definition?.reflect) {
534
+ return;
535
+ }
536
+ const attribute = this.attributeName(name, definition);
537
+ if (!attribute) {
538
+ return;
539
+ }
540
+ const serialized = serializeAttributeValue(value, definition, this.context.host);
541
+ this.reflectingAttributes.add(attribute);
542
+ try {
543
+ if (serialized === null) {
544
+ this.context.host.removeAttribute(attribute);
545
+ } else {
546
+ this.context.host.setAttribute(attribute, serialized);
547
+ }
548
+ } finally {
549
+ this.reflectingAttributes.delete(attribute);
550
+ }
551
+ }
552
+ attributeName(name, definition) {
553
+ if (definition?.attribute === false) {
554
+ return null;
555
+ }
556
+ return definition?.attribute ?? camelToKebab(name);
557
+ }
558
+ };
559
+
560
+ // src/core/render-controller.tsx
561
+ var import_react = require("react");
562
+ var import_client = require("react-dom/client");
563
+ var RenderController = class {
564
+ constructor(context) {
565
+ this.context = context;
566
+ }
567
+ context;
568
+ root;
569
+ unmounted = false;
570
+ render() {
571
+ if (this.unmounted) {
572
+ return;
573
+ }
574
+ this.root ??= (0, import_client.createRoot)(this.context.mount);
575
+ try {
576
+ this.root.render(this.createTree());
577
+ } catch (error) {
578
+ this.context.config.renderError?.(error, this.context.host);
579
+ throw error;
580
+ }
581
+ }
582
+ unmount() {
583
+ this.unmounted = true;
584
+ this.root?.unmount();
585
+ this.root = void 0;
586
+ }
587
+ reconnect() {
588
+ this.unmounted = false;
589
+ }
590
+ createTree() {
591
+ const Component2 = this.context.component;
592
+ const props = {
593
+ ...this.context.getProps(),
594
+ ...this.context.getSlotProps(),
595
+ ...this.context.getPortalProps(),
596
+ ...this.context.getEventProps(),
597
+ ref: this.context.refSetter
598
+ };
599
+ let element = (0, import_react.createElement)(Component2, props);
600
+ const Boundary = this.context.options.errorBoundary;
601
+ if (Boundary) {
602
+ element = (0, import_react.createElement)(Boundary, { error: void 0 }, element);
603
+ }
604
+ if (this.context.options.fallback !== void 0) {
605
+ element = (0, import_react.createElement)(import_react.Suspense, { fallback: this.context.options.fallback }, element);
606
+ }
607
+ if (this.context.options.wrap) {
608
+ element = this.context.options.wrap(element, this.context.host);
609
+ }
610
+ if (this.context.config.wrap) {
611
+ element = this.context.config.wrap(element, this.context.host);
612
+ }
613
+ if (this.context.options.strictMode ?? this.context.config.strictMode) {
614
+ element = (0, import_react.createElement)(import_react.StrictMode, void 0, element);
615
+ }
616
+ return element;
617
+ }
618
+ };
619
+
620
+ // src/core/slot-controller.tsx
621
+ var import_react2 = __toESM(require("react"), 1);
622
+ var SlotController = class {
623
+ constructor(options) {
624
+ this.options = options;
625
+ }
626
+ options;
627
+ createSlotProps() {
628
+ const props = {};
629
+ for (const [propName, definition] of Object.entries(this.options.slots ?? {})) {
630
+ props[propName] = this.createSlot(definition);
631
+ }
632
+ return props;
633
+ }
634
+ createSlot(definition) {
635
+ if (definition === true || definition === void 0) {
636
+ return (0, import_react2.createElement)("slot");
637
+ }
638
+ if (typeof definition === "string") {
639
+ return (0, import_react2.createElement)("slot", { name: definition });
640
+ }
641
+ const children = definition.fallback ? import_react2.default.Children.toArray(definition.fallback) : void 0;
642
+ return (0, import_react2.createElement)("slot", definition.name ? { name: definition.name } : void 0, children);
643
+ }
644
+ };
645
+
646
+ // src/core/style-controller.ts
647
+ var StyleController = class {
648
+ constructor(root, host, styles) {
649
+ this.root = root;
650
+ this.host = host;
651
+ this.styles = styles;
652
+ }
653
+ root;
654
+ host;
655
+ styles;
656
+ apply() {
657
+ if (!this.styles) {
658
+ return;
659
+ }
660
+ const inputs = this.resolveInputs(this.styles);
661
+ const sheets = inputs.filter((style) => typeof style !== "string");
662
+ const text = inputs.filter((style) => typeof style === "string");
663
+ if ("adoptedStyleSheets" in this.root && sheets.length > 0) {
664
+ const current = this.root.adoptedStyleSheets ?? [];
665
+ this.root.adoptedStyleSheets = [...current, ...sheets];
666
+ }
667
+ for (const styleText of text) {
668
+ const style = document.createElement("style");
669
+ style.textContent = styleText;
670
+ this.root.prepend(style);
671
+ }
672
+ }
673
+ resolveInputs(input) {
674
+ const resolved = typeof input === "function" ? input(this.host) : input;
675
+ if (Array.isArray(resolved)) {
676
+ return resolved.flatMap((item) => Array.isArray(item) ? [...item] : [item]);
677
+ }
678
+ return [resolved];
679
+ }
680
+ };
681
+
682
+ // src/core/create-react-element.ts
683
+ function createReactElement(tagName, component, options = {}) {
684
+ const normalizedTagName = normalizeCustomElementName(tagName);
685
+ const runtimeOptions = options;
686
+ if (typeof HTMLElement === "undefined") {
687
+ return {
688
+ tagName: normalizedTagName,
689
+ component,
690
+ options,
691
+ elementClass: class {
692
+ },
693
+ metadata: createMetadata(normalizedTagName, runtimeOptions)
694
+ };
695
+ }
696
+ const observedAttributes = PropertyController.observedAttributes(runtimeOptions);
697
+ const formAssociated = Boolean(options.form);
698
+ class ReactBridgeElement extends HTMLElement {
699
+ static observedAttributes = observedAttributes;
700
+ static formAssociated = formAssociated;
701
+ config = getReactElementGlobalConfig();
702
+ mount;
703
+ renderRoot;
704
+ propertyController;
705
+ eventController = new EventController(this, runtimeOptions);
706
+ slotController = new SlotController(runtimeOptions);
707
+ methodController = new MethodController(runtimeOptions);
708
+ formController = new FormController(this, runtimeOptions);
709
+ portalController;
710
+ renderController;
711
+ lifecycleController;
712
+ renderScheduled = false;
713
+ constructor() {
714
+ super();
715
+ this.renderRoot = options.shadow === false ? this : this.attachShadow({
716
+ mode: options.shadow?.mode ?? "open",
717
+ ...options.shadow?.delegatesFocus === void 0 ? {} : { delegatesFocus: options.shadow.delegatesFocus }
718
+ });
719
+ this.mount = document.createElement("span");
720
+ this.mount.setAttribute("part", "react-mount");
721
+ this.mount.setAttribute("data-rwcb-mount", "");
722
+ this.renderRoot.append(this.mount);
723
+ new StyleController(this.renderRoot, this, runtimeOptions.styles).apply();
724
+ this.portalController = new PortalController(this, this.renderRoot, runtimeOptions);
725
+ this.propertyController = new PropertyController({
726
+ host: this,
727
+ options: runtimeOptions,
728
+ development: this.config.development ?? false,
729
+ requestRender: () => this.requestRender(),
730
+ formValueChanged: (name, value) => {
731
+ this.formController.update(name, value);
732
+ if (name === runtimeOptions.form?.valueProp) {
733
+ this.formController.emitStandardChangeEvents();
734
+ }
735
+ }
736
+ });
737
+ this.renderController = new RenderController({
738
+ host: this,
739
+ component,
740
+ options: runtimeOptions,
741
+ config: this.config,
742
+ mount: this.mount,
743
+ getProps: () => this.propertyController.getProps(),
744
+ getEventProps: () => this.eventController.createEventProps(),
745
+ getSlotProps: () => this.slotController.createSlotProps(),
746
+ getPortalProps: () => this.portalController.getPortalProps(),
747
+ refSetter: this.methodController.createRefSetter()
748
+ });
749
+ this.lifecycleController = new LifecycleController(
750
+ this.renderController,
751
+ () => this.requestRender(),
752
+ () => this.portalController.cleanup()
753
+ );
754
+ this.propertyController.initializePreUpgradeProperties();
755
+ }
756
+ connectedCallback() {
757
+ this.formController.connect();
758
+ this.lifecycleController.connected();
759
+ }
760
+ disconnectedCallback() {
761
+ this.lifecycleController.disconnected(this);
762
+ }
763
+ attributeChangedCallback(name, _oldValue, newValue) {
764
+ this.propertyController.attributeChanged(name, newValue);
765
+ }
766
+ formResetCallback() {
767
+ const valueProp = runtimeOptions.form?.valueProp;
768
+ const defaultValue = valueProp ? runtimeOptions.props?.[valueProp]?.default : void 0;
769
+ if (valueProp) {
770
+ this.propertyController.setProperty(valueProp, defaultValue);
771
+ }
772
+ }
773
+ callReactMethod(name, ...args) {
774
+ return this.methodController.call(name, args, this);
775
+ }
776
+ requestRender() {
777
+ if (this.renderScheduled || !this.isConnected) {
778
+ return;
779
+ }
780
+ this.renderScheduled = true;
781
+ scheduleMicrotask(() => {
782
+ this.renderScheduled = false;
783
+ if (this.isConnected) {
784
+ this.renderController.render();
785
+ }
786
+ });
787
+ }
788
+ }
789
+ for (const prop of Object.keys(options.props ?? {})) {
790
+ Object.defineProperty(ReactBridgeElement.prototype, prop, {
791
+ get() {
792
+ return this.propertyController.getProperty(prop);
793
+ },
794
+ set(value) {
795
+ this.propertyController.setProperty(prop, value);
796
+ }
797
+ });
798
+ }
799
+ for (const methodName of Object.keys(options.methods ?? {})) {
800
+ if (methodName in ReactBridgeElement.prototype) {
801
+ throw new Error(`Cannot expose public method "${methodName}" because it already exists.`);
802
+ }
803
+ Object.defineProperty(ReactBridgeElement.prototype, methodName, {
804
+ value(...args) {
805
+ return this.callReactMethod(methodName, ...args);
806
+ }
807
+ });
808
+ }
809
+ return {
810
+ tagName: normalizedTagName,
811
+ component,
812
+ options,
813
+ elementClass: ReactBridgeElement,
814
+ metadata: createMetadata(normalizedTagName, runtimeOptions)
815
+ };
816
+ }
817
+
818
+ // src/core/define-react-element.ts
819
+ function defineReactElement(tagName, component, options = {}) {
820
+ assertBrowserApi(
821
+ "customElements",
822
+ typeof customElements === "undefined" ? void 0 : customElements
823
+ );
824
+ const definition = createReactElement(tagName, component, options);
825
+ if (!customElements.get(definition.tagName)) {
826
+ customElements.define(definition.tagName, definition.elementClass);
827
+ }
828
+ registerDefinition(definition);
829
+ return definition;
830
+ }
831
+ function defineReactElements(definitions2) {
832
+ assertBrowserApi(
833
+ "customElements",
834
+ typeof customElements === "undefined" ? void 0 : customElements
835
+ );
836
+ for (const definition of definitions2) {
837
+ if (!customElements.get(definition.tagName)) {
838
+ customElements.define(definition.tagName, definition.elementClass);
839
+ }
840
+ registerDefinition(definition);
841
+ }
842
+ return [...definitions2];
843
+ }
844
+
845
+ // src/react.ts
846
+ var react_default = import_react3.default;
847
+ function createComponentTag(tagName, component, options = {}) {
848
+ const definition = createReactElement(tagName, component, options);
849
+ return Object.assign(definition, {
850
+ define: () => defineReactElement(tagName, component, options)
851
+ });
852
+ }
853
+ function defineComponentTag(tagName, component, options = {}) {
854
+ const definition = defineReactElement(tagName, component, options);
855
+ return Object.assign(definition, {
856
+ define: () => definition
857
+ });
858
+ }
859
+ var createWebComponent = createComponentTag;
860
+ var defineWebComponent = defineComponentTag;
861
+ function configureReactApi(config) {
862
+ return configureReactElements(config);
863
+ }
864
+ // Annotate the CommonJS export names for ESM import in node:
865
+ 0 && (module.exports = {
866
+ Children,
867
+ Component,
868
+ Fragment,
869
+ Profiler,
870
+ PureComponent,
871
+ StrictMode,
872
+ Suspense,
873
+ cloneElement,
874
+ configureReactApi,
875
+ configureReactElements,
876
+ createComponentTag,
877
+ createContext,
878
+ createElement,
879
+ createReactElement,
880
+ createRef,
881
+ createWebComponent,
882
+ defineComponentTag,
883
+ defineReactElement,
884
+ defineReactElements,
885
+ defineWebComponent,
886
+ forwardRef,
887
+ getReactElementDefinition,
888
+ getReactElementGlobalConfig,
889
+ isReactElementDefined,
890
+ isValidElement,
891
+ lazy,
892
+ listReactElementDefinitions,
893
+ memo,
894
+ startTransition,
895
+ useCallback,
896
+ useContext,
897
+ useDebugValue,
898
+ useDeferredValue,
899
+ useEffect,
900
+ useId,
901
+ useImperativeHandle,
902
+ useInsertionEffect,
903
+ useLayoutEffect,
904
+ useMemo,
905
+ useReducer,
906
+ useRef,
907
+ useState,
908
+ useSyncExternalStore,
909
+ useTransition,
910
+ version
911
+ });
912
+ //# sourceMappingURL=react.cjs.map