Skip to content

Commit

Permalink
Map block: Allow block selection when Mapkit is being used (#38956)
Browse files Browse the repository at this point in the history
  • Loading branch information
coder-karen authored Aug 28, 2024
1 parent e9be6f8 commit e7eadc0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/fix-map-block-selection
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: enhancement

Map block: Allow maps on WordPress.com to be selectable.
48 changes: 44 additions & 4 deletions projects/plugins/jetpack/extensions/blocks/map/component/mapkit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Children, forwardRef, memo, useCallback, useEffect, useRef } from '@wordpress/element';
import {
Children,
forwardRef,
memo,
useCallback,
useEffect,
useRef,
useState,
} from '@wordpress/element';
import { get } from 'lodash';
import { MapkitProvider } from '../mapkit/context';
import {
Expand Down Expand Up @@ -45,13 +53,31 @@ const MapkitComponent = forwardRef(
return child;
}
} );
const [ isSelected, setIsSelected ] = useState( false );

useEffect( () => {
if ( error ) {
onError( 'mapkit_error', error );
}
}, [ error, onError ] );

const handleBlockClick = () => {
setIsSelected( true );
};

useEffect( () => {
const handleClickOutside = event => {
if ( ! mapRef.current.contains( event.target ) ) {
setIsSelected( false );
}
};

document.addEventListener( 'mousedown', handleClickOutside );
return () => {
document.removeEventListener( 'mousedown', handleClickOutside );
};
}, [ mapRef ] );

return (
<MapkitProvider
value={ {
Expand Down Expand Up @@ -80,11 +106,25 @@ const MapkitComponent = forwardRef(
onMapLoaded={ onMapLoaded }
/>
) : null }

{ /* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */ }
<div
style={ { height: mapHeight ? `${ mapHeight }px` : '400px' } }
className="wp-block-jetpack-map__gm-container"
style={ { height: mapHeight ? `${ mapHeight }px` : '400px', position: 'relative' } }
className="wp-block-jetpack-map__mapkit-wrapper"
ref={ mapRef }
/>
onClick={ handleBlockClick }
>
{ ! isSelected && <div className="wp-block-jetpack-map__select-overlay" /> }
{ /* Map container */ }
<div
className="wp-block-jetpack-map__gm-container"
style={ {
height: `${ mapHeight }px`,
pointerEvents: isSelected ? 'auto' : 'none',
} }
></div>
</div>

{ addPoint }
<InfoWindow mapProvider="mapkit" />
</MapkitProvider>
Expand Down
15 changes: 15 additions & 0 deletions projects/plugins/jetpack/extensions/blocks/map/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@
// This matches the color that MapBox uses as a placeholder before map tiles have loaded.
background-color: #e4e2de;
}

.wp-block-jetpack-map__gm-container {
position: absolute;
inset: 0;
z-index: 0;
}

/* Overlay for block selection */
.wp-block-jetpack-map__select-overlay {
position: absolute;
inset: 0;
z-index: 2;
background-color: transparent;
pointer-events: auto;
}
}

.wp-block-jetpack-map__height_input {
Expand Down

0 comments on commit e7eadc0

Please sign in to comment.