@guardian/interactive-component-library 0.1.0-alpha.29 → 0.1.0-alpha.30

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.
@@ -440,46 +440,25 @@
440
440
  unit,
441
441
  container: container$6
442
442
  };
443
- function useWindowSize() {
444
- const [windowSize, setWindowSize] = hooks.useState(() => {
445
- if (typeof window === "undefined")
446
- return {
447
- width: 0,
448
- height: 0
449
- };
450
- return {
451
- width: window.innerWidth,
452
- height: window.innerHeight
453
- };
454
- });
455
- hooks.useEffect(() => {
456
- if (typeof window === "undefined")
457
- return;
458
- function handleResize() {
459
- setWindowSize({
460
- width: window.innerWidth,
461
- height: window.innerHeight
462
- });
463
- }
464
- window.addEventListener("resize", handleResize);
465
- return () => {
466
- window.removeEventListener("resize", handleResize);
467
- };
468
- }, []);
469
- return windowSize;
470
- }
471
443
  function useContainerSize(containerRef) {
472
- const windowSize = useWindowSize();
473
444
  const [containerSize, setContainerSize] = hooks.useState();
474
445
  hooks.useLayoutEffect(() => {
475
446
  const container2 = containerRef.current;
476
447
  if (!container2)
477
448
  return;
478
- setContainerSize({
479
- width: container2.clientWidth,
480
- height: container2.clientHeight
449
+ const observer = new ResizeObserver((entries) => {
450
+ for (let entry of entries) {
451
+ setContainerSize({
452
+ width: entry.contentRect.width,
453
+ height: entry.contentRect.height
454
+ });
455
+ }
481
456
  });
482
- }, [containerRef, windowSize]);
457
+ observer.observe(container2);
458
+ return () => {
459
+ observer.disconnect();
460
+ };
461
+ }, [containerRef]);
483
462
  return containerSize;
484
463
  }
485
464
  const WaffleType = {
@@ -1563,17 +1542,13 @@
1563
1542
  }
1564
1543
  }) => {
1565
1544
  const wrapperRef = hooks.useRef(null);
1566
- const windowSize = useWindowSize();
1567
- const [width, setWidth] = hooks.useState(0);
1545
+ const containerSize = useContainerSize(wrapperRef);
1546
+ const width = containerSize ? containerSize.width : 0;
1568
1547
  const contentWidth = Math.floor(width - padding.left - padding.right);
1569
1548
  const contentHeight = contentWidth;
1570
1549
  const height = contentHeight + padding.top + padding.bottom;
1571
1550
  const yScale = scaleLinear(domain, [contentHeight, 0]);
1572
1551
  const show = width > 0;
1573
- hooks.useLayoutEffect(() => {
1574
- const newWidth = wrapperRef.current.getBoundingClientRect().width;
1575
- setWidth(newWidth);
1576
- }, [windowSize]);
1577
1552
  const y1Labels = hooks.useMemo(() => {
1578
1553
  let labels = lines.map((d2) => ({
1579
1554
  y: yScale(d2.y1),
@@ -3877,6 +3852,8 @@
3877
3852
  }, [width, height]);
3878
3853
  const organisedChildren = useOrganisedChildren(children);
3879
3854
  const renderSVG = containerSize && !config.drawToCanvas;
3855
+ const zoomControl2 = organisedChildren.controls["ZoomControl"];
3856
+ const renderZoomControl = zoomControl2 && zoom.enabled;
3880
3857
  return jsxRuntime.jsxs("div", {
3881
3858
  ref: containerRef,
3882
3859
  className: styles$4.container,
@@ -3897,9 +3874,9 @@
3897
3874
  })
3898
3875
  }), jsxRuntime.jsx("div", {
3899
3876
  className: styles$4.controls,
3900
- children: jsxRuntime.jsx("div", {
3877
+ children: renderZoomControl && jsxRuntime.jsx("div", {
3901
3878
  className: styles$4.zoomControl,
3902
- children: preact.cloneElement(organisedChildren.controls["ZoomControl"], {
3879
+ children: preact.cloneElement(zoomControl2, {
3903
3880
  onZoomIn: () => rendererRef.current.zoomIn(),
3904
3881
  onZoomOut: () => rendererRef.current.zoomOut()
3905
3882
  })
@@ -7425,6 +7402,34 @@
7425
7402
  });
7426
7403
  }
7427
7404
  }
7405
+ function useWindowSize() {
7406
+ const [windowSize, setWindowSize] = hooks.useState(() => {
7407
+ if (typeof window === "undefined")
7408
+ return {
7409
+ width: 0,
7410
+ height: 0
7411
+ };
7412
+ return {
7413
+ width: window.innerWidth,
7414
+ height: window.innerHeight
7415
+ };
7416
+ });
7417
+ hooks.useEffect(() => {
7418
+ if (typeof window === "undefined")
7419
+ return;
7420
+ function handleResize() {
7421
+ setWindowSize({
7422
+ width: window.innerWidth,
7423
+ height: window.innerHeight
7424
+ });
7425
+ }
7426
+ window.addEventListener("resize", handleResize);
7427
+ return () => {
7428
+ window.removeEventListener("resize", handleResize);
7429
+ };
7430
+ }, []);
7431
+ return windowSize;
7432
+ }
7428
7433
  const coalitionsWrapper = "_coalitionsWrapper_4egbd_9";
7429
7434
  const coalitionsContainer = "_coalitionsContainer_4egbd_14";
7430
7435
  const coalition = "_coalition_4egbd_9";