1

I have a react app that uses React-Leaflet. The Application renders a map, and on top of the map there should be a component that shows a modal. Instead, the modal is shown underneath the map. How can I make the modal show on top of the map instead like it's supposed to?

Code:

export default function AssignArea() {
  return (
    <Container maxWidth="sm" style={{ position:"absolute", zIndex: 100 }}>
      <Typography variant="h1" component="h2">
        ASSIGN AREA
      </Typography>
      <Input placeholder="Title"></Input>
      <SketchPicker />
      <Input></Input>
      <Button color="error" variant="text"><DeleteIcon/>CANCEL</Button>
      <Button color="primary" variant="text"><RoomIcon/>CONFIRM</Button>
    </Container>
  );
}
return (
  <MapContainer
    center={[mapBase.lat, mapBase.lng]}
    zoom={mapBase.zoom}
    maxBounds={bounds}
    style={{ height: '100vh', width: '100%', position: 'relative', zIndex: 0 }}
    zoomControl={true}
    attributionControl={true}
  >
    {/* Base map layer */}
    <TileLayer
      url='https://tile.openstreetmap.org/{z}/{x}/{y}.png'
      maxZoom={20}
      zoomOffset={-1}
      tileSize={512}
      zIndex={0}
    />
    <RemoveUkraineFlag />
    <Markers />
    <Zones />
    <UiBase />
    <Dropdown />
    {showAssignModalAtomValue ? <AssignArea /> : null}
  </MapContainer>
);

The MapContainer has a lower z-Index than the AssignArea component, but it still renders under the map. How come?

3
  • I don't think this can be fixed with zIndex, have you looked into createPortal? Commented Nov 4 at 14:39
  • 1
    Try to move modal outside MapContainer and then use position: fixed & higher zIndex. Commented Nov 4 at 15:20
  • Please edit to include a complete minimal reproducible example (including imports and dependency versions) such that readers can run the code and attempt to reproduce any issues you are describing. Be sure to include any exact reproduction steps if they are necessary, complete error messages and stacktraces, and debugging logs and details from any investigation. This should help readers better understand what you are asking. Commented Nov 4 at 15:50

1 Answer 1

0

Try to add the classes leaflet-top and leaflet-left to your overlay. This should add the required styles to make it a map overlay.

<Container className="leaflet-top leaflet-left">

Should do the trick.

See the docs here: https://react-leaflet.js.org/docs/example-react-control/.

An alternative would be to put the overlay outside of the MapContainer. If your map is 100% height an width it maybe should not be necessary to put it as a child of the map.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.