@webspatial/react-sdk 1.1.0 → 1.2.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.
@@ -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.1"
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
 
@@ -1158,12 +1164,12 @@ function useSync2DFrame(spatialId, portalInstanceObject, spatializedContainerObj
1158
1164
 
1159
1165
  // src/spatialized-container/hooks/useSpatializedElement.ts
1160
1166
  import { useEffect as useEffect6, useState as useState3 } from "react";
1161
- function useSpatializedElement(createSpatializedElement3, portalInstanceObject) {
1167
+ function useSpatializedElement(createSpatializedElement2, portalInstanceObject) {
1162
1168
  const [spatializedElement, setSpatializedElement] = useState3();
1163
1169
  useEffect6(() => {
1164
1170
  let isDestroyed = false;
1165
1171
  let spatializedElement2;
1166
- createSpatializedElement3().then(
1172
+ createSpatializedElement2().then(
1167
1173
  (inSpatializedElement) => {
1168
1174
  if (!isDestroyed) {
1169
1175
  spatializedElement2 = inSpatializedElement;
@@ -1181,7 +1187,7 @@ function useSpatializedElement(createSpatializedElement3, portalInstanceObject)
1181
1187
  spatializedElement2 = void 0;
1182
1188
  }
1183
1189
  };
1184
- }, [createSpatializedElement3, portalInstanceObject]);
1190
+ }, [createSpatializedElement2, portalInstanceObject]);
1185
1191
  return spatializedElement;
1186
1192
  }
1187
1193
 
@@ -1215,16 +1221,14 @@ function renderPlaceholderInSubPortal(portalInstanceObject, El) {
1215
1221
  function PortalSpatializedContainer(props) {
1216
1222
  const {
1217
1223
  spatializedContent: Content,
1218
- createSpatializedElement: createSpatializedElement3,
1224
+ createSpatializedElement: createSpatializedElement2,
1219
1225
  getExtraSpatializedElementProperties: getExtraSpatializedElementProperties2,
1220
1226
  onSpatialTap,
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,
@@ -1251,7 +1255,7 @@ function PortalSpatializedContainer(props) {
1251
1255
  }, []);
1252
1256
  useSync2DFrame(spatialId, portalInstanceObject, spatializedContainerObject);
1253
1257
  const spatializedElement = useSpatializedElement(
1254
- createSpatializedElement3,
1258
+ createSpatializedElement2,
1255
1259
  portalInstanceObject
1256
1260
  );
1257
1261
  const PlaceholderEl = renderPlaceholderInSubPortal(
@@ -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
  };
@@ -1462,16 +1476,14 @@ function SpatializedContainerBase(inprops, ref) {
1462
1476
  const {
1463
1477
  component: Component,
1464
1478
  spatializedContent,
1465
- createSpatializedElement: createSpatializedElement3,
1479
+ createSpatializedElement: createSpatializedElement2,
1466
1480
  getExtraSpatializedElementProperties: getExtraSpatializedElementProperties2,
1467
1481
  onSpatialTap: onSpatialTap2,
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
  },
@@ -1547,7 +1555,7 @@ function SpatializedContainerBase(inprops, ref) {
1547
1555
  }, [spatialContainerRefProxy.current]);
1548
1556
  const {
1549
1557
  spatializedContent,
1550
- createSpatializedElement: createSpatializedElement3,
1558
+ createSpatializedElement: createSpatializedElement2,
1551
1559
  getExtraSpatializedElementProperties: getExtraSpatializedElementProperties2,
1552
1560
  ...restProps
1553
1561
  } = props;
@@ -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
  },
@@ -1599,7 +1605,7 @@ function SpatializedContainerBase(inprops, ref) {
1599
1605
  );
1600
1606
  const {
1601
1607
  spatializedContent,
1602
- createSpatializedElement: createSpatializedElement3,
1608
+ createSpatializedElement: createSpatializedElement2,
1603
1609
  getExtraSpatializedElementProperties: getExtraSpatializedElementProperties2,
1604
1610
  ...restProps
1605
1611
  } = props;
@@ -1809,17 +1815,21 @@ import {
1809
1815
  useCallback as useCallback6,
1810
1816
  useContext as useContext8,
1811
1817
  useEffect as useEffect12,
1812
- useMemo as useMemo3
1818
+ useMemo as useMemo3,
1819
+ useState as useState6,
1820
+ useImperativeHandle,
1821
+ useRef as useRef4
1813
1822
  } from "react";
1814
1823
  import { Fragment as Fragment2, jsx as jsx8 } from "react/jsx-runtime";
1815
1824
  function getAbsoluteURL(url) {
1816
1825
  if (!url) {
1817
1826
  return "";
1818
1827
  }
1819
- if (url.startsWith("http") || url.startsWith("//")) {
1828
+ try {
1829
+ return new URL(url, document.baseURI).toString();
1830
+ } catch {
1820
1831
  return url;
1821
1832
  }
1822
- return window.location.origin + url;
1823
1833
  }
1824
1834
  function createLoadEvent(type, targetGetter) {
1825
1835
  const event = new CustomEvent(type, {
@@ -1882,43 +1892,23 @@ function SpatializedContent2(props) {
1882
1892
  }, [onError]);
1883
1893
  return /* @__PURE__ */ jsx8(Fragment2, {});
1884
1894
  }
1885
- async function createSpatializedElement2() {
1886
- return getSession().createSpatializedStatic3DElement();
1887
- }
1888
1895
  function SpatializedStatic3DElementContainerBase(props, ref) {
1896
+ const containerRef = useRef4(null);
1897
+ const [elementCreated, setElementCreated] = useState6(false);
1898
+ useImperativeHandle(ref, () => elementCreated ? containerRef.current : null, [elementCreated]);
1899
+ const createSpatializedElement2 = useCallback6(async () => {
1900
+ const element = await getSession().createSpatializedStatic3DElement();
1901
+ setElementCreated(true);
1902
+ return element;
1903
+ }, [setElementCreated]);
1889
1904
  const extraRefProps = useCallback6(
1890
1905
  (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
- });
1906
+ let modelTransform = new DOMMatrixReadOnly();
1919
1907
  return {
1920
- currentSrc: () => getAbsoluteURL(props.src),
1921
- ready: () => {
1908
+ get currentSrc() {
1909
+ return getAbsoluteURL(props.src);
1910
+ },
1911
+ get ready() {
1922
1912
  const spatializedElement = domProxy.__spatializedElement;
1923
1913
  const promise = spatializedElement.ready.then((success) => {
1924
1914
  if (success) {
@@ -1928,7 +1918,14 @@ function SpatializedStatic3DElementContainerBase(props, ref) {
1928
1918
  });
1929
1919
  return promise;
1930
1920
  },
1931
- entityTransform: () => domMatrixProxy
1921
+ get entityTransform() {
1922
+ return modelTransform;
1923
+ },
1924
+ set entityTransform(value) {
1925
+ modelTransform = value;
1926
+ const spatializedElement = domProxy.__spatializedElement;
1927
+ spatializedElement.updateModelTransform(modelTransform);
1928
+ }
1932
1929
  };
1933
1930
  },
1934
1931
  []
@@ -1936,7 +1933,7 @@ function SpatializedStatic3DElementContainerBase(props, ref) {
1936
1933
  return /* @__PURE__ */ jsx8(
1937
1934
  SpatializedContainer,
1938
1935
  {
1939
- ref,
1936
+ ref: containerRef,
1940
1937
  component: "div",
1941
1938
  createSpatializedElement: createSpatializedElement2,
1942
1939
  spatializedContent: SpatializedContent2,
@@ -1998,9 +1995,9 @@ function initScene(name, callback, options) {
1998
1995
  import { forwardRef as forwardRef9 } from "react";
1999
1996
 
2000
1997
  // src/spatialized-container-monitor/useMonitorDomChange.tsx
2001
- import { useRef as useRef4, useEffect as useEffect13, useMemo as useMemo4 } from "react";
1998
+ import { useRef as useRef5, useEffect as useEffect13, useMemo as useMemo4 } from "react";
2002
1999
  function useMonitorDomChange(inRef) {
2003
- const ref = useRef4(null);
2000
+ const ref = useRef5(null);
2004
2001
  useEffect13(() => {
2005
2002
  const observer = new MutationObserver((mutationsList) => {
2006
2003
  notifyDOMUpdate(mutationsList);
@@ -2093,6 +2090,9 @@ function withSpatialMonitor(El) {
2093
2090
  }
2094
2091
 
2095
2092
  // src/reality/components/Entity.tsx
2093
+ import { forwardRef as forwardRef11 } from "react";
2094
+
2095
+ // src/reality/components/BaseEntity.tsx
2096
2096
  import { forwardRef as forwardRef10 } from "react";
2097
2097
 
2098
2098
  // src/reality/context/RealityContext.tsx
@@ -2106,7 +2106,7 @@ var ParentContext = createContext6(null);
2106
2106
  var useParentContext = () => useContext10(ParentContext);
2107
2107
 
2108
2108
  // src/reality/hooks/useEntityTransform.tsx
2109
- import { useEffect as useEffect15, useRef as useRef5 } from "react";
2109
+ import { useEffect as useEffect15, useRef as useRef6 } from "react";
2110
2110
 
2111
2111
  // src/reality/utils/ResourceRegistry.ts
2112
2112
  var ResourceRegistry = class {
@@ -2122,7 +2122,8 @@ var ResourceRegistry = class {
2122
2122
  removeAndDestroy(id) {
2123
2123
  const p = this.resources.get(id);
2124
2124
  if (p) {
2125
- p.then((spatialObj) => spatialObj.destroy());
2125
+ p.then((spatialObj) => spatialObj.destroy()).catch(() => {
2126
+ });
2126
2127
  }
2127
2128
  this.resources.delete(id);
2128
2129
  }
@@ -2132,7 +2133,10 @@ var ResourceRegistry = class {
2132
2133
  destroy() {
2133
2134
  const pending = Array.from(this.resources.values());
2134
2135
  this.resources.clear();
2135
- pending.forEach((promise) => promise.then((spatialObj) => spatialObj.destroy()));
2136
+ pending.forEach(
2137
+ (promise) => promise.then((spatialObj) => spatialObj.destroy()).catch(() => {
2138
+ })
2139
+ );
2136
2140
  }
2137
2141
  };
2138
2142
 
@@ -2183,7 +2187,7 @@ var AbortResourceManager = class {
2183
2187
 
2184
2188
  // src/reality/hooks/useEntityTransform.tsx
2185
2189
  function useEntityTransform(entity, { position, rotation, scale }) {
2186
- const last = useRef5({});
2190
+ const last = useRef6({});
2187
2191
  useEffect15(() => {
2188
2192
  if (!entity) return;
2189
2193
  const shouldUpdate = !shallowEqualVec3(last.current.position, position) || !shallowEqualRotation(last.current.rotation, rotation) || !shallowEqualVec3(last.current.scale, scale);
@@ -2203,7 +2207,7 @@ function useEntityTransform(entity, { position, rotation, scale }) {
2203
2207
  }
2204
2208
 
2205
2209
  // src/reality/hooks/useEntityEvent.tsx
2206
- import { useEffect as useEffect17 } from "react";
2210
+ import { useEffect as useEffect17, useRef as useRef8 } from "react";
2207
2211
 
2208
2212
  // src/reality/type.ts
2209
2213
  var eventMap = {
@@ -2224,9 +2228,9 @@ var eventMap = {
2224
2228
  };
2225
2229
 
2226
2230
  // src/reality/hooks/useEntityRef.tsx
2227
- import { useImperativeHandle } from "react";
2231
+ import { useImperativeHandle as useImperativeHandle2 } from "react";
2228
2232
  var useEntityRef = (ref, instance) => {
2229
- useImperativeHandle(ref, () => instance);
2233
+ useImperativeHandle2(ref, () => instance);
2230
2234
  };
2231
2235
  var EntityRef = class {
2232
2236
  _entity;
@@ -2307,27 +2311,69 @@ function createEventProxy2(ev, instance) {
2307
2311
  }
2308
2312
  return instance;
2309
2313
  }
2314
+ if (prop === "bubbles") {
2315
+ return true;
2316
+ }
2317
+ if (prop === "offsetX") {
2318
+ const type = target.type;
2319
+ if (type === "spatialtap") {
2320
+ return target.detail?.location3D?.x ?? 0;
2321
+ }
2322
+ if (type === "spatialdragstart") {
2323
+ return target.detail?.startLocation3D?.x ?? 0;
2324
+ }
2325
+ return void 0;
2326
+ }
2327
+ if (prop === "offsetY") {
2328
+ const type = target.type;
2329
+ if (type === "spatialtap") {
2330
+ return target.detail?.location3D?.y ?? 0;
2331
+ }
2332
+ if (type === "spatialdragstart") {
2333
+ return target.detail?.startLocation3D?.y ?? 0;
2334
+ }
2335
+ return void 0;
2336
+ }
2337
+ if (prop === "offsetZ") {
2338
+ const type = target.type;
2339
+ if (type === "spatialtap") {
2340
+ return target.detail?.location3D?.z ?? 0;
2341
+ }
2342
+ if (type === "spatialdragstart") {
2343
+ return target.detail?.startLocation3D?.z ?? 0;
2344
+ }
2345
+ return void 0;
2346
+ }
2310
2347
  const val = target[prop];
2311
2348
  return typeof val === "function" ? val.bind(target) : val;
2312
2349
  }
2313
2350
  });
2314
2351
  }
2315
2352
  var useEntityEvent = ({ instance, ...handlers }) => {
2353
+ const eventsSetRef = useRef8(/* @__PURE__ */ new Set());
2316
2354
  useEffect17(() => {
2317
2355
  const entity = instance.entity;
2318
2356
  if (!entity) return;
2319
- const boundHandlers = [];
2320
2357
  Object.entries(eventMap).forEach(([reactKey, spatialEvent]) => {
2321
2358
  const handlerFn = handlers[reactKey];
2322
2359
  if (!handlerFn) return;
2323
2360
  const wrapped = (ev) => handlerFn(createEventProxy2(ev, instance));
2324
2361
  entity.addEvent(spatialEvent, wrapped);
2325
- boundHandlers.push(() => entity.removeEvent(spatialEvent));
2362
+ eventsSetRef.current.add(reactKey);
2326
2363
  });
2327
2364
  return () => {
2328
- boundHandlers.forEach((unbind) => unbind());
2329
2365
  };
2330
2366
  }, [instance.entity, ...Object.values(handlers)]);
2367
+ useEffect17(() => {
2368
+ const entity = instance.entity;
2369
+ if (!entity) return;
2370
+ return () => {
2371
+ for (let x of eventsSetRef.current) {
2372
+ entity.removeEvent(x);
2373
+ }
2374
+ eventsSetRef.current.clear();
2375
+ };
2376
+ }, [instance.entity]);
2331
2377
  return null;
2332
2378
  };
2333
2379
 
@@ -2346,7 +2392,7 @@ var useEntityId = ({ id, entity }) => {
2346
2392
  };
2347
2393
 
2348
2394
  // src/reality/hooks/useEntity.tsx
2349
- import { useEffect as useEffect19, useRef as useRef7 } from "react";
2395
+ import { useEffect as useEffect19, useRef as useRef9 } from "react";
2350
2396
  var useEntity = ({
2351
2397
  ref,
2352
2398
  id,
@@ -2357,10 +2403,10 @@ var useEntity = ({
2357
2403
  onSpatialDragStart,
2358
2404
  onSpatialDrag,
2359
2405
  onSpatialDragEnd,
2360
- onSpatialRotateStart,
2406
+ // onSpatialRotateStart,
2361
2407
  onSpatialRotate,
2362
2408
  onSpatialRotateEnd,
2363
- onSpatialMagnifyStart,
2409
+ // onSpatialMagnifyStart,
2364
2410
  onSpatialMagnify,
2365
2411
  onSpatialMagnifyEnd,
2366
2412
  // TODO: add other event handlers
@@ -2368,7 +2414,7 @@ var useEntity = ({
2368
2414
  }) => {
2369
2415
  const ctx = useRealityContext();
2370
2416
  const parent = useParentContext();
2371
- const instanceRef = useRef7(new EntityRef(null, ctx));
2417
+ const instanceRef = useRef9(new EntityRef(null, ctx));
2372
2418
  const forceUpdate = useForceUpdate2();
2373
2419
  useEffect19(() => {
2374
2420
  if (!ctx) return;
@@ -2409,10 +2455,10 @@ var useEntity = ({
2409
2455
  onSpatialDragStart,
2410
2456
  onSpatialDrag,
2411
2457
  onSpatialDragEnd,
2412
- onSpatialRotateStart,
2458
+ // onSpatialRotateStart,
2413
2459
  onSpatialRotate,
2414
2460
  onSpatialRotateEnd,
2415
- onSpatialMagnifyStart,
2461
+ // onSpatialMagnifyStart,
2416
2462
  onSpatialMagnify,
2417
2463
  onSpatialMagnifyEnd
2418
2464
  });
@@ -2420,115 +2466,94 @@ var useEntity = ({
2420
2466
  };
2421
2467
 
2422
2468
  // src/reality/hooks/useForceUpdate.tsx
2423
- import { useCallback as useCallback7, useState as useState7 } from "react";
2469
+ import { useCallback as useCallback7, useState as useState8 } from "react";
2424
2470
  var useForceUpdate2 = () => {
2425
- const [, setTick] = useState7(0);
2471
+ const [, setTick] = useState8(0);
2426
2472
  return useCallback7(() => setTick((tick) => tick + 1), []);
2427
2473
  };
2428
2474
 
2429
- // src/reality/components/Entity.tsx
2475
+ // src/reality/components/BaseEntity.tsx
2430
2476
  import { jsx as jsx12 } from "react/jsx-runtime";
2431
- var Entity = forwardRef10(
2432
- ({ id, children, position, rotation, scale, onSpatialTap, name }, ref) => {
2477
+ var BaseEntity = forwardRef10(
2478
+ ({ children, createEntity, ...rest }, ref) => {
2433
2479
  const ctx = useRealityContext();
2434
2480
  const entity = useEntity({
2481
+ ...rest,
2435
2482
  ref,
2436
- id,
2437
- position,
2438
- rotation,
2439
- scale,
2440
- onSpatialTap,
2441
- createEntity: async () => ctx.session.createEntity({ id, name })
2483
+ createEntity: (signal) => createEntity(ctx, signal)
2442
2484
  });
2443
2485
  if (!entity) return null;
2444
2486
  return /* @__PURE__ */ jsx12(ParentContext.Provider, { value: entity, children });
2445
2487
  }
2446
2488
  );
2447
2489
 
2490
+ // src/reality/components/Entity.tsx
2491
+ import { jsx as jsx13 } from "react/jsx-runtime";
2492
+ var Entity = forwardRef11((props, ref) => {
2493
+ const { id, name, children, ...rest } = props;
2494
+ return /* @__PURE__ */ jsx13(
2495
+ BaseEntity,
2496
+ {
2497
+ ...rest,
2498
+ id,
2499
+ ref,
2500
+ createEntity: async (ctxVal) => ctxVal.session.createEntity({ id, name }),
2501
+ children
2502
+ }
2503
+ );
2504
+ });
2505
+
2448
2506
  // src/reality/components/BoxEntity.tsx
2449
- import { forwardRef as forwardRef12 } from "react";
2507
+ import { forwardRef as forwardRef13 } from "react";
2450
2508
 
2451
2509
  // 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
- }
2510
+ import { forwardRef as forwardRef12 } from "react";
2511
+ import { jsx as jsx14 } from "react/jsx-runtime";
2512
+ var GeometryEntity = forwardRef12(
2513
+ ({ id, children, name, materials, geometryOptions, createGeometry, ...rest }, ref) => {
2514
+ return /* @__PURE__ */ jsx14(
2515
+ BaseEntity,
2516
+ {
2517
+ ...rest,
2518
+ id,
2519
+ ref,
2520
+ createEntity: async (ctx, signal) => {
2521
+ const manager = new AbortResourceManager(signal);
2522
+ try {
2523
+ const ent = await manager.addResource(
2524
+ () => ctx.session.createEntity({ id, name })
2525
+ );
2526
+ const geometry = await manager.addResource(
2527
+ () => createGeometry(geometryOptions)
2528
+ );
2529
+ const materialList = await Promise.all(
2530
+ materials?.map((id2) => ctx.resourceRegistry.get(id2)).filter(Boolean) ?? []
2531
+ );
2532
+ const modelComponent = await manager.addResource(
2533
+ () => ctx.session.createModelComponent({
2534
+ mesh: geometry,
2535
+ materials: materialList
2536
+ })
2537
+ );
2538
+ await ent.addComponent(modelComponent);
2539
+ return ent;
2540
+ } catch (error) {
2541
+ await manager.dispose();
2542
+ return null;
2543
+ }
2544
+ },
2545
+ children
2519
2546
  }
2520
- });
2521
- if (!entity) return null;
2522
- return /* @__PURE__ */ jsx13(ParentContext.Provider, { value: entity, children });
2547
+ );
2523
2548
  }
2524
2549
  );
2525
2550
 
2526
2551
  // src/reality/components/BoxEntity.tsx
2527
- import { jsx as jsx14 } from "react/jsx-runtime";
2528
- var BoxEntity = forwardRef12(
2552
+ import { jsx as jsx15 } from "react/jsx-runtime";
2553
+ var BoxEntity = forwardRef13(
2529
2554
  ({ children, ...props }, ref) => {
2530
2555
  const ctx = useRealityContext();
2531
- return /* @__PURE__ */ jsx14(
2556
+ return /* @__PURE__ */ jsx15(
2532
2557
  GeometryEntity,
2533
2558
  {
2534
2559
  ...props,
@@ -2548,10 +2573,10 @@ var BoxEntity = forwardRef12(
2548
2573
  );
2549
2574
 
2550
2575
  // src/reality/components/UnlitMaterial.tsx
2551
- import { useEffect as useEffect20, useRef as useRef8 } from "react";
2576
+ import { useEffect as useEffect20, useRef as useRef10 } from "react";
2552
2577
  var UnlitMaterial = ({ children, ...options }) => {
2553
2578
  const ctx = useRealityContext();
2554
- const materialRef = useRef8();
2579
+ const materialRef = useRef10();
2555
2580
  useEffect20(() => {
2556
2581
  if (!ctx) return;
2557
2582
  const { session, reality, resourceRegistry } = ctx;
@@ -2574,12 +2599,12 @@ var UnlitMaterial = ({ children, ...options }) => {
2574
2599
  };
2575
2600
 
2576
2601
  // 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(
2602
+ import { forwardRef as forwardRef14 } from "react";
2603
+ import { jsx as jsx16 } from "react/jsx-runtime";
2604
+ var SphereEntity = forwardRef14(
2580
2605
  ({ children, ...props }, ref) => {
2581
2606
  const ctx = useRealityContext();
2582
- return /* @__PURE__ */ jsx15(
2607
+ return /* @__PURE__ */ jsx16(
2583
2608
  GeometryEntity,
2584
2609
  {
2585
2610
  ...props,
@@ -2595,12 +2620,12 @@ var SphereEntity = forwardRef13(
2595
2620
  );
2596
2621
 
2597
2622
  // 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(
2623
+ import { forwardRef as forwardRef15 } from "react";
2624
+ import { jsx as jsx17 } from "react/jsx-runtime";
2625
+ var ConeEntity = forwardRef15(
2601
2626
  ({ children, ...props }, ref) => {
2602
2627
  const ctx = useRealityContext();
2603
- return /* @__PURE__ */ jsx16(
2628
+ return /* @__PURE__ */ jsx17(
2604
2629
  GeometryEntity,
2605
2630
  {
2606
2631
  ...props,
@@ -2617,12 +2642,12 @@ var ConeEntity = forwardRef14(
2617
2642
  );
2618
2643
 
2619
2644
  // 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(
2645
+ import { forwardRef as forwardRef16 } from "react";
2646
+ import { jsx as jsx18 } from "react/jsx-runtime";
2647
+ var CylinderEntity = forwardRef16(
2623
2648
  ({ children, ...props }, ref) => {
2624
2649
  const ctx = useRealityContext();
2625
- return /* @__PURE__ */ jsx17(
2650
+ return /* @__PURE__ */ jsx18(
2626
2651
  GeometryEntity,
2627
2652
  {
2628
2653
  ...props,
@@ -2639,12 +2664,12 @@ var CylinderEntity = forwardRef15(
2639
2664
  );
2640
2665
 
2641
2666
  // 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(
2667
+ import { forwardRef as forwardRef17 } from "react";
2668
+ import { jsx as jsx19 } from "react/jsx-runtime";
2669
+ var PlaneEntity = forwardRef17(
2645
2670
  ({ children, ...props }, ref) => {
2646
2671
  const ctx = useRealityContext();
2647
- return /* @__PURE__ */ jsx18(
2672
+ return /* @__PURE__ */ jsx19(
2648
2673
  GeometryEntity,
2649
2674
  {
2650
2675
  ...props,
@@ -2662,13 +2687,13 @@ var PlaneEntity = forwardRef16(
2662
2687
  );
2663
2688
 
2664
2689
  // src/reality/components/SceneGraph.tsx
2665
- import { jsx as jsx19 } from "react/jsx-runtime";
2690
+ import { jsx as jsx20 } from "react/jsx-runtime";
2666
2691
  var SceneGraph = ({ children }) => {
2667
- return /* @__PURE__ */ jsx19(ParentContext.Provider, { value: null, children });
2692
+ return /* @__PURE__ */ jsx20(ParentContext.Provider, { value: null, children });
2668
2693
  };
2669
2694
 
2670
2695
  // src/reality/components/ModelAsset.tsx
2671
- import { useEffect as useEffect21, useRef as useRef9 } from "react";
2696
+ import { useEffect as useEffect21, useRef as useRef11 } from "react";
2672
2697
  var resolveAssetUrl = (url) => {
2673
2698
  if (url.startsWith("http://") || url.startsWith("https://")) {
2674
2699
  return url;
@@ -2677,16 +2702,16 @@ var resolveAssetUrl = (url) => {
2677
2702
  };
2678
2703
  var ModelAsset = ({ children, ...options }) => {
2679
2704
  const ctx = useRealityContext();
2680
- const materialRef = useRef9();
2705
+ const materialRef = useRef11();
2681
2706
  useEffect21(() => {
2682
2707
  const controller = new AbortController();
2683
2708
  if (!ctx) return;
2684
2709
  const { session, reality, resourceRegistry } = ctx;
2685
2710
  const init = async () => {
2686
- const resolvedUrl = resolveAssetUrl(options.src);
2687
- const modelAssetPromise = session.createModelAsset({ url: resolvedUrl });
2688
- resourceRegistry.add(options.id, modelAssetPromise);
2689
2711
  try {
2712
+ const resolvedUrl = resolveAssetUrl(options.src);
2713
+ const modelAssetPromise = session.createModelAsset({ url: resolvedUrl });
2714
+ resourceRegistry.add(options.id, modelAssetPromise);
2690
2715
  const mat = await modelAssetPromise;
2691
2716
  if (controller.signal.aborted) {
2692
2717
  mat.destroy();
@@ -2708,58 +2733,64 @@ var ModelAsset = ({ children, ...options }) => {
2708
2733
  };
2709
2734
 
2710
2735
  // 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) {
2736
+ import { forwardRef as forwardRef18 } from "react";
2737
+ import { jsx as jsx21 } from "react/jsx-runtime";
2738
+ var ModelEntity = forwardRef18(
2739
+ ({ id, model, children, name, ...rest }, ref) => {
2740
+ return /* @__PURE__ */ jsx21(
2741
+ BaseEntity,
2742
+ {
2743
+ ...rest,
2744
+ id,
2745
+ ref,
2746
+ createEntity: async (ctx, signal) => {
2747
+ try {
2748
+ const modelAsset = await ctx.resourceRegistry.get(model);
2749
+ if (!modelAsset)
2750
+ throw new Error(`ModelEntity: model not found ${model}`);
2751
+ if (signal.aborted) return null;
2752
+ return ctx.session.createSpatialModelEntity(
2753
+ {
2754
+ modelAssetId: modelAsset.id,
2755
+ name
2756
+ },
2757
+ { id, name }
2758
+ );
2759
+ } catch (error) {
2729
2760
  return null;
2730
2761
  }
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
- }
2762
+ },
2763
+ children
2742
2764
  }
2743
- });
2744
- if (!entity) return null;
2745
- return /* @__PURE__ */ jsx20(ParentContext.Provider, { value: entity, children });
2765
+ );
2746
2766
  }
2747
2767
  );
2748
2768
 
2749
2769
  // src/reality/components/Reality.tsx
2750
2770
  import {
2751
- forwardRef as forwardRef18,
2771
+ forwardRef as forwardRef19,
2752
2772
  useCallback as useCallback8,
2753
2773
  useEffect as useEffect22,
2754
- useRef as useRef10,
2755
- useState as useState8
2774
+ useRef as useRef12,
2775
+ useState as useState9
2756
2776
  } 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);
2762
- const [isReady, setIsReady] = useState8(false);
2777
+ import { Fragment as Fragment3, jsx as jsx22, jsxs as jsxs3 } from "react/jsx-runtime";
2778
+ var Reality = forwardRef19(
2779
+ function RealityBase({ children, ...inProps }, ref) {
2780
+ const {
2781
+ onSpatialTap,
2782
+ onSpatialDragStart,
2783
+ onSpatialDrag,
2784
+ onSpatialDragEnd,
2785
+ onSpatialRotate,
2786
+ onSpatialRotateEnd,
2787
+ onSpatialMagnify,
2788
+ onSpatialMagnifyEnd,
2789
+ ...props
2790
+ } = inProps;
2791
+ const ctxRef = useRef12(null);
2792
+ const creationId = useRef12(0);
2793
+ const [isReady, setIsReady] = useState9(false);
2763
2794
  const cleanupReality = useCallback8(() => {
2764
2795
  ctxRef.current?.resourceRegistry.destroy();
2765
2796
  ctxRef.current?.reality.destroy();
@@ -2805,9 +2836,9 @@ var Reality = forwardRef18(
2805
2836
  return null;
2806
2837
  }
2807
2838
  }, [cleanupReality]);
2808
- const content = useCallback8(() => /* @__PURE__ */ jsx21(Fragment3, {}), []);
2839
+ const content = useCallback8(() => /* @__PURE__ */ jsx22(Fragment3, {}), []);
2809
2840
  return /* @__PURE__ */ jsxs3(RealityContext.Provider, { value: ctxRef.current, children: [
2810
- /* @__PURE__ */ jsx21(
2841
+ /* @__PURE__ */ jsx22(
2811
2842
  SpatializedContainer,
2812
2843
  {
2813
2844
  component: "div",
@@ -2823,9 +2854,9 @@ var Reality = forwardRef18(
2823
2854
  );
2824
2855
 
2825
2856
  // src/Model.tsx
2826
- import { forwardRef as forwardRef19 } from "react";
2857
+ import { forwardRef as forwardRef20 } from "react";
2827
2858
  import { Spatial as Spatial2 } from "@webspatial/core-sdk";
2828
- import { jsx as jsx22 } from "react/jsx-runtime";
2859
+ import { jsx as jsx23 } from "react/jsx-runtime";
2829
2860
  var spatial2 = new Spatial2();
2830
2861
  function ModelBase(props, ref) {
2831
2862
  const { "enable-xr": enableXR, ...restProps } = props;
@@ -2835,23 +2866,21 @@ function ModelBase(props, ref) {
2835
2866
  onSpatialDragStart,
2836
2867
  onSpatialDrag,
2837
2868
  onSpatialDragEnd,
2838
- onSpatialRotateStart,
2839
2869
  onSpatialRotate,
2840
2870
  onSpatialRotateEnd,
2841
- onSpatialMagnifyStart,
2842
2871
  onSpatialMagnify,
2843
2872
  onSpatialMagnifyEnd,
2844
2873
  ...modelProps
2845
2874
  } = restProps;
2846
- return /* @__PURE__ */ jsx22("model", { ref, ...modelProps });
2875
+ return /* @__PURE__ */ jsx23("model", { ref, ...modelProps });
2847
2876
  }
2848
- return /* @__PURE__ */ jsx22(SpatializedStatic3DElementContainer, { ref, ...restProps });
2877
+ return /* @__PURE__ */ jsx23(SpatializedStatic3DElementContainer, { ref, ...restProps });
2849
2878
  }
2850
- var Model = withSSRSupported(forwardRef19(ModelBase));
2879
+ var Model = withSSRSupported(forwardRef20(ModelBase));
2851
2880
  Model.displayName = "Model";
2852
2881
 
2853
2882
  // src/index.ts
2854
- var version = "1.1.0";
2883
+ var version = "1.2.1";
2855
2884
  if (typeof window !== "undefined") {
2856
2885
  initPolyfill();
2857
2886
  }