Skip to content

Commit 2f6adb4

Browse files
authored
feat(geo): Forward ref to react-map-gl component (#1791)
* Forward ref to react-map-gl component * Add changeset
1 parent 9c25f80 commit 2f6adb4

File tree

2 files changed

+64
-61
lines changed

2 files changed

+64
-61
lines changed

.changeset/nine-pillows-look.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/ui-react': minor
3+
---
4+
5+
Add forward ref functionality to MapView
Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Amplify, Auth } from 'aws-amplify';
22
import maplibregl from 'maplibre-gl';
33
import { AmplifyMapLibreRequest } from 'maplibre-gl-js-amplify';
4-
import React, { useEffect, useMemo, useState } from 'react';
4+
import React, { forwardRef, useEffect, useMemo, useState } from 'react';
55
import ReactMapGL from 'react-map-gl';
6-
import type { MapProps, TransformRequestFunction } from 'react-map-gl';
6+
import type { MapProps, MapRef, TransformRequestFunction } from 'react-map-gl';
77

88
export type MapViewProps = Omit<MapProps, 'transformRequest'>;
99

@@ -21,66 +21,64 @@ export type MapViewProps = Omit<MapProps, 'transformRequest'>;
2121
* return <MapView />
2222
* }
2323
*/
24-
export const MapView = ({
25-
mapLib,
26-
mapStyle,
27-
style,
28-
...props
29-
}: MapViewProps) => {
30-
const amplifyConfig = Amplify.configure() as any;
31-
const geoConfig = useMemo(
32-
() =>
33-
amplifyConfig.geo?.amazon_location_service ??
34-
amplifyConfig.geo?.AmazonLocationService ??
35-
{},
36-
[amplifyConfig]
37-
);
38-
const [transformRequest, setTransformRequest] = useState<
39-
TransformRequestFunction | undefined
40-
>();
24+
export const MapView = forwardRef<MapRef, MapViewProps>(
25+
({ mapLib, mapStyle, style, ...props }, ref) => {
26+
const amplifyConfig = Amplify.configure() as any;
27+
const geoConfig = useMemo(
28+
() =>
29+
amplifyConfig.geo?.amazon_location_service ??
30+
amplifyConfig.geo?.AmazonLocationService ??
31+
{},
32+
[amplifyConfig]
33+
);
34+
const [transformRequest, setTransformRequest] = useState<
35+
TransformRequestFunction | undefined
36+
>();
4137

42-
const styleProps = useMemo<React.CSSProperties>(
43-
() => ({
44-
height: '100vh',
45-
position: 'relative',
46-
width: '100vw',
47-
...style,
48-
}),
49-
[style]
50-
);
38+
const styleProps = useMemo<React.CSSProperties>(
39+
() => ({
40+
height: '100vh',
41+
position: 'relative',
42+
width: '100vw',
43+
...style,
44+
}),
45+
[style]
46+
);
5147

52-
/**
53-
* The transformRequest is a callback used by react-map-gl before it makes a request for an external URL. It signs
54-
* the request with AWS Sigv4 Auth, provided valid credentials, and is how we integrate react-map-gl with Amplify Geo
55-
* and Amazon Location Service. Once the transformRequest is created, we render the map.
56-
*/
57-
useEffect(() => {
58-
(async () => {
59-
const credentials = await Auth.currentUserCredentials();
48+
/**
49+
* The transformRequest is a callback used by react-map-gl before it makes a request for an external URL. It signs
50+
* the request with AWS Sigv4 Auth, provided valid credentials, and is how we integrate react-map-gl with Amplify Geo
51+
* and Amazon Location Service. Once the transformRequest is created, we render the map.
52+
*/
53+
useEffect(() => {
54+
(async () => {
55+
const credentials = await Auth.currentUserCredentials();
6056

61-
if (credentials) {
62-
const { region } = geoConfig;
63-
const { transformRequest: amplifyTransformRequest } =
64-
new AmplifyMapLibreRequest(credentials, region);
65-
setTransformRequest(() => amplifyTransformRequest);
66-
}
67-
})();
68-
}, [geoConfig]);
57+
if (credentials) {
58+
const { region } = geoConfig;
59+
const { transformRequest: amplifyTransformRequest } =
60+
new AmplifyMapLibreRequest(credentials, region);
61+
setTransformRequest(() => amplifyTransformRequest);
62+
}
63+
})();
64+
}, [geoConfig]);
6965

70-
/**
71-
* The mapLib property is used by react-map-gl@v7 to override the underlying map library. The default library is
72-
* mapbox-gl-js, which uses its own copyrighted license. We override the map library with the BSD-licensed
73-
* maplibre-gl-js.
74-
*
75-
* The default mapStyle we use is just the map ID provided by aws-exports.
76-
*/
77-
return transformRequest ? (
78-
<ReactMapGL
79-
{...props}
80-
mapLib={mapLib ?? maplibregl}
81-
mapStyle={mapStyle ?? geoConfig.maps?.default}
82-
style={styleProps}
83-
transformRequest={transformRequest}
84-
/>
85-
) : null;
86-
};
66+
/**
67+
* The mapLib property is used by react-map-gl@v7 to override the underlying map library. The default library is
68+
* mapbox-gl-js, which uses its own copyrighted license. We override the map library with the BSD-licensed
69+
* maplibre-gl-js.
70+
*
71+
* The default mapStyle we use is just the map ID provided by aws-exports.
72+
*/
73+
return transformRequest ? (
74+
<ReactMapGL
75+
{...props}
76+
mapLib={mapLib ?? maplibregl}
77+
mapStyle={mapStyle ?? geoConfig.maps?.default}
78+
ref={ref}
79+
style={styleProps}
80+
transformRequest={transformRequest}
81+
/>
82+
) : null;
83+
}
84+
);

0 commit comments

Comments
 (0)