Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map block: Allow block selection when Mapkit is being used #38956

Merged
merged 9 commits into from
Aug 28, 2024
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting that I'm just focussed on mouse click events - this PR doesn't introduce other accessibility issues but I did notice existing ones that should get fixed later: #38986

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
Loading