Skip to content

Commit edd2417

Browse files
committed
feat: cleanup comments
1 parent 82577f5 commit edd2417

File tree

7 files changed

+10
-83
lines changed

7 files changed

+10
-83
lines changed

src/components/map-3d/index.tsx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,20 +186,13 @@ export const Map3D = forwardRef<Map3DRef, Map3DProps>((props, ref) => {
186186

187187
const {addMap3DInstance, removeMap3DInstance} = context;
188188

189-
// Set up the map instance
190189
const [map3d, containerRef, map3dRef, cameraStateRef, isReady] =
191190
useMap3DInstance(props);
192191

193-
// Set up camera params tracking and updates
194192
useMap3DCameraParams(map3d, cameraStateRef, props);
195-
196-
// Set up event handlers
197193
useMap3DEvents(map3d, props);
198-
199-
// Set up options updates
200194
useMap3DOptions(map3d, props);
201195

202-
// Register/unregister map3d instance with APIProvider
203196
useEffect(() => {
204197
if (!map3d) return;
205198

@@ -212,8 +205,6 @@ export const Map3D = forwardRef<Map3DRef, Map3DProps>((props, ref) => {
212205
// eslint-disable-next-line react-hooks/exhaustive-deps
213206
}, [map3d, id]);
214207

215-
// Expose imperative handle for animations
216-
// Cast to extended type since @types/google.maps may not have animation methods yet
217208
const map3dWithAnimations = map3d as Map3DElementWithAnimations | null;
218209
useImperativeHandle(
219210
ref,
@@ -234,7 +225,6 @@ export const Map3D = forwardRef<Map3DRef, Map3DProps>((props, ref) => {
234225
[map3d, map3dWithAnimations]
235226
);
236227

237-
// Combine styles
238228
const combinedStyle = useMemo(
239229
() => ({
240230
...DEFAULT_CONTAINER_STYLE,
@@ -243,13 +233,11 @@ export const Map3D = forwardRef<Map3DRef, Map3DProps>((props, ref) => {
243233
[style]
244234
);
245235

246-
// Context value for child components
247236
const contextValue = useMemo<GoogleMaps3DContextValue>(
248237
() => ({map3d}),
249238
[map3d]
250239
);
251240

252-
// Don't render the custom element until everything is ready
253241
if (!isReady) {
254242
return (
255243
<div

src/components/map-3d/use-map-3d-camera-params.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,12 @@ export function useMap3DCameraParams(
4646
const tilt = props.tilt ?? null;
4747
const roll = props.roll ?? null;
4848

49-
// This effect runs on every render to check for camera differences
49+
// Runs on every render to sync controlled camera props with the map element
5050
useLayoutEffect(() => {
5151
if (!map3d) return;
5252

5353
const currentState = cameraStateRef.current;
5454

55-
// Check center
5655
if (
5756
lat !== null &&
5857
lng !== null &&
@@ -67,22 +66,18 @@ export function useMap3DCameraParams(
6766
};
6867
}
6968

70-
// Check range
7169
if (range !== null && currentState.range !== range) {
7270
map3d.range = range;
7371
}
7472

75-
// Check heading
7673
if (heading !== null && currentState.heading !== heading) {
7774
map3d.heading = heading;
7875
}
7976

80-
// Check tilt
8177
if (tilt !== null && currentState.tilt !== tilt) {
8278
map3d.tilt = tilt;
8379
}
8480

85-
// Check roll
8681
if (roll !== null && currentState.roll !== roll) {
8782
map3d.roll = roll;
8883
}

src/components/map-3d/use-map-3d-events.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ export function useMap3DEvents(
170170
onError
171171
} = props;
172172

173-
// Individual camera events
174173
useMap3DEvent(map3d, 'gmp-centerchange', onCenterChanged, createCameraEvent);
175174
useMap3DEvent(
176175
map3d,
@@ -182,7 +181,7 @@ export function useMap3DEvents(
182181
useMap3DEvent(map3d, 'gmp-rangechange', onRangeChanged, createCameraEvent);
183182
useMap3DEvent(map3d, 'gmp-rollchange', onRollChanged, createCameraEvent);
184183

185-
// Aggregated camera changed event
184+
// onCameraChanged aggregates all camera property change events into one handler
186185
useEffect(() => {
187186
if (!map3d || !onCameraChanged) return;
188187

@@ -201,7 +200,6 @@ export function useMap3DEvents(
201200
};
202201
}, [map3d, onCameraChanged]);
203202

204-
// Click event
205203
useEffect(() => {
206204
if (!map3d || !onClick) return;
207205

@@ -220,7 +218,6 @@ export function useMap3DEvents(
220218
return () => map3d.removeEventListener('gmp-click', handler);
221219
}, [map3d, onClick]);
222220

223-
// Steady change event
224221
useEffect(() => {
225222
if (!map3d || !onSteadyChange) return;
226223

@@ -237,13 +234,11 @@ export function useMap3DEvents(
237234
return () => map3d.removeEventListener('gmp-steadychange', handler);
238235
}, [map3d, onSteadyChange]);
239236

240-
// Animation end event
241237
useMap3DEvent(map3d, 'gmp-animationend', onAnimationEnd, (map3d, type) => ({
242238
type,
243239
map3d
244240
}));
245241

246-
// Error event
247242
useMap3DEvent(map3d, 'gmp-error', onError, (map3d, type) => ({
248243
type,
249244
map3d

src/components/map-3d/use-map-3d-instance.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,51 +29,38 @@ export function useMap3DInstance(
2929
cameraStateRef: CameraStateRef3D,
3030
isReady: boolean
3131
] {
32-
// Load the maps3d library
3332
const maps3dLib = useMapsLibrary('maps3d');
34-
35-
// Track if the custom element is defined
3633
const [customElementReady, setCustomElementReady] = useState(false);
37-
38-
// Container ref for the wrapper div
3934
const [, containerRef] = useCallbackRef<HTMLDivElement>();
40-
41-
// Ref for the gmp-map-3d element
4235
const [map3d, map3dRef] = useCallbackRef<google.maps.maps3d.Map3DElement>();
43-
44-
// Track camera state
4536
const cameraStateRef = useTrackedCameraStateRef3D(map3d);
4637

47-
// Wait for custom element definition
4838
useEffect(() => {
4939
customElements.whenDefined('gmp-map-3d').then(() => {
5040
setCustomElementReady(true);
5141
});
5242
}, []);
5343

54-
// Apply initial options when map3d element is ready
44+
// Apply initial options once when the element is first available
5545
useEffect(() => {
5646
if (!map3d) return;
5747

5848
const {
59-
// Extract camera props
6049
center,
6150
heading,
6251
tilt,
6352
range,
6453
roll,
65-
// Extract default* props (not applied to element)
6654
defaultCenter,
6755
defaultHeading,
6856
defaultTilt,
6957
defaultRange,
7058
defaultRoll,
71-
// Extract non-element props
59+
// Non-element props to exclude
7260
id,
7361
style,
7462
className,
7563
children,
76-
// Extract event props
7764
onCenterChanged,
7865
onHeadingChanged,
7966
onTiltChanged,
@@ -84,21 +71,17 @@ export function useMap3DInstance(
8471
onSteadyChange,
8572
onAnimationEnd,
8673
onError,
87-
// Extract options handled elsewhere (use-map-3d-options)
8874
mode,
8975
gestureHandling,
90-
// Remaining are element options
9176
...elementOptions
9277
} = props;
9378

94-
// Apply initial camera state from props or defaults
9579
const initialCenter = center ?? defaultCenter;
9680
const initialHeading = heading ?? defaultHeading;
9781
const initialTilt = tilt ?? defaultTilt;
9882
const initialRange = range ?? defaultRange;
9983
const initialRoll = roll ?? defaultRoll;
10084

101-
// Build initial options object
10285
const initialOptions: Partial<google.maps.maps3d.Map3DElementOptions> = {
10386
...elementOptions
10487
};
@@ -109,7 +92,6 @@ export function useMap3DInstance(
10992
if (initialRange !== undefined) initialOptions.range = initialRange;
11093
if (initialRoll !== undefined) initialOptions.roll = initialRoll;
11194

112-
// Apply all initial options to the element
11395
Object.assign(map3d, initialOptions);
11496
// eslint-disable-next-line react-hooks/exhaustive-deps
11597
}, [map3d]); // Only run when map3d element first becomes available

src/components/marker-3d.tsx

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,8 @@ export const Marker3D = forwardRef(function Marker3D(
161161
const [contentContainer, setContentContainer] =
162162
useState<HTMLDivElement | null>(null);
163163

164-
// Track whether we need the interactive variant
165164
const isInteractive = Boolean(onClick);
166165

167-
// Expose marker element via ref
168166
useImperativeHandle(
169167
ref,
170168
() =>
@@ -174,7 +172,6 @@ export const Marker3D = forwardRef(function Marker3D(
174172
[marker]
175173
);
176174

177-
// Create marker element and attach to map
178175
useEffect(() => {
179176
if (!map3d || !maps3dLibrary) return;
180177

@@ -188,50 +185,42 @@ export const Marker3D = forwardRef(function Marker3D(
188185
newMarker = new maps3dLibrary.Marker3DElement();
189186
}
190187

191-
// Append marker to map3d element
192188
map3d.appendChild(newMarker);
193189
setMarker(newMarker);
194190

195-
// Create content container for children
191+
// Hidden container used as React portal target for custom marker content
196192
const container = document.createElement('div');
197193
container.style.display = 'none';
198194
document.body.appendChild(container);
199195
setContentContainer(container);
200196

201197
return () => {
202-
// Remove marker from map
203198
if (newMarker.parentElement) {
204199
newMarker.parentElement.removeChild(newMarker);
205200
}
206-
// Clean up content container
207201
container.remove();
208202
setMarker(null);
209203
setContentContainer(null);
210204
};
211205
}, [map3d, maps3dLibrary, isInteractive]);
212206

213-
// Sync position prop
214207
useEffect(() => {
215208
if (!marker || position === undefined) return;
216209
marker.position = position;
217210
}, [marker, position]);
218211

219-
// Sync altitudeMode prop
220212
useEffect(() => {
221213
if (!marker || altitudeMode === undefined) return;
222214
marker.altitudeMode =
223215
altitudeMode as unknown as google.maps.maps3d.AltitudeMode;
224216
}, [marker, altitudeMode]);
225217

226-
// Sync collisionBehavior prop
227218
useEffect(() => {
228219
if (!marker || collisionBehavior === undefined) return;
229-
// Cast to the google.maps.CollisionBehavior type expected by Marker3DElement
230220
marker.collisionBehavior =
231221
collisionBehavior as google.maps.CollisionBehavior;
232222
}, [marker, collisionBehavior]);
233223

234-
// Sync simple props
235224
useEffect(() => {
236225
if (!marker) return;
237226
if (drawsWhenOccluded !== undefined)
@@ -258,29 +247,25 @@ export const Marker3D = forwardRef(function Marker3D(
258247
if (zIndex !== undefined) marker.zIndex = zIndex;
259248
}, [marker, zIndex]);
260249

261-
// Sync title prop (interactive only)
250+
// title is only available on Marker3DInteractiveElement
262251
useEffect(() => {
263252
if (!marker || !isInteractive) return;
264253
const interactiveMarker =
265254
marker as google.maps.maps3d.Marker3DInteractiveElement;
266255
if (title !== undefined) interactiveMarker.title = title;
267256
}, [marker, title, isInteractive]);
268257

269-
// Bind click event for interactive marker
270258
useDomEventListener(marker, 'gmp-click', onClick);
271259

272-
// Process children and move to marker slot with template wrapping
260+
// Move React children to marker's slot, wrapping img/svg in <template> as required by the API
273261
useLayoutEffect(() => {
274-
// Skip if a child component (like Pin) is handling content
275262
if (contentHandledExternally) return;
276263
if (!marker || !contentContainer) return;
277264

278-
// Clear any existing slotted content
279265
while (marker.firstChild) {
280266
marker.removeChild(marker.firstChild);
281267
}
282268

283-
// Process each child node in the content container
284269
const childNodes = Array.from(contentContainer.childNodes);
285270

286271
for (const node of childNodes) {
@@ -290,24 +275,20 @@ export const Marker3D = forwardRef(function Marker3D(
290275
const tagName = element.tagName.toLowerCase();
291276

292277
if (tagName === 'img' || tagName === 'svg') {
293-
// Wrap img and svg in template element
294278
const template = document.createElement('template');
295279
template.content.appendChild(element.cloneNode(true));
296280
marker.appendChild(template);
297281
} else {
298-
// Append other elements directly (e.g., PinElement)
299282
marker.appendChild(element.cloneNode(true));
300283
}
301284
}
302285
}, [marker, contentContainer, children, contentHandledExternally]);
303286

304-
// Memoize context value
305287
const contextValue = useMemo(
306288
() => ({marker, setContentHandledExternally}),
307289
[marker]
308290
);
309291

310-
// Render children into hidden container, then useLayoutEffect moves them
311292
if (!contentContainer) return null;
312293

313294
return (

0 commit comments

Comments
 (0)