@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.
package/dist/web/index.js CHANGED
@@ -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'] = "web"
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
 
@@ -1199,12 +1205,12 @@ function useSync2DFrame(spatialId, portalInstanceObject, spatializedContainerObj
1199
1205
 
1200
1206
  // src/spatialized-container/hooks/useSpatializedElement.ts
1201
1207
  import { useEffect as useEffect6, useState as useState3 } from "react";
1202
- function useSpatializedElement(createSpatializedElement3, portalInstanceObject) {
1208
+ function useSpatializedElement(createSpatializedElement2, portalInstanceObject) {
1203
1209
  const [spatializedElement, setSpatializedElement] = useState3();
1204
1210
  useEffect6(() => {
1205
1211
  let isDestroyed = false;
1206
1212
  let spatializedElement2;
1207
- createSpatializedElement3().then(
1213
+ createSpatializedElement2().then(
1208
1214
  (inSpatializedElement) => {
1209
1215
  if (!isDestroyed) {
1210
1216
  spatializedElement2 = inSpatializedElement;
@@ -1222,7 +1228,7 @@ function useSpatializedElement(createSpatializedElement3, portalInstanceObject)
1222
1228
  spatializedElement2 = void 0;
1223
1229
  }
1224
1230
  };
1225
- }, [createSpatializedElement3, portalInstanceObject]);
1231
+ }, [createSpatializedElement2, portalInstanceObject]);
1226
1232
  return spatializedElement;
1227
1233
  }
1228
1234
 
@@ -1256,16 +1262,14 @@ function renderPlaceholderInSubPortal(portalInstanceObject, El) {
1256
1262
  function PortalSpatializedContainer(props) {
1257
1263
  const {
1258
1264
  spatializedContent: Content,
1259
- createSpatializedElement: createSpatializedElement3,
1265
+ createSpatializedElement: createSpatializedElement2,
1260
1266
  getExtraSpatializedElementProperties: getExtraSpatializedElementProperties2,
1261
1267
  onSpatialTap,
1262
1268
  onSpatialDragStart,
1263
1269
  onSpatialDrag,
1264
1270
  onSpatialDragEnd,
1265
- onSpatialRotateStart,
1266
1271
  onSpatialRotate,
1267
1272
  onSpatialRotateEnd,
1268
- onSpatialMagnifyStart,
1269
1273
  onSpatialMagnify,
1270
1274
  onSpatialMagnifyEnd,
1271
1275
  [SpatialID]: spatialId,
@@ -1292,7 +1296,7 @@ function PortalSpatializedContainer(props) {
1292
1296
  }, []);
1293
1297
  useSync2DFrame(spatialId, portalInstanceObject, spatializedContainerObject);
1294
1298
  const spatializedElement = useSpatializedElement(
1295
- createSpatializedElement3,
1299
+ createSpatializedElement2,
1296
1300
  portalInstanceObject
1297
1301
  );
1298
1302
  const PlaceholderEl = renderPlaceholderInSubPortal(
@@ -1339,16 +1343,6 @@ function PortalSpatializedContainer(props) {
1339
1343
  spatializedElement.onSpatialDragStart = onSpatialDragStart;
1340
1344
  }
1341
1345
  }, [spatializedElement, onSpatialDragStart]);
1342
- useEffect7(() => {
1343
- if (spatializedElement) {
1344
- spatializedElement.onSpatialRotateStart = onSpatialRotateStart;
1345
- }
1346
- }, [spatializedElement, onSpatialRotateStart]);
1347
- useEffect7(() => {
1348
- if (spatializedElement) {
1349
- spatializedElement.onSpatialMagnifyStart = onSpatialMagnifyStart;
1350
- }
1351
- }, [spatializedElement, onSpatialMagnifyStart]);
1352
1346
  return /* @__PURE__ */ jsxs(PortalInstanceContext.Provider, { value: portalInstanceObject, children: [
1353
1347
  spatializedElement && portalInstanceObject.dom && /* @__PURE__ */ jsx3(Content, { spatializedElement, ...restProps }),
1354
1348
  PlaceholderEl
@@ -1356,7 +1350,7 @@ function PortalSpatializedContainer(props) {
1356
1350
  }
1357
1351
 
1358
1352
  // src/spatialized-container/hooks/useSpatialEvents.ts
1359
- function createEventProxy(event, currentTargetGetter) {
1353
+ function createEventProxy(event, currentTargetGetter, offsetXGetter, offsetYGetter, offsetZGetter) {
1360
1354
  return new Proxy(event, {
1361
1355
  get(target, prop) {
1362
1356
  if (prop === "currentTarget") {
@@ -1365,20 +1359,41 @@ function createEventProxy(event, currentTargetGetter) {
1365
1359
  if (prop === "isTrusted") {
1366
1360
  return true;
1367
1361
  }
1362
+ if (prop === "bubbles") {
1363
+ return false;
1364
+ }
1365
+ if (prop === "offsetX" && offsetXGetter) {
1366
+ return offsetXGetter(target) ?? 0;
1367
+ }
1368
+ if (prop === "offsetY" && offsetYGetter) {
1369
+ return offsetYGetter(target) ?? 0;
1370
+ }
1371
+ if (prop === "offsetZ" && offsetZGetter) {
1372
+ return offsetZGetter(target) ?? 0;
1373
+ }
1368
1374
  return Reflect.get(target, prop);
1369
1375
  }
1370
1376
  });
1371
1377
  }
1372
- function createEventHandler(handler, currentTargetGetter) {
1378
+ function createEventHandler(handler, currentTargetGetter, offsetXGetter, offsetYGetter, offsetZGetter) {
1373
1379
  return handler ? (event) => {
1374
- const proxyEvent = createEventProxy(event, currentTargetGetter);
1380
+ const proxyEvent = createEventProxy(
1381
+ event,
1382
+ currentTargetGetter,
1383
+ offsetXGetter,
1384
+ offsetYGetter,
1385
+ offsetZGetter
1386
+ );
1375
1387
  handler(proxyEvent);
1376
1388
  } : void 0;
1377
1389
  }
1378
1390
  function useSpatialEventsBase(spatialEvents, currentTargetGetter) {
1379
1391
  const onSpatialTap = createEventHandler(
1380
1392
  spatialEvents.onSpatialTap,
1381
- currentTargetGetter
1393
+ currentTargetGetter,
1394
+ (ev) => ev.detail?.location3D?.x,
1395
+ (ev) => ev.detail?.location3D?.y,
1396
+ (ev) => ev.detail?.location3D?.z
1382
1397
  );
1383
1398
  const onSpatialDrag = createEventHandler(
1384
1399
  spatialEvents.onSpatialDrag,
@@ -1406,19 +1421,18 @@ function useSpatialEventsBase(spatialEvents, currentTargetGetter) {
1406
1421
  );
1407
1422
  const onSpatialDragStart = createEventHandler(
1408
1423
  spatialEvents.onSpatialDragStart,
1409
- currentTargetGetter
1424
+ currentTargetGetter,
1425
+ (ev) => ev.detail?.startLocation3D?.x,
1426
+ (ev) => ev.detail?.startLocation3D?.y,
1427
+ (ev) => ev.detail?.startLocation3D?.z
1410
1428
  );
1411
- const onSpatialRotateStart = createEventHandler(spatialEvents.onSpatialRotateStart, currentTargetGetter);
1412
- const onSpatialMagnifyStart = createEventHandler(spatialEvents.onSpatialMagnifyStart, currentTargetGetter);
1413
1429
  return {
1414
1430
  onSpatialTap,
1415
1431
  onSpatialDragStart,
1416
1432
  onSpatialDrag,
1417
1433
  onSpatialDragEnd,
1418
- onSpatialRotateStart,
1419
1434
  onSpatialRotate,
1420
1435
  onSpatialRotateEnd,
1421
- onSpatialMagnifyStart,
1422
1436
  onSpatialMagnify,
1423
1437
  onSpatialMagnifyEnd
1424
1438
  };
@@ -1503,16 +1517,14 @@ function SpatializedContainerBase(inprops, ref) {
1503
1517
  const {
1504
1518
  component: Component,
1505
1519
  spatializedContent,
1506
- createSpatializedElement: createSpatializedElement3,
1520
+ createSpatializedElement: createSpatializedElement2,
1507
1521
  getExtraSpatializedElementProperties: getExtraSpatializedElementProperties2,
1508
1522
  onSpatialTap: onSpatialTap2,
1509
1523
  onSpatialDragStart: onSpatialDragStart2,
1510
1524
  onSpatialDrag: onSpatialDrag2,
1511
1525
  onSpatialDragEnd: onSpatialDragEnd2,
1512
- onSpatialRotateStart: onSpatialRotateStart2,
1513
1526
  onSpatialRotate: onSpatialRotate2,
1514
1527
  onSpatialRotateEnd: onSpatialRotateEnd2,
1515
- onSpatialMagnifyStart: onSpatialMagnifyStart2,
1516
1528
  onSpatialMagnify: onSpatialMagnify2,
1517
1529
  onSpatialMagnifyEnd: onSpatialMagnifyEnd2,
1518
1530
  extraRefProps: extraRefProps2,
@@ -1539,10 +1551,8 @@ function SpatializedContainerBase(inprops, ref) {
1539
1551
  onSpatialDragStart,
1540
1552
  onSpatialDrag,
1541
1553
  onSpatialDragEnd,
1542
- onSpatialRotateStart,
1543
1554
  onSpatialRotate,
1544
1555
  onSpatialRotateEnd,
1545
- onSpatialMagnifyStart,
1546
1556
  onSpatialMagnify,
1547
1557
  onSpatialMagnifyEnd,
1548
1558
  extraRefProps,
@@ -1556,10 +1566,8 @@ function SpatializedContainerBase(inprops, ref) {
1556
1566
  onSpatialDragStart,
1557
1567
  onSpatialDrag,
1558
1568
  onSpatialDragEnd,
1559
- onSpatialRotateStart,
1560
1569
  onSpatialRotate,
1561
1570
  onSpatialRotateEnd,
1562
- onSpatialMagnifyStart,
1563
1571
  onSpatialMagnify,
1564
1572
  onSpatialMagnifyEnd
1565
1573
  },
@@ -1588,7 +1596,7 @@ function SpatializedContainerBase(inprops, ref) {
1588
1596
  }, [spatialContainerRefProxy.current]);
1589
1597
  const {
1590
1598
  spatializedContent,
1591
- createSpatializedElement: createSpatializedElement3,
1599
+ createSpatializedElement: createSpatializedElement2,
1592
1600
  getExtraSpatializedElementProperties: getExtraSpatializedElementProperties2,
1593
1601
  ...restProps
1594
1602
  } = props;
@@ -1625,10 +1633,8 @@ function SpatializedContainerBase(inprops, ref) {
1625
1633
  onSpatialDragStart,
1626
1634
  onSpatialDrag,
1627
1635
  onSpatialDragEnd,
1628
- onSpatialRotateStart,
1629
1636
  onSpatialRotate,
1630
1637
  onSpatialRotateEnd,
1631
- onSpatialMagnifyStart,
1632
1638
  onSpatialMagnify,
1633
1639
  onSpatialMagnifyEnd
1634
1640
  },
@@ -1640,7 +1646,7 @@ function SpatializedContainerBase(inprops, ref) {
1640
1646
  );
1641
1647
  const {
1642
1648
  spatializedContent,
1643
- createSpatializedElement: createSpatializedElement3,
1649
+ createSpatializedElement: createSpatializedElement2,
1644
1650
  getExtraSpatializedElementProperties: getExtraSpatializedElementProperties2,
1645
1651
  ...restProps
1646
1652
  } = props;
@@ -1850,17 +1856,21 @@ import {
1850
1856
  useCallback as useCallback6,
1851
1857
  useContext as useContext8,
1852
1858
  useEffect as useEffect12,
1853
- useMemo as useMemo3
1859
+ useMemo as useMemo3,
1860
+ useState as useState6,
1861
+ useImperativeHandle,
1862
+ useRef as useRef4
1854
1863
  } from "react";
1855
1864
  import { Fragment as Fragment2, jsx as jsx8 } from "react/jsx-runtime";
1856
1865
  function getAbsoluteURL(url) {
1857
1866
  if (!url) {
1858
1867
  return "";
1859
1868
  }
1860
- if (url.startsWith("http") || url.startsWith("//")) {
1869
+ try {
1870
+ return new URL(url, document.baseURI).toString();
1871
+ } catch {
1861
1872
  return url;
1862
1873
  }
1863
- return window.location.origin + url;
1864
1874
  }
1865
1875
  function createLoadEvent(type, targetGetter) {
1866
1876
  const event = new CustomEvent(type, {
@@ -1923,43 +1933,23 @@ function SpatializedContent2(props) {
1923
1933
  }, [onError]);
1924
1934
  return /* @__PURE__ */ jsx8(Fragment2, {});
1925
1935
  }
1926
- async function createSpatializedElement2() {
1927
- return getSession().createSpatializedStatic3DElement();
1928
- }
1929
1936
  function SpatializedStatic3DElementContainerBase(props, ref) {
1937
+ const containerRef = useRef4(null);
1938
+ const [elementCreated, setElementCreated] = useState6(false);
1939
+ useImperativeHandle(ref, () => elementCreated ? containerRef.current : null, [elementCreated]);
1940
+ const createSpatializedElement2 = useCallback6(async () => {
1941
+ const element = await getSession().createSpatializedStatic3DElement();
1942
+ setElementCreated(true);
1943
+ return element;
1944
+ }, [setElementCreated]);
1930
1945
  const extraRefProps = useCallback6(
1931
1946
  (domProxy) => {
1932
- const modelTransform = new DOMMatrix();
1933
- let needupdate = false;
1934
- const triggerUpdate = () => {
1935
- const spatializedElement = domProxy.__spatializedElement;
1936
- spatializedElement.updateModelTransform(modelTransform);
1937
- needupdate = false;
1938
- };
1939
- const domMatrixProxy = new Proxy(modelTransform, {
1940
- get(target, prop, receiver) {
1941
- const value = Reflect.get(target, prop, receiver);
1942
- if (typeof value === "function") {
1943
- return function(...args) {
1944
- requestAnimationFrame(triggerUpdate);
1945
- return value.apply(target, args);
1946
- };
1947
- } else {
1948
- return value;
1949
- }
1950
- },
1951
- set(target, prop, value) {
1952
- const success = Reflect.set(target, prop, value);
1953
- if (!needupdate) {
1954
- needupdate = true;
1955
- requestAnimationFrame(triggerUpdate);
1956
- }
1957
- return success;
1958
- }
1959
- });
1947
+ let modelTransform = new DOMMatrixReadOnly();
1960
1948
  return {
1961
- currentSrc: () => getAbsoluteURL(props.src),
1962
- ready: () => {
1949
+ get currentSrc() {
1950
+ return getAbsoluteURL(props.src);
1951
+ },
1952
+ get ready() {
1963
1953
  const spatializedElement = domProxy.__spatializedElement;
1964
1954
  const promise = spatializedElement.ready.then((success) => {
1965
1955
  if (success) {
@@ -1969,7 +1959,14 @@ function SpatializedStatic3DElementContainerBase(props, ref) {
1969
1959
  });
1970
1960
  return promise;
1971
1961
  },
1972
- entityTransform: () => domMatrixProxy
1962
+ get entityTransform() {
1963
+ return modelTransform;
1964
+ },
1965
+ set entityTransform(value) {
1966
+ modelTransform = value;
1967
+ const spatializedElement = domProxy.__spatializedElement;
1968
+ spatializedElement.updateModelTransform(modelTransform);
1969
+ }
1973
1970
  };
1974
1971
  },
1975
1972
  []
@@ -1977,7 +1974,7 @@ function SpatializedStatic3DElementContainerBase(props, ref) {
1977
1974
  return /* @__PURE__ */ jsx8(
1978
1975
  SpatializedContainer,
1979
1976
  {
1980
- ref,
1977
+ ref: containerRef,
1981
1978
  component: "div",
1982
1979
  createSpatializedElement: createSpatializedElement2,
1983
1980
  spatializedContent: SpatializedContent2,
@@ -2039,9 +2036,9 @@ function initScene(name, callback) {
2039
2036
  import { forwardRef as forwardRef9 } from "react";
2040
2037
 
2041
2038
  // src/spatialized-container-monitor/useMonitorDomChange.tsx
2042
- import { useRef as useRef4, useEffect as useEffect13, useMemo as useMemo4 } from "react";
2039
+ import { useRef as useRef5, useEffect as useEffect13, useMemo as useMemo4 } from "react";
2043
2040
  function useMonitorDomChange(inRef) {
2044
- const ref = useRef4(null);
2041
+ const ref = useRef5(null);
2045
2042
  useEffect13(() => {
2046
2043
  const observer = new MutationObserver((mutationsList) => {
2047
2044
  notifyDOMUpdate(mutationsList);
@@ -2134,6 +2131,9 @@ function withSpatialMonitor(El) {
2134
2131
  }
2135
2132
 
2136
2133
  // src/reality/components/Entity.tsx
2134
+ import { forwardRef as forwardRef11 } from "react";
2135
+
2136
+ // src/reality/components/BaseEntity.tsx
2137
2137
  import { forwardRef as forwardRef10 } from "react";
2138
2138
 
2139
2139
  // src/reality/context/RealityContext.tsx
@@ -2147,7 +2147,7 @@ var ParentContext = createContext6(null);
2147
2147
  var useParentContext = () => useContext10(ParentContext);
2148
2148
 
2149
2149
  // src/reality/hooks/useEntityTransform.tsx
2150
- import { useEffect as useEffect15, useRef as useRef5 } from "react";
2150
+ import { useEffect as useEffect15, useRef as useRef6 } from "react";
2151
2151
 
2152
2152
  // src/reality/utils/ResourceRegistry.ts
2153
2153
  var ResourceRegistry = class {
@@ -2163,7 +2163,8 @@ var ResourceRegistry = class {
2163
2163
  removeAndDestroy(id) {
2164
2164
  const p = this.resources.get(id);
2165
2165
  if (p) {
2166
- p.then((spatialObj) => spatialObj.destroy());
2166
+ p.then((spatialObj) => spatialObj.destroy()).catch(() => {
2167
+ });
2167
2168
  }
2168
2169
  this.resources.delete(id);
2169
2170
  }
@@ -2173,7 +2174,10 @@ var ResourceRegistry = class {
2173
2174
  destroy() {
2174
2175
  const pending = Array.from(this.resources.values());
2175
2176
  this.resources.clear();
2176
- pending.forEach((promise) => promise.then((spatialObj) => spatialObj.destroy()));
2177
+ pending.forEach(
2178
+ (promise) => promise.then((spatialObj) => spatialObj.destroy()).catch(() => {
2179
+ })
2180
+ );
2177
2181
  }
2178
2182
  };
2179
2183
 
@@ -2224,7 +2228,7 @@ var AbortResourceManager = class {
2224
2228
 
2225
2229
  // src/reality/hooks/useEntityTransform.tsx
2226
2230
  function useEntityTransform(entity, { position, rotation, scale }) {
2227
- const last = useRef5({});
2231
+ const last = useRef6({});
2228
2232
  useEffect15(() => {
2229
2233
  if (!entity) return;
2230
2234
  const shouldUpdate = !shallowEqualVec3(last.current.position, position) || !shallowEqualRotation(last.current.rotation, rotation) || !shallowEqualVec3(last.current.scale, scale);
@@ -2244,7 +2248,7 @@ function useEntityTransform(entity, { position, rotation, scale }) {
2244
2248
  }
2245
2249
 
2246
2250
  // src/reality/hooks/useEntityEvent.tsx
2247
- import { useEffect as useEffect17 } from "react";
2251
+ import { useEffect as useEffect17, useRef as useRef8 } from "react";
2248
2252
 
2249
2253
  // src/reality/type.ts
2250
2254
  var eventMap = {
@@ -2265,9 +2269,9 @@ var eventMap = {
2265
2269
  };
2266
2270
 
2267
2271
  // src/reality/hooks/useEntityRef.tsx
2268
- import { useImperativeHandle } from "react";
2272
+ import { useImperativeHandle as useImperativeHandle2 } from "react";
2269
2273
  var useEntityRef = (ref, instance) => {
2270
- useImperativeHandle(ref, () => instance);
2274
+ useImperativeHandle2(ref, () => instance);
2271
2275
  };
2272
2276
  var EntityRef = class {
2273
2277
  _entity;
@@ -2348,27 +2352,69 @@ function createEventProxy2(ev, instance) {
2348
2352
  }
2349
2353
  return instance;
2350
2354
  }
2355
+ if (prop === "bubbles") {
2356
+ return true;
2357
+ }
2358
+ if (prop === "offsetX") {
2359
+ const type = target.type;
2360
+ if (type === "spatialtap") {
2361
+ return target.detail?.location3D?.x ?? 0;
2362
+ }
2363
+ if (type === "spatialdragstart") {
2364
+ return target.detail?.startLocation3D?.x ?? 0;
2365
+ }
2366
+ return void 0;
2367
+ }
2368
+ if (prop === "offsetY") {
2369
+ const type = target.type;
2370
+ if (type === "spatialtap") {
2371
+ return target.detail?.location3D?.y ?? 0;
2372
+ }
2373
+ if (type === "spatialdragstart") {
2374
+ return target.detail?.startLocation3D?.y ?? 0;
2375
+ }
2376
+ return void 0;
2377
+ }
2378
+ if (prop === "offsetZ") {
2379
+ const type = target.type;
2380
+ if (type === "spatialtap") {
2381
+ return target.detail?.location3D?.z ?? 0;
2382
+ }
2383
+ if (type === "spatialdragstart") {
2384
+ return target.detail?.startLocation3D?.z ?? 0;
2385
+ }
2386
+ return void 0;
2387
+ }
2351
2388
  const val = target[prop];
2352
2389
  return typeof val === "function" ? val.bind(target) : val;
2353
2390
  }
2354
2391
  });
2355
2392
  }
2356
2393
  var useEntityEvent = ({ instance, ...handlers }) => {
2394
+ const eventsSetRef = useRef8(/* @__PURE__ */ new Set());
2357
2395
  useEffect17(() => {
2358
2396
  const entity = instance.entity;
2359
2397
  if (!entity) return;
2360
- const boundHandlers = [];
2361
2398
  Object.entries(eventMap).forEach(([reactKey, spatialEvent]) => {
2362
2399
  const handlerFn = handlers[reactKey];
2363
2400
  if (!handlerFn) return;
2364
2401
  const wrapped = (ev) => handlerFn(createEventProxy2(ev, instance));
2365
2402
  entity.addEvent(spatialEvent, wrapped);
2366
- boundHandlers.push(() => entity.removeEvent(spatialEvent));
2403
+ eventsSetRef.current.add(reactKey);
2367
2404
  });
2368
2405
  return () => {
2369
- boundHandlers.forEach((unbind) => unbind());
2370
2406
  };
2371
2407
  }, [instance.entity, ...Object.values(handlers)]);
2408
+ useEffect17(() => {
2409
+ const entity = instance.entity;
2410
+ if (!entity) return;
2411
+ return () => {
2412
+ for (let x of eventsSetRef.current) {
2413
+ entity.removeEvent(x);
2414
+ }
2415
+ eventsSetRef.current.clear();
2416
+ };
2417
+ }, [instance.entity]);
2372
2418
  return null;
2373
2419
  };
2374
2420
 
@@ -2387,7 +2433,7 @@ var useEntityId = ({ id, entity }) => {
2387
2433
  };
2388
2434
 
2389
2435
  // src/reality/hooks/useEntity.tsx
2390
- import { useEffect as useEffect19, useRef as useRef7 } from "react";
2436
+ import { useEffect as useEffect19, useRef as useRef9 } from "react";
2391
2437
  var useEntity = ({
2392
2438
  ref,
2393
2439
  id,
@@ -2398,10 +2444,10 @@ var useEntity = ({
2398
2444
  onSpatialDragStart,
2399
2445
  onSpatialDrag,
2400
2446
  onSpatialDragEnd,
2401
- onSpatialRotateStart,
2447
+ // onSpatialRotateStart,
2402
2448
  onSpatialRotate,
2403
2449
  onSpatialRotateEnd,
2404
- onSpatialMagnifyStart,
2450
+ // onSpatialMagnifyStart,
2405
2451
  onSpatialMagnify,
2406
2452
  onSpatialMagnifyEnd,
2407
2453
  // TODO: add other event handlers
@@ -2409,7 +2455,7 @@ var useEntity = ({
2409
2455
  }) => {
2410
2456
  const ctx = useRealityContext();
2411
2457
  const parent = useParentContext();
2412
- const instanceRef = useRef7(new EntityRef(null, ctx));
2458
+ const instanceRef = useRef9(new EntityRef(null, ctx));
2413
2459
  const forceUpdate = useForceUpdate2();
2414
2460
  useEffect19(() => {
2415
2461
  if (!ctx) return;
@@ -2450,10 +2496,10 @@ var useEntity = ({
2450
2496
  onSpatialDragStart,
2451
2497
  onSpatialDrag,
2452
2498
  onSpatialDragEnd,
2453
- onSpatialRotateStart,
2499
+ // onSpatialRotateStart,
2454
2500
  onSpatialRotate,
2455
2501
  onSpatialRotateEnd,
2456
- onSpatialMagnifyStart,
2502
+ // onSpatialMagnifyStart,
2457
2503
  onSpatialMagnify,
2458
2504
  onSpatialMagnifyEnd
2459
2505
  });
@@ -2461,115 +2507,94 @@ var useEntity = ({
2461
2507
  };
2462
2508
 
2463
2509
  // src/reality/hooks/useForceUpdate.tsx
2464
- import { useCallback as useCallback7, useState as useState7 } from "react";
2510
+ import { useCallback as useCallback7, useState as useState8 } from "react";
2465
2511
  var useForceUpdate2 = () => {
2466
- const [, setTick] = useState7(0);
2512
+ const [, setTick] = useState8(0);
2467
2513
  return useCallback7(() => setTick((tick) => tick + 1), []);
2468
2514
  };
2469
2515
 
2470
- // src/reality/components/Entity.tsx
2516
+ // src/reality/components/BaseEntity.tsx
2471
2517
  import { jsx as jsx12 } from "react/jsx-runtime";
2472
- var Entity = forwardRef10(
2473
- ({ id, children, position, rotation, scale, onSpatialTap, name }, ref) => {
2518
+ var BaseEntity = forwardRef10(
2519
+ ({ children, createEntity, ...rest }, ref) => {
2474
2520
  const ctx = useRealityContext();
2475
2521
  const entity = useEntity({
2522
+ ...rest,
2476
2523
  ref,
2477
- id,
2478
- position,
2479
- rotation,
2480
- scale,
2481
- onSpatialTap,
2482
- createEntity: async () => ctx.session.createEntity({ id, name })
2524
+ createEntity: (signal) => createEntity(ctx, signal)
2483
2525
  });
2484
2526
  if (!entity) return null;
2485
2527
  return /* @__PURE__ */ jsx12(ParentContext.Provider, { value: entity, children });
2486
2528
  }
2487
2529
  );
2488
2530
 
2531
+ // src/reality/components/Entity.tsx
2532
+ import { jsx as jsx13 } from "react/jsx-runtime";
2533
+ var Entity = forwardRef11((props, ref) => {
2534
+ const { id, name, children, ...rest } = props;
2535
+ return /* @__PURE__ */ jsx13(
2536
+ BaseEntity,
2537
+ {
2538
+ ...rest,
2539
+ id,
2540
+ ref,
2541
+ createEntity: async (ctxVal) => ctxVal.session.createEntity({ id, name }),
2542
+ children
2543
+ }
2544
+ );
2545
+ });
2546
+
2489
2547
  // src/reality/components/BoxEntity.tsx
2490
- import { forwardRef as forwardRef12 } from "react";
2548
+ import { forwardRef as forwardRef13 } from "react";
2491
2549
 
2492
2550
  // src/reality/components/GeometryEntity.tsx
2493
- import { forwardRef as forwardRef11 } from "react";
2494
- import { jsx as jsx13 } from "react/jsx-runtime";
2495
- var GeometryEntity = forwardRef11(
2496
- ({
2497
- id,
2498
- position,
2499
- rotation,
2500
- scale,
2501
- onSpatialTap,
2502
- onSpatialDragStart,
2503
- onSpatialDrag,
2504
- onSpatialDragEnd,
2505
- onSpatialRotateStart,
2506
- onSpatialRotate,
2507
- onSpatialRotateEnd,
2508
- onSpatialMagnifyStart,
2509
- onSpatialMagnify,
2510
- onSpatialMagnifyEnd,
2511
- // TODO: add other event handlers
2512
- children,
2513
- name,
2514
- materials,
2515
- geometryOptions,
2516
- createGeometry
2517
- }, ref) => {
2518
- const ctx = useRealityContext();
2519
- const entity = useEntity({
2520
- ref,
2521
- id,
2522
- position,
2523
- rotation,
2524
- scale,
2525
- onSpatialTap,
2526
- onSpatialDragStart,
2527
- onSpatialDrag,
2528
- onSpatialDragEnd,
2529
- onSpatialRotateStart,
2530
- onSpatialRotate,
2531
- onSpatialRotateEnd,
2532
- onSpatialMagnifyStart,
2533
- onSpatialMagnify,
2534
- onSpatialMagnifyEnd,
2535
- createEntity: async (signal) => {
2536
- const manager = new AbortResourceManager(signal);
2537
- try {
2538
- const ent = await manager.addResource(
2539
- () => ctx.session.createEntity({ id, name })
2540
- );
2541
- const geometry = await manager.addResource(
2542
- () => createGeometry(geometryOptions)
2543
- );
2544
- const materialList = await Promise.all(
2545
- materials?.map((id2) => ctx.resourceRegistry.get(id2)).filter(Boolean) ?? []
2546
- );
2547
- const modelComponent = await manager.addResource(
2548
- () => ctx.session.createModelComponent({
2549
- mesh: geometry,
2550
- materials: materialList
2551
- })
2552
- );
2553
- await ent.addComponent(modelComponent);
2554
- return ent;
2555
- } catch (error) {
2556
- console.error(error);
2557
- await manager.dispose();
2558
- return null;
2559
- }
2551
+ import { forwardRef as forwardRef12 } from "react";
2552
+ import { jsx as jsx14 } from "react/jsx-runtime";
2553
+ var GeometryEntity = forwardRef12(
2554
+ ({ id, children, name, materials, geometryOptions, createGeometry, ...rest }, ref) => {
2555
+ return /* @__PURE__ */ jsx14(
2556
+ BaseEntity,
2557
+ {
2558
+ ...rest,
2559
+ id,
2560
+ ref,
2561
+ createEntity: async (ctx, signal) => {
2562
+ const manager = new AbortResourceManager(signal);
2563
+ try {
2564
+ const ent = await manager.addResource(
2565
+ () => ctx.session.createEntity({ id, name })
2566
+ );
2567
+ const geometry = await manager.addResource(
2568
+ () => createGeometry(geometryOptions)
2569
+ );
2570
+ const materialList = await Promise.all(
2571
+ materials?.map((id2) => ctx.resourceRegistry.get(id2)).filter(Boolean) ?? []
2572
+ );
2573
+ const modelComponent = await manager.addResource(
2574
+ () => ctx.session.createModelComponent({
2575
+ mesh: geometry,
2576
+ materials: materialList
2577
+ })
2578
+ );
2579
+ await ent.addComponent(modelComponent);
2580
+ return ent;
2581
+ } catch (error) {
2582
+ await manager.dispose();
2583
+ return null;
2584
+ }
2585
+ },
2586
+ children
2560
2587
  }
2561
- });
2562
- if (!entity) return null;
2563
- return /* @__PURE__ */ jsx13(ParentContext.Provider, { value: entity, children });
2588
+ );
2564
2589
  }
2565
2590
  );
2566
2591
 
2567
2592
  // src/reality/components/BoxEntity.tsx
2568
- import { jsx as jsx14 } from "react/jsx-runtime";
2569
- var BoxEntity = forwardRef12(
2593
+ import { jsx as jsx15 } from "react/jsx-runtime";
2594
+ var BoxEntity = forwardRef13(
2570
2595
  ({ children, ...props }, ref) => {
2571
2596
  const ctx = useRealityContext();
2572
- return /* @__PURE__ */ jsx14(
2597
+ return /* @__PURE__ */ jsx15(
2573
2598
  GeometryEntity,
2574
2599
  {
2575
2600
  ...props,
@@ -2589,10 +2614,10 @@ var BoxEntity = forwardRef12(
2589
2614
  );
2590
2615
 
2591
2616
  // src/reality/components/UnlitMaterial.tsx
2592
- import { useEffect as useEffect20, useRef as useRef8 } from "react";
2617
+ import { useEffect as useEffect20, useRef as useRef10 } from "react";
2593
2618
  var UnlitMaterial = ({ children, ...options }) => {
2594
2619
  const ctx = useRealityContext();
2595
- const materialRef = useRef8();
2620
+ const materialRef = useRef10();
2596
2621
  useEffect20(() => {
2597
2622
  if (!ctx) return;
2598
2623
  const { session, reality, resourceRegistry } = ctx;
@@ -2615,12 +2640,12 @@ var UnlitMaterial = ({ children, ...options }) => {
2615
2640
  };
2616
2641
 
2617
2642
  // src/reality/components/SphereEntity.tsx
2618
- import { forwardRef as forwardRef13 } from "react";
2619
- import { jsx as jsx15 } from "react/jsx-runtime";
2620
- var SphereEntity = forwardRef13(
2643
+ import { forwardRef as forwardRef14 } from "react";
2644
+ import { jsx as jsx16 } from "react/jsx-runtime";
2645
+ var SphereEntity = forwardRef14(
2621
2646
  ({ children, ...props }, ref) => {
2622
2647
  const ctx = useRealityContext();
2623
- return /* @__PURE__ */ jsx15(
2648
+ return /* @__PURE__ */ jsx16(
2624
2649
  GeometryEntity,
2625
2650
  {
2626
2651
  ...props,
@@ -2636,12 +2661,12 @@ var SphereEntity = forwardRef13(
2636
2661
  );
2637
2662
 
2638
2663
  // src/reality/components/ConeEntity.tsx
2639
- import { forwardRef as forwardRef14 } from "react";
2640
- import { jsx as jsx16 } from "react/jsx-runtime";
2641
- var ConeEntity = forwardRef14(
2664
+ import { forwardRef as forwardRef15 } from "react";
2665
+ import { jsx as jsx17 } from "react/jsx-runtime";
2666
+ var ConeEntity = forwardRef15(
2642
2667
  ({ children, ...props }, ref) => {
2643
2668
  const ctx = useRealityContext();
2644
- return /* @__PURE__ */ jsx16(
2669
+ return /* @__PURE__ */ jsx17(
2645
2670
  GeometryEntity,
2646
2671
  {
2647
2672
  ...props,
@@ -2658,12 +2683,12 @@ var ConeEntity = forwardRef14(
2658
2683
  );
2659
2684
 
2660
2685
  // src/reality/components/CylinderEntity.tsx
2661
- import { forwardRef as forwardRef15 } from "react";
2662
- import { jsx as jsx17 } from "react/jsx-runtime";
2663
- var CylinderEntity = forwardRef15(
2686
+ import { forwardRef as forwardRef16 } from "react";
2687
+ import { jsx as jsx18 } from "react/jsx-runtime";
2688
+ var CylinderEntity = forwardRef16(
2664
2689
  ({ children, ...props }, ref) => {
2665
2690
  const ctx = useRealityContext();
2666
- return /* @__PURE__ */ jsx17(
2691
+ return /* @__PURE__ */ jsx18(
2667
2692
  GeometryEntity,
2668
2693
  {
2669
2694
  ...props,
@@ -2680,12 +2705,12 @@ var CylinderEntity = forwardRef15(
2680
2705
  );
2681
2706
 
2682
2707
  // src/reality/components/PlaneEntity.tsx
2683
- import { forwardRef as forwardRef16 } from "react";
2684
- import { jsx as jsx18 } from "react/jsx-runtime";
2685
- var PlaneEntity = forwardRef16(
2708
+ import { forwardRef as forwardRef17 } from "react";
2709
+ import { jsx as jsx19 } from "react/jsx-runtime";
2710
+ var PlaneEntity = forwardRef17(
2686
2711
  ({ children, ...props }, ref) => {
2687
2712
  const ctx = useRealityContext();
2688
- return /* @__PURE__ */ jsx18(
2713
+ return /* @__PURE__ */ jsx19(
2689
2714
  GeometryEntity,
2690
2715
  {
2691
2716
  ...props,
@@ -2703,13 +2728,13 @@ var PlaneEntity = forwardRef16(
2703
2728
  );
2704
2729
 
2705
2730
  // src/reality/components/SceneGraph.tsx
2706
- import { jsx as jsx19 } from "react/jsx-runtime";
2731
+ import { jsx as jsx20 } from "react/jsx-runtime";
2707
2732
  var SceneGraph = ({ children }) => {
2708
- return /* @__PURE__ */ jsx19(ParentContext.Provider, { value: null, children });
2733
+ return /* @__PURE__ */ jsx20(ParentContext.Provider, { value: null, children });
2709
2734
  };
2710
2735
 
2711
2736
  // src/reality/components/ModelAsset.tsx
2712
- import { useEffect as useEffect21, useRef as useRef9 } from "react";
2737
+ import { useEffect as useEffect21, useRef as useRef11 } from "react";
2713
2738
  var resolveAssetUrl = (url) => {
2714
2739
  if (url.startsWith("http://") || url.startsWith("https://")) {
2715
2740
  return url;
@@ -2718,16 +2743,16 @@ var resolveAssetUrl = (url) => {
2718
2743
  };
2719
2744
  var ModelAsset = ({ children, ...options }) => {
2720
2745
  const ctx = useRealityContext();
2721
- const materialRef = useRef9();
2746
+ const materialRef = useRef11();
2722
2747
  useEffect21(() => {
2723
2748
  const controller = new AbortController();
2724
2749
  if (!ctx) return;
2725
2750
  const { session, reality, resourceRegistry } = ctx;
2726
2751
  const init = async () => {
2727
- const resolvedUrl = resolveAssetUrl(options.src);
2728
- const modelAssetPromise = session.createModelAsset({ url: resolvedUrl });
2729
- resourceRegistry.add(options.id, modelAssetPromise);
2730
2752
  try {
2753
+ const resolvedUrl = resolveAssetUrl(options.src);
2754
+ const modelAssetPromise = session.createModelAsset({ url: resolvedUrl });
2755
+ resourceRegistry.add(options.id, modelAssetPromise);
2731
2756
  const mat = await modelAssetPromise;
2732
2757
  if (controller.signal.aborted) {
2733
2758
  mat.destroy();
@@ -2749,58 +2774,64 @@ var ModelAsset = ({ children, ...options }) => {
2749
2774
  };
2750
2775
 
2751
2776
  // src/reality/components/ModelEntity.tsx
2752
- import { forwardRef as forwardRef17 } from "react";
2753
- import { jsx as jsx20 } from "react/jsx-runtime";
2754
- var ModelEntity = forwardRef17(
2755
- ({ id, model, position, rotation, scale, onSpatialTap, children, name }, ref) => {
2756
- const ctx = useRealityContext();
2757
- const entity = useEntity({
2758
- ref,
2759
- id,
2760
- position,
2761
- rotation,
2762
- scale,
2763
- onSpatialTap,
2764
- createEntity: async (signal) => {
2765
- try {
2766
- const modelAsset = await ctx.resourceRegistry.get(model);
2767
- if (!modelAsset)
2768
- throw new Error(`ModelEntity: model not found ${model}`);
2769
- if (signal.aborted) {
2777
+ import { forwardRef as forwardRef18 } from "react";
2778
+ import { jsx as jsx21 } from "react/jsx-runtime";
2779
+ var ModelEntity = forwardRef18(
2780
+ ({ id, model, children, name, ...rest }, ref) => {
2781
+ return /* @__PURE__ */ jsx21(
2782
+ BaseEntity,
2783
+ {
2784
+ ...rest,
2785
+ id,
2786
+ ref,
2787
+ createEntity: async (ctx, signal) => {
2788
+ try {
2789
+ const modelAsset = await ctx.resourceRegistry.get(model);
2790
+ if (!modelAsset)
2791
+ throw new Error(`ModelEntity: model not found ${model}`);
2792
+ if (signal.aborted) return null;
2793
+ return ctx.session.createSpatialModelEntity(
2794
+ {
2795
+ modelAssetId: modelAsset.id,
2796
+ name
2797
+ },
2798
+ { id, name }
2799
+ );
2800
+ } catch (error) {
2770
2801
  return null;
2771
2802
  }
2772
- return ctx.session.createSpatialModelEntity(
2773
- {
2774
- modelAssetId: modelAsset.id,
2775
- name
2776
- },
2777
- { id, name }
2778
- );
2779
- } catch (error) {
2780
- console.error("ModelEntity error:", error);
2781
- return null;
2782
- }
2803
+ },
2804
+ children
2783
2805
  }
2784
- });
2785
- if (!entity) return null;
2786
- return /* @__PURE__ */ jsx20(ParentContext.Provider, { value: entity, children });
2806
+ );
2787
2807
  }
2788
2808
  );
2789
2809
 
2790
2810
  // src/reality/components/Reality.tsx
2791
2811
  import {
2792
- forwardRef as forwardRef18,
2812
+ forwardRef as forwardRef19,
2793
2813
  useCallback as useCallback8,
2794
2814
  useEffect as useEffect22,
2795
- useRef as useRef10,
2796
- useState as useState8
2815
+ useRef as useRef12,
2816
+ useState as useState9
2797
2817
  } from "react";
2798
- import { Fragment as Fragment3, jsx as jsx21, jsxs as jsxs3 } from "react/jsx-runtime";
2799
- var Reality = forwardRef18(
2800
- function RealityBase({ children, ...props }, ref) {
2801
- const ctxRef = useRef10(null);
2802
- const creationId = useRef10(0);
2803
- const [isReady, setIsReady] = useState8(false);
2818
+ import { Fragment as Fragment3, jsx as jsx22, jsxs as jsxs3 } from "react/jsx-runtime";
2819
+ var Reality = forwardRef19(
2820
+ function RealityBase({ children, ...inProps }, ref) {
2821
+ const {
2822
+ onSpatialTap,
2823
+ onSpatialDragStart,
2824
+ onSpatialDrag,
2825
+ onSpatialDragEnd,
2826
+ onSpatialRotate,
2827
+ onSpatialRotateEnd,
2828
+ onSpatialMagnify,
2829
+ onSpatialMagnifyEnd,
2830
+ ...props
2831
+ } = inProps;
2832
+ const ctxRef = useRef12(null);
2833
+ const creationId = useRef12(0);
2834
+ const [isReady, setIsReady] = useState9(false);
2804
2835
  const cleanupReality = useCallback8(() => {
2805
2836
  ctxRef.current?.resourceRegistry.destroy();
2806
2837
  ctxRef.current?.reality.destroy();
@@ -2846,9 +2877,9 @@ var Reality = forwardRef18(
2846
2877
  return null;
2847
2878
  }
2848
2879
  }, [cleanupReality]);
2849
- const content = useCallback8(() => /* @__PURE__ */ jsx21(Fragment3, {}), []);
2880
+ const content = useCallback8(() => /* @__PURE__ */ jsx22(Fragment3, {}), []);
2850
2881
  return /* @__PURE__ */ jsxs3(RealityContext.Provider, { value: ctxRef.current, children: [
2851
- /* @__PURE__ */ jsx21(
2882
+ /* @__PURE__ */ jsx22(
2852
2883
  SpatializedContainer,
2853
2884
  {
2854
2885
  component: "div",
@@ -2864,8 +2895,8 @@ var Reality = forwardRef18(
2864
2895
  );
2865
2896
 
2866
2897
  // src/Model.tsx
2867
- import { forwardRef as forwardRef19 } from "react";
2868
- import { jsx as jsx22 } from "react/jsx-runtime";
2898
+ import { forwardRef as forwardRef20 } from "react";
2899
+ import { jsx as jsx23 } from "react/jsx-runtime";
2869
2900
  var spatial2 = new Spatial();
2870
2901
  function ModelBase(props, ref) {
2871
2902
  const { "enable-xr": enableXR, ...restProps } = props;
@@ -2875,23 +2906,21 @@ function ModelBase(props, ref) {
2875
2906
  onSpatialDragStart,
2876
2907
  onSpatialDrag,
2877
2908
  onSpatialDragEnd,
2878
- onSpatialRotateStart,
2879
2909
  onSpatialRotate,
2880
2910
  onSpatialRotateEnd,
2881
- onSpatialMagnifyStart,
2882
2911
  onSpatialMagnify,
2883
2912
  onSpatialMagnifyEnd,
2884
2913
  ...modelProps
2885
2914
  } = restProps;
2886
- return /* @__PURE__ */ jsx22("model", { ref, ...modelProps });
2915
+ return /* @__PURE__ */ jsx23("model", { ref, ...modelProps });
2887
2916
  }
2888
- return /* @__PURE__ */ jsx22(SpatializedStatic3DElementContainer, { ref, ...restProps });
2917
+ return /* @__PURE__ */ jsx23(SpatializedStatic3DElementContainer, { ref, ...restProps });
2889
2918
  }
2890
- var Model = withSSRSupported(forwardRef19(ModelBase));
2919
+ var Model = withSSRSupported(forwardRef20(ModelBase));
2891
2920
  Model.displayName = "Model";
2892
2921
 
2893
2922
  // src/index.ts
2894
- var version = "1.1.0";
2923
+ var version = "1.2.1";
2895
2924
  if (typeof window !== "undefined") {
2896
2925
  initPolyfill();
2897
2926
  }