@webspatial/react-sdk 1.1.0 → 1.2.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.
@@ -2,7 +2,7 @@
2
2
  (function(){
3
3
  if(typeof window === 'undefined') return;
4
4
  if(!window.__webspatialsdk__) window.__webspatialsdk__ = {}
5
- window.__webspatialsdk__['react-sdk-version'] = "1.1.0"
5
+ window.__webspatialsdk__['react-sdk-version'] = "1.2.0"
6
6
  window.__webspatialsdk__['XR_ENV'] = "avp"
7
7
  })()
8
8
 
@@ -300,7 +300,7 @@ var SpatialContainerRefProxy = class {
300
300
  }
301
301
  const extraProps = cacheExtraRefProps;
302
302
  if (extraProps.hasOwnProperty(prop)) {
303
- return extraProps[prop]();
303
+ return extraProps[prop];
304
304
  }
305
305
  }
306
306
  const value = Reflect.get(target, prop);
@@ -335,6 +335,12 @@ var SpatialContainerRefProxy = class {
335
335
  self.transformVisibilityTaskContainerDom.className = value;
336
336
  }
337
337
  }
338
+ if (typeof prop === "string" && self.extraRefProps) {
339
+ if (!cacheExtraRefProps) {
340
+ cacheExtraRefProps = self.extraRefProps(domProxy);
341
+ }
342
+ cacheExtraRefProps[prop] = value;
343
+ }
338
344
  return Reflect.set(target, prop, value);
339
345
  }
340
346
  }
@@ -680,7 +686,7 @@ var StandardSpatializedContainer = forwardRef(
680
686
  function injectSpatialDefaultStyle() {
681
687
  const styleElement = document.createElement("style");
682
688
  styleElement.type = "text/css";
683
- styleElement.innerHTML = " .xr-spatial-default { --xr-back: 0; --xr-depth: 0; --xr-z-index: 0; --xr-background-material: none; } ";
689
+ styleElement.innerHTML = " :where(.xr-spatial-default) { --xr-back: 0; --xr-depth: 0; --xr-z-index: 0; --xr-background-material: none; } ";
684
690
  document.head.appendChild(styleElement);
685
691
  }
686
692
 
@@ -1221,10 +1227,8 @@ function PortalSpatializedContainer(props) {
1221
1227
  onSpatialDragStart,
1222
1228
  onSpatialDrag,
1223
1229
  onSpatialDragEnd,
1224
- onSpatialRotateStart,
1225
1230
  onSpatialRotate,
1226
1231
  onSpatialRotateEnd,
1227
- onSpatialMagnifyStart,
1228
1232
  onSpatialMagnify,
1229
1233
  onSpatialMagnifyEnd,
1230
1234
  [SpatialID]: spatialId,
@@ -1298,16 +1302,6 @@ function PortalSpatializedContainer(props) {
1298
1302
  spatializedElement.onSpatialDragStart = onSpatialDragStart;
1299
1303
  }
1300
1304
  }, [spatializedElement, onSpatialDragStart]);
1301
- useEffect7(() => {
1302
- if (spatializedElement) {
1303
- spatializedElement.onSpatialRotateStart = onSpatialRotateStart;
1304
- }
1305
- }, [spatializedElement, onSpatialRotateStart]);
1306
- useEffect7(() => {
1307
- if (spatializedElement) {
1308
- spatializedElement.onSpatialMagnifyStart = onSpatialMagnifyStart;
1309
- }
1310
- }, [spatializedElement, onSpatialMagnifyStart]);
1311
1305
  return /* @__PURE__ */ jsxs(PortalInstanceContext.Provider, { value: portalInstanceObject, children: [
1312
1306
  spatializedElement && portalInstanceObject.dom && /* @__PURE__ */ jsx3(Content, { spatializedElement, ...restProps }),
1313
1307
  PlaceholderEl
@@ -1315,7 +1309,7 @@ function PortalSpatializedContainer(props) {
1315
1309
  }
1316
1310
 
1317
1311
  // src/spatialized-container/hooks/useSpatialEvents.ts
1318
- function createEventProxy(event, currentTargetGetter) {
1312
+ function createEventProxy(event, currentTargetGetter, offsetXGetter, offsetYGetter, offsetZGetter) {
1319
1313
  return new Proxy(event, {
1320
1314
  get(target, prop) {
1321
1315
  if (prop === "currentTarget") {
@@ -1324,20 +1318,41 @@ function createEventProxy(event, currentTargetGetter) {
1324
1318
  if (prop === "isTrusted") {
1325
1319
  return true;
1326
1320
  }
1321
+ if (prop === "bubbles") {
1322
+ return false;
1323
+ }
1324
+ if (prop === "offsetX" && offsetXGetter) {
1325
+ return offsetXGetter(target) ?? 0;
1326
+ }
1327
+ if (prop === "offsetY" && offsetYGetter) {
1328
+ return offsetYGetter(target) ?? 0;
1329
+ }
1330
+ if (prop === "offsetZ" && offsetZGetter) {
1331
+ return offsetZGetter(target) ?? 0;
1332
+ }
1327
1333
  return Reflect.get(target, prop);
1328
1334
  }
1329
1335
  });
1330
1336
  }
1331
- function createEventHandler(handler, currentTargetGetter) {
1337
+ function createEventHandler(handler, currentTargetGetter, offsetXGetter, offsetYGetter, offsetZGetter) {
1332
1338
  return handler ? (event) => {
1333
- const proxyEvent = createEventProxy(event, currentTargetGetter);
1339
+ const proxyEvent = createEventProxy(
1340
+ event,
1341
+ currentTargetGetter,
1342
+ offsetXGetter,
1343
+ offsetYGetter,
1344
+ offsetZGetter
1345
+ );
1334
1346
  handler(proxyEvent);
1335
1347
  } : void 0;
1336
1348
  }
1337
1349
  function useSpatialEventsBase(spatialEvents, currentTargetGetter) {
1338
1350
  const onSpatialTap = createEventHandler(
1339
1351
  spatialEvents.onSpatialTap,
1340
- currentTargetGetter
1352
+ currentTargetGetter,
1353
+ (ev) => ev.detail?.location3D?.x,
1354
+ (ev) => ev.detail?.location3D?.y,
1355
+ (ev) => ev.detail?.location3D?.z
1341
1356
  );
1342
1357
  const onSpatialDrag = createEventHandler(
1343
1358
  spatialEvents.onSpatialDrag,
@@ -1365,19 +1380,18 @@ function useSpatialEventsBase(spatialEvents, currentTargetGetter) {
1365
1380
  );
1366
1381
  const onSpatialDragStart = createEventHandler(
1367
1382
  spatialEvents.onSpatialDragStart,
1368
- currentTargetGetter
1383
+ currentTargetGetter,
1384
+ (ev) => ev.detail?.startLocation3D?.x,
1385
+ (ev) => ev.detail?.startLocation3D?.y,
1386
+ (ev) => ev.detail?.startLocation3D?.z
1369
1387
  );
1370
- const onSpatialRotateStart = createEventHandler(spatialEvents.onSpatialRotateStart, currentTargetGetter);
1371
- const onSpatialMagnifyStart = createEventHandler(spatialEvents.onSpatialMagnifyStart, currentTargetGetter);
1372
1388
  return {
1373
1389
  onSpatialTap,
1374
1390
  onSpatialDragStart,
1375
1391
  onSpatialDrag,
1376
1392
  onSpatialDragEnd,
1377
- onSpatialRotateStart,
1378
1393
  onSpatialRotate,
1379
1394
  onSpatialRotateEnd,
1380
- onSpatialMagnifyStart,
1381
1395
  onSpatialMagnify,
1382
1396
  onSpatialMagnifyEnd
1383
1397
  };
@@ -1468,10 +1482,8 @@ function SpatializedContainerBase(inprops, ref) {
1468
1482
  onSpatialDragStart: onSpatialDragStart2,
1469
1483
  onSpatialDrag: onSpatialDrag2,
1470
1484
  onSpatialDragEnd: onSpatialDragEnd2,
1471
- onSpatialRotateStart: onSpatialRotateStart2,
1472
1485
  onSpatialRotate: onSpatialRotate2,
1473
1486
  onSpatialRotateEnd: onSpatialRotateEnd2,
1474
- onSpatialMagnifyStart: onSpatialMagnifyStart2,
1475
1487
  onSpatialMagnify: onSpatialMagnify2,
1476
1488
  onSpatialMagnifyEnd: onSpatialMagnifyEnd2,
1477
1489
  extraRefProps: extraRefProps2,
@@ -1498,10 +1510,8 @@ function SpatializedContainerBase(inprops, ref) {
1498
1510
  onSpatialDragStart,
1499
1511
  onSpatialDrag,
1500
1512
  onSpatialDragEnd,
1501
- onSpatialRotateStart,
1502
1513
  onSpatialRotate,
1503
1514
  onSpatialRotateEnd,
1504
- onSpatialMagnifyStart,
1505
1515
  onSpatialMagnify,
1506
1516
  onSpatialMagnifyEnd,
1507
1517
  extraRefProps,
@@ -1515,10 +1525,8 @@ function SpatializedContainerBase(inprops, ref) {
1515
1525
  onSpatialDragStart,
1516
1526
  onSpatialDrag,
1517
1527
  onSpatialDragEnd,
1518
- onSpatialRotateStart,
1519
1528
  onSpatialRotate,
1520
1529
  onSpatialRotateEnd,
1521
- onSpatialMagnifyStart,
1522
1530
  onSpatialMagnify,
1523
1531
  onSpatialMagnifyEnd
1524
1532
  },
@@ -1584,10 +1592,8 @@ function SpatializedContainerBase(inprops, ref) {
1584
1592
  onSpatialDragStart,
1585
1593
  onSpatialDrag,
1586
1594
  onSpatialDragEnd,
1587
- onSpatialRotateStart,
1588
1595
  onSpatialRotate,
1589
1596
  onSpatialRotateEnd,
1590
- onSpatialMagnifyStart,
1591
1597
  onSpatialMagnify,
1592
1598
  onSpatialMagnifyEnd
1593
1599
  },
@@ -1816,10 +1822,11 @@ function getAbsoluteURL(url) {
1816
1822
  if (!url) {
1817
1823
  return "";
1818
1824
  }
1819
- if (url.startsWith("http") || url.startsWith("//")) {
1825
+ try {
1826
+ return new URL(url, document.baseURI).toString();
1827
+ } catch {
1820
1828
  return url;
1821
1829
  }
1822
- return window.location.origin + url;
1823
1830
  }
1824
1831
  function createLoadEvent(type, targetGetter) {
1825
1832
  const event = new CustomEvent(type, {
@@ -1888,37 +1895,12 @@ async function createSpatializedElement2() {
1888
1895
  function SpatializedStatic3DElementContainerBase(props, ref) {
1889
1896
  const extraRefProps = useCallback6(
1890
1897
  (domProxy) => {
1891
- const modelTransform = new DOMMatrix();
1892
- let needupdate = false;
1893
- const triggerUpdate = () => {
1894
- const spatializedElement = domProxy.__spatializedElement;
1895
- spatializedElement.updateModelTransform(modelTransform);
1896
- needupdate = false;
1897
- };
1898
- const domMatrixProxy = new Proxy(modelTransform, {
1899
- get(target, prop, receiver) {
1900
- const value = Reflect.get(target, prop, receiver);
1901
- if (typeof value === "function") {
1902
- return function(...args) {
1903
- requestAnimationFrame(triggerUpdate);
1904
- return value.apply(target, args);
1905
- };
1906
- } else {
1907
- return value;
1908
- }
1909
- },
1910
- set(target, prop, value) {
1911
- const success = Reflect.set(target, prop, value);
1912
- if (!needupdate) {
1913
- needupdate = true;
1914
- requestAnimationFrame(triggerUpdate);
1915
- }
1916
- return success;
1917
- }
1918
- });
1898
+ let modelTransform = new DOMMatrixReadOnly();
1919
1899
  return {
1920
- currentSrc: () => getAbsoluteURL(props.src),
1921
- ready: () => {
1900
+ get currentSrc() {
1901
+ return getAbsoluteURL(props.src);
1902
+ },
1903
+ get ready() {
1922
1904
  const spatializedElement = domProxy.__spatializedElement;
1923
1905
  const promise = spatializedElement.ready.then((success) => {
1924
1906
  if (success) {
@@ -1928,7 +1910,14 @@ function SpatializedStatic3DElementContainerBase(props, ref) {
1928
1910
  });
1929
1911
  return promise;
1930
1912
  },
1931
- entityTransform: () => domMatrixProxy
1913
+ get entityTransform() {
1914
+ return modelTransform;
1915
+ },
1916
+ set entityTransform(value) {
1917
+ modelTransform = value;
1918
+ const spatializedElement = domProxy.__spatializedElement;
1919
+ spatializedElement.updateModelTransform(modelTransform);
1920
+ }
1932
1921
  };
1933
1922
  },
1934
1923
  []
@@ -2093,6 +2082,9 @@ function withSpatialMonitor(El) {
2093
2082
  }
2094
2083
 
2095
2084
  // src/reality/components/Entity.tsx
2085
+ import { forwardRef as forwardRef11 } from "react";
2086
+
2087
+ // src/reality/components/BaseEntity.tsx
2096
2088
  import { forwardRef as forwardRef10 } from "react";
2097
2089
 
2098
2090
  // src/reality/context/RealityContext.tsx
@@ -2122,7 +2114,8 @@ var ResourceRegistry = class {
2122
2114
  removeAndDestroy(id) {
2123
2115
  const p = this.resources.get(id);
2124
2116
  if (p) {
2125
- p.then((spatialObj) => spatialObj.destroy());
2117
+ p.then((spatialObj) => spatialObj.destroy()).catch(() => {
2118
+ });
2126
2119
  }
2127
2120
  this.resources.delete(id);
2128
2121
  }
@@ -2132,7 +2125,10 @@ var ResourceRegistry = class {
2132
2125
  destroy() {
2133
2126
  const pending = Array.from(this.resources.values());
2134
2127
  this.resources.clear();
2135
- pending.forEach((promise) => promise.then((spatialObj) => spatialObj.destroy()));
2128
+ pending.forEach(
2129
+ (promise) => promise.then((spatialObj) => spatialObj.destroy()).catch(() => {
2130
+ })
2131
+ );
2136
2132
  }
2137
2133
  };
2138
2134
 
@@ -2203,7 +2199,7 @@ function useEntityTransform(entity, { position, rotation, scale }) {
2203
2199
  }
2204
2200
 
2205
2201
  // src/reality/hooks/useEntityEvent.tsx
2206
- import { useEffect as useEffect17 } from "react";
2202
+ import { useEffect as useEffect17, useRef as useRef7 } from "react";
2207
2203
 
2208
2204
  // src/reality/type.ts
2209
2205
  var eventMap = {
@@ -2307,27 +2303,69 @@ function createEventProxy2(ev, instance) {
2307
2303
  }
2308
2304
  return instance;
2309
2305
  }
2306
+ if (prop === "bubbles") {
2307
+ return true;
2308
+ }
2309
+ if (prop === "offsetX") {
2310
+ const type = target.type;
2311
+ if (type === "spatialtap") {
2312
+ return target.detail?.location3D?.x ?? 0;
2313
+ }
2314
+ if (type === "spatialdragstart") {
2315
+ return target.detail?.startLocation3D?.x ?? 0;
2316
+ }
2317
+ return void 0;
2318
+ }
2319
+ if (prop === "offsetY") {
2320
+ const type = target.type;
2321
+ if (type === "spatialtap") {
2322
+ return target.detail?.location3D?.y ?? 0;
2323
+ }
2324
+ if (type === "spatialdragstart") {
2325
+ return target.detail?.startLocation3D?.y ?? 0;
2326
+ }
2327
+ return void 0;
2328
+ }
2329
+ if (prop === "offsetZ") {
2330
+ const type = target.type;
2331
+ if (type === "spatialtap") {
2332
+ return target.detail?.location3D?.z ?? 0;
2333
+ }
2334
+ if (type === "spatialdragstart") {
2335
+ return target.detail?.startLocation3D?.z ?? 0;
2336
+ }
2337
+ return void 0;
2338
+ }
2310
2339
  const val = target[prop];
2311
2340
  return typeof val === "function" ? val.bind(target) : val;
2312
2341
  }
2313
2342
  });
2314
2343
  }
2315
2344
  var useEntityEvent = ({ instance, ...handlers }) => {
2345
+ const eventsSetRef = useRef7(/* @__PURE__ */ new Set());
2316
2346
  useEffect17(() => {
2317
2347
  const entity = instance.entity;
2318
2348
  if (!entity) return;
2319
- const boundHandlers = [];
2320
2349
  Object.entries(eventMap).forEach(([reactKey, spatialEvent]) => {
2321
2350
  const handlerFn = handlers[reactKey];
2322
2351
  if (!handlerFn) return;
2323
2352
  const wrapped = (ev) => handlerFn(createEventProxy2(ev, instance));
2324
2353
  entity.addEvent(spatialEvent, wrapped);
2325
- boundHandlers.push(() => entity.removeEvent(spatialEvent));
2354
+ eventsSetRef.current.add(reactKey);
2326
2355
  });
2327
2356
  return () => {
2328
- boundHandlers.forEach((unbind) => unbind());
2329
2357
  };
2330
2358
  }, [instance.entity, ...Object.values(handlers)]);
2359
+ useEffect17(() => {
2360
+ const entity = instance.entity;
2361
+ if (!entity) return;
2362
+ return () => {
2363
+ for (let x of eventsSetRef.current) {
2364
+ entity.removeEvent(x);
2365
+ }
2366
+ eventsSetRef.current.clear();
2367
+ };
2368
+ }, [instance.entity]);
2331
2369
  return null;
2332
2370
  };
2333
2371
 
@@ -2346,7 +2384,7 @@ var useEntityId = ({ id, entity }) => {
2346
2384
  };
2347
2385
 
2348
2386
  // src/reality/hooks/useEntity.tsx
2349
- import { useEffect as useEffect19, useRef as useRef7 } from "react";
2387
+ import { useEffect as useEffect19, useRef as useRef8 } from "react";
2350
2388
  var useEntity = ({
2351
2389
  ref,
2352
2390
  id,
@@ -2357,10 +2395,10 @@ var useEntity = ({
2357
2395
  onSpatialDragStart,
2358
2396
  onSpatialDrag,
2359
2397
  onSpatialDragEnd,
2360
- onSpatialRotateStart,
2398
+ // onSpatialRotateStart,
2361
2399
  onSpatialRotate,
2362
2400
  onSpatialRotateEnd,
2363
- onSpatialMagnifyStart,
2401
+ // onSpatialMagnifyStart,
2364
2402
  onSpatialMagnify,
2365
2403
  onSpatialMagnifyEnd,
2366
2404
  // TODO: add other event handlers
@@ -2368,7 +2406,7 @@ var useEntity = ({
2368
2406
  }) => {
2369
2407
  const ctx = useRealityContext();
2370
2408
  const parent = useParentContext();
2371
- const instanceRef = useRef7(new EntityRef(null, ctx));
2409
+ const instanceRef = useRef8(new EntityRef(null, ctx));
2372
2410
  const forceUpdate = useForceUpdate2();
2373
2411
  useEffect19(() => {
2374
2412
  if (!ctx) return;
@@ -2409,10 +2447,10 @@ var useEntity = ({
2409
2447
  onSpatialDragStart,
2410
2448
  onSpatialDrag,
2411
2449
  onSpatialDragEnd,
2412
- onSpatialRotateStart,
2450
+ // onSpatialRotateStart,
2413
2451
  onSpatialRotate,
2414
2452
  onSpatialRotateEnd,
2415
- onSpatialMagnifyStart,
2453
+ // onSpatialMagnifyStart,
2416
2454
  onSpatialMagnify,
2417
2455
  onSpatialMagnifyEnd
2418
2456
  });
@@ -2426,109 +2464,88 @@ var useForceUpdate2 = () => {
2426
2464
  return useCallback7(() => setTick((tick) => tick + 1), []);
2427
2465
  };
2428
2466
 
2429
- // src/reality/components/Entity.tsx
2467
+ // src/reality/components/BaseEntity.tsx
2430
2468
  import { jsx as jsx12 } from "react/jsx-runtime";
2431
- var Entity = forwardRef10(
2432
- ({ id, children, position, rotation, scale, onSpatialTap, name }, ref) => {
2469
+ var BaseEntity = forwardRef10(
2470
+ ({ children, createEntity, ...rest }, ref) => {
2433
2471
  const ctx = useRealityContext();
2434
2472
  const entity = useEntity({
2473
+ ...rest,
2435
2474
  ref,
2436
- id,
2437
- position,
2438
- rotation,
2439
- scale,
2440
- onSpatialTap,
2441
- createEntity: async () => ctx.session.createEntity({ id, name })
2475
+ createEntity: (signal) => createEntity(ctx, signal)
2442
2476
  });
2443
2477
  if (!entity) return null;
2444
2478
  return /* @__PURE__ */ jsx12(ParentContext.Provider, { value: entity, children });
2445
2479
  }
2446
2480
  );
2447
2481
 
2482
+ // src/reality/components/Entity.tsx
2483
+ import { jsx as jsx13 } from "react/jsx-runtime";
2484
+ var Entity = forwardRef11((props, ref) => {
2485
+ const { id, name, children, ...rest } = props;
2486
+ return /* @__PURE__ */ jsx13(
2487
+ BaseEntity,
2488
+ {
2489
+ ...rest,
2490
+ id,
2491
+ ref,
2492
+ createEntity: async (ctxVal) => ctxVal.session.createEntity({ id, name }),
2493
+ children
2494
+ }
2495
+ );
2496
+ });
2497
+
2448
2498
  // src/reality/components/BoxEntity.tsx
2449
- import { forwardRef as forwardRef12 } from "react";
2499
+ import { forwardRef as forwardRef13 } from "react";
2450
2500
 
2451
2501
  // src/reality/components/GeometryEntity.tsx
2452
- import { forwardRef as forwardRef11 } from "react";
2453
- import { jsx as jsx13 } from "react/jsx-runtime";
2454
- var GeometryEntity = forwardRef11(
2455
- ({
2456
- id,
2457
- position,
2458
- rotation,
2459
- scale,
2460
- onSpatialTap,
2461
- onSpatialDragStart,
2462
- onSpatialDrag,
2463
- onSpatialDragEnd,
2464
- onSpatialRotateStart,
2465
- onSpatialRotate,
2466
- onSpatialRotateEnd,
2467
- onSpatialMagnifyStart,
2468
- onSpatialMagnify,
2469
- onSpatialMagnifyEnd,
2470
- // TODO: add other event handlers
2471
- children,
2472
- name,
2473
- materials,
2474
- geometryOptions,
2475
- createGeometry
2476
- }, ref) => {
2477
- const ctx = useRealityContext();
2478
- const entity = useEntity({
2479
- ref,
2480
- id,
2481
- position,
2482
- rotation,
2483
- scale,
2484
- onSpatialTap,
2485
- onSpatialDragStart,
2486
- onSpatialDrag,
2487
- onSpatialDragEnd,
2488
- onSpatialRotateStart,
2489
- onSpatialRotate,
2490
- onSpatialRotateEnd,
2491
- onSpatialMagnifyStart,
2492
- onSpatialMagnify,
2493
- onSpatialMagnifyEnd,
2494
- createEntity: async (signal) => {
2495
- const manager = new AbortResourceManager(signal);
2496
- try {
2497
- const ent = await manager.addResource(
2498
- () => ctx.session.createEntity({ id, name })
2499
- );
2500
- const geometry = await manager.addResource(
2501
- () => createGeometry(geometryOptions)
2502
- );
2503
- const materialList = await Promise.all(
2504
- materials?.map((id2) => ctx.resourceRegistry.get(id2)).filter(Boolean) ?? []
2505
- );
2506
- const modelComponent = await manager.addResource(
2507
- () => ctx.session.createModelComponent({
2508
- mesh: geometry,
2509
- materials: materialList
2510
- })
2511
- );
2512
- await ent.addComponent(modelComponent);
2513
- return ent;
2514
- } catch (error) {
2515
- console.error(error);
2516
- await manager.dispose();
2517
- return null;
2518
- }
2502
+ import { forwardRef as forwardRef12 } from "react";
2503
+ import { jsx as jsx14 } from "react/jsx-runtime";
2504
+ var GeometryEntity = forwardRef12(
2505
+ ({ id, children, name, materials, geometryOptions, createGeometry, ...rest }, ref) => {
2506
+ return /* @__PURE__ */ jsx14(
2507
+ BaseEntity,
2508
+ {
2509
+ ...rest,
2510
+ id,
2511
+ ref,
2512
+ createEntity: async (ctx, signal) => {
2513
+ const manager = new AbortResourceManager(signal);
2514
+ try {
2515
+ const ent = await manager.addResource(
2516
+ () => ctx.session.createEntity({ id, name })
2517
+ );
2518
+ const geometry = await manager.addResource(
2519
+ () => createGeometry(geometryOptions)
2520
+ );
2521
+ const materialList = await Promise.all(
2522
+ materials?.map((id2) => ctx.resourceRegistry.get(id2)).filter(Boolean) ?? []
2523
+ );
2524
+ const modelComponent = await manager.addResource(
2525
+ () => ctx.session.createModelComponent({
2526
+ mesh: geometry,
2527
+ materials: materialList
2528
+ })
2529
+ );
2530
+ await ent.addComponent(modelComponent);
2531
+ return ent;
2532
+ } catch (error) {
2533
+ await manager.dispose();
2534
+ return null;
2535
+ }
2536
+ },
2537
+ children
2519
2538
  }
2520
- });
2521
- if (!entity) return null;
2522
- return /* @__PURE__ */ jsx13(ParentContext.Provider, { value: entity, children });
2539
+ );
2523
2540
  }
2524
2541
  );
2525
2542
 
2526
2543
  // src/reality/components/BoxEntity.tsx
2527
- import { jsx as jsx14 } from "react/jsx-runtime";
2528
- var BoxEntity = forwardRef12(
2544
+ import { jsx as jsx15 } from "react/jsx-runtime";
2545
+ var BoxEntity = forwardRef13(
2529
2546
  ({ children, ...props }, ref) => {
2530
2547
  const ctx = useRealityContext();
2531
- return /* @__PURE__ */ jsx14(
2548
+ return /* @__PURE__ */ jsx15(
2532
2549
  GeometryEntity,
2533
2550
  {
2534
2551
  ...props,
@@ -2548,10 +2565,10 @@ var BoxEntity = forwardRef12(
2548
2565
  );
2549
2566
 
2550
2567
  // src/reality/components/UnlitMaterial.tsx
2551
- import { useEffect as useEffect20, useRef as useRef8 } from "react";
2568
+ import { useEffect as useEffect20, useRef as useRef9 } from "react";
2552
2569
  var UnlitMaterial = ({ children, ...options }) => {
2553
2570
  const ctx = useRealityContext();
2554
- const materialRef = useRef8();
2571
+ const materialRef = useRef9();
2555
2572
  useEffect20(() => {
2556
2573
  if (!ctx) return;
2557
2574
  const { session, reality, resourceRegistry } = ctx;
@@ -2574,12 +2591,12 @@ var UnlitMaterial = ({ children, ...options }) => {
2574
2591
  };
2575
2592
 
2576
2593
  // src/reality/components/SphereEntity.tsx
2577
- import { forwardRef as forwardRef13 } from "react";
2578
- import { jsx as jsx15 } from "react/jsx-runtime";
2579
- var SphereEntity = forwardRef13(
2594
+ import { forwardRef as forwardRef14 } from "react";
2595
+ import { jsx as jsx16 } from "react/jsx-runtime";
2596
+ var SphereEntity = forwardRef14(
2580
2597
  ({ children, ...props }, ref) => {
2581
2598
  const ctx = useRealityContext();
2582
- return /* @__PURE__ */ jsx15(
2599
+ return /* @__PURE__ */ jsx16(
2583
2600
  GeometryEntity,
2584
2601
  {
2585
2602
  ...props,
@@ -2595,12 +2612,12 @@ var SphereEntity = forwardRef13(
2595
2612
  );
2596
2613
 
2597
2614
  // src/reality/components/ConeEntity.tsx
2598
- import { forwardRef as forwardRef14 } from "react";
2599
- import { jsx as jsx16 } from "react/jsx-runtime";
2600
- var ConeEntity = forwardRef14(
2615
+ import { forwardRef as forwardRef15 } from "react";
2616
+ import { jsx as jsx17 } from "react/jsx-runtime";
2617
+ var ConeEntity = forwardRef15(
2601
2618
  ({ children, ...props }, ref) => {
2602
2619
  const ctx = useRealityContext();
2603
- return /* @__PURE__ */ jsx16(
2620
+ return /* @__PURE__ */ jsx17(
2604
2621
  GeometryEntity,
2605
2622
  {
2606
2623
  ...props,
@@ -2617,12 +2634,12 @@ var ConeEntity = forwardRef14(
2617
2634
  );
2618
2635
 
2619
2636
  // src/reality/components/CylinderEntity.tsx
2620
- import { forwardRef as forwardRef15 } from "react";
2621
- import { jsx as jsx17 } from "react/jsx-runtime";
2622
- var CylinderEntity = forwardRef15(
2637
+ import { forwardRef as forwardRef16 } from "react";
2638
+ import { jsx as jsx18 } from "react/jsx-runtime";
2639
+ var CylinderEntity = forwardRef16(
2623
2640
  ({ children, ...props }, ref) => {
2624
2641
  const ctx = useRealityContext();
2625
- return /* @__PURE__ */ jsx17(
2642
+ return /* @__PURE__ */ jsx18(
2626
2643
  GeometryEntity,
2627
2644
  {
2628
2645
  ...props,
@@ -2639,12 +2656,12 @@ var CylinderEntity = forwardRef15(
2639
2656
  );
2640
2657
 
2641
2658
  // src/reality/components/PlaneEntity.tsx
2642
- import { forwardRef as forwardRef16 } from "react";
2643
- import { jsx as jsx18 } from "react/jsx-runtime";
2644
- var PlaneEntity = forwardRef16(
2659
+ import { forwardRef as forwardRef17 } from "react";
2660
+ import { jsx as jsx19 } from "react/jsx-runtime";
2661
+ var PlaneEntity = forwardRef17(
2645
2662
  ({ children, ...props }, ref) => {
2646
2663
  const ctx = useRealityContext();
2647
- return /* @__PURE__ */ jsx18(
2664
+ return /* @__PURE__ */ jsx19(
2648
2665
  GeometryEntity,
2649
2666
  {
2650
2667
  ...props,
@@ -2662,13 +2679,13 @@ var PlaneEntity = forwardRef16(
2662
2679
  );
2663
2680
 
2664
2681
  // src/reality/components/SceneGraph.tsx
2665
- import { jsx as jsx19 } from "react/jsx-runtime";
2682
+ import { jsx as jsx20 } from "react/jsx-runtime";
2666
2683
  var SceneGraph = ({ children }) => {
2667
- return /* @__PURE__ */ jsx19(ParentContext.Provider, { value: null, children });
2684
+ return /* @__PURE__ */ jsx20(ParentContext.Provider, { value: null, children });
2668
2685
  };
2669
2686
 
2670
2687
  // src/reality/components/ModelAsset.tsx
2671
- import { useEffect as useEffect21, useRef as useRef9 } from "react";
2688
+ import { useEffect as useEffect21, useRef as useRef10 } from "react";
2672
2689
  var resolveAssetUrl = (url) => {
2673
2690
  if (url.startsWith("http://") || url.startsWith("https://")) {
2674
2691
  return url;
@@ -2677,16 +2694,16 @@ var resolveAssetUrl = (url) => {
2677
2694
  };
2678
2695
  var ModelAsset = ({ children, ...options }) => {
2679
2696
  const ctx = useRealityContext();
2680
- const materialRef = useRef9();
2697
+ const materialRef = useRef10();
2681
2698
  useEffect21(() => {
2682
2699
  const controller = new AbortController();
2683
2700
  if (!ctx) return;
2684
2701
  const { session, reality, resourceRegistry } = ctx;
2685
2702
  const init = async () => {
2686
- const resolvedUrl = resolveAssetUrl(options.src);
2687
- const modelAssetPromise = session.createModelAsset({ url: resolvedUrl });
2688
- resourceRegistry.add(options.id, modelAssetPromise);
2689
2703
  try {
2704
+ const resolvedUrl = resolveAssetUrl(options.src);
2705
+ const modelAssetPromise = session.createModelAsset({ url: resolvedUrl });
2706
+ resourceRegistry.add(options.id, modelAssetPromise);
2690
2707
  const mat = await modelAssetPromise;
2691
2708
  if (controller.signal.aborted) {
2692
2709
  mat.destroy();
@@ -2708,57 +2725,63 @@ var ModelAsset = ({ children, ...options }) => {
2708
2725
  };
2709
2726
 
2710
2727
  // src/reality/components/ModelEntity.tsx
2711
- import { forwardRef as forwardRef17 } from "react";
2712
- import { jsx as jsx20 } from "react/jsx-runtime";
2713
- var ModelEntity = forwardRef17(
2714
- ({ id, model, position, rotation, scale, onSpatialTap, children, name }, ref) => {
2715
- const ctx = useRealityContext();
2716
- const entity = useEntity({
2717
- ref,
2718
- id,
2719
- position,
2720
- rotation,
2721
- scale,
2722
- onSpatialTap,
2723
- createEntity: async (signal) => {
2724
- try {
2725
- const modelAsset = await ctx.resourceRegistry.get(model);
2726
- if (!modelAsset)
2727
- throw new Error(`ModelEntity: model not found ${model}`);
2728
- if (signal.aborted) {
2728
+ import { forwardRef as forwardRef18 } from "react";
2729
+ import { jsx as jsx21 } from "react/jsx-runtime";
2730
+ var ModelEntity = forwardRef18(
2731
+ ({ id, model, children, name, ...rest }, ref) => {
2732
+ return /* @__PURE__ */ jsx21(
2733
+ BaseEntity,
2734
+ {
2735
+ ...rest,
2736
+ id,
2737
+ ref,
2738
+ createEntity: async (ctx, signal) => {
2739
+ try {
2740
+ const modelAsset = await ctx.resourceRegistry.get(model);
2741
+ if (!modelAsset)
2742
+ throw new Error(`ModelEntity: model not found ${model}`);
2743
+ if (signal.aborted) return null;
2744
+ return ctx.session.createSpatialModelEntity(
2745
+ {
2746
+ modelAssetId: modelAsset.id,
2747
+ name
2748
+ },
2749
+ { id, name }
2750
+ );
2751
+ } catch (error) {
2729
2752
  return null;
2730
2753
  }
2731
- return ctx.session.createSpatialModelEntity(
2732
- {
2733
- modelAssetId: modelAsset.id,
2734
- name
2735
- },
2736
- { id, name }
2737
- );
2738
- } catch (error) {
2739
- console.error("ModelEntity error:", error);
2740
- return null;
2741
- }
2754
+ },
2755
+ children
2742
2756
  }
2743
- });
2744
- if (!entity) return null;
2745
- return /* @__PURE__ */ jsx20(ParentContext.Provider, { value: entity, children });
2757
+ );
2746
2758
  }
2747
2759
  );
2748
2760
 
2749
2761
  // src/reality/components/Reality.tsx
2750
2762
  import {
2751
- forwardRef as forwardRef18,
2763
+ forwardRef as forwardRef19,
2752
2764
  useCallback as useCallback8,
2753
2765
  useEffect as useEffect22,
2754
- useRef as useRef10,
2766
+ useRef as useRef11,
2755
2767
  useState as useState8
2756
2768
  } from "react";
2757
- import { Fragment as Fragment3, jsx as jsx21, jsxs as jsxs3 } from "react/jsx-runtime";
2758
- var Reality = forwardRef18(
2759
- function RealityBase({ children, ...props }, ref) {
2760
- const ctxRef = useRef10(null);
2761
- const creationId = useRef10(0);
2769
+ import { Fragment as Fragment3, jsx as jsx22, jsxs as jsxs3 } from "react/jsx-runtime";
2770
+ var Reality = forwardRef19(
2771
+ function RealityBase({ children, ...inProps }, ref) {
2772
+ const {
2773
+ onSpatialTap,
2774
+ onSpatialDragStart,
2775
+ onSpatialDrag,
2776
+ onSpatialDragEnd,
2777
+ onSpatialRotate,
2778
+ onSpatialRotateEnd,
2779
+ onSpatialMagnify,
2780
+ onSpatialMagnifyEnd,
2781
+ ...props
2782
+ } = inProps;
2783
+ const ctxRef = useRef11(null);
2784
+ const creationId = useRef11(0);
2762
2785
  const [isReady, setIsReady] = useState8(false);
2763
2786
  const cleanupReality = useCallback8(() => {
2764
2787
  ctxRef.current?.resourceRegistry.destroy();
@@ -2805,9 +2828,9 @@ var Reality = forwardRef18(
2805
2828
  return null;
2806
2829
  }
2807
2830
  }, [cleanupReality]);
2808
- const content = useCallback8(() => /* @__PURE__ */ jsx21(Fragment3, {}), []);
2831
+ const content = useCallback8(() => /* @__PURE__ */ jsx22(Fragment3, {}), []);
2809
2832
  return /* @__PURE__ */ jsxs3(RealityContext.Provider, { value: ctxRef.current, children: [
2810
- /* @__PURE__ */ jsx21(
2833
+ /* @__PURE__ */ jsx22(
2811
2834
  SpatializedContainer,
2812
2835
  {
2813
2836
  component: "div",
@@ -2823,9 +2846,9 @@ var Reality = forwardRef18(
2823
2846
  );
2824
2847
 
2825
2848
  // src/Model.tsx
2826
- import { forwardRef as forwardRef19 } from "react";
2849
+ import { forwardRef as forwardRef20 } from "react";
2827
2850
  import { Spatial as Spatial2 } from "@webspatial/core-sdk";
2828
- import { jsx as jsx22 } from "react/jsx-runtime";
2851
+ import { jsx as jsx23 } from "react/jsx-runtime";
2829
2852
  var spatial2 = new Spatial2();
2830
2853
  function ModelBase(props, ref) {
2831
2854
  const { "enable-xr": enableXR, ...restProps } = props;
@@ -2835,23 +2858,21 @@ function ModelBase(props, ref) {
2835
2858
  onSpatialDragStart,
2836
2859
  onSpatialDrag,
2837
2860
  onSpatialDragEnd,
2838
- onSpatialRotateStart,
2839
2861
  onSpatialRotate,
2840
2862
  onSpatialRotateEnd,
2841
- onSpatialMagnifyStart,
2842
2863
  onSpatialMagnify,
2843
2864
  onSpatialMagnifyEnd,
2844
2865
  ...modelProps
2845
2866
  } = restProps;
2846
- return /* @__PURE__ */ jsx22("model", { ref, ...modelProps });
2867
+ return /* @__PURE__ */ jsx23("model", { ref, ...modelProps });
2847
2868
  }
2848
- return /* @__PURE__ */ jsx22(SpatializedStatic3DElementContainer, { ref, ...restProps });
2869
+ return /* @__PURE__ */ jsx23(SpatializedStatic3DElementContainer, { ref, ...restProps });
2849
2870
  }
2850
- var Model = withSSRSupported(forwardRef19(ModelBase));
2871
+ var Model = withSSRSupported(forwardRef20(ModelBase));
2851
2872
  Model.displayName = "Model";
2852
2873
 
2853
2874
  // src/index.ts
2854
- var version = "1.1.0";
2875
+ var version = "1.2.0";
2855
2876
  if (typeof window !== "undefined") {
2856
2877
  initPolyfill();
2857
2878
  }