@guardian/interactive-component-library 0.2.0 → 0.3.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.
@@ -3663,9 +3663,33 @@ class Extent {
3663
3663
  this.maxX = maxX;
3664
3664
  this.maxY = maxY;
3665
3665
  }
3666
+ get width() {
3667
+ return this.maxX - this.minX;
3668
+ }
3669
+ get height() {
3670
+ return this.maxY - this.minY;
3671
+ }
3672
+ /**
3673
+ * Turn extent into a flat array of numbers: [minX, minY, maxX, maxY].
3674
+ *
3675
+ * @returns {[number, number, number, number]} - The extent as a flat array
3676
+ */
3666
3677
  flat() {
3667
3678
  return [this.minX, this.minY, this.maxX, this.maxY];
3668
3679
  }
3680
+ /**
3681
+ * Create a new extent where the coordinates are multiplied by scaleFactor.
3682
+ *
3683
+ * @param {number} scaleFactor
3684
+ * @returns {Extent} - The scaled
3685
+ */
3686
+ scale(scaleFactor) {
3687
+ const scaled = (
3688
+ /** @type {ExtentLike} */
3689
+ [this.minX, this.minY, this.maxX, this.maxY].map((d2) => d2 * scaleFactor)
3690
+ );
3691
+ return Extent.convert(scaled);
3692
+ }
3669
3693
  /**
3670
3694
  * Check if the passed coordinate is contained or on the edge of the extent.
3671
3695
  *
@@ -3928,7 +3952,10 @@ class View {
3928
3952
  const baseResolution = resolutionForExtent(baseExtent, this.viewPortSize);
3929
3953
  return baseResolution;
3930
3954
  }
3931
- // calculates the upper and lower zoom scales
3955
+ /**
3956
+ * Get the lower and upper zoom scales
3957
+ * @returns {[number, number]} - The lower and upper zoom scales
3958
+ */
3932
3959
  get scaleExtent() {
3933
3960
  const maxScale = zoomLevelToZoomScale(this.maxZoom, this.baseResolution);
3934
3961
  return [1, maxScale];
@@ -3959,27 +3986,22 @@ class View {
3959
3986
  }
3960
3987
  }
3961
3988
  /**
3962
- *
3963
3989
  * @param {import("./formats/GeoJSON").GeoJSONFeature} geoJSON
3964
3990
  */
3965
3991
  fitObject(geoJSON) {
3966
3992
  this.projection.fitExtent(this.getMapExtent(), geoJSON);
3967
3993
  ++this.projection.revision;
3968
3994
  }
3969
- // returns bounds relative to the viewport
3970
- boundsForExtent(extent) {
3971
- const SW = this.projection([extent[0], extent[1]]);
3972
- const NE = this.projection([extent[2], extent[3]]);
3973
- const minX = SW[0] / this.pixelRatio;
3974
- const minY = NE[1] / this.pixelRatio;
3975
- const maxX = NE[0] / this.pixelRatio;
3976
- const maxY = SW[1] / this.pixelRatio;
3977
- const width = maxX - minX;
3978
- const height = maxY - minY;
3979
- return [
3980
- [minX, minY],
3981
- [width, height]
3982
- ];
3995
+ /**
3996
+ * Returns extent in projection coordinates
3997
+ *
3998
+ * @param {Extent} extent
3999
+ * @returns {Extent} - The extent relative to the current viewport
4000
+ */
4001
+ projectExtent(extent) {
4002
+ const [minX, minY] = this.projection([extent.minX, extent.minY]);
4003
+ const [maxX, maxY] = this.projection([extent.maxX, extent.maxY]);
4004
+ return new Extent(minX, minY, maxX, maxY).scale(1 / this.pixelRatio);
3983
4005
  }
3984
4006
  invert(point) {
3985
4007
  const { projection, pixelRatio, transform } = this.getState();
@@ -6014,7 +6036,12 @@ let Map$1 = class Map2 {
6014
6036
  }
6015
6037
  zoomToFeature(feature, focalPoint, padding = { top: 40, right: 40, bottom: 40, left: 40 }) {
6016
6038
  const extent = feature.getExtent();
6017
- const [[featureX, featureY], [featureWidth, featureHeight]] = this.view.boundsForExtent(extent);
6039
+ const {
6040
+ minX: featureX,
6041
+ minY: featureY,
6042
+ width: featureWidth,
6043
+ height: featureHeight
6044
+ } = this.view.projectExtent(extent);
6018
6045
  const [viewPortWidth, viewPortHeight] = this.view.viewPortSize;
6019
6046
  const paddedViewPortWidth = viewPortWidth - padding.left - padding.right;
6020
6047
  const paddedViewPortHeight = viewPortHeight - padding.top - padding.bottom;
@@ -6477,7 +6504,12 @@ class Polygon extends Geometry {
6477
6504
  class Point extends Geometry {
6478
6505
  constructor({ type = "Point", coordinates }) {
6479
6506
  super({ type, extent: null, coordinates });
6480
- this.extent = [...coordinates, ...coordinates];
6507
+ this.extent = new Extent(
6508
+ coordinates[0],
6509
+ coordinates[1],
6510
+ coordinates[0],
6511
+ coordinates[1]
6512
+ );
6481
6513
  }
6482
6514
  _getProjected(projection) {
6483
6515
  return {
@@ -7630,90 +7662,90 @@ const styles$2 = {
7630
7662
  mobileHelpText: mobileHelpText$1
7631
7663
  };
7632
7664
  const mobileHelpText = "Use two fingers to zoom";
7633
- Map$1.Component = forwardRef(
7634
- ({
7635
- config,
7636
- inModalState = false,
7637
- onLoad,
7638
- children
7639
- }, ref) => {
7640
- const targetRef = useRef();
7641
- const [map, setMap] = useState(
7642
- /** @type {Map | null} */
7643
- null
7644
- );
7645
- const [zoomHelpText, setZoomHelpText] = useState("");
7646
- const [showHelpText, setShowHelpText] = useState(false);
7647
- useEffect(() => {
7648
- var _a;
7649
- if (!targetRef.current) return;
7650
- const map2 = new Map$1({
7651
- ...config,
7652
- target: targetRef.current
7653
- });
7654
- map2.collaborativeGesturesEnabled(true);
7655
- setMap(map2);
7656
- if (ref) {
7657
- ref.current = map2;
7665
+ const Component = ({
7666
+ config,
7667
+ inModalState = false,
7668
+ onLoad,
7669
+ children,
7670
+ mapRef
7671
+ }) => {
7672
+ const targetRef = useRef();
7673
+ const [map, setMap] = useState(
7674
+ /** @type {Map | null} */
7675
+ null
7676
+ );
7677
+ const [zoomHelpText, setZoomHelpText] = useState("");
7678
+ const [showHelpText, setShowHelpText] = useState(false);
7679
+ useEffect(() => {
7680
+ var _a;
7681
+ if (!targetRef.current) return;
7682
+ const map2 = new Map$1({
7683
+ ...config,
7684
+ target: targetRef.current
7685
+ });
7686
+ map2.collaborativeGesturesEnabled(true);
7687
+ setMap(map2);
7688
+ if (mapRef) {
7689
+ mapRef.current = map2;
7690
+ }
7691
+ if (onLoad) {
7692
+ onLoad(map2);
7693
+ }
7694
+ let zoomHelpText2 = "";
7695
+ if (
7696
+ // @ts-ignore
7697
+ ((_a = navigator.userAgentData) == null ? void 0 : _a.mobile) || navigator.userAgent.indexOf("Mobile") !== -1
7698
+ ) {
7699
+ zoomHelpText2 = mobileHelpText;
7700
+ } else {
7701
+ zoomHelpText2 = navigator.userAgent.indexOf("Mac") !== -1 ? "Use ⌘ + scroll to zoom" : "Use Ctrl + scroll to zoom";
7702
+ }
7703
+ setZoomHelpText(zoomHelpText2);
7704
+ return () => {
7705
+ map2.destroy();
7706
+ setMap(null);
7707
+ if (mapRef) {
7708
+ mapRef.current = null;
7658
7709
  }
7659
- if (onLoad) {
7660
- onLoad(map2);
7710
+ };
7711
+ }, [config, onLoad, mapRef]);
7712
+ useEffect(() => {
7713
+ if (!map) return;
7714
+ let timeoutID;
7715
+ map.onFilterEvent((showHelpText2) => {
7716
+ if (timeoutID) clearTimeout(timeoutID);
7717
+ setShowHelpText(showHelpText2);
7718
+ if (showHelpText2) {
7719
+ timeoutID = setTimeout(() => {
7720
+ setShowHelpText(false);
7721
+ }, 1e3);
7661
7722
  }
7662
- let zoomHelpText2 = "";
7663
- if (
7664
- // @ts-ignore
7665
- ((_a = navigator.userAgentData) == null ? void 0 : _a.mobile) || navigator.userAgent.indexOf("Mobile") !== -1
7666
- ) {
7667
- zoomHelpText2 = mobileHelpText;
7668
- } else {
7669
- zoomHelpText2 = navigator.userAgent.indexOf("Mac") !== -1 ? "Use ⌘ + scroll to zoom" : "Use Ctrl + scroll to zoom";
7723
+ });
7724
+ return () => {
7725
+ if (timeoutID) clearTimeout(timeoutID);
7726
+ };
7727
+ }, [map]);
7728
+ useEffect(() => {
7729
+ if (!map) return;
7730
+ map.collaborativeGesturesEnabled(!inModalState);
7731
+ }, [map, inModalState]);
7732
+ return /* @__PURE__ */ jsxs("figure", { ref: targetRef, className: styles$2.mapContainer, children: [
7733
+ /* @__PURE__ */ jsxs(
7734
+ "div",
7735
+ {
7736
+ className: styles$2.helpTextContainer,
7737
+ style: { opacity: showHelpText ? 1 : 0 },
7738
+ "aria-hidden": true,
7739
+ children: [
7740
+ /* @__PURE__ */ jsx("p", { className: [styles$2.helpText, styles$2.desktopHelpText].join(" "), children: zoomHelpText }),
7741
+ /* @__PURE__ */ jsx("p", { className: [styles$2.helpText, styles$2.mobileHelpText].join(" "), children: mobileHelpText })
7742
+ ]
7670
7743
  }
7671
- setZoomHelpText(zoomHelpText2);
7672
- return () => {
7673
- map2.destroy();
7674
- setMap(null);
7675
- if (ref) {
7676
- ref.current = null;
7677
- }
7678
- };
7679
- }, [config, onLoad, ref]);
7680
- useEffect(() => {
7681
- if (!map) return;
7682
- let timeoutID;
7683
- map.onFilterEvent((showHelpText2) => {
7684
- if (timeoutID) clearTimeout(timeoutID);
7685
- setShowHelpText(showHelpText2);
7686
- if (showHelpText2) {
7687
- timeoutID = setTimeout(() => {
7688
- setShowHelpText(false);
7689
- }, 1e3);
7690
- }
7691
- });
7692
- return () => {
7693
- if (timeoutID) clearTimeout(timeoutID);
7694
- };
7695
- }, [map]);
7696
- useEffect(() => {
7697
- if (!map) return;
7698
- map.collaborativeGesturesEnabled(!inModalState);
7699
- }, [map, inModalState]);
7700
- return /* @__PURE__ */ jsxs("figure", { ref: targetRef, className: styles$2.mapContainer, children: [
7701
- /* @__PURE__ */ jsxs(
7702
- "div",
7703
- {
7704
- className: styles$2.helpTextContainer,
7705
- style: { opacity: showHelpText ? 1 : 0 },
7706
- "aria-hidden": true,
7707
- children: [
7708
- /* @__PURE__ */ jsx("p", { className: [styles$2.helpText, styles$2.desktopHelpText].join(" "), children: zoomHelpText }),
7709
- /* @__PURE__ */ jsx("p", { className: [styles$2.helpText, styles$2.mobileHelpText].join(" "), children: mobileHelpText })
7710
- ]
7711
- }
7712
- ),
7713
- /* @__PURE__ */ jsx(MapProvider, { map, children })
7714
- ] });
7715
- }
7716
- );
7744
+ ),
7745
+ /* @__PURE__ */ jsx(MapProvider, { map, children })
7746
+ ] });
7747
+ };
7748
+ Map$1.Component = Component;
7717
7749
  const optionPicker = "_optionPicker_1b561_1";
7718
7750
  const title$1 = "_title_1b561_13";
7719
7751
  const options = "_options_1b561_20";