gistda-sphere-react

Popup

An information bubble anchored to a location, with text, HTML or React content

import { Popup, SphereMap } from "gistda-sphere-react";

<SphereMap center={{ lon: 100.5018, lat: 13.7563 }} style={{ height: 350 }} zoom={12}>
  <Popup
    position={{ lon: 100.5018, lat: 13.7563 }}
    title="Bangkok"
    detail="Capital of Thailand"
  />
</SphereMap>

The shorter samples below leave out <SphereMap>, but every overlay goes inside one.

A <Popup> is open while it's rendered: render it conditionally, and remove it in onClose. For popups that open when a marker or shape is clicked, use their detail, popup or popupContent props instead.

Props

PropTypeDefaultDescription
positionLocationrequiredWhere the popup points. Changing it moves the popup without re-creating it.
titlestring–Title text.
detailstring–Body text or HTML.
childrenReactNode–React content rendered in the body, below the title.
htmlstring–Fully custom HTML, replacing the title and body layout.
loadDetail(element) => void–Fill the body element yourself when the popup opens.
loadHtml(element) => void–Fill the whole popup element yourself each time it opens.
sizeSize–Fixed size, { width, height }.
closablebooleantrueShow the close button.
optionsRecord<string, unknown>–Extra options for new sphere.Popup(), including MapLibre popup options such as anchor, offset, className or closeOnClick.
onClose(popup) => void–The user closed the popup.
refRef<PopupRef>–See Ref.

React content

import { Popup } from "gistda-sphere-react";

{selected && (
  <Popup
    position={selected.location}
    title={selected.name}
    onClose={() => setSelected(null)}
  >
    <img src={selected.photo} alt="" width={200} />
    <button onClick={() => book(selected.id)}>Book a table</button>
  </Popup>
)}

Show a popup where the user clicks

import { type Location, Popup, SphereMap } from "gistda-sphere-react";
import { useState } from "react";

const [clicked, setClicked] = useState<Location | null>(null);

<SphereMap onClick={({ lat, lon }) => setClicked({ lat, lon })}>
  {clicked && (
    <Popup
      position={clicked}
      title="Coordinates"
      detail={`${clicked.lat.toFixed(5)}, ${clicked.lon.toFixed(5)}`}
      onClose={() => setClicked(null)}
    />
  )}
</SphereMap>

Ref

MethodDescription
getPopup()The underlying sphere.Popup.
getElement()The popup's DOM element.
getPosition() / setPosition(location)Read or move the popup.
setTitle(text) / setDetail(text)Change the text without re-creating the popup.

setPosition moves the popup until the position prop changes to a new location. Changing title, detail or size re-creates the popup at position, so keep the location in state if the popup should stay where you moved it.

On this page